Module: StateTransitionHelper

Included in:
ScriptExecutionState
Defined in:
lib/help/state_transition_helper.rb

Instance Method Summary collapse

Instance Method Details

#attach_volume(volume_id, instance_id, temp_device_name, timeout = 240) ⇒ Object

Attaches an EBS volume to an instance Input Parameters:

  • volume_id => EC2 ID for the EBS Volume to be attached

  • instance_id => EC2 ID for the instance to which the volume is supposed to be attached to

  • temp_device_name => device name to be used for attaching (e.g. /dev/sdj1)



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/help/state_transition_helper.rb', line 391

def attach_volume(volume_id, instance_id, temp_device_name, timeout = 240)
  post_message("going to attach volume #{volume_id} to instance #{instance_id} on device #{temp_device_name}...")
  @logger.info "attach volume #{volume_id} to instance #{instance_id} on device #{temp_device_name} (using a timeout of #{timeout}s)"
  ec2_handler().attach_volume(:volume_id => volume_id,
    :instance_id => instance_id,
    :device => temp_device_name
  )
  done = false
  #timeout = 120
  while timeout > 0
    res = ec2_handler().describe_volumes(:volume_id => volume_id)
    vol_state = res['volumeSet']['item'][0]['status']
    attachment_state = "attaching"
    if res['volumeSet']['item'][0]['attachmentSet'] != nil
      attachment_state = res['volumeSet']['item'][0]['attachmentSet']['item'][0]['status']
    end
    @logger.debug "storage attaching: volume state: #{vol_state}, attachment state: #{attachment_state}"
    if vol_state == 'in-use' && attachment_state == 'attached' 
      done = true
      timeout = 0
    end
    sleep(10)
    timeout -= 10
  end
  msg = ""
  if !done
    msg = "Failed to attach volume '#{volume_id}' to instance '#{instance_id}"
    @logger.error "#{msg}"
    raise Exception.new("volume #{volume_id} not attached")
  else
    msg = "volume #{volume_id} successfully attached" 
    @logger.info "#{msg}"
  end
  post_message("#{msg}")
end

#connect(dns_name, user_name, ssh_keyfile = nil, ssh_keydata = nil, trials = 10, wait_between_trials = 30) ⇒ Object

Connects to the remote host via SSH. Params:

  • dns_name => machine to connect to

  • user_name => name to be used for connection

  • ssh_keyfile => key-file used for ssh

  • ssh_keydata => contents of key-file (either use ssh_keyfile or ssh_keydata)

Returns:

  • OS of the connected machine



41
42
43
44
45
46
47
48
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
# File 'lib/help/state_transition_helper.rb', line 41

def connect(dns_name, user_name, ssh_keyfile = nil, ssh_keydata = nil,
    trials = 10, wait_between_trials = 30)
  post_message("connecting '#{user_name}' to #{dns_name}...")
  connected = false
  last_connection_problem = ""
  remaining_trials = trials
  while !connected && remaining_trials > 0
    remaining_trials -= 1
    if ssh_keyfile != nil
      begin
        @logger.info("connecting using keyfile")
        remote_handler().connect_with_keyfile(dns_name, ssh_keyfile)
        connected = true
      rescue Exception => e
        @logger.info("connection failed due to #{e}")
        last_connection_problem = "#{e}"
        @logger.debug(e.backtrace.select(){|line| line.include?("state_transition_helper")}.join("\n"))
      end
    elsif ssh_keydata != nil
      begin
        @logger.info("connecting using keydata")
        remote_handler().connect(dns_name, user_name, ssh_keydata)
        connected = true
      rescue Exception => e
        @logger.info("connection failed due to #{e}")
        last_connection_problem = "#{e}"
        @logger.debug(e.backtrace.select(){|line| line.include?("state_transition_helper")}.join("\n"))
      end
    else
      raise Exception.new("no key information specified")
    end
    if !connected
      sleep(wait_between_trials) #try again
    end
  end
  if !connected
    raise Exception.new("connection attempts stopped (#{last_connection_problem})")
  end
  os = remote_handler().retrieve_os()
  sudo = remote_handler().use_sudo ? " [sudo]" : ""
  if os =~ /^Please login as .+$/i
    post_message("not connected to #{dns_name}#{sudo} due to an invalid user.")
    @logger.error "not connected to #{dns_name}#{sudo} due to an invalid user."
    raise Exception.new("invalid user: #{os}")
  end
  post_message("connected to #{dns_name}#{sudo}. OS installed is #{os}")
  @logger.info "connected to #{dns_name}#{sudo} OS installed is #{os}"
  return os
end

#copy_distribution(destination_path) ⇒ Object

Copy all files of a running linux distribution via rsync to a mounted directory Input Parameters:

  • destination_path => where to copy to



982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
# File 'lib/help/state_transition_helper.rb', line 982

def copy_distribution(destination_path)
  post_message("going to start copying files to #{destination_path}. This may take quite a time...")
  @logger.debug "start copying to #{destination_path}"
  start = Time.new.to_i
  if remote_handler().tools_installed?("rsync")
    @logger.debug "use rsync command line"
    status = remote_handler().local_rsync("/", "#{destination_path}", "#{destination_path}")
    status = remote_handler().local_rsync("/dev/", "#{destination_path}/dev/")
    if status == false
      raise Exception.new("failed to copy distribution remotely using rsync")
    end
  else
    @logger.debug "use cp command line"
    status = remote_handler().local_rcopy("/", "#{destination_path}", "/proc /sys /dev /mnt")
    if status == false
      raise Exception.new("failed to copy distribution remotely using cp")
    end
    status = remote_handler().mkdir("#{destination_path}/proc")
    status = remote_handler().mkdir("#{destination_path}/sys")
    status = remote_handler().mkdir("#{destination_path}/mnt")
    status = remote_handler().mkdir("#{destination_path}/dev")
  end
  endtime = Time.new.to_i
  @logger.info "copy took #{(endtime-start)}s"
  post_message("copying is done (took #{endtime-start})s")
end

#create_fs(dns_name, device) ⇒ Object

Create a file-system on a given machine (assumes to be connected already). Input Parameters:

  • dns_name => IP used

  • device => device to be used for file-system creation (e.g. /dev/sdj)



606
607
608
609
610
611
612
613
614
# File 'lib/help/state_transition_helper.rb', line 606

def create_fs(dns_name, device)
  post_message("going to create filesystem on #{dns_name} to #{device}...")
  @logger.debug "create filesystem on #{dns_name} to #{device}"
  status = remote_handler().create_filesystem("ext3", device)
  if status == false
    raise Exception.new("Failed to create ext3 filesystem on #{device} device on #{dns_name}")
  end
  post_message("filesystem system successfully created on device #{device}")
end

#create_image_from_instance(instance_id, name, description) ⇒ Object

Create an AMI from an EC2 instance



558
559
560
561
562
563
564
# File 'lib/help/state_transition_helper.rb', line 558

def create_image_from_instance(instance_id, name, description)
  post_message("going to create AMI from instance #{instance_id}...")
  @logger.debug "create AMI from instance #{instance_id} as #{name}"
  res = ec2_handler().create_image(:instance_id => instance_id, :name => name, :description => description)
  image_id = res['imageId']
  return image_id
end

#create_labeled_fs(dns_name, device, type, label) ⇒ Object

Create a file-system on a given machine (assumes to be connected already). Input Parameters:

  • dns_name => IP used

  • device => device to be used for file-system creation (e.g. /dev/sdj)

  • type => filesystem type (ext2, ext3, ext4)

  • label => add a label to the partition



622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/help/state_transition_helper.rb', line 622

def create_labeled_fs(dns_name, device, type, label)
  post_message("going to create filesystem on #{dns_name} to #{device}...")
  @logger.debug "create filesystem of type '#{type}' (default is ext3) on '#{dns_name}' to '#{device}'"
  fs_type = "ext3"
  if !type.nil? && !type.empty?
    fs_type = type
  end
  #XXX: detect new kernel that have /dev/xvdX device node instead of /dev/sdX
  if device =~ /\/dev\/sd[a-z]/
    if !remote_handler().file_exists?(device)
      post_message("'#{device}' device node not found, checking for new kernel support...")
      @logger.debug "'#{device}' device node not found, checking for new kernel support" 
      new_device = device.gsub('sd', 'xvd')
      if remote_handler().file_exists?(new_device)
        post_message("'#{new_device}' device node found")
        @logger.debug "'#{new_device}' device node found"
        device = new_device
      end
    end
  #elsif device =~/\/dev\/xvd[a-z]/
  end

  @logger.debug "create '#{fs_type}' filesystem on device '#{device}'"
  status = remote_handler().create_filesystem(fs_type, device)
  if status == false
    raise Exception.new("Failed to create #{type} filesystem on #{device} device on #{dns_name}")
  end
  post_message("#{fs_type} filesystem system successfully created on device #{device}")
  if !label.nil? && !label.empty?
    post_message("going to add label #{label} for device #{device}...")
    @logger.debug "add label '#{label}' to device '#{device}'"
    if remote_handler().set_device_label_ext(device, label, fs_type)
      post_message("label #{label} added to device #{device}")
    else
      raise Exception.new("Failed to add label #{label} to device #{device}")
    end
  end
