49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/build-cloud/instance.rb', line 49
def create
options = @options.dup
if exists?
if options[:tags]
create_tags(options[:tags])
end
return
end
@log.info( "Creating instance #{options[:name]}" )
if options[:security_group_names] or options[:security_group_ids]
unless options[:security_group_ids]
options[:security_group_ids] = []
options[:security_group_names].each do |sg|
options[:security_group_ids] << BuildCloud::SecurityGroup.get_id_by_name( sg )
end
options.delete(:security_group_names)
end
end
if options[:subnet_id] or options[:subnet_name]
unless options[:subnet_id]
options[:subnet_id] = BuildCloud::Subnet.get_id_by_name( options[:subnet_name] )
options.delete(:subnet_name)
end
end
unless options[:vpc_id]
options[:vpc_id] = BuildCloud::VPC.get_id_by_name( options[:vpc_name] )
options.delete(:vpc_name)
end
if options[:private_ip_address] and options[:network_interfaces]
puts "WARNING: InvalidParameterCombination => Network interfaces and an instance-level private IP address should not be specified on the same request"
puts "Using Network interface"
options.delete(:private_ip_address)
end
if options[:subnet_id] and options[:network_interfaces]
puts "WARNING: InvalidParameterCombination => Network interfaces and subnet_ids should not be specified on the same request"
puts "Using Network interface"
options.delete(:subnet_id)
end
if options[:user_data]
options[:user_data] = JSON.generate( options[:user_data] )
elsif options[:user_data_file]
user_data_file_path = File.join( Dir.pwd, options[:user_data_file])
if File.exists?( user_data_file_path )
options[:user_data] = File.read( user_data_file_path )
options.delete(:user_data_file)
else
@log.error("config lists a :user_data_file that doesn't exist at #{options[:user_data_file]}")
end
elsif options[:user_data_template]
variable_hash = options[:user_data_variables]
user_data_template_path = ''
if options[:user_data_template].include? '/'
user_data_template_path = options[:user_data_template]
else
user_data_template_path = File.join( Dir.pwd, options[:user_data_template])
end
if File.exists?( user_data_template_path )
template = File.read( user_data_template_path )
buffer = ERB.new(template,nil,'-').result(binding)
options[:user_data] = buffer
options.delete(:user_data_variables)
options.delete(:user_data_template)
else
@log.error("config lists a :user_data_template that doesn't exist at #{options[:user_data_template]}")
end
end
options[:network_interfaces].each { |iface|
if ! iface[:network_interface_name].nil?
interface_id = BuildCloud::NetworkInterface.get_id_by_name( iface[:network_interface_name] )
iface['NetworkInterfaceId'] = interface_id
iface.delete(:network_interface_name)
end
} unless options[:network_interfaces].nil?
@log.debug( options.inspect )
instance = @ec2.servers.new( options )
instance.save
@log.debug( instance.inspect )
if options[:ebs_volumes]
instance = @ec2.servers.get(instance.id)
instance_state = instance.state
begin
Timeout::timeout(60) {
until instance_state == 'running'
@log.info( "instance not ready yet: #{instance_state}" )
sleep 3
instance = @ec2.servers.get(instance.id)
instance_state = instance.state
end
@log.debug("Instance state: #{instance_state}")
}
rescue Timeout::Error
@log.error("Waiting on availability for instance: #{instance.id}, timed out")
end
instance_id = instance.id
options[:ebs_volumes].each do |vol|
vol_id = BuildCloud::EBSVolume.get_id_by_name( vol[:name] )
attach_response = @ec2.attach_volume(instance_id, vol_id, vol[:device])
@log.debug( attach_response.inspect )
volume_state = @ec2.volumes.get(vol_id).state
begin
Timeout::timeout(30) {
until volume_state == 'in-use'
@log.info( "Volume not attached yet: #{volume_state}" )
sleep 3
volume_state = @ec2.volumes.get(vol_id).state
end
@log.debug("Volume state: #{volume_state}")
}
rescue Timeout::Error
@log.error("Operation to attach volume: #{vol[:name]}, timed out")
end
if vol[:delete_on_termination] and volume_state == 'in-use'
request_resp = @ec2.modify_instance_attribute(
instance_id,
{ "BlockDeviceMapping.DeviceName" => "#{vol[:device]}",
"BlockDeviceMapping.Ebs.DeleteOnTermination" => true }
)
unless request_resp.body["return"]
@log.error("Failed to set delete_on_termination for volume: #{vol[:name]}")
end
@log.debug( request_resp.inspect )
end
end
end
end
|