ec2-host 0.3.1 → 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.
@@ -1,14 +1,18 @@
1
1
  class EC2
2
2
  class Host
3
- RoleData = Struct.new(
4
- :role1, :role2, :role3
5
- )
6
-
7
3
  # Represents each role
8
4
  class RoleData
9
- def self.initialize(role)
10
- role1, role2, role3 = role.split(Config.role_tag_delimiter)
11
- self.new(role1, role2, role3)
5
+ attr_reader :role1, :role2, :role3
6
+
7
+ def initialize(role1, role2 = nil, role3 = nil)
8
+ @role1 = role1
9
+ @role2 = role2
10
+ @role3 = role3
11
+ end
12
+
13
+ def self.build(role)
14
+ role1, role2, role3 = role.split(Config.role_tag_delimiter, 3)
15
+ new(role1, role2, role3)
12
16
  end
13
17
 
14
18
  # @return [String] something like "admin:jenkins:slave"
@@ -1,13 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  shared_examples_for 'host' do
4
- it 'should have parameters' do
4
+ it 'should respond_to' do
5
5
  [ :hostname,
6
6
  :roles,
7
+ :region,
8
+ :service,
7
9
  :status,
8
10
  :tags,
9
- :service,
10
- :region,
11
11
  :instance,
12
12
  :instance_id,
13
13
  :private_ip_address,
@@ -19,7 +19,7 @@ shared_examples_for 'host' do
19
19
  :start_date,
20
20
  :usages,
21
21
  ].each do |k|
22
- expect{ subject.__send__(k) }.not_to raise_error
22
+ expect(subject.respond_to?(k)).to be_truthy
23
23
  end
24
24
  end
25
25
  end
@@ -27,48 +27,71 @@ end
27
27
  describe EC2::Host do
28
28
  describe 'options' do
29
29
  context do
30
- let(:subject) { EC2::Host.new(hostname: 'test', options: {foo:'bar'}) }
30
+ let(:subject) { EC2::Host.new(hostname: 'ec2-host-web', options: {foo:'bar'}) }
31
31
  it { expect(subject.options).to eq({foo:'bar'}) }
32
- it { expect(subject.conditions).to eq([{hostname: ['test']}]) }
32
+ it { expect(subject.conditions).to eq([{hostname: ['ec2-host-web']}]) }
33
33
  end
34
34
 
35
35
  context do
36
- let(:subject) { EC2::Host.new({hostname: 'foo'}, {hostname: 'bar'}, options: {foo:'bar'}) }
36
+ let(:subject) { EC2::Host.new({hostname: 'ec2-host-web'}, {hostname: 'ec2-host-db'}, options: {foo:'bar'}) }
37
37
  it { expect(subject.options).to eq({foo:'bar'}) }
38
- it { expect(subject.conditions).to eq([{hostname: ['foo']}, {hostname: ['bar']}]) }
38
+ it { expect(subject.conditions).to eq([{hostname: ['ec2-host-web']}, {hostname: ['ec2-host-db']}]) }
39
39
  end
40
40
  end
41
41
 
42
- describe '#get_value' do
43
- let(:hosts) { EC2::Host.new(instance_id: 'i-85900780').to_a }
44
- let(:subject) { hosts.first }
45
- it { expect(subject.get_value('instance.instance_id')).to eql('i-85900780') }
42
+ context '#to_hash' do
43
+ let(:subject) { EC2::Host.new(service: 'ec2-host').first.to_hash }
44
+
45
+ it 'keys' do
46
+ expect(subject.keys).to eq([
47
+ 'hostname',
48
+ 'roles',
49
+ 'region',
50
+ 'service',
51
+ 'status',
52
+ 'tags',
53
+ 'instance_id',
54
+ 'private_ip_address',
55
+ 'public_ip_address',
56
+ 'launch_time',
57
+ 'state',
58
+ 'monitoring'
59
+ ])
60
+ end
61
+
62
+ it 'values are not empty' do
63
+ expect(subject.values.any? {|v| v.nil? or (v.respond_to?(:empty?) and v.empty?) }).to be_falsey
64
+ end
46
65
  end
47
66
 
48
- context 'by instance_id' do
49
- let(:hosts) { EC2::Host.new(instance_id: 'i-85900780').to_a }
67
+ context 'by hostname' do
68
+ let(:hosts) { EC2::Host.new(hostname: 'ec2-host-web').to_a }
50
69
  let(:subject) { hosts.first }