end

#create_security_group_with_rules(name, desc, rules) ⇒ Object

Create a Security group with specific rules NB: - if the Security Group already exists add teh required rules if not present

- do not try to delete the Security Group because this will disallow multi-tasks and parallelization


569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/help/state_transition_helper.rb', line 569

def create_security_group_with_rules(name, desc, rules)
  post_message("going to create '#{name}' Security Group...")
  @logger.debug "create Security Group (name: #{name}, desc: #{desc})"
  begin
    res = ec2_handler().create_security_group(:group_name => name, :group_description => desc)
  rescue AWS::InvalidGroupDuplicate => e
    @logger.warn "'#{name}' Security Group already exists: #{e.to_s}"
  ensure
    rules.each(){ |rule|
      begin
        ec2_handler().authorize_security_group_ingress(:group_name => name,
          :ip_protocol => rule[:ip_protocol], :from_port => rule[:from_port], :to_port => rule[:to_port], :cidr_ip => rule[:cidr_ip])
      rescue AWS::InvalidPermissionDuplicate => e
        @logger.warn "'#{rule[:ip_protocol].upcase()} (#{rule[:from_port]}-#{rule[:to_port]})' rule already exists: #{e.to_s}"
      end
    }
  end
  return true
end

#create_snapshot(volume_id, description = "") ⇒ Object

Creates a snapshot for an EBS volume.

Input Parameters
  • volume_id => EC2 ID for the EBS volume to be snapshotted

Returns:

  • snapshot_id => EC2 ID for the snapshot created



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/help/state_transition_helper.rb', line 477

def create_snapshot(volume_id, description = "")
  post_message("going to create a snapshot for volume #{volume_id}...")
  @logger.debug "create snapshot for volume #{volume_id}"
  start_time = Time.new.to_i
  res = ec2_handler().create_snapshot(:volume_id => volume_id,
    :description => description)
  snapshot_id = res['snapshotId']
  @logger.info "snapshot_id = #{snapshot_id}"
  done = false
  while !done
    sleep(5)
    #TODO: check for timeout?
    res = ec2_handler().describe_snapshots(:snapshot_id => snapshot_id)
    @logger.debug "snapshot creating: #{res.inspect}"
    if res['snapshotSet']['item'][0]['status'] == 'completed'
      done = true
    end
  end
  end_time = Time.new.to_i
  @logger.info "create snapshot took #{(end_time-start_time)}s"
  post_message("snapshot is done with ID=#{snapshot_id} (took #{(end_time-start_time)}s)")
  return snapshot_id
end

#create_volume(availability_zone, size = "10") ⇒ Object

Creates a new EBS volume. Input Parameters:

  • availability_zone => availability zone for the volume

  • size => size in Gigabytes

Returns

  • volume_id => EC2 EBS Volume ID



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/help/state_transition_helper.rb', line 336

def create_volume(availability_zone, size = "10")
  post_message("going to create a new EBS volume of size #{size}GB...")
  @logger.debug "create volume in zone #{availability_zone}"
  res = ec2_handler().create_volume(:availability_zone => availability_zone, :size => size.to_s)
  volume_id = res['volumeId']
  started = false
  while !started
    sleep(5)
    #TODO: check for timeout?
    res = ec2_handler().describe_volumes(:volume_id => volume_id)
    state = res['volumeSet']['item'][0]['status']
    @logger.debug "volume state #{state}"
    if state == 'available'
      started = true
    end
  end
  post_message("EBS volume #{volume_id} is ready")
  return volume_id
end

#create_volume_from_snapshot(snapshot_id, availability_zone) ⇒ Object

Creates a new EBS volume from a snapshot ID. Input Parameters:

  • availability_zone => availability zone for the volume

  • size => size of the volume to be created

  • snapshot_id => EC2 Snapshot ID used to create the volume

Returns

  • volume_id => EC2 EBS Volume ID created



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/help/state_transition_helper.rb', line 363

def create_volume_from_snapshot(snapshot_id, availability_zone)
  post_message("going to create a new EBS volume from the specified snapshot...")
  @logger.debug "create volume in zone #{availability_zone}"
  start_time = Time.new.to_i
  res = ec2_handler().create_volume(:snapshot_id => snapshot_id, :availability_zone => availability_zone)
  volume_id = res['volumeId']
  started = false
  while !started
    sleep(5)
    #TODO: check for timeout?
    res = ec2_handler().describe_volumes(:volume_id => volume_id)
    state = res['volumeSet']['item'][0]['status']
    @logger.debug "volume state #{state}"
    if state == 'available'
      started = true
    end
  end
  end_time = Time.new.to_i
  @logger.info "create volume from snapshot took #{(end_time-start_time)}s"
  post_message("EBS volume #{volume_id} is ready (took #{end_time-start_time}s)")
  return volume_id
end

#delete_security_group(name) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/help/state_transition_helper.rb', line 589

def delete_security_group(name)
  post_message("going to delete '#{name}' Security Group...")
  @logger.debug "delete Security Group (name: #{name})"
  res = ec2_handler().describe_security_groups(:group_name => name)
  if res['securityGroupInfo']['item'].size > 0
    @logger.debug "'#{name}' Security Group found."
    res = ec2_handler().delete_security_group(:group_name => name) 
  else
    @logger.warn "'#{name}' Security Group not found."
  end
  return true
end

#delete_snapshot(snapshot_id) ⇒ Object

Deletes a snapshot.



502
503
504
505
506
507
508
# File 'lib/help/state_transition_helper.rb', line 502

def delete_snapshot(snapshot_id)
  post_message("going to delete snapshot #{snapshot_id}...")
  @logger.info("going to delete snapshot #{snapshot_id}...")
  ec2_handler().delete_snapshot(:snapshot_id => snapshot_id)
  @logger.info("snapshot #{snapshot_id} deleted")
  post_message("snapshot #{snapshot_id} deleted")
end

#delete_volume(volume_id) ⇒ Object

Delete an EBS volume. Input Parameters:

  • volume_id => EC2 ID for the EBS Volume to be deleted



465
466
467
468
469
470
# File 'lib/help/state_transition_helper.rb', line 465

def delete_volume(volume_id)
  post_message("going to delete volume #{volume_id} (no longer needed)...")
  @logger.debug "delete volume #{volume_id}"
  ec2_handler().delete_volume(:volume_id => volume_id)
  post_message("volume #{volume_id} deleted")
end

#describe_instance(instance_id) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/help/state_transition_helper.rb', line 217

def describe_instance(instance_id)
  res = ec2_handler.describe_instances(:instance_id => instance_id)
  state = res['reservationSet']['item'][0]['instancesSet']['item'][0]['instanceState']
  @logger.info "instance is in state #{state['name']} (#{state['code']})"
  dns_name = ""
  availability_zone = ""
  kernel_id = ""
  ramdisk_id = ""
  architecture = ""
  root_device_name = ""
  if state['code'].to_i == 16
    started = true
    post_message("instance is up and running")
    dns_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['dnsName']
    availability_zone = res['reservationSet']['item'][0]['instancesSet']['item'][0]['placement']['availabilityZone']
    kernel_id = res['reservationSet']['item'][0]['instancesSet']['item'][0]['kernelId']
    ramdisk_id = res['reservationSet']['item'][0]['instancesSet']['item'][0]['ramdiskId']
    architecture = res['reservationSet']['item'][0]['instancesSet']['item'][0]['architecture']
    root_device_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['rootDeviceName']
  else
    post_message("instance is not up and running: code #{state['code'].to_i}")
    availability_zone = res['reservationSet']['item'][0]['instancesSet']['item'][0]['placement']['availabilityZone']
    kernel_id = res['reservationSet']['item'][0]['instancesSet']['item'][0]['kernelId']
    ramdisk_id = res['reservationSet']['item'][0]['instancesSet']['item'][0]['ramdiskId']
    architecture = res['reservationSet']['item'][0]['instancesSet']['item'][0]['architecture']
    root_device_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['rootDeviceName']
  end
  return instance_id, dns_name, availability_zone, kernel_id, ramdisk_id, architecture, root_device_name
end

#detach_volume(volume_id, instance_id, timeout = 240) ⇒ Object

Detach an EBS volume from an instance. Input Parameters:

  • volume_id => EC2 ID for the EBS Volume to be detached

  • instance_id => EC2 ID for the instance to detach from



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/help/state_transition_helper.rb', line 431

