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.


1292
1293
1294
# File 'lib/commands.rb', line 1292

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

Instance Attribute Details

#instance_idObject

Returns the value of attribute instance_id.


1287
1288
1289
# File 'lib/commands.rb', line 1287

def instance_id
  @instance_id
end

#jobflow_detailObject

Returns the value of attribute jobflow_detail.


1287
1288
1289
# File 'lib/commands.rb', line 1287

def jobflow_detail
  @jobflow_detail
end

#jobflow_idObject

Returns the value of attribute jobflow_id.


1287
1288
1289
# File 'lib/commands.rb', line 1287

def jobflow_id
  @jobflow_id
end

#key_pair_fileObject

Returns the value of attribute key_pair_file.


1287
1288
1289
# File 'lib/commands.rb', line 1287

def key_pair_file
  @key_pair_file
end

#no_waitObject

Returns the value of attribute no_wait.


1287
1288
1289
# File 'lib/commands.rb', line 1287

def no_wait
  @no_wait
end

Instance Method Details

#enact(client) ⇒ Object


1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
# File 'lib/commands.rb', line 1315

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

  ec2_endpoint = "https://ec2.amazonaws.com"
  az = self.jobflow_detail['Instances']['Placement']['AvailabilityZone']
  reg_length = "us-east-1".length
  if az[0, reg_length] == "us-east-1" then
    ec2_endpoint = "https://ec2.us-east-1.amazonaws.com"
  elsif az[0, reg_length] == "us-west-1" then
    ec2_endpoint = "https://ec2.us-west-1.amazonaws.com"
  elsif az[0, reg_length] == "eu-west-1" then
    ec2_endpoint = "https://ec2.eu-west-1.amazonaws.com"
  elsif az[0, reg_length] == "ap-southeast-1" then
    ec2_endpoint = "https://ec2.ap-southeast-1.amazonaws.com"
  elsif az[0, reg_length] == "ap-northeast-1" then
    ec2_endpoint = "https://ec2.ap-northeast-1.amazonaws.com"
  end
  commands.global_options[:ec2_endpoint] = ec2_endpoint
  
  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


1296
1297
1298
# File 'lib/commands.rb', line 1296

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

#wait_for_jobflow(client) ⇒ Object


1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
# File 'lib/commands.rb', line 1300

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