koma 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33a965754d80b189a101cea02a9e879803b774ab
4
- data.tar.gz: 1808ccb3e7afa6090c289d35735c245b2040a391
3
+ metadata.gz: db1380979186368ebfa605559dab0218e3a89263
4
+ data.tar.gz: e117f8bdb660c5ce0e55c15413e7e123339bdfbd
5
5
  SHA512:
6
- metadata.gz: 6281c8a5b558259025f43ca058ca32fa7a473cc6c32498871da4ffea6ac9dcd2b36cd95cb0cfeca2be0bcaba174dbf575895ac4844550be14bed361e0e4a9eaf
7
- data.tar.gz: d4bc1790b1ba8c7ea700053299e1650f6bc35373d5ce89cf1c0973f913d1a093dd5e131765c34426323e8eb0ef762b68bbd86d75c43adb3bbd6ccd7ea46e6a98
6
+ metadata.gz: eb850c346b24037bb842395391f1ed7449707f62eff0788a72f83d00eecd120a4797ccefa9317299a401757c7763ea85a0110a5ae666f6c8208248ac12507fbe
7
+ data.tar.gz: 573f446930f97f3edcf13b0915561f1e94efc1c21588cef06b52881d8bf2a24289b12ce8db1d0c03090e84a9ce80e7691d6ad8f7e17e6b064efa51c81cec1ec9
data/README.md CHANGED
@@ -63,17 +63,22 @@ Host example_jp
63
63
  IdentityFile /path/to/more/example_jp_rsa
64
64
  EOF
65
65
 
66
- $ cat ssh_config_tmp | koma ssh
66
+ $ cat ssh_config_tmp | koma ssh --key platform,platform_version
67
67
  ```
68
68
 
69
+ Use [sconb](https://github.com/k1LoW/sconb).
70
+
69
71
  ```sh
70
- $ vagrant ssh-config | koma ssh --key cpu,kernel
72
+ $ sconb dump example_* | sconb restore | koma ssh --key platform,platform_version
71
73
  ```
72
74
 
75
+ Gather vagrant box host inventory.
76
+
73
77
  ```sh
74
- $ sconb dump example.com | sconb restore | koma ssh --key platform,platform_version
78
+ $ vagrant ssh-config | koma ssh --key cpu,kernel
75
79
  ```
76
80
 
81
+
77
82
  ## Host inventory keys
78
83
 
79
84
  ```sh
@@ -20,7 +20,39 @@ module Koma
20
20
  result
21
21
  end
22
22
 
23
+ def run_command(command)
24
+ if host.include?(',')
25
+ list = host.split(',').uniq
26
+ results = Parallel.map(list) do |h|
27
+ run_command_via_ssh(h, options, command)
28
+ end
29
+ arr = [list, results].transpose
30
+ result = Hash[*arr.flatten]
31
+ else
32
+ result = run_command_via_ssh(host, options, command)
33
+ end
34
+ result
35
+ end
36
+
23
37
  def gather_via_ssh(host, options)
38
+ set :ssh_options, build_ssh_options(host, options)
39
+ out(options[:key])
40
+ end
41
+
42
+ def run_command_via_ssh(host, options, command)
43
+ set :ssh_options, build_ssh_options(host, options)
44
+ result = Specinfra.backend.run_command(command)
45
+ {
46
+ exit_signal: result.exit_signal,
47
+ exit_status: result.exit_status,
48
+ stderr: result.stderr,
49
+ stdout: result.stdout
50
+ }
51
+ end
52
+
53
+ private
54
+
55
+ def build_ssh_options(host, options)
24
56
  user, host = host.split('@') if host.include?('@')
25
57
  set :backend, :ssh
26
58
  set :host, host
@@ -34,9 +66,7 @@ module Koma
34
66
  ssh_options[:keys] = [options[:identity_file]] if options[:identity_file]
35
67
  ssh_options[:port] = options[:port] if options[:port]
36
68
  end
37
-
38
- set :ssh_options, ssh_options
39
- out(options[:key])
69
+ ssh_options
40
70
  end
41
71
  end
42
72
  end
data/lib/koma/cli.rb CHANGED
@@ -5,40 +5,54 @@ require 'yaml'
5
5
  module Koma
6
6
  class CLI < Thor
7
7
  desc 'ssh <host1,host2,..>', 'stdout remote host inventory'