def detach_volume(volume_id, instance_id, timeout = 240)
  post_message("going to detach volume #{volume_id} from instance #{instance_id}...")
  @logger.info "detach volume #{volume_id} from instance #{instance_id} (using a timeout of #{timeout}s)"
  ec2_handler().detach_volume(:volume_id => volume_id,
    :instance_id => instance_id
  )
  done = false
  #timeout = 120
  while timeout > 0
    sleep(3)
    res = ec2_handler().describe_volumes(:volume_id => volume_id)
    @logger.debug "volume detaching: #{res.inspect}"
    if res['volumeSet']['item'][0]['status'] == 'available'
      done = true
      timeout = 0
    end
    sleep(10)
    timeout -= 10
  end
  msg = ""
  if !done
    msg = "Failed to detach volume '#{volume_id}' from instance '#{instance_id}"
    @logger.error "#{msg}"
    raise Exception.new("volume #{volume_id} not detached")
  else
    msg = "volume #{volume_id} successfully detached" 
    @logger.info "#{msg}"
  end
  post_message("#{msg}")
end

#determine_file(ip, user_name, ssh_keydata, file_candidates) ⇒ Object

From a list of existing files, return the first that exists



1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
# File 'lib/help/state_transition_helper.rb', line 1174

def determine_file(ip, user_name, ssh_keydata, file_candidates)
  connect(ip, user_name, nil, ssh_keydata)
  begin
    file_candidates.each() {|file_path|
      if remote_handler().file_exists?(file_path)
        return file_path
      end
    }
    return nil
  rescue
    raise
  ensure
    disconnect()
  end
end

#disable_ssh_tty(host) ⇒ Object



1131
1132
1133
1134
1135
1136
# File 'lib/help/state_transition_helper.rb', line 1131

def disable_ssh_tty(host)
  post_message("going to disable SSH tty on #{host}...")
  @logger.info "disable SSH tty on #{host}"
  remote_handler().disable_sudoers_requiretty()
  post_message("SSH tty disabled")
end

#disconnectObject

If a remote command handler is connected, disconnect him silently.



92
93
94
95
96
97
98
# File 'lib/help/state_transition_helper.rb', line 92

def disconnect
  begin
    remote_handler().disconnect()
  rescue
  end
  self.remote_handler= nil
end

#ec2_handlerObject



1482
1483
1484
1485
1486
1487
# File 'lib/help/state_transition_helper.rb', line 1482

def ec2_handler()
  if @ec2_handler == nil
    @ec2_handler = @context[:ec2_api_handler]
  end
  @ec2_handler
end

#ec2_handler=(ec2_handler) ⇒ Object



1489
1490
1491
# File 'lib/help/state_transition_helper.rb', line 1489

def ec2_handler=(ec2_handler)
  @ec2_handler = ec2_handler
end

#enable_ssh_tty(host) ⇒ Object



1138
1139
1140
1141
1142
1143
# File 'lib/help/state_transition_helper.rb', line 1138

def enable_ssh_tty(host)
  post_message("going to enable SSH tty on #{host}...")
  @logger.info "enable SSH tty on #{host}"
  remote_handler().enable_sudoers_requiretty()
  post_message("SSH tty enabled")
end

#get_aws_kernel_image_aki(source_endpoint, source_aki, target_endpoint) ⇒ Object

Mapping AmazonKernel Image IDs From documentation: docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?UserProvidedkernels.html

  • US-East-1

    aki-4c7d9525 ec2-public-images/pv-grub-hd00-V1.01-i386.gz.manifest.xml
    aki-4e7d9527 ec2-public-images/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml
    aki-407d9529 ec2-public-images/pv-grub-hd0-V1.01-i386.gz.manifest.xml
    aki-427d952b ec2-public-images/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml
    aki-525ea73b ec2-public-images/pv-grub-hd00_1.02-i386.gz.manifest.xml
    aki-8e5ea7e7 ec2-public-images/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-805ea7e9 ec2-public-images/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-825ea7eb ec2-public-images/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    
  • US-West-1

    aki-9da0f1d8 ec2-public-images-us-west-1/pv-grub-hd00-V1.01-i386.gz.manifest.xml
    aki-9fa0f1da ec2-public-images-us-west-1/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml
    aki-99a0f1dc ec2-public-images-us-west-1/pv-grub-hd0-V1.01-i386.gz.manifest.xml
    aki-9ba0f1de ec2-public-images-us-west-1/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml
    aki-87396bc2 ec2-public-images-us-west-1/pv-grub-hd00_1.02-i386.gz.manifest.xml
    aki-81396bc4 ec2-public-images-us-west-1/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-83396bc6 ec2-public-images-us-west-1/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-8d396bc8 ec2-public-images-us-west-1/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    
  • US-West-2

    aki-dee26fee ec2-public-images-us-west-2/pv-grub-hd00-V1.01-i386.gz.manifest.xml
    aki-90e26fa0 ec2-public-images-us-west-2/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml
    aki-dce26fec ec2-public-images-us-west-2/pv-grub-hd0-V1.01-i386.gz.manifest.xml
    aki-ace26f9c ec2-public-images-us-west-2/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml
    aki-c0e26ff0 ec2-public-images-us-west-2/pv-grub-hd00_1.02-i386.gz.manifest.xml
    aki-94e26fa4 ec2-public-images-us-west-2/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-c2e26ff2 ec2-public-images-us-west-2/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-98e26fa8 ec2-public-images-us-west-2/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    
  • EU-West-1

    aki-47eec433 ec2-public-images-eu/pv-grub-hd00-V1.01-i386.gz.manifest.xml
    aki-41eec435 ec2-public-images-eu/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml
    aki-4deec439 ec2-public-images-eu/pv-grub-hd0-V1.01-i386.gz.manifest.xml
    aki-4feec43b ec2-public-images-eu/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml
    aki-8a6657fe ec2-public-images-eu/pv-grub-hd00_1.02-i386.gz.manifest.xml
    aki-60695814 ec2-public-images-eu/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-64695810 ec2-public-images-eu/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-62695816 ec2-public-images-eu/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    
  • AP-SouthEast-1

    aki-6fd5aa3d ec2-public-images-ap-southeast-1/pv-grub-hd00-V1.01-i386.gz.manifest.xml
    aki-6dd5aa3f ec2-public-images-ap-southeast-1/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml
    aki-13d5aa41 ec2-public-images-ap-southeast-1/pv-grub-hd0-V1.01-i386.gz.manifest.xml
    aki-11d5aa43 ec2-public-images-ap-southeast-1/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml
    aki-a0225af2 ec2-public-images-ap-southeast-1/pv-grub-hd00_1.02-i386.gz.manifest.xml
    aki-a6225af4 ec2-public-images-ap-southeast-1/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-a4225af6 ec2-public-images-ap-southeast-1/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-aa225af8 ec2-public-images-ap-southeast-1/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    
  • AP-NorthEast-1

    aki-d209a2d3 ec2-public-images-ap-northeast-1/pv-grub-hd0-V1.01-i386.gz.manifest.xml
    aki-d409a2d5 ec2-public-images-ap-northeast-1/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml
    aki-d609a2d7 ec2-public-images-ap-northeast-1/pv-grub-hd00-V1.01-i386.gz.manifest.xml
    aki-d809a2d9 ec2-public-images-ap-northeast-1/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml
    aki-e85df7e9 ec2-public-images-ap-northeast-1/pv-grub-hd00_1.02-i386.gz.manifest.xml
    aki-ea5df7eb ec2-public-images-ap-northeast-1/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-ec5df7ed ec2-public-images-ap-northeast-1/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-ee5df7ef ec2-public-images-ap-northeast-1/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    
  • SA-East-1

    aki-cc3ce3d1 ec2-public-images-sa-east-1/pv-grub-hd0_1.02-x86_64.gz.manifest.xml
    aki-bc3ce3a1 ec2-public-images-sa-east-1/pv-grub-hd0_1.02-i386.gz.manifest.xml
    aki-d23ce3cf ec2-public-images-sa-east-1/pv-grub-hd00_1.02-x86_64.gz.manifest.xml
    aki-823ce39f ec2-public-images-sa-east-1/pv-grub-hd00_1.02-i386.gz.manifest.xml
    


1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
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
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
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'lib/help/state_transition_helper.rb', line 1251

