Class: Commands::EipCommand

Inherits:
Command show all
Defined in:
lib/commands.rb

Constant Summary collapse

CLOSED_DOWN_STATES =
Set.new(%w(TERMINATED SHUTTING_DOWN COMPLETED FAILED))
WAITING_OR_RUNNING_STATES =
Set.new(%w(WAITING RUNNING))

Instance Attribute Summary collapse

Attributes inherited from Command

#arg, #commands, #description, #logger, #name

Instance Method Summary collapse

Methods inherited from Command

#get_field, #has_value, #have, #option, #require, #require_single_jobflow, #resolve, #validate

Constructor Details

#initialize(*args) ⇒ EipCommand

Returns a new instance of EipCommand.



1336
1337
1338
# File 'lib/commands.rb', line 1336

def initialize(*args)
  super(*args)
end

Instance Attribute Details

#instance_idObject

Returns the value of attribute instance_id.



1331
1332
1333
# File 'lib/commands.rb', line 1331

def instance_id
  @instance_id
end

#jobflow_detailObject

Returns the value of attribute jobflow_detail.



1331
1332
1333
# File 'lib/commands.rb', line 1331

def jobflow_detail
  @jobflow_detail
end

#jobflow_idObject

Returns the value of attribute jobflow_id.



1331
1332
1333
# File 'lib/commands.rb', line 1331

def jobflow_id
  @jobflow_id
end

#key_pair_fileObject

Returns the value of attribute key_pair_file.



1331
1332
1333
# File 'lib/commands.rb', line 1331

def key_pair_file
  @key_pair_file
end

#no_waitObject

Returns the value of attribute no_wait.



1331
1332
1333
# File 'lib/commands.rb', line 1331

def no_wait
  @no_wait
end

Instance Method Details

#ec2_endpoint_from_az(az) ⇒ Object



1368
1369
1370
# File 'lib/commands.rb', line 1368

def ec2_endpoint_from_az(az)
  return "https://ec2.#{region_from_az(az)}.amazonaws.com"      
end

#enact(client) ⇒ Object



1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
# File 'lib/commands.rb', line 1372

def enact(client)
  self.jobflow_id = require_single_jobflow
  self.jobflow_detail = client.describe_jobflow_with_id(self.jobflow_id)
  if ! get_field(:no_wait) then
    wait_for_jobflow(client)
  end
  self.instance_id = self.jobflow_detail['Instances']['MasterInstanceId']
  if ! self.instance_id then
    logger.error("The master instance is not available yet for jobflow #{self.jobflow_id}. It might still be starting.")
    exit(-1)
  end

  az = self.jobflow_detail['Instances']['Placement']['AvailabilityZone']

  commands.global_options[:ec2_endpoint] = ec2_endpoint_from_az(az)
  
  self.key_pair_file = require(:key_pair_file, "Missing required option --key-pair-file for #{name}")
  eip = get_field(:arg)

  ec2_client = Ec2ClientWrapper.new(commands, logger)

  if ! eip then
    begin
      response = ec2_client.allocate_address()
    rescue Exception => e
      logger.error("Error during AllocateAddres: " + e.message)
      if get_field(:trace) then
        logger.puts(e.backtrace.join("\n"))
      end
      exit(-1)
    end

    eip = response['publicIp']
    logger.info("Allocated Public IP: #{eip}...")
  end

  begin
    response = ec2_client.associate_address(self.instance_id, eip)
    logger.info("Public IP: #{eip} was assigned to jobflow #{self.jobflow_id}")
  rescue Exception => e
    logger.error("Error during AssociateAddres: " + e.to_s)
    if get_field(:trace) then
      logger.puts(e.backtrace.join("\n"))
    end
    exit(-1)
  end

end

#exec(cmd) ⇒ Object



1340
1341
1342
# File 'lib/commands.rb', line 1340

def exec(cmd)
  commands.exec(cmd)
end

#region_from_az(az) ⇒ Object



1359
1360
1361
1362
1363
1364
1365
1366
# File 'lib/commands.rb', line 1359

def region_from_az(az)
  md = az.match(/((\w+-)+\d+)\w+/)
  if md then
    md[1]
  else
    raise "Unable to convert Availability Zone '#{az}' to region"
  end
end

#wait_for_jobflow(client) ⇒ Object



1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
# File 'lib/commands.rb', line 1344

def wait_for_jobflow(client)
  while true do
    state = resolve(self.jobflow_detail, "ExecutionStatusDetail", "State")
    if WAITING_OR_RUNNING_STATES.include?(state) then
      break
    elsif CLOSED_DOWN_STATES.include?(state) then
      raise RuntimeError, "Jobflow entered #{state} while waiting to assign Elastic IP"
    else
      logger.info("Jobflow is in state #{state}, waiting....")
      sleep(30)
      self.jobflow_detail = client.describe_jobflow_with_id(jobflow_id)
    end
  end
end