ec2-mini 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +31 -0
- data/bin/ec2-mini +6 -1
- data/lib/ec2-mini/cli.rb +131 -101
- data/lib/ec2-mini/version.rb +1 -1
- data/spec/lib/cil_spec.rb +17 -0
- metadata +4 -2
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Install
|
2
|
+
---
|
3
|
+
|
4
|
+
`gem install ec2-mini`
|
5
|
+
|
6
|
+
Setup
|
7
|
+
---
|
8
|
+
|
9
|
+
vi ~/.ec2-mini
|
10
|
+
|
11
|
+
```
|
12
|
+
access_key_id: 'XXX'
|
13
|
+
secret_access_key: 'XXX'
|
14
|
+
region: 'XXX'
|
15
|
+
```
|
16
|
+
|
17
|
+
Command
|
18
|
+
---
|
19
|
+
|
20
|
+
`ec2-mini [role] [command]`
|
21
|
+
|
22
|
+
Example
|
23
|
+
---
|
24
|
+
|
25
|
+
`ec2-mini ap-server backup`
|
26
|
+
|
27
|
+
`ec2-mini ap-server +3`
|
28
|
+
|
29
|
+
`ec2-mini ap-server -1`
|
30
|
+
|
31
|
+
`ec2-mini ap-server count`
|
data/bin/ec2-mini
CHANGED
@@ -4,4 +4,9 @@ $:.unshift File.expand_path("../../lib", __FILE__)
|
|
4
4
|
|
5
5
|
require "ec2-mini/cli"
|
6
6
|
|
7
|
-
|
7
|
+
home_config = "#{ENV['HOME']}/.ec2-mini"
|
8
|
+
current_config = "#{ENV['PWD']}/.ec2-mini"
|
9
|
+
config_file = File.exist?(home_config) ? home_config : current_config
|
10
|
+
|
11
|
+
client = EC2Mini::CLI.new(nil, config_file)
|
12
|
+
client.start
|
data/lib/ec2-mini/cli.rb
CHANGED
@@ -3,115 +3,145 @@ require 'yaml'
|
|
3
3
|
|
4
4
|
module EC2Mini
|
5
5
|
class CLI
|
6
|
-
def self.start(options = {})
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def initialize(options = nil, config_file = '')
|
8
|
+
@options = options || load_config_file(config_file)
|
9
|
+
|
10
|
+
['access_key_id', 'secret_access_key', 'region'].each do |attribute|
|
11
|
+
error "Not set #{attribute}." unless @options[attribute]
|
11
12
|
end
|
13
|
+
end
|
12
14
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
exit 0 unless ARGV[1] =~ /^([+-]?[0-9]+)|backup|count$/
|
17
|
-
|
18
|
-
role = ARGV[0]
|
19
|
-
operation = ARGV[1]
|
20
|
-
|
21
|
-
access_key_id = options['access_key_id']
|
22
|
-
secret_access_key = options['secret_access_key']
|
23
|
-
region = options['region']
|
24
|
-
ec2 = AWS::EC2.new(access_key_id: access_key_id, secret_access_key: secret_access_key, region: region)
|
25
|
-
|
26
|
-
#operation
|
27
|
-
case operation
|
28
|
-
when /^\+/
|
29
|
-
|
30
|
-
command = operation.scan(/([0-9]+$)/)[0][0].to_i
|
31
|
-
|
32
|
-
# search instance
|
33
|
-
running_instance = ''
|
34
|
-
ec2.instances.tagged('Mini', role).to_a.each do |instance|
|
35
|
-
next if instance.status != :running
|
36
|
-
running_instance = instance
|
37
|
-
break
|
38
|
-
end
|
39
|
-
|
40
|
-
# search ami
|
41
|
-
ami = ec2.images.with_owner("self").filter("name", role).first
|
42
|
-
|
43
|
-
# for create instance
|
44
|
-
image_id = ami.id
|
45
|
-
security_groups = running_instance.groups
|
46
|
-
zone = running_instance.availability_zone
|
47
|
-
key_name = running_instance.key_name
|
48
|
-
instance_type = running_instance.instance_type
|
49
|
-
|
50
|
-
command.times do
|
51
|
-
running_instance = ec2.instances.create(
|
52
|
-
instance_type: instance_type,
|
53
|
-
key_name: key_name,
|
54
|
-
image_id: image_id,
|
55
|
-
availability_zone: zone,
|
56
|
-
security_groups: security_groups
|
57
|
-
)
|
58
|
-
running_instance.add_tag('Name', value: role)
|
59
|
-
running_instance.add_tag('Mini', value: role)
|
60
|
-
end
|
61
|
-
puts "successfully created #{role} #{operation}"
|
62
|
-
|
63
|
-
when /^\-/
|
64
|
-
|
65
|
-
command = operation.scan(/([0-9]+$)/)[0][0].to_i
|
66
|
-
|
67
|
-
# search instance
|
68
|
-
# TODO Warning launch_time sort
|
69
|
-
instances = ec2.instances.tagged('Mini', role)
|
70
|
-
instances = instances.to_a.reverse
|
71
|
-
|
72
|
-
instances.each do |instance|
|
73
|
-
next if instance.status != :running || command <= 0
|
74
|
-
instance.terminate
|
75
|
-
command -= 1
|
76
|
-
end
|
77
|
-
|
78
|
-
puts "successfully removed #{role} #{operation}"
|
15
|
+
def start(role = nil, command = nil)
|
16
|
+
role_regex = /^[\w\-]+$/
|
17
|
+
command_regex = /^([+-][0-9]+)|backup|count$/
|
79
18
|
|
80
|
-
|
19
|
+
error "Not set role." if !(role =~ role_regex) && !(ARGV[0] =~ role_regex)
|
20
|
+
error "Not set command." if !(command =~ command_regex) && !(ARGV[1] =~ command_regex)
|
81
21
|
|
82
|
-
|
83
|
-
|
84
|
-
amis = ec2.images.with_owner("self").filter("name", role)
|
85
|
-
amis.first.deregister unless amis.count.zero?
|
86
|
-
|
87
|
-
# search instance
|
88
|
-
running_instance = ''
|
89
|
-
ec2.instances.tagged('Mini', role).to_a.each do |instance|
|
90
|
-
next if instance.status != :running
|
91
|
-
running_instance = instance
|
92
|
-
break
|
93
|
-
end
|
94
|
-
|
95
|
-
# create ami
|
96
|
-
image = running_instance.create_image(role, {:description => role, :no_reboot => true})
|
97
|
-
begin
|
98
|
-
sleep 1
|
99
|
-
print '.'
|
100
|
-
image = ec2.images[image.id]
|
101
|
-
end until image.state != :pending
|
102
|
-
if image.state == :failure
|
103
|
-
puts "create image failed: #{image.state_reason}"
|
104
|
-
exit 1
|
105
|
-
end
|
106
|
-
puts 'successfully created backup'
|
22
|
+
role = role || ARGV[0]
|
23
|
+
command = command || ARGV[1]
|
107
24
|
|
25
|
+
case command
|
26
|
+
when /^\+[0-9]+/
|
27
|
+
number = command.scan(/([0-9]+$)/)[0][0].to_i
|
28
|
+
up(role, number)
|
29
|
+
when /^\-[0-9]+/
|
30
|
+
number = command.scan(/([0-9]+$)/)[0][0].to_i
|
31
|
+
down(role, number)
|
32
|
+
when 'backup'
|
33
|
+
backup(role)
|
108
34
|
when 'count'
|
109
|
-
count
|
110
|
-
|
111
|
-
|
112
|
-
end
|
113
|
-
puts "#{role}: #{count} instances running"
|
35
|
+
count(role)
|
36
|
+
else
|
37
|
+
error 'Not command'
|
114
38
|
end
|
115
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def load_config_file(config_file)
|
43
|
+
error "Not found .ec2-mini file." unless File.exist?(config_file)
|
44
|
+
YAML.load_file(config_file)
|
45
|
+
end
|
46
|
+
|
47
|
+
def ec2_client
|
48
|
+
AWS::EC2.new(
|
49
|
+
access_key_id: @options['access_key_id'],
|
50
|
+
secret_access_key: @options['secret_access_key'],
|
51
|
+
region: @options['region']
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def backup(role)
|
56
|
+
|
57
|
+
# TODO
|
58
|
+
# deregister old ami
|
59
|
+
amis = ec2_client.images.with_owner("self").filter("name", role)
|
60
|
+
amis.first.deregister unless amis.count
|
61
|
+
|
62
|
+
# search instance
|
63
|
+
running_instance = ''
|
64
|
+
ec2_client.instances.tagged('Mini', role).to_a.each do |instance|
|
65
|
+
next if instance.status != :running
|
66
|
+
running_instance = instance
|
67
|
+
break
|
68
|
+
end
|
69
|
+
|
70
|
+
# create ami
|
71
|
+
image = running_instance.create_image(role, { description: role, no_reboot: true })
|
72
|
+
begin
|
73
|
+
sleep 1
|
74
|
+
print '.'
|
75
|
+
image = ec2_client.images[image.id]
|
76
|
+
end until image.state != :pending
|
77
|
+
if image.state == :failure
|
78
|
+
error "create image failed: #{image.state_reason}"
|
79
|
+
end
|
80
|
+
puts 'successfully created backup'
|
81
|
+
end
|
82
|
+
|
83
|
+
def count(role)
|
84
|
+
count = 0
|
85
|
+
ec2_client.instances.tagged('Mini', role).each do |instance|
|
86
|
+
count += 1 if instance.status == :running
|
87
|
+
end
|
88
|
+
puts "#{role}: #{count} instances running"
|
89
|
+
end
|
90
|
+
|
91
|
+
def up(role, number)
|
92
|
+
|
93
|
+
# search instance
|
94
|
+
running_instance = ''
|
95
|
+
ec2_client.instances.tagged('Mini', role).to_a.each do |instance|
|
96
|
+
next if instance.status != :running
|
97
|
+
running_instance = instance
|
98
|
+
break
|
99
|
+
end
|
100
|
+
|
101
|
+
# search ami
|
102
|
+
ami = ec2_client.images.with_owner("self").filter("name", role).first
|
103
|
+
|
104
|
+
# for create instance
|
105
|
+
image_id = ami.id
|
106
|
+
security_groups = running_instance.groups
|
107
|
+
zone = running_instance.availability_zone
|
108
|
+
key_name = running_instance.key_name
|
109
|
+
instance_type = running_instance.instance_type
|
110
|
+
|
111
|
+
number.times do
|
112
|
+
running_instance = ec2_client.instances.create(
|
113
|
+
instance_type: instance_type,
|
114
|
+
key_name: key_name,
|
115
|
+
image_id: image_id,
|
116
|
+
availability_zone: zone,
|
117
|
+
security_groups: security_groups
|
118
|
+
)
|
119
|
+
running_instance.add_tag('Name', value: role)
|
120
|
+
running_instance.add_tag('Mini', value: role)
|
121
|
+
end
|
122
|
+
puts "successfully created #{role} #{operation}"
|
123
|
+
end
|
124
|
+
|
125
|
+
def down(role, number)
|
126
|
+
|
127
|
+
# search instance
|
128
|
+
# TODO Warning launch_time sort
|
129
|
+
instances = ec2_client.instances.tagged('Mini', role)
|
130
|
+
instances = instances.to_a.reverse
|
131
|
+
|
132
|
+
instances.each do |instance|
|
133
|
+
next if instance.status != :running || number <= 0
|
134
|
+
instance.terminate
|
135
|
+
number -= 1
|
136
|
+
end
|
137
|
+
|
138
|
+
puts "successfully removed #{role} #{operation}"
|
139
|
+
end
|
140
|
+
|
141
|
+
def error(message = '')
|
142
|
+
puts message
|
143
|
+
exit 1
|
144
|
+
end
|
145
|
+
|
116
146
|
end
|
117
147
|
end
|
data/lib/ec2-mini/version.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'ec2-mini/cli'
|
2
|
+
|
3
|
+
describe EC2Mini::CLI do
|
4
|
+
describe '#initialize' do
|
5
|
+
context 'not exist config file' do
|
6
|
+
it "is failure" do
|
7
|
+
expect{ EC2Mini::CLI.new(nil, '') }.to raise_error
|
8
|
+
end
|
9
|
+
end
|
10
|
+
context 'exist config file' do
|
11
|
+
it "is success" do
|
12
|
+
# TODO ENV['PWD']
|
13
|
+
expect(EC2Mini::CLI.new(nil, "#{ENV['PWD']}/spec/support/.ec2-mini").class).to eq EC2Mini::CLI
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ec2-mini
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-05-
|
12
|
+
date: 2013-05-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: aws-sdk
|
@@ -37,6 +37,8 @@ files:
|
|
37
37
|
- bin/ec2-mini
|
38
38
|
- lib/ec2-mini/cli.rb
|
39
39
|
- lib/ec2-mini/version.rb
|
40
|
+
- README.md
|
41
|
+
- spec/lib/cil_spec.rb
|
40
42
|
homepage: http://github.com/camelmasa/ec2-mini
|
41
43
|
licenses:
|
42
44
|
- MIT
|