def get_aws_kernel_image_aki(source_endpoint, source_aki, target_endpoint)
  map = { 'us-east-1' => {'aki-4c7d9525' => 'pv-grub-hd00-V1.01-i386',
                          'aki-4e7d9527' => 'pv-grub-hd00-V1.01-x86_64',
                          'aki-407d9529' => 'pv-grub-hd0-V1.01-i386',
                          'aki-427d952b' => 'pv-grub-hd0-V1.01-x86_64',
                          'aki-525ea73b' => 'pv-grub-hd00_1.02-i386',
                          'aki-8e5ea7e7' => 'pv-grub-hd00_1.02-x86_64',
                          'aki-805ea7e9' => 'pv-grub-hd0_1.02-i386',
                          'aki-825ea7eb' => 'pv-grub-hd0_1.02-x86_64',
                          'aki-b6aa75df' => 'pv-grub-hd0_1.03-i386',
                          'aki-88aa75e1' => 'pv-grub-hd0_1.03-x86_64',
                          'aki-b2aa75db' => 'pv-grub-hd00_1.03-i386',
                          'aki-b4aa75dd' => 'pv-grub-hd00_1.03-x86_64',

                          #RHEL kernel Amazon Kernel ID
                          'aki-36ed075f' => 'aki-rhel-i386',		# RH-pv-grub-hd0-V1.01-i386
                          'aki-08ed0761' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                          #Ubuntu kernel Amazon Kernel ID
                          'aki-5f15f636' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         }, 
          'us-west-1' => {'aki-9da0f1d8' => 'pv-grub-hd00-V1.01-i386',
                          'aki-9fa0f1da' => 'pv-grub-hd00-V1.01-x86_64',
                          'aki-99a0f1dc' => 'pv-grub-hd0-V1.01-i386',
                          'aki-9ba0f1de' => 'pv-grub-hd0-V1.01-x86_64',
                          'aki-87396bc2' => 'pv-grub-hd00_1.02-i386',
                          'aki-81396bc4' => 'pv-grub-hd00_1.02-x86_64',
                          'aki-83396bc6' => 'pv-grub-hd0_1.02-i386',
                          'aki-8d396bc8' => 'pv-grub-hd0_1.02-x86_64',
                          'aki-f57e26b0' => 'pv-grub-hd0_1.03-i386',
                          'aki-f77e26b2' => 'pv-grub-hd0_1.03-x86_64',
                          'aki-e97e26ac' => 'pv-grub-hd00_1.03-i386',
                          'aki-eb7e26ae' => 'pv-grub-hd00_1.03-x86_64',

                          #RHEL kernel Amazon Kernel ID:
                          'aki-772c7f32' => 'aki-rhel-i386',		# RH-pv-grub-hd0-V1.01-i386
                          'aki-712c7f34' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                          #Ubuntu kernel Amazon Kernel ID
                          'aki-773c6d32' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         },
          'us-west-2' => {'aki-dee26fee' => 'pv-grub-hd00-V1.01-i386',
                          'aki-90e26fa0' => 'pv-grub-hd00-V1.01-x86_64',
                          'aki-dce26fec' => 'pv-grub-hd0-V1.01-i386',
                          'aki-ace26f9c' => 'pv-grub-hd0-V1.01-x86_64',
                          'aki-c0e26ff0' => 'pv-grub-hd00_1.02-i386',
                          'aki-94e26fa4' => 'pv-grub-hd00_1.02-x86_64',
                          'aki-c2e26ff2' => 'pv-grub-hd0_1.02-i386',
                          'aki-98e26fa8' => 'pv-grub-hd0_1.02-x86_64',
                          'aki-fa37baca' => 'pv-grub-hd0_1.03-i386',
                          'aki-fc37bacc' => 'pv-grub-hd0_1.03-x86_64',
                          'aki-f637bac6' => 'pv-grub-hd00_1.03-i386',
                          'aki-f837bac8' => 'pv-grub-hd00_1.03-x86_64',

                          #RHEL kernel Amazon Kernel ID
                          'aki-2efa771e' => 'aki-rhel-i386',		# RH-pv-grub-hd0-V1.01-i386
                          'aki-10fa7720' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                          #Ubuntu kernel Amazon Kernel ID
                          '' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         },
          'eu-west-1' => {'aki-47eec433' => 'pv-grub-hd00-V1.01-i386',
                          'aki-41eec435' => 'pv-grub-hd00-V1.01-x86_64',
                          'aki-4deec439' => 'pv-grub-hd0-V1.01-i386',
                          'aki-4feec43b' => 'pv-grub-hd0-V1.01-x86_64',
                          'aki-8a6657fe' => 'pv-grub-hd00_1.02-i386',
                          'aki-60695814' => 'pv-grub-hd00_1.02-x86_64',
                          'aki-64695810' => 'pv-grub-hd0_1.02-i386',
                          'aki-62695816' => 'pv-grub-hd0_1.02-x86_64',
                          'aki-75665e01' => 'pv-grub-hd0_1.03-i386',
                          'aki-71665e05' => 'pv-grub-hd0_1.03-x86_64',
                          'aki-89655dfd' => 'pv-grub-hd00_1.03-i386',
                          'aki-8b655dff' => 'pv-grub-hd00_1.03-x86_64',

                          #RHEL kernel Amazon Kernel ID
                          'aki-af0a3ddb' => 'aki-rhel-i386',		# RH-pv-grub-hd0-V1.01-i386
                          'aki-a90a3ddd' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                          #Ubuntu kernel Amazon Kernel ID
                          'aki-b02a01c4' => 'aki-ubuntu-karmic-v2.6.31-302.i386'	
                         },
          'ap-southeast-1' => {'aki-6fd5aa3d' => 'pv-grub-hd00-V1.01-i386',
                               'aki-6dd5aa3f' => 'pv-grub-hd00-V1.01-x86_64',
                               'aki-13d5aa41' => 'pv-grub-hd0-V1.01-i386',
                               'aki-11d5aa43' => 'pv-grub-hd0-V1.01-x86_64',
                               'aki-a0225af2' => 'pv-grub-hd00_1.02-i386',
                               'aki-a6225af4' => 'pv-grub-hd00_1.02-x86_64',
                               'aki-a4225af6' => 'pv-grub-hd0_1.02-i386',
                               'aki-aa225af8' => 'pv-grub-hd0_1.02-x86_64',
                               'aki-f81354aa' => 'pv-grub-hd0_1.03-i386',
                               'aki-fe1354ac' => 'pv-grub-hd0_1.03-x86_64',
                               'aki-f41354a6' => 'pv-grub-hd00_1.03-i386',
                               'aki-fa1354a8' => 'pv-grub-hd00_1.03-x86_64',

                               #RHEL kernel Amazon Kernel ID
                               'aki-9c235ace' => 'aki-rhel-i386',	# RH-pv-grub-hd0-V1.01-i386
                               'aki-82235ad0' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                               #Ubuntu kernel Amazon Kernel ID
                               'aki-87f38cd5' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         },
          'ap-southeast-2' => {'' => 'pv-grub-hd00-V1.01-i386',
                               '' => 'pv-grub-hd00-V1.01-x86_64',
                               '' => 'pv-grub-hd0-V1.01-i386',
                               '' => 'pv-grub-hd0-V1.01-x86_64',
                               '' => 'pv-grub-hd00_1.02-i386',
                               '' => 'pv-grub-hd00_1.02-x86_64',
                               '' => 'pv-grub-hd0_1.02-i386',
                               '' => 'pv-grub-hd0_1.02-x86_64',
                               'aki-33990e09' => 'pv-grub-hd0_1.03-i386',
                               'aki-31990e0b' => 'pv-grub-hd0_1.03-x86_64',
                               'aki-3f990e05' => 'pv-grub-hd00_1.03-i386',
                               'aki-3d990e07' => 'pv-grub-hd00_1.03-x86_64',

                               #RHEL kernel Amazon Kernel ID
                               'aki-9b8413a1' => 'aki-rhel-i386',	# RH-pv-grub-hd0-V1.01-i386
                               'aki-998413a3' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                               #Ubuntu kernel Amazon Kernel ID
                               '' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         },

          'ap-northeast-1' => {'aki-d209a2d3' => 'pv-grub-hd00-V1.01-i386',
                               'aki-d409a2d5' => 'pv-grub-hd00-V1.01-x86_64',
                               'aki-d609a2d7' => 'pv-grub-hd0-V1.01-i386',
                               'aki-d809a2d9' => 'pv-grub-hd0-V1.01-x86_64',
                               'aki-e85df7e9' => 'pv-grub-hd00_1.02-i386',
                               'aki-ea5df7eb' => 'pv-grub-hd00_1.02-x86_64',
                               'aki-ec5df7ed' => 'pv-grub-hd0_1.02-i386',
                               'aki-ee5df7ef' => 'pv-grub-hd0_1.02-x86_64',
                               'aki-42992843' => 'pv-grub-hd0_1.03-i386',
                               'aki-44992845' => 'pv-grub-hd0_1.03-x86_64',
                               'aki-3e99283f' => 'pv-grub-hd00_1.03-i386',
                               'aki-40992841' => 'pv-grub-hd00_1.03-x86_64',

                               #RHEL kernel Amazon Kernel ID
                               'aki-66c06a67' => 'aki-rhel-i386',	# RH-pv-grub-hd0-V1.01-i386
                               'aki-68c06a69' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                               #Ubuntu kernel Amazon Kernel ID
                               'aki-540fa455' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         },
          'sa-east-1' => {'aki-803ce39d' => 'pv-grub-hd00-V1.01-i386',
                          'aki-d03ce3cd' => 'pv-grub-hd00-V1.01-x86_64',
                          'aki-863ce39b' => 'pv-grub-hd0-V1.01-i386',
                          'aki-d63ce3cb' => 'pv-grub-hd0-V1.01-x86_64',
                          'aki-823ce39f' => 'pv-grub-hd00_1.02-i386',
                          'aki-d23ce3cf' => 'pv-grub-hd00_1.02-x86_64',
                          'aki-bc3ce3a1' => 'pv-grub-hd0_1.02-i386',
                          'aki-cc3ce3d1' => 'pv-grub-hd0_1.02-x86_64',
                          'aki-ca8f51d7' => 'pv-grub-hd0_1.03-i386',
                          'aki-c48f51d9' => 'pv-grub-hd0_1.03-x86_64',
                          'aki-ce8f51d3' => 'pv-grub-hd00_1.03-i386',
                          'aki-c88f51d5' => 'pv-grub-hd00_1.03-x86_64',

                          #RHEL kernel Amazon Kernel ID
                          'aki-1a38e707' => 'aki-rhel-i386',		# RH-pv-grub-hd0-V1.01-i386
                          'aki-1438e709' => 'aki-rhel-x86_64',	# RH-pv-grub-hd0-V1.01-x86_64

                          #Ubuntu kernel Amazon Kernel ID
                          '' => 'aki-ubuntu-karmic-v2.6.31-302.i386'
                         }
        }
  target_aki = ''
  source_region = get_aws_region_from_endpoint(source_endpoint)
  target_region = get_aws_region_from_endpoint(target_endpoint)
  post_message("mapping AKI '#{source_aki}' from #{source_region} region to #{target_region} region...")

  if map[source_region] == nil
    Exception.new("source region not supported")
  elsif map[target_region] == nil
    Exception.new("target region not supported")
  else
    if map[source_region][source_aki] == nil
      Exception.new("aki not found in source region")
    else
      pv_grub_info = map[source_region][source_aki]
      map[target_region].each() {|key, value|
        if pv_grub_info.eql?(value)
          @logger.debug "found AKI: #{key} for #{value}"
          target_aki = key
          break
        end
      }
    end
  end
  post_message("AKI mapped to #{target_aki}")
  return target_aki