51
70
  it_should_behave_like 'host'
52
71
  it { expect(hosts.size).to eq(1) }
53
- it { expect(subject.hostname).to eq('test') }
72
+ it { expect(subject.hostname).to eq('ec2-host-web') }
54
73
  end
55
74
 
56
- context 'by hostname' do
57
- let(:hosts) { EC2::Host.new(hostname: 'test').to_a }
58
- let(:subject) { hosts.first }
59
- it_should_behave_like 'host'
60
- it { expect(hosts.size).to eq(1) }
61
- it { expect(subject.hostname).to eq('test') }
75
+ context 'by instance_id' do
76
+ let(:instance_id) { EC2::Host.new(service: 'ec2-host').first.instance_id }
77
+
78
+ context 'by instance_id' do
79
+ let(:hosts) { EC2::Host.new(instance_id: instance_id).to_a }
80
+ let(:subject) { hosts.first }
81
+ it_should_behave_like 'host'
82
+ it { expect(hosts.size).to eq(1) }
83
+ it { expect(subject.instance_id).to eq(instance_id) }
84
+ end
62
85
  end
63
86
 
64
87
  context 'by role' do
65
88
  context 'by a role' do
66
- let(:subject) { EC2::Host.new(role: 'admin:ami').first }
89
+ let(:subject) { EC2::Host.new(role: 'web:test').first }
67
90
  it_should_behave_like 'host'
68
91
  end
69
92
 
70
93
  context 'by a role1' do
71
- let(:subject) { EC2::Host.new(role1: 'admin').first }
94
+ let(:subject) { EC2::Host.new(role1: 'web').first }
72
95
  it_should_behave_like 'host'
73
96
  end
74
97
 
@@ -76,12 +99,13 @@ describe EC2::Host do
76
99
  let(:hosts) {
77
100
  EC2::Host.new(
78
101
  {
79
- role1: 'admin',
80
- role2: 'ami',
102
+ role1: 'web',
103
+ role2: 'test',
81
104
  },
82
105
  {
83
- role1: 'isucon4',
84
- }
106
+ role1: 'db',
107
+ role2: 'test',
108
+ },
85
109
  ).to_a
86
110
  }
87
111
  let(:subject) { hosts.first }
@@ -90,15 +114,15 @@ describe EC2::Host do
90
114
  end
91
115
  end
92
116
 
93
- # This is for DeNA use
94
- context 'by usage (an alias of usage)' do
117
+ # for compatibility with dino-host
118
+ context 'by usage' do
95
119
  context 'by a usage' do
96
- let(:subject) { EC2::Host.new(usage: 'admin:ami').first }
120
+ let(:subject) { EC2::Host.new(usage: 'web:test').first }
97
121
  it_should_behave_like 'host'
98
122
  end
99
123
 
100
124
  context 'by a usage1' do
101
- let(:subject) { EC2::Host.new(usage1: 'admin').first }
125
+ let(:subject) { EC2::Host.new(usage1: 'web').first }
102
126
  it_should_behave_like 'host'
103
127
  end
104
128
 
@@ -106,12 +130,13 @@ describe EC2::Host do
106
130
  let(:hosts) {
107
131
  EC2::Host.new(
108
132
  {
109
- usage1: 'admin',
110
- usage2: 'ami',
133
+ usage1: 'web',
134
+ usage2: 'test',
111
135
  },
112
136
  {
113
- usage1: 'isucon4',
114
- }
137
+ usage1: 'db',
138
+ usage2: 'test',
139
+ },
115
140
  ).to_a
116
141
  }
117
142
  let(:subject) { hosts.first }
@@ -120,7 +145,7 @@ describe EC2::Host do
120
145
  end
121
146
  end
122
147
 
123
- context 'by status' do
148
+ context 'by status (optional array tags)' do
124
149
  context 'by a status' do
125
150
  let(:subject) { EC2::Host.new(status: :active).first }
126
151
  it_should_behave_like 'host'
@@ -146,14 +171,14 @@ describe EC2::Host do
146
171
  end
147
172
  end
148
173
 
149
- context 'by service' do
174
+ context 'by service (optional string tags)' do
150
175
  context 'by a service' do
