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) ⇒ 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)



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/help/state_transition_helper.rb', line 305

def attach_volume(volume_id, instance_id, temp_device_name)
  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}"
  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(5)
    timeout -= 5
  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 = 5, wait_between_trials = 20) ⇒ 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



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

def connect(dns_name, user_name, ssh_keyfile = nil, ssh_keydata = nil,
    trials = 5, wait_between_trials = 20)
  post_message("connecting '#{user_name}' to #{dns_name}...")
  connected = false
  last_connection_problem = ""
  remaining_trials = trials-1
  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]" : ""
  post_message("connected to #{dns_name}#{sudo}. OS installed is #{os}")
  @logger.info "connected to #{dns_name}#{sudo}"
  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



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# File 'lib/help/state_transition_helper.rb', line 720

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)



452
453
454
455
456
457
458
459
460
# File 'lib/help/state_transition_helper.rb', line 452

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")
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



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/help/state_transition_helper.rb', line 468

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
  @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_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



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/help/state_transition_helper.rb', line 391

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}"
  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
  post_message("snapshot is done with ID=#{snapshot_id}")
  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



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/help/state_transition_helper.rb', line 253

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



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/help/state_transition_helper.rb', line 280

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}"
  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
  post_message("EBS volume #{volume_id} is ready")
  return volume_id
end

#delete_snapshot(snapshot_id) ⇒ Object

Deletes a snapshot.



413
414
415
416
417
418
419
# File 'lib/help/state_transition_helper.rb', line 413

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



379
380
381
382
383
384
# File 'lib/help/state_transition_helper.rb', line 379

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

#detach_volume(volume_id, instance_id) ⇒ 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



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/help/state_transition_helper.rb', line 345

def detach_volume(volume_id, instance_id)
  post_message("going to detach volume #{volume_id} from instance #{instance_id}...")
  @logger.info "detach volume #{volume_id} from instance #{instance_id}"
  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(5)
    timeout -= 5
  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



820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# File 'lib/help/state_transition_helper.rb', line 820

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



781
782
783
784
785
786
# File 'lib/help/state_transition_helper.rb', line 781

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

#disconnectObject

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



86
87
88
89
90
91
92
# File 'lib/help/state_transition_helper.rb', line 86

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

#ec2_handlerObject



993
994
995
996
997
998
# File 'lib/help/state_transition_helper.rb', line 993

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

#ec2_handler=(ec2_handler) ⇒ Object



1000
1001
1002
# File 'lib/help/state_transition_helper.rb', line 1000

def ec2_handler=(ec2_handler)
  @ec2_handler = ec2_handler
end

#enable_ssh_tty(host) ⇒ Object



788
789
790
791
792
793
# File 'lib/help/state_transition_helper.rb', line 788

def enable_ssh_tty(host)
  post_message("going to enable SSH tty on #{host}...")
  @logger.debug "enable SSH tty on"
  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
    
  • 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
    


883
884
885
886
887
888
889
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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'lib/help/state_transition_helper.rb', line 883

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'
                         }, 
          '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'
                         },
          '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'
                         },
          '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'
                         },
          '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'
                         }
        }
  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



957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/help/state_transition_helper.rb', line 957

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

#get_partition_fs_type(part) ⇒ Object

Get partition filesytem type



674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/help/state_transition_helper.rb', line 674

def get_partition_fs_type(part)
  post_message("Retrieving '#{part}' partition filesystem type...")
  @logger.debug "get #{part} partition filesystel 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



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

def get_partition_fs_type_and_label(part)
  post_message("Retrieving '#{part}' partition filesystem type...")
  @logger.debug "get #{part} partition filesystel 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



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/help/state_transition_helper.rb', line 613

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_root_partition_fs_typeObject

Get root filesytem type



630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/help/state_transition_helper.rb', line 630

def get_root_partition_fs_type()
  post_message("Retrieving '/' root partition filesystem type...")
  @logger.debug "get root partition filesystel 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



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# File 'lib/help/state_transition_helper.rb', line 645

def get_root_partition_fs_type_and_label()
  post_message("Retrieving '/' root partition filesystem type and label...")
  @logger.debug "get root partition filesystel 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



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/help/state_transition_helper.rb', line 596

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

#launch_instance(ami_id, key_name, security_group_name, ec2_handler = nil, type = 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



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

def launch_instance(ami_id, key_name, security_group_name, ec2_handler = nil, type = 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
  )
  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']
    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
end

#mount_fs(mount_point, device) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/help/state_transition_helper.rb', line 519

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 instaed 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



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/help/state_transition_helper.rb', line 496

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



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/help/state_transition_helper.rb', line 432

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}' and arch '#{architecture}'"
  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



795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
# File 'lib/help/state_transition_helper.rb', line 795

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...")
  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_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
  }
  post_message("remote copy operation done")
end

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



762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# File 'lib/help/state_transition_helper.rb', line 762

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



978
979
980
981
982
983
984
985
986
987
# File 'lib/help/state_transition_helper.rb', line 978

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



989
990
991
# File 'lib/help/state_transition_helper.rb', line 989

def remote_handler=(remote_handler)
  @remote_handler = remote_handler
end

#retrieve_instancesObject



239
240
241
242
243
244
245
# File 'lib/help/state_transition_helper.rb', line 239

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

#retrieve_security_groupsObject



231
232
233
234
235
236
237
# File 'lib/help/state_transition_helper.rb', line 231

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

#shut_down_instance(instance_id) ⇒ Object

Shuts down an instance. Input Parameters:

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/help/state_transition_helper.rb', line 212

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

#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



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

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 > 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 == 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

#unmount_fs(mount_point) ⇒ Object

Unmount a drive Input Parameters:

  • mount_point => directory to be unmounted



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

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 mounted" 
    @logger.info "#{msg}"
  end
  post_message("#{msg}")
end

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



814
815
816
817
# File 'lib/help/state_transition_helper.rb', line 814

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)



752
753
754
755
756
757
758
759
760
# File 'lib/help/state_transition_helper.rb', line 752

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.info("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