end

#get_aws_region_from_endpoint(endpoint) ⇒ Object



1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
# File 'lib/help/state_transition_helper.rb', line 1441

def get_aws_region_from_endpoint(endpoint)
  region = "us-east-1" 
  case endpoint
    when /us-east/
      region = "us-east-1"
    when /us-west-1/
      region = "us-west-1"
    when /us-west-2/
      region = "us-west-2"
    when /eu-west/
      region = "eu-west-1"
    when /ap-southeast-1/
      region = "ap-southeast-1"
    when /ap-southeast-2/
      region = "ap-southeast-2"
    when /ap-northeast/
      region = "ap-northeast-1"
    when /sa-east-1/
      region = "sa-east-1"
    else
      region = "us-east-1"
  end
  return region
end

#get_partition_count(device) ⇒ Object

Get partition count



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/help/state_transition_helper.rb', line 765

def get_partition_count(device)
  post_message("Checking partition on device '#{device}'...")
  #XXX: detect new kernel that have /dev/xvdX device node instead of /dev/sdX
  if device =~ /\/dev\/sd[a-z]/
    if !remote_handler().file_exists?(device)
      post_message("'#{device}' device node not found, checking for new kernel support...")
      @logger.debug "'#{device}' device node not found, checking for new kernel support" 
      new_device = device.gsub('sd', 'xvd')
      if remote_handler().file_exists?(new_device)
        post_message("'#{new_device}' device node found")
        @logger.debug "'#{new_device}' device node found"
        device = new_device
      end
    end
  end
  parts = remote_handler().get_device_partition(device).split(/[[:space:]]+/)
  @logger.debug "Found '#{parts}' on device '#{device}'"
  return parts.size()
end

#get_partition_fs_type(part) ⇒ Object

Get partition filesytem type



919
920
921
922
923
924
925
926
927
928
929
930
931
# File 'lib/help/state_transition_helper.rb', line 919

def get_partition_fs_type(part)
  post_message("Retrieving '#{part}' partition filesystem type...")
  @logger.debug "get #{part} partition filesystem type"
  # get partition device and then its fs type
  part_fs_type = remote_handler().get_partition_fs_type(part)
  @logger.debug "Found '#{part_fs_type}' as filesystem type"
  if part_fs_type.nil? || part_fs_type.empty?
    raise Exception.new("Failed to retrieve filesystem type for '#{part}' partition")
  else
    post_message("'#{part}' partition contains an #{part_fs_type} filesystem")
  end
  return part_fs_type
end

#get_partition_fs_type_and_label(part) ⇒ Object

Get partition filesytem type and label



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'lib/help/state_transition_helper.rb', line 934

def get_partition_fs_type_and_label(part)
  post_message("Retrieving '#{part}' partition filesystem type...")
  @logger.debug "get #{part} partition filesystem type"
  # get partition device and then its fs type
  part_fs_type = remote_handler().get_partition_fs_type(part)
  @logger.debug "Found '#{part_fs_type}' as filesystem type"
  if part_fs_type.nil? || part_fs_type.empty?
    raise Exception.new("Failed to retrieve filesystem type for '#{part}' partition")
  else
    post_message("'#{part}' partition contains an #{part_fs_type} filesystem")
  end
  part_device = remote_handler().get_partition_device(part)
  @logger.debug "Found '#{part_device}' as partition device"
  if part_device.nil? || part_device.empty?
    raise Exception.new("Failed to retrieve device for '#{part}' partition")
  else
     post_message("'#{part}' partitition on device '#{part_device}'")
  end
  part_label = remote_handler().get_device_label_ext(part_device, part_fs_type)
  @logger.debug "Found label '#{part_label}'"
  if part_label.nil? || part_label.empty?
    post_message("'#{part}' partition has no label specified")
  else
    post_message("'#{part}' partition label '#{part_label}' for device node '#{part_device}'")
  end
  return part_fs_type, part_label
end

#get_partition_label(part) ⇒ Object

Get partition label



858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# File 'lib/help/state_transition_helper.rb', line 858

def get_partition_label(part)
  post_message("Retrieving '#{part}' partition label if any...")
  @logger.debug "get #{part} partition label"
  # get part device and then its label
  part_device = remote_handler().get_partition_device(part)
  @logger.debug "Found '#{part_device}' as partition device"
  label = remote_handler().get_device_label(part_device)
  @logger.debug "Found label '#{label}'"
  if label.nil? || label.empty?
    post_message("'#{part}' partition has no label specified")
  else
    post_message("'#{part}' partition label '#{label}' for device node '#{part_device}'")
  end
  return label
end

#get_partition_table(device) ⇒ Object

Get partition table



786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# File 'lib/help/state_transition_helper.rb', line 786

def get_partition_table(device)
  post_message("Retrieving the partition table of device '#{device}'...")
  #XXX: detect new kernel that have /dev/xvdX device node instead of /dev/sdX
  if device =~ /\/dev\/sd[a-z]/
    if !remote_handler().file_exists?(device)
      post_message("'#{device}' device node not found, checking for new kernel support...")
      @logger.debug "'#{device}' device node not found, checking for new kernel support" 
      new_device = device.gsub('sd', 'xvd')
      if remote_handler().file_exists?(new_device)
        post_message("'#{new_device}' device node found")
        @logger.debug "'#{new_device}' device node found"
        device = new_device
      end
    end
  end
  partition_table = remote_handler().get_partition_table(device)
  # remove ^M char
  partition_table.gsub!("\015", "")
  @logger.debug "Found the following partition table on device '#{device}'\n#{partition_table}"
  return partition_table
end

#get_root_device_nameObject

Get root partition



829
830
831
832
833
834
835
836
837
838
# File 'lib/help/state_transition_helper.rb', line 829

def get_root_device_name()
  post_message("Retrieving '/' root device name...")
  @logger.debug "issuing very first mount..."
  mount_output = remote_handler().mount_output()
  @logger.debug "mount output:\n#{mount_output}"
  @logger.debug "get root device name"
  root_device = remote_handler().get_root_device()
  @logger.debug "Found '#{root_device}' as root device"
  return root_device
end

#get_root_partition_fs_typeObject

Get root filesytem type



875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/help/state_transition_helper.rb', line 875

def get_root_partition_fs_type()
  post_message("Retrieving '/' root partition filesystem type...")
  @logger.debug "get root partition filesystem type"
  # get root device and then its fs type
  root_fs_type = remote_handler().get_root_fs_type()
  @logger.debug "Found '#{root_fs_type}' as root filesystem type"
  if root_fs_type.nil? || root_fs_type.empty?
    raise Exception.new("Failed to retrieve filesystem type for '/' root partition")
  else
    post_message("'/' root partition contains an #{root_fs_type} filesystem")
  end
  return root_fs_type
