@@ -37,6 +37,7 @@ def call(env)
37
37
user_data = region_config . user_data
38
38
block_device_mapping = region_config . block_device_mapping
39
39
elastic_ip = region_config . elastic_ip
40
+ allocate_elastic_ip = region_config . allocate_elastic_ip
40
41
terminate_on_shutdown = region_config . terminate_on_shutdown
41
42
iam_instance_profile_arn = region_config . iam_instance_profile_arn
42
43
iam_instance_profile_name = region_config . iam_instance_profile_name
@@ -47,7 +48,7 @@ def call(env)
47
48
end
48
49
49
50
# If there is a subnet ID then warn the user
50
- if subnet_id && ! elastic_ip
51
+ if subnet_id and not elastic_ip and not allocate_elastic_ip
51
52
env [ :ui ] . warn ( I18n . t ( "vagrant_aws.launch_vpc_warning" ) )
52
53
end
53
54
@@ -62,7 +63,8 @@ def call(env)
62
63
env [ :ui ] . info ( " -- IAM Instance Profile ARN: #{ iam_instance_profile_arn } " ) if iam_instance_profile_arn
63
64
env [ :ui ] . info ( " -- IAM Instance Profile Name: #{ iam_instance_profile_name } " ) if iam_instance_profile_name
64
65
env [ :ui ] . info ( " -- Private IP: #{ private_ip_address } " ) if private_ip_address
65
- env [ :ui ] . info ( " -- Elastic IP: #{ elastic_ip } " ) if elastic_ip
66
+ env [ :ui ] . info ( " -- Elastic IP: #{ elastic_ip . inspect } " ) if elastic_ip
67
+ env [ :ui ] . info ( " -- Allocate Elastic IP: #{ allocate_elastic_ip . inspect } " ) if allocate_elastic_ip
66
68
env [ :ui ] . info ( " -- User Data: yes" ) if user_data
67
69
env [ :ui ] . info ( " -- Security Groups: #{ security_groups . inspect } " ) if !security_groups . empty?
68
70
env [ :ui ] . info ( " -- User Data: #{ user_data } " ) if user_data
@@ -80,6 +82,8 @@ def call(env)
80
82
:iam_instance_profile_name => iam_instance_profile_name ,
81
83
:tags => tags ,
82
84
:user_data => user_data ,
85
+ :elastic_ip => elastic_ip ,
86
+ :allocate_elastic_ip => allocate_elastic_ip ,
83
87
:block_device_mapping => block_device_mapping ,
84
88
:instance_initiated_shutdown_behavior => terminate_on_shutdown == true ? "terminate" : nil
85
89
}
@@ -138,9 +142,14 @@ def call(env)
138
142
@logger . info ( "Time to instance ready: #{ env [ :metrics ] [ "instance_ready_time" ] } " )
139
143
140
144
# Allocate and associate an elastic IP if requested
145
+ #if elastic_ip
146
+ # domain = subnet_id ? 'vpc' : 'standard'
147
+ # do_elastic_ip(env, domain, server)
148
+ #end
141
149
if elastic_ip
142
- domain = subnet_id ? 'vpc' : 'standard'
143
- do_elastic_ip ( env , domain , server )
150
+ associate_elastic_ip ( env , elastic_ip )
151
+ elsif allocate_elastic_ip
152
+ allocate_and_associate_elastic_ip ( env , allocate_elastic_ip )
144
153
end
145
154
146
155
if !env [ :interrupted ]
@@ -232,6 +241,47 @@ def terminate(env)
232
241
destroy_env [ :force_confirm_destroy ] = true
233
242
env [ :action_runner ] . run ( Action . action_destroy , destroy_env )
234
243
end
244
+
245
+ def associate_elastic_ip ( env , elastic_ip )
246
+ begin
247
+ eip = env [ :aws_compute ] . addresses . get ( elastic_ip )
248
+ if eip . nil?
249
+ terminate ( env )
250
+ raise Errors ::FogError ,
251
+ :message => "Elastic IP specified not found: #{ elastic_ip } "
252
+ end
253
+ @logger . info ( "eip - #{ eip } " )
254
+ env [ :aws_compute ] . associate_address ( env [ :machine ] . id , nil , nil , eip . allocation_id )
255
+ env [ :ui ] . info ( I18n . t ( "vagrant_aws.elastic_ip_allocated" ) )
256
+ rescue Fog ::Compute ::AWS ::NotFound => e
257
+ # Invalid elasticip doesn't have its own error so we catch and
258
+ # check the error message here.
259
+ if e . message =~ /Elastic IP/
260
+ terminate ( env )
261
+ raise Errors ::FogError ,
262
+ :message => "Elastic IP not found: #{ elastic_ip } "
263
+ end
264
+ raise
265
+ end
266
+ end
267
+
268
+ def allocate_and_associate_elastic_ip ( env , allocate_elastic_ip )
269
+ begin
270
+ allocated_eip = env [ :aws_compute ] . allocate_address ( allocate_elastic_ip )
271
+ associate_elastic_ip ( env , allocated_eip [ :body ] [ "publicIp" ] )
272
+ rescue Fog ::Compute ::AWS ::NotFound => e
273
+ # Invalid computation of elasticip doesn't have its own error so we catch and
274
+ # check the error message here.
275
+ if e . message =~ /Elastic IP/
276
+ terminate ( env )
277
+ raise Errors ::FogError ,
278
+ :message => "Elastic IP not allocated with allocate_elastic_ip option: #{ allocate_elastic_ip } "
279
+ end
280
+ raise
281
+ end
282
+ end
283
+
284
+
235
285
end
236
286
end
237
287
end
0 commit comments