ec2-mini 0.0.1
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.
- data/bin/ec2-mini +7 -0
- data/lib/ec2-mini/cli.rb +117 -0
- data/lib/ec2-mini/version.rb +3 -0
- metadata +66 -0
data/bin/ec2-mini
ADDED
data/lib/ec2-mini/cli.rb
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
require 'aws-sdk'
|
|
2
|
+
require 'yaml'
|
|
3
|
+
|
|
4
|
+
module EC2Mini
|
|
5
|
+
class CLI
|
|
6
|
+
def self.start(options = {})
|
|
7
|
+
|
|
8
|
+
unless File.exist?(ENV['HOME'] + '/.ec2-mini')
|
|
9
|
+
puts ".ec2-mini is not found."
|
|
10
|
+
exit 0
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
options = YAML.load_file(ENV['HOME'] + '/.ec2-mini')
|
|
14
|
+
|
|
15
|
+
exit 0 unless ARGV[0] =~ /^[\w\-]+$/
|
|
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}"
|
|
79
|
+
|
|
80
|
+
when 'backup'
|
|
81
|
+
|
|
82
|
+
# TODO
|
|
83
|
+
# deregister old ami
|
|
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'
|
|
107
|
+
|
|
108
|
+
when 'count'
|
|
109
|
+
count = 0
|
|
110
|
+
ec2.instances.tagged('Mini', role).each do |instance|
|
|
111
|
+
count += 1 if instance.status == :running
|
|
112
|
+
end
|
|
113
|
+
puts "#{role}: #{count} instances running"
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ec2-mini
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Masahiro Saito
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-05-11 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: aws-sdk
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
none: false
|
|
26
|
+
requirements:
|
|
27
|
+
- - ! '>='
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '0'
|
|
30
|
+
description: ec2-mini
|
|
31
|
+
email: camelmasa@gmail.com
|
|
32
|
+
executables:
|
|
33
|
+
- ec2-mini
|
|
34
|
+
extensions: []
|
|
35
|
+
extra_rdoc_files: []
|
|
36
|
+
files:
|
|
37
|
+
- bin/ec2-mini
|
|
38
|
+
- lib/ec2-mini/cli.rb
|
|
39
|
+
- lib/ec2-mini/version.rb
|
|
40
|
+
homepage: http://github.com/camelmasa/ec2-mini
|
|
41
|
+
licenses:
|
|
42
|
+
- MIT
|
|
43
|
+
post_install_message:
|
|
44
|
+
rdoc_options: []
|
|
45
|
+
require_paths:
|
|
46
|
+
- lib
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
none: false
|
|
49
|
+
requirements:
|
|
50
|
+
- - ! '>='
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '0'
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
none: false
|
|
55
|
+
requirements:
|
|
56
|
+
- - ! '>='
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '0'
|
|
59
|
+
requirements: []
|
|
60
|
+
rubyforge_project:
|
|
61
|
+
rubygems_version: 1.8.23
|
|
62
|
+
signing_key:
|
|
63
|
+
specification_version: 3
|
|
64
|
+
summary: ec2-mini
|
|
65
|
+
test_files: []
|
|
66
|
+
has_rdoc:
|