Class: Instance::EC2
Overview
for EC2 instances aws_id is a random id name is a value of ‘Name’ tag
Constant Summary
Constants inherited
from Instance
NA_STATUS
Instance Attribute Summary
Attributes inherited from Instance
#description, #groups, #index, #name, #prefix, #region, #role, #zone
Instance Method Summary
collapse
Methods inherited from Instance
#alive?, #aws_id, #config, #connection, create, #display, #has_approximate_status?, #initialize, #inspect, #instances, #logical_zone, #matches?, #method_missing, name_for, #physical_zone, #region_physical_zone, #region_zone, #security_groups, #status, #terminated?, #to_s
Constructor Details
This class inherits a constructor from Instance
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Instance
Instance Method Details
#attached_volumes ⇒ Object
133
134
135
|
# File 'lib/maws/instance/ec2.rb', line 133
def attached_volumes
description.attached_ebs_volume_ids
end
|
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/maws/instance/ec2.rb', line 7
def create
return if alive?
info "creating EC2 #{name}..."
image_id = config(:image_id) || connection.image_id_for_image_name(config(:image_name))
if image_id.nil?
info "no AMI id found with name '#{config(:image_name)}'"
return
end
results = connection.ec2.launch_instances(image_id,
:availability_zone => region_physical_zone,
:key_name => config(:keypair),
:min_count => 1,
:max_count => 1,
:group_names => security_groups,
:user_data => config(:user_data),
:monitoring_enabled => config(:monitoring_enabled),
:instance_type => config(:instance_type))
self.description = Description::EC2.new(results.first)
end
|
39
40
41
42
43
44
|
# File 'lib/maws/instance/ec2.rb', line 39
def create_tags
tag_instance_name
tag_volumes_names
info "...done (#{name} or #{aws_id} is ready)"
end
|
89
90
91
92
93
|
# File 'lib/maws/instance/ec2.rb', line 89
def destroy
return unless alive?
connection.ec2.terminate_instances(aws_id)
info "destroying EC2 #{name} (#{aws_id})"
end
|
#display_fields ⇒ Object
141
142
143
|
# File 'lib/maws/instance/ec2.rb', line 141
def display_fields
super + [:dns_name, :aws_instance_id, :aws_image_id]
end
|
137
138
139
|
# File 'lib/maws/instance/ec2.rb', line 137
def service
:ec2
end
|
#set_prefix(prefix) ⇒ Object
30
31
32
33
34
35
36
37
|
# File 'lib/maws/instance/ec2.rb', line 30
def set_prefix(prefix)
@prefix = prefix
old_name = @name
@name = self.class.name_for(@config, @prefix, @zone, @role, @index)
info "renaming #{old_name} to #{@name}"
create_tags
end
|
#ssh_available? ⇒ Boolean
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/maws/instance/ec2.rb', line 111
def ssh_available?
return false unless alive? && self.dns_name && !self.dns_name.empty?
begin
3.times { begin
Net::SSH.start(dns_name, "phoneyuser", {:auth_methods => ["publickey"], :timeout => 1, :keys_only => true })
rescue Errno::EHOSTUNREACH
sleep 2
end
}
rescue Net::SSH::AuthenticationFailed
return true
rescue Object
return false
end
end
|
103
104
105
106
107
108
109
|
# File 'lib/maws/instance/ec2.rb', line 103
def start
return unless alive?
if status == 'stopped'
connection.ec2.start_instances(aws_id)
info "starting EC2 #{name} (#{aws_id})"
end
end
|
95
96
97
98
99
100
101
|
# File 'lib/maws/instance/ec2.rb', line 95
def stop
return unless alive?
if status == 'running'
connection.ec2.stop_instances(aws_id)
info "stopping EC2 #{name} (#{aws_id})"
end
end
|
#tag_instance_name ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/maws/instance/ec2.rb', line 46
def tag_instance_name
retries_left = 20
loop do
begin
connection.ec2.create_tags(aws_id, {'Name' => name})
info "tagged EC2 instance #{aws_id} as #{name}"
return
rescue RightAws::AwsError => error
if error.message =~ /^InvalidInstanceID.NotFound/
info "TAGGING FAILED. RETRYING..."
else
raise error
end
end
retries_left -= 1
retries_left > 0 ? sleep(1) : break
end
error "Couldn't not tag #{aws_id} with name #{name}. It might not exist on AWS"
end
|
#tag_volumes_names ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/maws/instance/ec2.rb', line 67
def tag_volumes_names
retries_left = 30
loop do
if volume_ids.count > 0
volume_ids.each {|vid|
connection.ec2.create_tags(vid, {'Name' => name})
info "tagged EBS volume #{vid} as #{name}"
}
return
end
@maws.resync_instances
retries_left -= 1
retries_left > 0 ? sleep(1) : break
end
error "No volumes found for #{name} (#{aws_id})"
end
|
#volume_ids ⇒ Object
129
130
131
|
# File 'lib/maws/instance/ec2.rb', line 129
def volume_ids
description.ebs_volume_ids
end
|