8
- option :key,
9
- type: :string,
10
- banner: '<key1,key2,..>',
11
- desc: 'inventory keys',
12
- aliases: :k
13
- option :yaml,
14
- type: :boolean,
15
- desc: 'stdout YAML',
16
- aliases: :y
17
- option :identity_file,
18
- type: :string,
19
- banner: '<identity_file>',
20
- desc: 'identity file',
21
- aliases: :i
22
- option :port,
23
- type: :numeric,
24
- banner: '<port>',
25
- desc: 'port',
26
- aliases: :p
8
+ option :key, type: :string, banner: '<key1,key2,..>', desc: 'inventory keys', aliases: :k
9
+ option :yaml, type: :boolean, desc: 'stdout YAML', aliases: :Y
10
+ option :identity_file, type: :string, banner: '<identity_file>', desc: 'identity file', aliases: :i
11
+ option :port, type: :numeric, banner: '<port>', desc: 'port', aliases: :p
27
12
  Koma::HostInventory.disabled_keys.each do |key|
28
- option "with-#{key}",
29
- type: :boolean,
30
- desc: "enable #{key}"
13
+ option "with-#{key}", type: :boolean, desc: "enable #{key}"
31
14
  end
32
15
  def ssh(host = nil)
33
16
  if host.nil?
34
17
  begin
35
- stdin = timeout(5) do
36
- $stdin.read
37
- end
18
+ stdin = timeout(1) { $stdin.read }
19
+ rescue Timeout::Error
20
+ end
21
+ ret = stdin.split("\n").select { |line| line =~ /^Host ([^\s\*]+)/ }.map do |line|
22
+ line =~ /^Host ([^\s]+)/
23
+ Regexp.last_match[1]
24
+ end
25
+ host = ret.join(',')
26
+ end
27
+ backend = Koma::Backend::Ssh.new(host, options)
28
+ backend.stdin = stdin if stdin
29
+ gathered = backend.gather
30
+ if options[:yaml]
31
+ puts YAML.dump(gathered)
32
+ else
33
+ puts JSON.pretty_generate(gathered)
34
+ end
35
+ end
38
36
 
37
+ desc 'run-command <host1,host2,..> <command>', 'run command on a remote machine'
38
+ option :yaml, type: :boolean, desc: 'stdout YAML', aliases: :Y
39
+ option :identity_file, type: :string, banner: '<identity_file>', desc: 'identity file', aliases: :i
40
+ option :port, type: :numeric, banner: '<port>', desc: 'port', aliases: :p
41
+ def run_command(host = nil, command = nil)
42
+ if stdin.nil?
43
+ STDERR.puts 'ERROR: "koma run-command" was called with no arguments'
44
+ STDERR.puts 'Usage: "koma run-command <host1,host2,..> <command>"'
45
+ return
46
+ end
47
+ if command.nil?
48
+ command = host
49
+ begin
50
+ stdin = timeout(1) { $stdin.read }
39
51
  rescue Timeout::Error
40
- STDERR.puts 'ERROR: "koma ssh" was called with no arguments'
41
- STDERR.puts 'Usage: "koma ssh <host1,host2,..>"'
52
+ end
53
+ if stdin.nil?
54
+ STDERR.puts 'ERROR: "koma run-command" was called with no arguments'
55
+ STDERR.puts 'Usage: "koma run-command <host1,host2,..> <command>"'
42
56
  return
43
57
  end
44
58
  ret = stdin.split("\n").select { |line| line =~ /^Host ([^\s\*]+)/ }.map do |line|
@@ -49,27 +63,19 @@ module Koma
49
63
  end
50
64
  backend = Koma::Backend::Ssh.new(host, options)
51
65
  backend.stdin = stdin if stdin
66
+ gathered = backend.run_command(command)
52
67
  if options[:yaml]
53
- puts YAML.dump(backend.gather)
68
+ puts YAML.dump(gathered)
54
69
  else
55
- puts JSON.pretty_generate(backend.gather)
70
+ puts JSON.pretty_generate(gathered)
56
71
  end
57
72
  end
58
73
 
59
74
  desc 'exec', 'stdout local host inventory'
60
- option :key,
61
- type: :string,
62
- banner: '<key1,key2,..>',
63
- desc: 'inventory keys',
64
- aliases: :k
65
- option :yaml,
66
- type: :boolean,
67
- desc: 'stdout YAML',
68
- aliases: :y
75
+ option :key, type: :string, banner: '<key1,key2,..>', desc: 'inventory keys', aliases: :k
76
+ option :yaml, type: :boolean, desc: 'stdout YAML', aliases: :Y
69
77
  Koma::HostInventory.disabled_keys.each do |key|
70
- option "with-#{key}",
71
- type: :boolean,
72
- desc: "enable #{key}"
78
+ option "with-#{key}", type: :boolean, desc: "enable #{key}"
73
79
  end
74
80
  def exec
75
81
  backend = Koma::Backend::Exec.new(nil, options)
data/lib/koma/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Koma
2
- VERSION = '0.9.0'
2
+ VERSION = '0.10.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - k1LoW
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-26 00:00:00.000000000 Z
11
+ date: 2016-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor