Module: Stem::Instance
- Extended by:
- Instance
- Includes:
- Util
- Included in:
- Instance
- Defined in:
- lib/stem/instance.rb,
lib/stem/instance_types.rb
Constant Summary
collapse
- Types =
{"m1.small" => {
"architecture" => "i386"},
"m1.large" => {
"architecture" => "x86_64"},
"m1.xlarge" => {
"architecture" => "x86_64"},
"t1.micro" => {
"architecture" => "i386"},
"m2.xlarge" => {
"architecture" => "x86_64"},
"m2.2xlarge" => {
"architecture" => "x86_64"},
"m2.4xlarge" => {
"architecture" => "x86_64"},
"c1.medium" => {
"architecture" => "i386"},
"c1.xlarge" => {
"architecture" => "x86_64"},
"cc1.4xlarge" => {
"architecture" => "x86_64"},
"cg1.4xlarge" => {
"architecture" => "x86_64"}
}
Instance Method Summary
collapse
Methods included from Util
#get_filter_opts, #swirl, #tags_to_filter, #tagset_to_hash
Instance Method Details
#describe(instance) ⇒ Object
74
75
76
77
|
# File 'lib/stem/instance.rb', line 74
def describe instance
throw "You must provide an instance ID to describe" unless instance
swirl.call("DescribeInstances", "InstanceId" => instance)["reservationSet"][0]["instancesSet"][0]
end
|
#destroy(instance_id) ⇒ Object
62
63
64
|
# File 'lib/stem/instance.rb', line 62
def destroy instance_id
swirl.call "TerminateInstances", "InstanceId" => instance_id
end
|
#launch(config, userdata = nil) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/stem/instance.rb', line 6
def launch config, userdata = nil
throw "No config provided" unless config
config = aggregate_hash_options_for_ami!(config)
ami = config["ami"]
opt = {
"SecurityGroup.#" => config["groups"] || [],
"MinCount" => "1",
"MaxCount" => "1",
"KeyName" => config["key_name"] || "default",
"InstanceType" => config["instance_type"] || "m1.small",
"ImageId" => ami
}
opt.merge! "Placement.AvailabilityZone" => config["availability_zone"] if config["availability_zone"]
if config["volumes"]
devices = []
sizes = []
config["volumes"].each do |v|
puts "Adding a volume of #{v["size"]} to be mounted at #{v["device"]}."
devices << v["device"]
sizes << v["size"].to_s
end
opt.merge! "BlockDeviceMapping.#.Ebs.VolumeSize" => sizes,
"BlockDeviceMapping.#.DeviceName" => devices
end
if userdata
puts "Userdata provided, encoded and sent to the instance."
opt.merge!({ "UserData" => Base64.encode64(userdata)})
end
response = swirl.call "RunInstances", opt
instance_id = response["instancesSet"].first["instanceId"]
if config['tags'] && !config['tags'].empty?
i = 0
begin
Tag::create(instance_id, config['tags'])
rescue Swirl::InvalidRequest => e
if i < 5 && e.message =~ /does not exist/
i += 1
retry
end
raise e
end
end
instance_id
end
|
#list ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/stem/instance.rb', line 79
def list
instances = swirl.call("DescribeInstances")
lookup = {}
instances["reservationSet"].each do |r|
r["instancesSet"].each do |i|
lookup[i["imageId"]] = nil
end
end
amis = swirl.call("DescribeImages", "ImageId" => lookup.keys)["imagesSet"]
amis.each do |ami|
name = ami["name"] || ami["imageId"]
if !ami["description"] || ami["description"][0..1] != "%%"
name.gsub!(/^(.{8}).+(.{8})/) { $1 + "..." + $2 }
name = "(foreign) " + name
end
lookup[ami["imageId"]] = name
end
reservations = instances["reservationSet"]
unless reservations.nil? or reservations.empty?
puts "------------------------------------------"
puts "Instances"
puts "------------------------------------------"
reservations.each do |r|
groups = r["groupSet"].map { |g| g["groupId"] }.join(",")
r["instancesSet"].each do |i|
name = lookup[i["imageId"]]
puts "%-15s %-15s %-15s %-20s %s" % [ i["instanceId"], i["ipAddress"] || "no ip", i["instanceState"]["name"], groups, name ]
end
end
end
result = swirl.call "DescribeImages", "Owner" => "self"
images = result["imagesSet"].select { |img| img["name"] }
unless images.nil? or images.empty?
puts "------------------------------------------"
puts "AMIs"
puts "------------------------------------------"
iwidth = images.map { |img| img["name"].length }.max + 1
images.each do |img|
puts "%-#{iwidth}s %s" % [ img["name"], img["imageId"] ]
end
end
end
|
#restart(instance_id) ⇒ Object
58
59
60
|
# File 'lib/stem/instance.rb', line 58
def restart instance_id
swirl.call "RebootInstances", "InstanceId" => instance_id
end
|
#start(instance_id) ⇒ Object
70
71
72
|
# File 'lib/stem/instance.rb', line 70
def start instance_id
swirl.call "StartInstances", "InstanceId" => instance_id
end
|
#stop(instance_id) ⇒ Object
66
67
68
|
# File 'lib/stem/instance.rb', line 66
def stop instance_id
swirl.call "StopInstances", "InstanceId" => instance_id
end
|
#tagged(*tags) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/stem/instance.rb', line 127
def tagged *tags
return if tags.empty?
opts = { "tag-key" => tags.map {|t| t.to_s } }
instances = swirl.call "DescribeInstances", get_filter_opts(opts)
ids = []
instances["reservationSet"].each do |r|
r["instancesSet"].each do |i|
ids << i["instanceId"]
end
end
ids
end
|