end

#get_root_partition_fs_type_and_labelObject

Get root filesytem type and label



890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/help/state_transition_helper.rb', line 890

def get_root_partition_fs_type_and_label()
  post_message("Retrieving '/' root partition filesystem type and label...")
  @logger.debug "get root partition filesystem type"
  # get root device and then its fs type
  root_fs_type = remote_handler().get_root_fs_type()
  @logger.debug "Found '#{root_fs_type}' as root filesystem type"
  if root_fs_type.nil? || root_fs_type.empty?
    raise Exception.new("Failed to retrieve filesystem type for '/' root partition")
  else
    post_message("'/' root partition contains an #{root_fs_type} filesystem")
  end
  root_device = remote_handler().get_root_device()
  @logger.debug "Found '#{root_device}' as root device"
  if root_device.nil? || root_device.empty?
    raise Exception.new("Failed to retrieve root device for '/' root partition")
  else
     post_message("'/' root partitition on root device '#{root_device}'")
  end
  root_label = remote_handler().get_device_label_ext(root_device, root_fs_type)
  @logger.debug "Found label '#{root_label}'"
  if root_label.nil? || root_label.empty?
    post_message("'/' root partition has no label specified")
  else
    post_message("'/' root partition label '#{root_label}' for root device node '#{root_device}'")
  end
  return root_fs_type, root_label
end

#get_root_partition_labelObject

Get root partition label



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
# File 'lib/help/state_transition_helper.rb', line 841

def get_root_partition_label()
  post_message("Retrieving '/' root partition label if any...")
  @logger.debug "get root partition label"
  # get root device and then its label
  root_device = remote_handler().get_root_device()
  @logger.debug "Found '#{root_device}' as root device"
  label = remote_handler().get_device_label(root_device)
  @logger.debug "Found label '#{label}'"
  if label.nil? || label.empty?
    post_message("'/' root partition has no label specified")
  else
    post_message("'/' root partition label '#{label}' for root device node '#{root_device}'")
  end
  return label
end

#get_root_volume_id(instance_id) ⇒ Object

Retrieve the root volume ID of an instance



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# File 'lib/help/state_transition_helper.rb', line 963

def get_root_volume_id(instance_id)
  post_message("getting 'root volume ID' (volume ID attached as RootDeviceName) of '#{instance_id}' instance...")
  volume_id = nil
  res = ec2_handler().describe_instances(:instance_id => instance_id)
  root_device_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['rootDeviceName']
  block_device_map = res['reservationSet']['item'][0]['instancesSet']['item'][0]['blockDeviceMapping']['item']
  block_device_map.each(){|blk_dev|
    if blk_dev['deviceName'] == root_device_name
      volume_id = blk_dev['ebs']['volumeId']
      post_message("found '#{volume_id}' attached as '#{root_device_name}' rootDeviceName")
      break
    end
  }
  return volume_id
end

#launch_instance(ami_id, key_name, security_group_name, ec2_handler = nil, type = nil, availability_zone = nil) ⇒ Object

Launch an instance based on an AMI ID Input Parameters:

  • ami_id => ID of the AMI to be launched

  • key_name => name of the key to access the instance

  • security_group_name => name of the security group to be used

  • type => type of instance to start

Returned information:

  • instance_id => ID of the started instance

  • dns_name => DNS name of the started instance

  • availability_zone => Availability zone of the started instance

  • kernel_id => EC2 Kernel ID of the started instance

  • ramdisk_id => EC2 Ramdisk ID of the started instance

  • architecture => architecture (e.g. 386i, 64x) of the started instance



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
# File 'lib/help/state_transition_helper.rb', line 113

def launch_instance(ami_id, key_name, security_group_name, ec2_handler = nil, type = nil, availability_zone = nil)
  ec2_handler = ec2_handler() if ec2_handler == nil
  post_message("starting up instance to execute the script (AMI = #{ami_id}) ...")
  @logger.debug "start up AMI #{ami_id}"
  # find out the image architecture first
  image_props = ec2_handler.describe_images(:image_id => ami_id)
  architecture = image_props['imagesSet']['item'][0]['architecture']
  instance_type = "m1.small"
  #instance_type = "t1.micro"
  if architecture != "i386"
    instance_type = "m1.large"
  end
  instance_type = type if type != nil
  arch_log_msg = "Architecture of image #{ami_id} is #{architecture}. Use instance_type #{instance_type}."
  @logger.info arch_log_msg
  post_message(arch_log_msg)
  # now start it
  res = ec2_handler.run_instances(:image_id => ami_id,
    :security_group => security_group_name, :key_name => key_name,
    :instance_type => instance_type, 
    :availability_zone => availability_zone
  )
  instance_id = res['instancesSet']['item'][0]['instanceId']
  @logger.info "started instance #{instance_id}"
  post_message("Started instance #{instance_id}. wait until it is ready...")
  #availability_zone , key_name/group_name
  started = false
  while started == false
    sleep(5)
    res = ec2_handler.describe_instances(:instance_id => instance_id)
    state = res['reservationSet']['item'][0]['instancesSet']['item'][0]['instanceState']
    @logger.info "instance is in state #{state['name']} (#{state['code']})"
    if state['code'].to_i == 16
      started = true
      post_message("instance is up and running")
      dns_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['dnsName']
      availability_zone = res['reservationSet']['item'][0]['instancesSet']['item'][0]['placement']['availabilityZone']
      kernel_id = res['reservationSet']['item'][0]['instancesSet']['item'][0]['kernelId']
      ramdisk_id = res['reservationSet']['item'][0]['instancesSet']['item'][0]['ramdiskId']
      architecture = res['reservationSet']['item'][0]['instancesSet']['item'][0]['architecture']
      root_device_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['rootDeviceName']
    elsif state['code'].to_i != 0
      post_message("instance in state #{state['name']}")
      raise Exception.new('instance failed to start up')
    else
      post_message("instance still starting up...")
    end
  end
  return instance_id, dns_name, availability_zone, kernel_id, ramdisk_id, architecture, root_device_name
end

#local_decompress_and_dump_file_to_device(source_filename, target_device) ⇒ Object

decompress and dump a file using gunzip and dd



1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
# File 'lib/help/state_transition_helper.rb', line 1088

def local_decompress_and_dump_file_to_device(source_filename, target_device)
  post_message("going to start decompressing and dumping file '#{source_filename}' to '#{target_device}' target device. This may take quite a time...")
  @logger.info "start decompressing and dumping '#{source_filename}' to '#{target_device}'"
  start_time = Time.new.to_i
  if remote_handler().tools_installed?("dd") && remote_handler().tools_installed?("gunzip")
    @logger.debug "use dd and gzip command line"
    status = remote_handler().local_decompress_and_dump(source_filename, target_device)
    if status == false
      @logger.error "failed to decompress and dump file"
      raise Exception.new("failed to decompress and dump file")
    end
  else
    @logger.error "dd and/or gunzip tools not installed"
    raise Exception.new("dd and/or gunzip tools not installed")
  end
  end_time = Time.new.to_i
  filesize = remote_handler().file_size(source_filename)
  @logger.info "decompress and dump file of #{filesize} took #{(end_time-start_time)}s"
  post_message("decompressing and dumping file of #{filesize} done (took #{end_time-start_time}s)")
end

#local_dump_and_compress_device_to_file(source_device, target_filename) ⇒ Object

dump and compress a device using dd and gzip



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/help/state_transition_helper.rb', line 1044

def local_dump_and_compress_device_to_file(source_device, target_filename)
  post_message("going to start dumping and compressing source device '#{source_device}' to '#{target_filename}' file. This may take quite a time...")
  @logger.info "start dumping and compressing '#{source_device}' to '#{target_filename}'"
  start_time = Time.new.to_i
  if remote_handler().tools_installed?("dd") && remote_handler().tools_installed?("gzip")
    @logger.debug "use dd and gzip command line"
    status = remote_handler().local_dump_and_compress(source_device, target_filename)
    if status == false
      @logger.error "failed to dump and compress device"
      raise Exception.new("failed to dump and compress device")
    end
  else
    @logger.error "dd and/or gzip tools not installed"
    raise Exception.new("dd and/or gzip tools not installed")
  end
  end_time = Time.new.to_i
  filesize = remote_handler().file_size(target_filename)
  @logger.info "dump and compress to a #{filesize} file took #{(end_time-start_time)}s"
  post_message("dumping and compressing to a #{filesize} file done (took #{end_time-start_time}s)")
end

#local_dump_device_to_file(source_device, target_filename) ⇒ Object

dump a device using dd



1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/help/state_transition_helper.rb', line 1066

