ec2-host 0.0.9 → 0.1.0

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: 0dc25b078ef99bec51f8d6d3d34c847c12482dab
4
- data.tar.gz: 942cbde8877b6f20681cd3d098bf0ae04133ea3e
3
+ metadata.gz: d0bc795de2c9dd28b237cf8a442d13434532d13e
4
+ data.tar.gz: 417e73f78134b37a660bc614dfc07e74ebfd1890
5
5
  SHA512:
6
- metadata.gz: 0c5e37497cbc0b41c888ed077542a1b5659631152f0de62678ad2ed5db6df45cfd77d7ab54efa093d603ab3fb638ed2cbacb626e60753d0af3c077e66b11135e
7
- data.tar.gz: 8bb986e56f4508a652942a16e28ee0ba9f0f110d40e6640d064d866e76c0d983873b9336f4a2d581a81b156849e446e0056aafc2d2f561cee544618b04fa1529
6
+ metadata.gz: 19efa5260b4e445e9f3024c7228a02e8477736feb678aa52ed6596846551b520f74c97f82c04044fa93d4f3b52b2322179379db63841f1622c2001daca2f12fc
7
+ data.tar.gz: 720fa2e40471c42fd373a6bba4a00dd16eb596d548f325e376fa693da6cb6d54e0610537190d6d9a26d82d6e3bca99e8b16cba4b44524aa6f426103a65264ffc
data/CHANGELOG.md CHANGED
@@ -1,8 +1,15 @@
1
+ # 0.1.0 (2015/08/11)
2
+
3
+ Enhancements:
4
+
5
+ * Alias `--json` to `-j` option
6
+ * Possible speed up for instance_id, role, role1, role2, role3 condition
7
+
1
8
  # 0.0.9 (2015/08/10)
2
9
 
3
10
  Enhancements:
4
11
 
5
- * Add --state and --monitoring option
12
+ * Add `--state` and `--monitoring` option
6
13
  * Remove terminated instances from list as default
7
14
  * Support nested key such as instance.instance_id (as library)
8
15
 
data/README.md CHANGED
@@ -58,13 +58,17 @@ Usage:
58
58
 
59
59
  Options:
60
60
  -h, [--hostname=one two three] # name or private_dns_name
61
- --usage, [--role=one two three] # role
61
+ -r, --usage, -u, [--role=one two three] # role
62
62
  --r1, --usage1, --u1, [--role1=one two three] # role1, the 1st part of role delimited by :
63
63
  --r2, --usage2, --u2, [--role2=one two three] # role2, the 2nd part of role delimited by :
64
64
  --r3, --usage3, --u3, [--role3=one two three] # role3, the 3rd part of role delimited by :
65
+ [--instance-id=one two three] # instance_id
66
+ [--state=one two three] # state
67
+ [--monitoring=one two three] # monitoring
65
68
  --ip, [--private-ip], [--no-private-ip] # show private ip address instead of hostname
66
69
  [--public-ip], [--no-public-ip] # show public ip address instead of hostname
67
70
  -i, [--info], [--no-info] # show host info, not only hostname
71
+ -j, [--json], [--no-json] # show detailed host info in json
68
72
  [--debug], [--no-debug] # debug mode
69
73
 
70
74
  Search EC2 hosts
data/ec2-host.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "ec2-host"
3
- gem.version = '0.0.9'
3
+ gem.version = '0.1.0'
4
4
  gem.author = ['Naotoshi Seo']
5
5
  gem.email = ['sonots@gmail.com']
6
6
  gem.homepage = 'https://github.com/sonots/ec2-host'
data/lib/ec2/host.rb CHANGED
@@ -40,7 +40,7 @@ class EC2
40
40
 
41
41
  # @return [Host::Data] representing myself
42
42
  def self.me
43
- new(instance_id: ClientUtil.get_instance_id).each do |d|
43
+ new(instance_id: ClientUtil.instance_id).each do |d|
44
44
  return d
45
45
  end
46
46
  raise 'Not Found'
@@ -92,7 +92,7 @@ class EC2
92
92
  # @yieldparam [Host::Data] data entry
93
93
  def each(&block)
94
94
  @conditions.each do |condition|
