knife-cssh 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b75656ede8c6472b81c390adcd7dbb30e5a587fe
4
- data.tar.gz: 95698d86e540563eaab5d69d30430454cb775e42
3
+ metadata.gz: 6b140d9a065370a3698b0b55e56284ebcc8cccd1
4
+ data.tar.gz: fb6867caba3f6a95f5786c0488162bb09100d1a1
5
5
  SHA512:
6
- metadata.gz: 572f2bd3d0509286abc3d1ec1f7a5cd5bb9ff84e3fd8326d857d81d44ab75b033b663889c5c7fb8e013bd1b535462a6d5f2cf336eee9fab7ce9471786007276f
7
- data.tar.gz: a1076952adf9013a75bfa3a8ee3169bb2608fb7e4bf877dd7aeeac25783af740ebfc7469dfaab8f732b4532b0a92671bb653fe8ef9dda6be821ae35e6063d53a
6
+ metadata.gz: 3ec2c45d19bab083ec44e801f4d1a87a487a5faba412063de10c435c8b4b9b7072fd50fc55db4e081b5f809f1b10d943e0e9d4ead609b23fb4a1150181ed372f
7
+ data.tar.gz: fc1fee5218d680ce6fbbf0f0fd4f3d570c5c333c3e7bed60dc3c789b6439067a87b8d8f87f3f1ae5b7a193f95248271212113ece0993eae6e9601d06bd393fbd
data/README.md CHANGED
@@ -22,3 +22,8 @@ Author
22
22
  ------
23
23
 
24
24
  Nicolas Szalay : < nicolas.szalay _at_ fotolia _dot_ com >
25
+
26
+ Contributors
27
+ ------------
28
+
29
+ Bence Kiglics : < bence.kiglics _at_ gmail _dot_ com >
@@ -1,19 +1,71 @@
1
1
  require 'chef/knife'
2
2
 
3
3
  module KnifeCssh
4
+
5
+ def self.which(*cmds)
6
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
7
+
8
+ cmds.each do |cmd|
9
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
10
+ exts.each do |ext|
11
+ exe = File.join(path, "#{cmd}#{ext}")
12
+ return exe if File.executable?(exe) && !File.directory?(exe)
13
+ end
14
+ end
15
+ end
16
+
17
+ return nil
18
+ end
19
+
20
+ def self.find_command(*cmds)
21
+ cmd = self.which *cmds
22
+ return cmd if not cmd.nil?
23
+
24
+ nil
25
+ end
26
+
4
27
  class CsshSummon < Chef::Knife
5
28
 
6
29
  banner "knife cssh summon QUERY"
7
30
 
31
+ option :login,
32
+ :short => '-l USER',
33
+ :long => '--login USER',
34
+ :description => 'Username to use for login',
35
+ :default => ENV['USER']
36
+
37
+ option :cssh_command,
38
+ :short => '-c COMMAND',
39
+ :long => '--cssh-command COMMAND',
40
+ :description => 'Command to use instead of cssh/csshX',
41
+ :default => KnifeCssh::find_command('csshX', 'cssh'),
42
+ :proc => Proc.new { |cmd| KnifeCssh::find_command(cmd) }
43
+
44
+ SPECIFIC_OPTIONS = {
45
+ 'tmux-cssh' => {
46
+ :user_switch => '-u'
47
+ },
48
+ :default => {
49
+ :user_switch => '-l'
50
+ }
51
+ }
52
+
8
53
  deps do
9
54
  require 'chef/node'
10
55
  require 'chef/environment'
11
56
  require 'chef/api_client'
12
57
  require 'chef/knife/search'
58
+ require 'shellwords'
13
59
  end
14
60
 
15
61
  def run
16
62
  # this chef is mainly from chef own knife search command
63
+ if name_args.length < 1
64
+ puts 'Missing argument QUERY!'
65
+ show_usage
66
+ exit 1
67
+ end
68
+
17
69
  query = name_args[0]
18
70
  q = Chef::Search::Query.new
19
71
  escaped_query = URI.escape(query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
@@ -21,7 +73,8 @@ module KnifeCssh
21
73
 
22
74
  begin
23
75
  q.search('node', escaped_query, 'X_CHEF_id_CHEF_X asc', 0, 100) do |item|
24
- result_items.push(item["fqdn"]) if item.has_key?("fqdn")
76
+ remote_host = extract_host item
77
+ result_items.push remote_host if not remote_host.nil?
25
78
  end
26
79
  rescue Net::HTTPServerException => e
27
80
  msg = Chef::JSONCompat.from_json(e.response.body)["error"].first
@@ -29,7 +82,37 @@ module KnifeCssh
29
82
  exit 1
30
83
  end
31
84
 
32
- %x[cssh #{result_items.join(" ")}]
85
+ call_cssh result_items
86
+ end
87
+
88
+ private
89
+
90
+ def extract_host(item)
91
+ return item[:ec2][:public_ipv4] if item.has_key? :ec2
92
+ return item[:ipaddress] if not item[:ipaddress].nil?
93
+ item[:fqdn]
94
+ end
95
+
96
+ def call_cssh(hosts)
97
+ if config[:cssh_command].nil?
98
+ puts "Unable to find any suitable cssh command on PATH!"
99
+ exit 1
100
+ end
101
+
102
+ %x[#{config[:cssh_command]} #{get_impl_opt :user_switch} #{config[:login].shellescape} #{hosts.join(" ")}]
103
+ end
104
+
105
+ def get_impl_opt(key)
106
+ cmdname = cssh_command_name
107
+ if SPECIFIC_OPTIONS.has_key?(cmdname) and SPECIFIC_OPTIONS[cmdname].has_key?(key)
108
+ return SPECIFIC_OPTIONS[cmdname][key]
109
+ end
110
+
111
+ SPECIFIC_OPTIONS[:default][key]
112
+ end
113
+
114
+ def cssh_command_name
115
+ File.basename config[:cssh_command]
33
116
  end
34
117
  end
35
118
  end
@@ -1,3 +1,3 @@
1
1
  module KnifeCssh
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-cssh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Szalay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-05 00:00:00.000000000 Z
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Summon cssh from a chef search
14
14
  email: nico@rottenbytes.info