koma 0.4.0 → 0.5.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: 3eee13c68f078a3f02b2d19e063eff87acd4e0fb
4
- data.tar.gz: 3f0b322b862a2059da3ee03eb62fdbb8b555b47e
3
+ metadata.gz: 383af2846d313675f2b9194c6c7a1ea1219911d6
4
+ data.tar.gz: 732fc3eb5a5e422ea97ecaac95e286d6f2c2df06
5
5
  SHA512:
6
- metadata.gz: 3349038bfe0a67bc48dc05eb1568efd4d5c1b6b8a84fa02eb27ffda0c723f78a395a9a2ae90064c1c52acb7d6bf966dae359c17ff1a3cc9c3cef10af9d5071cc
7
- data.tar.gz: 5115f5fbd67158edb19ab6ea258fca8447ac14f32d1714b8a179c98512ed10f977dd50271b13e3dc69c94b3fd82b752396f1bf2855d265c20c785bbf1be1cb70
6
+ metadata.gz: 4f676fd8818f751fc9e4d0423670807570f81d66241ae04a91b6f92aa8c99c16f06301902c1cc7d5bc70d7c285eae81bd31bdc8c945b665408e5ac76a14c350f
7
+ data.tar.gz: d61ba847dcf476e333b466efe99dd1d609dd14d3b9e2690a0db2e19ab33538670981d8e65eddd3c0980af9665f1caf616e119cc6882b29474ada52392a297292
data/README.md CHANGED
@@ -32,15 +32,15 @@ $ koma ssh example.com
32
32
 
33
33
  And get stdout like [this](stdout_sample.json).
34
34
 
35
- ### Multi host gathering
35
+ ### Gather multi host
36
36
 
37
37
  ```sh
38
38
  $ koma ssh example.com,example.jp
39
39
  {
40
- "example.com" : {
40
+ "example.com": {
41
41
  ...
42
42
  },
43
- "example.jp" : {
43
+ "example.jp": {
44
44
  ...
45
45
  }
46
46
  }
@@ -0,0 +1,32 @@
1
+ include Specinfra::Helper::Set
2
+
3
+ class Koma::Backend::Base
4
+ attr_reader :host, :options
5
+
6
+ def initialize(host, options)
7
+ @host = host
8
+ @options = options
9
+ end
10
+
11
+ def out(key = nil)
12
+ out = {}
13
+ keys = if key.nil?
14
+ inventory_keys
15
+ else
16
+ key.split(',')
17
+ end
18
+ keys.each do |k|
19
+ begin
20
+ out[k] = Specinfra.backend.host_inventory[k]
21
+ out[k] = Specinfra.backend.host_inventory[k].inspect if k == 'ec2'
22
+ rescue NotImplementedError
23
+ out[k] = nil
24
+ end
25
+ end
26
+ out
27
+ end
28
+
29
+ def inventory_keys
30
+ Specinfra::HostInventory.inventory_keys
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ module Koma
2
+ module Backend
3
+ class Exec < Base
4
+ def gather
5
+ set :backend, :exec
6
+ result = out(options[:key])
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,36 @@
1
+ require 'parallel'
2
+
3
+ module Koma
4
+ module Backend
5
+ class Ssh < Base
6
+ attr_reader :host, :options
7
+
8
+ def gather
9
+ if host.include?(',')
10
+ list = host.split(',')
11
+ results = Parallel.map(list, in_thread: 4) do |h|
12
+ gather_via_ssh(h, options)
13
+ end
14
+ arr = [list, results].transpose
15
+ result = Hash[*arr.flatten]
16
+ else
17
+ result = gather_via_ssh(host, options)
18
+ end
19
+ result
20
+ end
21
+
22
+ def gather_via_ssh(host, options)
23
+ user, host = host.split('@') if host.include?('@')
24
+ set :backend, :ssh
25
+ set :host, host
26
+ set :request_pty, true
27
+ ssh_options = Net::SSH::Config.for(host)
28
+ ssh_options[:user] = user if user
29
+ ssh_options[:keys] = [options[:identity_file]] if options[:identity_file]
30
+ ssh_options[:port] = options[:port] if options[:port]
31
+ set :ssh_options, ssh_options
32
+ out(options[:key])
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ module Koma
2
+ module Backend
3
+ end
4
+ end
5
+
6
+ require 'koma/backend/base'
7
+ require 'koma/backend/exec'
8
+ require 'koma/backend/ssh'
data/lib/koma/cli.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'thor'
2
2
  require 'json'
3
- require 'parallel'
4
-
5
- include Specinfra::Helper::Set
3
+ require 'yaml'
6
4
 
7
5
  module Koma
8
6
  class CLI < Thor
@@ -23,17 +21,8 @@ module Koma
23
21
  desc: 'port',
24
22
  aliases: :p
25
23
  def ssh(host)
26
- if host.include?(',')
27
- list = host.split(',')
28
- results = Parallel.map(list, in_thread: 4) do |h|
29
- ssh_out(h, options)
30
- end
31
- arr = [list, results].transpose
32
- puts JSON.pretty_generate(Hash[*arr.flatten])
33
- else
34
- result = ssh_out(host, options)
35
- puts JSON.pretty_generate(result)
36
- end
24
+ backend = Koma::Backend::Ssh.new(host, options)
25
+ puts JSON.pretty_generate(backend.gather)
37
26
  end
38
27
 
39
28
  desc 'exec', 'stdout local host inventory'
@@ -43,53 +32,15 @@ module Koma
43
32
  desc: 'inventory keys',
44
33
  aliases: :k
45
34
  def exec
46
- set :backend, :exec
47
- result = out(options[:key])
48
- puts JSON.pretty_generate(result)
35
+ backend = Koma::Backend::Exec.new(nil, options)
36
+ puts JSON.pretty_generate(backend.gather)
49
37
  end
50
38
 
51
39
  desc 'keys', 'host inventory keys'
52
40
  def keys
53
- inventory_keys.each do |key|
41
+ Specinfra::HostInventory.inventory_keys.each do |key|
54
42
  puts key
55
43
  end
56
44
  end
57
-
58
- private
59
-
60
- def inventory_keys
61
- Specinfra::HostInventory.inventory_keys
62
- end
63
-
64
- def ssh_out(host, options)
65
- user, host = host.split('@') if host.include?('@')
66
- set :backend, :ssh
67
- set :host, host
68
- set :request_pty, true
69
- ssh_options = Net::SSH::Config.for(host)
70
- ssh_options[:user] = user if user
71
- ssh_options[:keys] = [options[:identity_file]] if options[:identity_file]
72
- ssh_options[:port] = options[:port] if options[:port]
73
- set :ssh_options, ssh_options
74
- out(options[:key])
75
- end
76
-
77
- def out(key = nil)
78
- out = {}
79
- keys = if key.nil?
80
- inventory_keys
81
- else
82
- key.split(',')
83
- end
84
- keys.each do |k|
85
- begin
86
- out[k] = Specinfra.backend.host_inventory[k]
87
- out[k] = Specinfra.backend.host_inventory[k].inspect if k == 'ec2'
88
- rescue NotImplementedError
89
- out[k] = nil
90
- end
91
- end
92
- out
93
- end
94
45
  end
95
46
  end
data/lib/koma/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Koma
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
data/lib/koma.rb CHANGED
@@ -3,6 +3,7 @@ require 'net/ssh'
3
3
  require 'specinfra'
4
4
  require 'specinfra/helper/set'
5
5
  require 'koma/ext'
6
+ require 'koma/backend'
6
7
  require 'koma/cli'
7
8
  require 'koma/version'
8
9
 
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.4.0
4
+ version: 0.5.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-14 00:00:00.000000000 Z
11
+ date: 2016-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -171,6 +171,10 @@ files:
171
171
  - exe/koma
172
172
  - koma.gemspec
173
173
  - lib/koma.rb
174
+ - lib/koma/backend.rb
175
+ - lib/koma/backend/base.rb
176
+ - lib/koma/backend/exec.rb
177
+ - lib/koma/backend/ssh.rb
174
178
  - lib/koma/cli.rb
175
179
  - lib/koma/ext.rb
176
180
  - lib/koma/ext/specinfra/command/base/inventory.rb