151
- let(:subject) { EC2::Host.new(service: 'isucon4').first }
176
+ let(:subject) { EC2::Host.new(service: 'ec2-host').first }
152
177
  it_should_behave_like 'host'
153
178
  end
154
179
 
155
180
  context 'by multiple services (or)' do
156
- let(:hosts) { EC2::Host.new(service: ['test', 'isucon4']) }
181
+ let(:hosts) { EC2::Host.new(service: ['test', 'ec2-host']) }
157
182
  let(:subject) { hosts.first }
158
183
  it_should_behave_like 'host'
159
184
  end
@@ -161,18 +186,18 @@ describe EC2::Host do
161
186
 
162
187
  context 'by region' do
163
188
  context 'by a region' do
164
- let(:subject) { EC2::Host.new(region: 'ap-northeast-1').first }
189
+ let(:subject) { EC2::Host.new(region: ENV['AWS_DEFAULT_REGION']).first }
165
190
  it_should_behave_like 'host'
166
191
  end
167
192
 
168
193
  context 'by multiple regions (or)' do
169
- let(:hosts) { EC2::Host.new(region: ['ap-northeast-1']) }
194
+ let(:hosts) { EC2::Host.new(region: [ENV['AWS_DEFAULT_REGION']]) }
170
195
  let(:subject) { hosts.first }
171
196
  it_should_behave_like 'host'
172
197
  end
173
198
  end
174
199
 
175
- context 'by tags' do
200
+ context 'by tags (optional array tags)' do
176
201
  context 'by a tag' do
177
202
  let(:subject) { EC2::Host.new(tags: 'master').first }
178
203
  it_should_behave_like 'host'
@@ -1,12 +1,12 @@
1
1
  ENV['RACK_ENV'] = 'test'
2
2
  Bundler.require :test
3
3
 
4
- require 'simplecov'
5
- SimpleCov.start do
6
- add_filter 'vendor/'
7
- add_filter 'spec/'
8
- add_group 'libs', 'lib'
9
- end
4
+ # require 'simplecov'
5
+ # SimpleCov.start do
6
+ # add_filter 'vendor/'
7
+ # add_filter 'spec/'
8
+ # add_group 'libs', 'lib'
9
+ # end
10
10
 
11
11
  Bundler.require :default # <- need this *after* simplecov
12
12
  require 'pry'