95
- search(ClientUtil.get_instances, condition, &block)
95
+ search(ClientUtil.instances(condition), condition, &block)
96
96
  end
97
97
  return self
98
98
  end
data/lib/ec2/host/cli.rb CHANGED
@@ -51,6 +51,7 @@ class EC2
51
51
  :type => :boolean,
52
52
  :desc => "show host info, not only hostname"
53
53
  option :json,
54
+ :aliases => %w[-j],
54
55
  :type => :boolean,
55
56
  :desc => "show detailed host info in json"
56
57
  option :debug,
@@ -4,15 +4,33 @@ require 'aws-sdk'
4
4
  class EC2
5
5
  class Host
6
6
  class ClientUtil
7
- def self.get_instances
8
- # I do not use describe_instances(filter:) because it does not support array tag ..
9
- return @instances if @instances
10
- Aws.config.update(region: Config.aws_region, credentials: Config.aws_credentials)
11
- ec2 = Aws::EC2::Client.new
12
- @instances = ec2.describe_instances.reservations.map(&:instances).flatten
7
+ def self.ec2(reload = false)
8
+ if @ec2.nil? || reload
9
+ Aws.config.update(region: Config.aws_region, credentials: Config.aws_credentials)
10
+ @ec2 = Aws::EC2::Client.new
11
+ end
12
+ @ec2
13
+ end
14
+
15
+ def self.instances(condition)
16
+ describe_instances =
17
+ if instance_id = condition[:instance_id]
18
+ ec2.describe_instances(instance_ids: Array(instance_id))
19
+ elsif role = (condition[:role] || condition[:usage]) and role.size == 1
20
+ ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role.first}*"]}])
21
+ elsif role1 = (condition[:role1] || condition[:usage1]) and role1.size == 1
22
+ ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role1.first}*"]}])
23
+ elsif role2 = (condition[:role2] || condition[:usage2]) and role2.size == 1
24
+ ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role2.first}*"]}])
25
+ elsif role3 = (condition[:role3] || condition[:usage3]) and role3.size == 1
26
+ ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role3.first}*"]}])
27
+ else
28
+ ec2.describe_instances
29
+ end
30
+ describe_instances.reservations.map(&:instances).flatten
13
31
  end
14
32
 
15
- def self.get_instance_id
33
+ def self.instance_id
16
34
  return @instance_id if @instance_id
17
35
  begin
18
36
  http_conn = Net::HTTP.new('169.254.169.254')
data/spec/host_spec.rb CHANGED
@@ -39,6 +39,14 @@ describe EC2::Host do
39
39
  end
40
40
  end
41
41
 
42
+ context 'by instance_id' do
43
+ let(:hosts) { EC2::Host.new(instance_id: 'i-85900780').to_a }
44
+ let(:subject) { hosts.first }
45
+ it_should_behave_like 'host'
46
+ it { expect(hosts.size).to eq(1) }
47
+ it { expect(subject.hostname).to eq('test') }
48
+ end
49
+
42
50
  context 'by hostname' do
43
51
  let(:hosts) { EC2::Host.new(hostname: 'test').to_a }
44
52
  let(:subject) { hosts.first }
@@ -49,6 +57,11 @@ describe EC2::Host do
49
57
 
50
58
  context 'by role' do
51
59
  context 'by a role' do
60
+ let(:subject) { EC2::Host.new(role: 'admin:ami').first }
61
+ it_should_behave_like 'host'
62
+ end
63
+
64
+ context 'by a role1' do
52
65
  let(:subject) { EC2::Host.new(role1: 'admin').first }
53
66
  it_should_behave_like 'host'
54
67
  end
@@ -74,6 +87,11 @@ describe EC2::Host do
74
87
  # This is for DeNA use
75
88
  context 'by usage (an alias of usage)' do
76
89
  context 'by a usage' do
90
+ let(:subject) { EC2::Host.new(usage: 'admin:ami').first }
91
+ it_should_behave_like 'host'
92
+ end
93
+
94
+ context 'by a usage1' do
77
95
  let(:subject) { EC2::Host.new(usage1: 'admin').first }
78
96
  it_should_behave_like 'host'
79
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-host
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo