kanmon 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 3e7a715138c4ecc5d5d10a150bfca964d497e937839f6b66a9d05e514b90053b
4
- data.tar.gz: 5ccf83c54ea6098279fe7029a7f09f46e4da9f214669ed18168e272dd77bfa87
3
+ metadata.gz: 5b4d103ec610dd12ece7cb2441693380c913b50720259188c6bfb82d71bcea41
4
+ data.tar.gz: ee97f396d6053055d5393e4724f8a38261349ff4720ee3c021553a003cefd56e
5
5
  SHA512:
6
- metadata.gz: 6d594dcb84cd1bf89431bcf8f78f4e5e24203adb0bb8b64ee274df6afab7f910a193660ae59630890110966eea4209a6308b26dc25c4dcaeb17612aeebff767c
7
- data.tar.gz: 386aacf6e68e101384882f23ae909719ec4717b52b254d4e6a285123a71fccebe7bf2e58e0531181211f2bf8e7099acc729c9f58bcb192f12aeaad898656add9
6
+ metadata.gz: 39eca19f86316209ac6774dc589bc544f79954b1efeb795723dfdff19322102b1409f6df93e1d24784754b1c9a6f8848ecf1b5fea19c1ea2a98ae57e1b2cbdfd
7
+ data.tar.gz: cab9be8e3106cb3248226495f1bb551507575ca2e8d9cc60b93c022b1d7def4ea23c87d314ea2c1d5ff72bbdc9b2c18292357eb7b8ba2b95e90df80d8fa8b530
data/lib/kanmon/cli.rb CHANGED
@@ -1,58 +1,35 @@
1
1
  require "thor"
2
+ require "shellwords"
2
3
 
3
- require "kanmon/myip"
4
+ require "kanmon/securitygroup"
4
5
 
5
6
  module Kanmon
6
7
  class CLI < Thor
7
- class_option :file, aliases: "f", type: :string, default: "kanmon.yml", banner: "FILE", desc: "Load configure from FILE"
8
+ class_option :kanmon_config, aliases: "f", type: :string, default: "kanmon.yml", banner: "FILE", desc: "Load configure from FILE"
8
9
 
9
10
  desc "open", "Commands about add rules to SecurityGroup"
10
11
  def open
11
- myip = Kanmon::MyIP.get
12
- rule = {
13
- direction: "ingress",
14
- port_range_min: 22,
15
- port_range_max: 22,
16
- ethertype: "IPv4",
17
- protocol: "tcp",
18
- remote_ip_prefix: "#{myip}/32",
19
- security_group_id: @config["security_group"],
20
- }
21
-
22
- Yao::SecurityGroupRule.create(rule)
12
+ @sg.open
23
13
  puts "Success!!"
24
14
  end
25
15
 
26
16
  desc "close", "Commands about delete rules from SecurityGroup"
27
17
  def close
28
- myip = Kanmon::MyIP.get
29
- tenant_id = Yao.current_tenant_id
30
-
31
- rule = {
32
- direction: "ingress",
33
- port_range_min: 22,
34
- port_range_max: 22,
35
- ethertype: "IPv4",
36
- protocol: "tcp",
37
- security_group_id: @config["security_group"],
38
- tenant_id: tenant_id,
39
- remote_ip_prefix: "#{myip}/32"
40
- }
41
-
42
- result = Yao::SecurityGroupRule.list(rule)
18
+ @sg.close
19
+ puts "Success!!"
20
+ end
43
21
 
44
- if result.empty?
45
- puts "Not found"
46
- exit(1)
47
- end
22
+ desc "ssh HOSTNAME", "Commands about exec ssh"
23
+ def ssh(*args)
24
+ invoke CLI, [:exec], args.unshift("ssh")
25
+ end
48
26
 
49
- result.each do |rule|
50
- id = rule.id
51
- puts "Delete #{id}"
52
- Yao::SecurityGroupRule.destroy(id)
27
+ desc "exec COMMAND", "Commands about open, exec command, close"
28
+ def exec(*args)
29
+ @sg.open do
30
+ command = Shellwords.join(args)
31
+ Process.wait spawn(command)
53
32
  end
54
-
55
- puts "Success!!"
56
33
  end
57
34
 
58
35
  desc "version", "Commands about show version"
@@ -64,7 +41,8 @@ module Kanmon
64
41
  def invoke_command(command, *args)
65
42
  unless %w(help version).include?(command.name)
66
43
  Kanmon.init_yao
67
- @config = Kanmon.load_config(options[:file])
44
+ @config = Kanmon.load_config(options[:kanmon_config])
45
+ @sg = SecurityGroup.new(@config["security_group"])
68
46
  end
69
47
 
70
48
  super
@@ -0,0 +1,58 @@
1
+ require "yao"
2
+
3
+ require "kanmon/myip"
4
+
5
+ module Kanmon
6
+ class SecurityGroup
7
+ def initialize(id, ip = nil)
8
+ @id = id
9
+ @ip = ip || Kanmon::MyIP.get
10
+ @tenant_id = Yao.current_tenant_id
11
+ end
12
+
13
+ def open
14
+ result = Yao::SecurityGroupRule.create(rule)
15
+ puts "Added Rule: #{result.id}"
16
+
17
+ if block_given?
18
+ begin
19
+ yield
20
+ ensure
21
+ delete_rules([result])
22
+ end
23
+ end
24
+ end
25
+
26
+ def close
27
+ result = Yao::SecurityGroupRule.list(rule)
28
+
29
+ if result.empty?
30
+ puts "Rule not found"
31
+ else
32
+ delete_rules(result)
33
+ end
34
+ end
35
+
36
+ private
37
+ def rule
38
+ {
39
+ direction: "ingress",
40
+ port_range_min: 22,
41
+ port_range_max: 22,
42
+ ethertype: "IPv4",
43
+ protocol: "tcp",
44
+ security_group_id: @id,
45
+ tenant_id: @tenant_id,
46
+ remote_ip_prefix: "#{@ip}/32"
47
+ }
48
+ end
49
+
50
+ def delete_rules(rules)
51
+ rules.each do |rule|
52
+ id = rule.id
53
+ puts "Delete Rule: #{id}"
54
+ Yao::SecurityGroupRule.destroy(id)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Kanmon
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kanmon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuki Koya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-23 00:00:00.000000000 Z
11
+ date: 2018-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yao
@@ -102,6 +102,7 @@ files:
102
102
  - lib/kanmon.rb
103
103
  - lib/kanmon/cli.rb
104
104
  - lib/kanmon/myip.rb
105
+ - lib/kanmon/securitygroup.rb
105
106
  - lib/kanmon/version.rb
106
107
  homepage: https://github.com/buty4649/kanmon/
107
108
  licenses: []