def local_dump_device_to_file(source_device, target_filename)
  post_message("going to start dumping source device '#{source_device}' to '#{target_filename}' file. This may take quite a time...")
  @logger.info "start dumping '#{source_device}' to '#{target_filename}'"
  start_time = Time.new.to_i
  if remote_handler().tools_installed?("dd")
    @logger.debug "use dd command line"
    status = remote_handler().local_dump(source_device, target_filename)
    if status == false
      @logger.error "failed to dump device"
      raise Exception.new("failed to dump device")
    end
  else
    @logger.error "dd tool not installed"
    raise Exception.new("dd tool not installed")
  end
  end_time = Time.new.to_i
  filesize = remote_handler().file_size(target_filename)
  @logger.info "dump to a #{filesize} file took #{(end_time-start_time)}s"
  post_message("dumping to a #{filesize} file done (took #{end_time-start_time}s)")
end

#local_dump_file_to_device(source_filename, target_device) ⇒ Object

dump a file using dd



1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'lib/help/state_transition_helper.rb', line 1110

def local_dump_file_to_device(source_filename, target_device)
  post_message("going to start dumping file '#{source_filename}' to '#{target_device}' target device. This may take quite a time...")
  @logger.info "start dumping '#{source_filename}' to '#{target_device}'"
  start_time = Time.new.to_i
  if remote_handler().tools_installed?("dd")
    @logger.debug "use dd command line"
    status = remote_handler().local_dump(source_filename, target_device)
    if status == false
      @logger.error "failed to dump file"
      raise Exception.new("failed to dump file")
    end
  else
    @logger.error "dd tool not installed"
    raise Exception.new("dd tool not installed")
  end
  end_time = Time.new.to_i
  filesize = remote_handler().file_size(source_filename)
  @logger.info "dump file of #{filesize} took #{(end_time-start_time)}s"
  post_message("dumping file of #{filesize} done (took #{end_time-start_time}s)")
end

#mount_fs(mount_point, device) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/help/state_transition_helper.rb', line 688

def mount_fs(mount_point, device)
  post_message("going to mount #{device} on #{mount_point}...")
  @logger.debug "mount #{device} on #{mount_point}"
  if !remote_handler.file_exists?(mount_point)
    post_message("creating mount point #{mount_point}...")
    @logger.debug "creating mount point #{mount_point}"
    remote_handler().mkdir(mount_point)
  end
  #XXX: detect new kernel that have /dev/xvdX device node instead of /dev/sdX
  if device =~ /\/dev\/sd[a-z]/
    if !remote_handler().file_exists?(device)
      post_message("'#{device}' device node not found, checking for new kernel support...")
      @logger.debug "'#{device}' device node not found, checking for new kernel support" 
      new_device = device.gsub('sd', 'xvd')
      if remote_handler().file_exists?(new_device)
        post_message("'#{new_device}' device node found")
        @logger.debug "'#{new_device}' device node found"
        device = new_device
      end
    end
  #elsif device =~/\/dev\/xvd[a-z]/
  end

  done = false
  timeout = 120
  while timeout > 0
    res = remote_handler().mount(device, mount_point)
    if remote_handler().drive_mounted?(mount_point)
      done = true
      timeout = 0
    end
    sleep(5)
    timeout -= 5
  end
  msg = ""
  if !done
    msg = "Failed to mount device '#{device}' to '#{mount_point}"
    @logger.error "#{msg}"
    raise Exception.new("device #{device} not mounted")
  else
    msg = "device #{device} successfully mounted" 
    @logger.info "#{msg}"
  end
  post_message("#{msg}")
end

#mount_fs_old(mount_point, device) ⇒ Object

Create a file-system on a given machine (assumes to be connected already). Input Parameters:

  • mount_point => directory to be mounted on the device

  • device => device used for mounting



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/help/state_transition_helper.rb', line 665

def mount_fs_old(mount_point, device)
  post_message("going to mount #{device} on #{mount_point}...")
  @logger.debug "mount #{device} on #{mount_point}"
  if !remote_handler.file_exists?(mount_point)
    remote_handler().mkdir(mount_point)
  end
  remote_handler().mount(device, mount_point)
  trials = 3
  mounted = false
  while trials > 0
    sleep(5) #give mount some time
    if remote_handler().drive_mounted?(mount_point)
      mounted = true
      break
    end
    trials -= trials
  end
  if !mounted
    raise Exception.new("drive #{mount_point} not mounted")
  end
  post_message("mount successful")
end

#register_snapshot(snapshot_id, name, root_device_name, description, kernel_id, ramdisk_id, architecture) ⇒ Object

Registers a snapshot as EBS-booted AMI. Input Parameters:

  • snapshot_id => EC2 Snapshot ID used to be used

  • name => name of the AMI to be created

  • root_device_name => Root device name (e.g. /dev/sdj) to be used for AMI registration

  • description => description of the AMI to be created

  • kernel_id => EC2 Kernel ID to be used for AMI registration

  • ramdisk_id => EC2 Ramdisk ID to be used for AMI registration

  • architecture => architecture (e.g. 386i, 64x) to be used for AMI registration

Returns:

  • image_id => ID of the AMI created and registered



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/help/state_transition_helper.rb', line 541

def register_snapshot(snapshot_id, name, root_device_name, description, kernel_id, ramdisk_id, architecture)
  post_message("going to register snapshot #{snapshot_id}...")
  @logger.debug "register snapshot #{snapshot_id} as #{name} using AKI '#{kernel_id}', ARI '#{ramdisk_id}', arch '#{architecture}' and RootDevice '#{root_device_name}'"
  res = ec2_handler().register_image_updated(:snapshot_id => snapshot_id,
    :kernel_id => kernel_id, :architecture => architecture,
    :root_device_name => root_device_name,
    :description => description, :name => name,
    :ramdisk_id => ramdisk_id
  )
  @logger.debug "result of registration = #{res.inspect}"
  image_id = res['imageId']
  @logger.info "resulting image_id = #{image_id}"
  post_message("snapshot #{snapshot_id} successfully registered as AMI #{image_id} ")
  return image_id
end

#remote_copy(user_name, keyname, source_dir, dest_machine, dest_user, dest_dir) ⇒ Object



1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
# File 'lib/help/state_transition_helper.rb', line 1145

def remote_copy(user_name, keyname, source_dir, dest_machine, dest_user, dest_dir)
  post_message("going to remote copy all files from volume. This may take some time...")
  @logger.info "going to remote copy all files from volume. This may take some time..."
  key_path_candidates = ["/#{user_name}/.ssh/", "/home/#{user_name}/.ssh/"]
  start_time = Time.new.to_i
  key_path_candidates.each() {|key_path|
    key_file = "#{key_path}#{keyname}.pem"
    if remote_handler().file_exists?(key_path)
      if remote_handler().tools_installed?("rsync")
        @logger.debug "use rsync command on #{key_file}"
        remote_handler().remote_rsync(key_file, source_dir, dest_machine, dest_user, dest_dir)
      else
        @logger.debug "use scp command #{key_file}"
        remote_handler().scp(key_file, source_dir, dest_machine, dest_user, dest_dir)
      end
      break
    end
  }
  end_time = Time.new.to_i
  @logger.info "remote copy operation took #{(end_time-start_time)}s"
  post_message("remote copy operation done (took #{end_time-start_time}s)")
end

#remote_copy_old(user_name, keyname, source_dir, dest_machine, dest_dir) ⇒ Object



1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/help/state_transition_helper.rb', line 1024

def remote_copy_old(user_name, keyname, source_dir, dest_machine, dest_dir)
  post_message("going to remote copy all files from volume. This may take some time...")
  key_path_candidates = ["/#{user_name}/.ssh/", "/home/#{user_name}/.ssh/"]
  key_path_candidates.each() {|key_path|
    key_file = "#{key_path}#{keyname}.pem"
    if remote_handler().file_exists?(key_path)
      if remote_handler().tools_installed?("rsync")
        @logger.debug "use rsync command on #{key_file}"
        remote_handler().remote_rsync(key_file, source_dir, dest_machine, dest_dir)
      else
        @logger.debug "use scp command #{key_file}"
        remote_handler().scp(key_file, source_dir, dest_machine, dest_dir)
      end
      break
    end
  }
  post_message("remote copy operation done")
end

#remote_handlerObject

setting/retrieving handlers



1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
# File 'lib/help/state_transition_helper.rb', line 1467

def remote_handler()
  if @remote_handler == nil
    if @context[:remote_command_handler] == nil
      @context[:remote_command_handler] = RemoteCommandHandler.new
    else
      @remote_handler = @context[:remote_command_handler]
    end
  end
  @remote_handler
end

#remote_handler=(remote_handler) ⇒ Object



1478
1479
1480
# File 'lib/help/state_transition_helper.rb', line 1478

def remote_handler=(remote_handler)
  @remote_handler = remote_handler
end

#retrieve_instancesObject



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/help/state_transition_helper.rb', line 318