@@ -0,0 +1,39 @@
1
+ provider "aws" {
2
+ }
3
+
4
+ data "aws_ami" "ubuntu" {
5
+ most_recent = true
6
+ filter {
7
+ name = "name"
8
+ values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
9
+ }
10
+ filter {
11
+ name = "virtualization-type"
12
+ values = ["hvm"]
13
+ }
14
+ owners = ["099720109477"] # Canonical
15
+ }
16
+
17
+ resource "aws_instance" "ec2-host-web" {
18
+ ami = "${data.aws_ami.ubuntu.id}"
19
+ instance_type = "t2.nano"
20
+ tags {
21
+ Name = "ec2-host-web"
22
+ Roles = "foo,web:test"
23
+ Service = "ec2-host"
24
+ Status = "reserve"
25
+ Tags = "standby"
26
+ }
27
+ }
28
+
29
+ resource "aws_instance" "ec2-host-db" {
30
+ ami = "${data.aws_ami.ubuntu.id}"
31
+ instance_type = "t2.nano"
32
+ tags {
33
+ Name = "ec2-host-db"
34
+ Roles = "foo,db:test"
35
+ Service = "ec2-host"
36
+ Status = "active"
37
+ Tags = "master"
38
+ }
39
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-host
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-21 00:00:00.000000000 Z
11
+ date: 2016-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: hashie
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: dotenv
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -173,29 +159,53 @@ extensions: []
173
159
  extra_rdoc_files: []
174
160
  files:
175
161
  - ".gitignore"
162
+ - ".yardopts"
176
163
  - CHANGELOG.md
177
164
  - Gemfile
178
165
  - LICENSE
179
166
  - README.md
180
167
  - Rakefile
181
168
  - bin/ec2-host
182
- - doc.sh
169
+ - docs/EC2.html
170
+ - docs/EC2/Host.html
171
+ - docs/EC2/Host/CLI.html
172
+ - docs/EC2/Host/Config.html
173
+ - docs/EC2/Host/EC2Client.html
174
+ - docs/EC2/Host/HashUtil.html
175
+ - docs/EC2/Host/HostData.html
176
+ - docs/EC2/Host/RoleData.html
177
+ - docs/EC2/Host/StringUtil.html
178
+ - docs/_index.html
179
+ - docs/class_list.html
180
+ - docs/css/common.css
181
+ - docs/css/full_list.css
182
+ - docs/css/style.css
183
+ - docs/file.LICENSE.html
184
+ - docs/file.README.html
185
+ - docs/file_list.html
186
+ - docs/frames.html
187
+ - docs/index.html
188
+ - docs/js/app.js
189
+ - docs/js/full_list.js
190
+ - docs/js/jquery.js
191
+ - docs/method_list.html
192
+ - docs/top-level-namespace.html
183
193
  - ec2-host.gemspec
184
194
  - example/aws-sdk.rb
195
+ - example/example.conf
185
196
  - example/example.rb
186
197
  - lib/ec2-host.rb
187
198
  - lib/ec2/host.rb
188
199
  - lib/ec2/host/cli.rb
189
- - lib/ec2/host/client_util.rb
190
200
  - lib/ec2/host/config.rb
201
+ - lib/ec2/host/ec2_client.rb
191
202
  - lib/ec2/host/hash_util.rb
192
203
  - lib/ec2/host/host_data.rb
193
204
  - lib/ec2/host/role_data.rb
194
205
  - lib/ec2/host/string_util.rb
195
- - sample.conf
196
- - spec/README.md
197
206
  - spec/host_spec.rb
198
207
  - spec/spec_helper.rb
208
+ - terraform.tf
199
209
  homepage: https://github.com/sonots/ec2-host
200
210
  licenses:
201
211
  - MIT
@@ -216,12 +226,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
226
  version: '0'
217
227
  requirements: []
218
228
  rubyforge_project:
219
- rubygems_version: 2.5.1
229
+ rubygems_version: 2.5.2
220
230
  signing_key:
221
231
  specification_version: 4
222
232
  summary: Search hosts on AWS EC2
223
233
  test_files:
224
- - spec/README.md
225
234
  - spec/host_spec.rb
226
235
  - spec/spec_helper.rb
227
236
  has_rdoc:
data/doc.sh DELETED
@@ -1,5 +0,0 @@
1
- #!/bin/sh
2
- bundle exec yardoc
3
- rsync -au --delete doc/ /tmp/doc
4
- git checkout gh-pages
5
- rsync -au --delete /tmp/doc/ doc
@@ -1,44 +0,0 @@
1
- require 'net/http'
2
- require 'aws-sdk'
3
-
4
- class EC2
5
- class Host
6
- class ClientUtil
7
- def self.ec2(reload = false)
8
- if @ec2.nil? || reload
9
- @ec2 = Aws::EC2::Client.new(region: Config.aws_region, credentials: Config.aws_credentials)
10
- end
11
- @ec2
12
- end
13
-
14
- def self.instances(condition)
15
- describe_instances =
16
- if instance_id = condition[:instance_id]
17
- ec2.describe_instances(instance_ids: Array(instance_id))
18
- elsif role = (condition[:role] || condition[:usage]) and role.size == 1
19
- ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role.first}*"]}])
20
- elsif role1 = (condition[:role1] || condition[:usage1]) and role1.size == 1
21
- ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role1.first}*"]}])
22
- elsif role2 = (condition[:role2] || condition[:usage2]) and role2.size == 1
23
- ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role2.first}*"]}])
24
- elsif role3 = (condition[:role3] || condition[:usage3]) and role3.size == 1
25
- ec2.describe_instances(filters: [{name: "tag:#{Config.roles_tag}", values: ["*#{role3.first}*"]}])
26
- else
27
- ec2.describe_instances
28
- end
29
- describe_instances.reservations.map(&:instances).flatten
30
- end
31
-
32
- def self.instance_id
33
- return @instance_id if @instance_id
34
- begin
35
- http_conn = Net::HTTP.new('169.254.169.254')
36
- http_conn.open_timeout = 5
37
- @instance_id = http_conn.start {|http| http.get('/latest/meta-data/instance-id').body }
38
- rescue Net::OpenTimeout
39
- raise "HTTP connection to 169.254.169.254 is timeout. Probably, not an EC2 instance?"
40
- end
41
- end
42
- end
43
- end
44
- end