def retrieve_instances()
  @context[:script].post_message("going to retrieve all instances...")
  inst = @context[:ec2_api_handler].describe_instances()
  n = 0
  unless inst['reservationSet'] == nil
    n = inst['reservationSet']['item'].size
  end
  @context[:script].post_message("found #{n} instances")
  @logger.info("found #{n} instances")
  @context[:ec2_instances] = inst
end

#retrieve_security_groupsObject



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/help/state_transition_helper.rb', line 306

def retrieve_security_groups()
  @context[:script].post_message("going to retrieve security groups...")
  sgs = @context[:ec2_api_handler].describe_security_groups()
  n = 0
  unless sgs['securityGroupInfo'] == nil
    n = sgs['securityGroupInfo']['item'].size
  end
  @context[:script].post_message("found #{n} security groups")
  @logger.info("found #{n} security groups")
  @context[:security_groups] = sgs
end

#set_partition_table(device, partition_table) ⇒ Object

Set partition table



809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/help/state_transition_helper.rb', line 809

def set_partition_table(device, partition_table)
  post_message("Setting the partition table of device '#{device}'...")
  #XXX: detect new kernel that have /dev/xvdX device node instead of /dev/sdX
  if device =~ /\/dev\/sd[a-z]/
    if !remote_handler().file_exists?(device)
      post_message("'#{device}' device node not found, checking for new kernel support...")
      @logger.debug "'#{device}' device node not found, checking for new kernel support" 
      new_device = device.gsub('sd', 'xvd')
      if remote_handler().file_exists?(new_device)
        post_message("'#{new_device}' device node found")
        @logger.debug "'#{new_device}' device node found"
        device = new_device
      end
    end
  end
  remote_handler().set_partition_table(device, partition_table.gsub(/\/dev\/(s|xv)d[a-z]/, "#{device}"))
  @logger.debug "Partition table set on device '#{device}'"
end

#shut_down_instance(instance_id) ⇒ Object

Shuts down an instance. Input Parameters:

  • instance_id => ID of the instance to be shut down



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/help/state_transition_helper.rb', line 250

def shut_down_instance(instance_id)
  post_message("going to shut down the temporary instance #{instance_id}...")
  @logger.debug "shutdown instance #{instance_id}"
  res = ec2_handler().terminate_instances(:instance_id => instance_id)
  done = false
  while done == false
    sleep(5)
    res = ec2_handler().describe_instances(:instance_id => instance_id)
    state = res['reservationSet']['item'][0]['instancesSet']['item'][0]['instanceState']
    @logger.debug "instance in state #{state['name']} (#{state['code']})"
    if state['code'].to_i == 48
      done = true
    elsif state['code'].to_i != 32
      raise Exception.new('instance failed to shut down')
    end
  end
  post_message("instance #{instance_id} is terminated")
end

#snapshot_accessible(snapshot_id) ⇒ Object

Is Snapshot accessible?



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/help/state_transition_helper.rb', line 511

def snapshot_accessible(snapshot_id)
  post_message("checking snapshot '#{snapshot_id}' accessibility...")
  @logger.info("checking snapshot '#{snapshot_id}' accessibility...")
  begin
    ec2_handler().describe_snapshots(:snapshot_id => snapshot_id)
  rescue Exception => e
    if e.to_str =~ /does not exist/ && e.to_str =~ /#{snapshot_id}/
      post_message("'#{snapshot_id}' NOT accessible")
      @logger.info("'#{snapshot_id}' NOT accessible")
      return false
    else
      raise e
    end
 end
 post_message("'#{snapshot_id}' accessible")
 @logger.info("'#{snapshot_id}' accessible")
 return true
end

#start_instance(instance_id, timeout = 240) ⇒ Object

Start an instance Input Paramters:

  • instance_id => ID of the instance to start

  • timeout => a timeout for waiting instance to start to avoid infinite loop (default set to 4m)

Return Parameters (Array):

  • instance_id

  • public_dns_name



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
214
215
# File 'lib/help/state_transition_helper.rb', line 171

def start_instance(instance_id, timeout = 240)
  dns_name = ""
  post_message("going to start instance '#{instance_id}'...")
  res = ec2_handler().describe_instances(:instance_id => instance_id)
  state = res['reservationSet']['item'][0]['instancesSet']['item'][0]['instanceState']
  if state['code'].to_i == 16
    dns_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['dnsName'] 
    msg = "instance '#{instance_id}' already started"
    @logger.warn "#{msg}"
    post_message("#{msg}")
    done = true
  else
    @logger.debug "start instance #{instance_id}"
    ec2_handler().start_instances(:instance_id => instance_id)
  end
  while timeout > 1 && !done
    res = ec2_handler().describe_instances(:instance_id => instance_id)
    state = res['reservationSet']['item'][0]['instancesSet']['item'][0]['instanceState']
    @logger.debug "instance in state '#{state['name']}' (#{state['code']})"
    if state['code'].to_i == 16 
      done = true
      timeout = 0
      dns_name = res['reservationSet']['item'][0]['instancesSet']['item'][0]['dnsName']
    elsif state['code'].to_i != 0 
      done = false
      timeout = 0
      msg = "instance in state '#{state['name']}'"
      @logger.error "#{msg}"
      post_message("#{msg}")
    end
    sleep(5)
    timeout -= 5
  end
  msg = ""
  if !done
    msg = "Failed to start instance '#{instance_id}"
    @logger.error "#{msg}"
    raise Exception.new("Unable to start instance '#{instance_id}'}")
  else
    msg = "'#{instance_id}' successfully started" 
    @logger.info "#{msg}" 
  end
  post_message("#{msg}")
  return instance_id, dns_name
end

#stop_instance(instance_id, timeout = 240) ⇒ Object

Stop an instance. Input Parameters:

  • instance_id => ID of the instance to be shut down



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/help/state_transition_helper.rb', line 272

def stop_instance(instance_id, timeout = 240)
  post_message("going to stop the temporary instance #{instance_id}...")
  @logger.debug "stop instance #{instance_id}"
  res = ec2_handler().stop_instances(:instance_id => instance_id)
  done = false
  while timeout > 0 && !done
    res = ec2_handler().describe_instances(:instance_id => instance_id)
    state = res['reservationSet']['item'][0]['instancesSet']['item'][0]['instanceState']
    @logger.debug "instance in state '#{state['name']}' (#{state['code']})"
    if state['code'].to_i == 80 
      done = true
      timeout = 0
    elsif state['code'].to_i != 64 
      done = false
      timeout = 0
      msg = "instance in state '#{state['name']}'"
      @logger.error "#{msg}"
      post_message("#{msg}")
    end
    sleep(5)
    timeout -= 5
  end
  msg = ""
  if !done
    msg = "Failed to stop instance '#{instance_id}"
    @logger.error "#{msg}"
    raise Exception.new("Unable to stop instance '#{instance_id}'}")
  else
    msg = "'#{instance_id}' successfully stopped" 
    @logger.info "#{msg}" 
  end
  post_message("#{msg}")
end

#unmount_fs(mount_point) ⇒ Object

Unmount a drive Input Parameters:

  • mount_point => directory to be unmounted



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/help/state_transition_helper.rb', line 737

def unmount_fs(mount_point)
  post_message("going to unmount #{mount_point}...")
  @logger.debug "unmount #{mount_point}"

  done = false
  timeout = 120
  while timeout > 0
    res = remote_handler().umount(mount_point)
    if !remote_handler().drive_mounted?(mount_point)
      done = true
      timeout = 0
    end
    sleep(5)
    timeout -= 5
  end
  msg = ""
  if !done
    msg = "Failed to umount '#{mount_point}'"
    @logger.error "#{msg}"
    raise Exception.new("#{mount_point} still mounted")
  else
    msg = "#{mount_point} successfully unmounted" 
    @logger.info "#{msg}"
  end
  post_message("#{msg}")
end

#upload_file(ip, user, key_data, file, target_file) ⇒ Object



1168
1169
1170
1171
# File 'lib/help/state_transition_helper.rb', line 1168

def upload_file(ip, user, key_data, file, target_file)
  post_message("going to upload #{file} to #{user}@#{ip}:#{target_file}")
  remote_handler().upload(ip, user, key_data, file, target_file)
end

#zip_volume(source_dir, zip_file_dest, zip_file_name) ⇒ Object

Zips all files on a mounted-directory into a file Input Parameters:

  • source_dir => where to copy from

  • zip_file_dest => path where the zip-file should be stored

# zip_file_name => name of the zip file (without .zip suffix)



1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'lib/help/state_transition_helper.rb', line 1014

def zip_volume(source_dir, zip_file_dest, zip_file_name)
  post_message("going to zip the EBS volume")
  stderr = remote_handler().zip(source_dir, zip_file_dest+"/"+zip_file_name)
  if stderr.size > 0
    @logger.warn("zip operation generated error and might not be complete. output: #{stderr.join("\n")}")
    post_message("zip operation generated error and might not be complete. output: #{stderr.join("\n")}")
  end
  post_message("EBS volume successfully zipped")
end