Class: CopyMsWindowsSnapshot

Inherits:
Ec2Script show all
Defined in:
lib/scripts/ec2/copy_mswindows_snapshot.rb

Overview

Copy a given snapshot to another region

  • start up instance in source-region, create a snapshot from the mounted EBS

  • then create volume from snapshot, attach volume, and mount it

  • start up instance in destination-region, create empty volume of same size, attache volume, and mount it

  • copy the destination key to the source instance

  • perform an rsynch sync -PHAXaz –rsh “ssh -i /home/$src_user/.ssh/id_$dst_keypair” –rsync-path “sudo rsync” $src_dir/ $dst_user@$dst_public_fqdn:$dst_dir/

  • create a snapshot of the volume

  • register the snapshot as AMI

  • clean-up everything

Defined Under Namespace

Classes: BackupedDataState, CopyMsWindowsSnapshotState, DataCopiedState, InitialState, InitialStateDone, KeyInPlaceState, RestoredDataState, SourceInstanceLaunchedState, SourceVolumeReadyState, TargetInstanceLaunchedState, TargetSnapshotCreatedState, TargetVolumeReadyState

Constant Summary

Constants inherited from Ec2Script

Ec2Script::CS_AWS_TIMEOUT, Ec2Script::CS_SEC_GRP_DESC, Ec2Script::CS_SEC_GRP_NAME

Instance Method Summary collapse

Methods inherited from Ec2Script

#get_execution_result, #post_message, #register_progress_message_listener, #register_state_change_listener, #start_script

Constructor Details

#initialize(input_params) ⇒ CopyMsWindowsSnapshot

context information needed

  • the EC2 credentials (see #Ec2Script)

  • ami_id => the ID of the AMI to be copied in another region

  • target_ec2_handler => The EC2 handler connected to the region where the snapshot is being copied to

  • source_ssh_username => The username for ssh for source-instance (default = root)

  • source_key_name => Key name of the instance that manages the snaphot-volume in the source region

  • source_ssh_key_data => Key information for the security group that starts the AMI [if not set, use ssh_key_files]

  • source_ssh_key_files => Key information for the security group that starts the AMI

  • target_ssh_username => The username for ssh for target-instance (default = root)

  • target_key_name => Key name of the instance that manages the snaphot-volume in the target region

  • target_ssh_key_data => Key information for the security group that starts the AMI [if not set, use ssh_key_files]

  • target_ssh_key_files => Key information for the security group that starts the AMI

  • target_ami_id => ID of the AMI to start in the target region

  • name => name of new AMI to be created

  • description => description of new AMI to be created



38
39
40
41
42
# File 'lib/scripts/ec2/copy_mswindows_snapshot.rb', line 38

def initialize(input_params)
  super(input_params)
  @local_ec2_helper = Ec2Helper.new(@input_params[:ec2_api_handler])
  @remote_ec2_helper = Ec2Helper.new(@input_params[:target_ec2_handler]) 
end

Instance Method Details

#check_input_parametersObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
# File 'lib/scripts/ec2/copy_mswindows_snapshot.rb', line 44

def check_input_parameters()
  if @input_params[:snapshot_id] == nil && !(@input_params[:snapshot_id] =~ /^snap-.*$/)
    raise Exception.new("Invalid Snapshot ID specified: #{@input_params[:snapshot_id]}")
  end
  # AWS Linux AMI, source and target region
  if @input_params[:source_ami_id] == nil && !(@input_params[:source_ami_id] =~ /^ami-.*$/)
    raise Exception.new("Invalid source AMI ID specified: #{@input_params[:source_ami_id]}")
  end
  if @input_params[:target_ami_id] == nil && !(@input_params[:target_ami_id] =~ /^ami-.*$/)
    raise Exception.new("Invalid target AMI ID specified: #{@input_params[:target_ami_id]}")
  end
  # AWS SecurityGroup, source and target regions
  if @input_params[:source_security_group] == nil
    @input_params[:source_security_group] = "default"
  end
  if !@local_ec2_helper.check_open_port(@input_params[:source_security_group], 22)
    post_message("'#{@input_params[:source_security_group]}' Security Group not opened port 22 for connect via SSH in source region")
    @input_params[:source_security_group] = nil
  else
    post_message("'#{@input_params[:source_security_group]}' Security Group opened port 22 for connect via SSH in source region")
  end
  if @input_params[:target_security_group] == nil
    @input_params[:target_security_group] = "default"
  end
  if !@remote_ec2_helper.check_open_port(@input_params[:target_security_group], 22)
    post_message("'#{@input_params[:target_security_group]}' Security Group not opened port 22 for connect via SSH in target region")
    @input_params[:target_security_group] = nil
  else
    post_message("'#{@input_params[:target_security_group]}' Security Group opened port 22 for connect via SSH in target region")
  end
  # AWS KeyPair, source an dtarget
  if @input_params[:source_key_name] == nil || @input_params[:source_key_name].empty?()
    raise Exception.new("No KeyPair name specified for source region")
  else
    @local_ec2_helper.check_keypair(@input_params[:source_key_name])
  end
  if @input_params[:target_key_name] == nil || @input_params[:target_key_name].empty?()
    raise Exception.new("No KeyPair name specified for target region")
  else
    @remote_ec2_helper.check_keypair(@input_params[:target_key_name])
  end
  # Device to use for volume
  if @input_params[:root_device_name] == nil
    @input_params[:root_device_name] = "/dev/sda1"
  end
  if @input_params[:device_name] == nil
    @input_params[:device_name] = "/dev/sdj"
  end
  if @input_params[:temp_device_name] == nil
    @input_params[:temp_device_name] = "/dev/sdk"
  end
  if @input_params[:temp_device_name] == @input_params[:device_name]
    raise Exception.new("Device name '#{@input_params[:device_name]}' and temporary device name '#{@input_params[:temp_device_name]}' must be different")
  end
  # SSH Parameters, source and target region
  if @input_params[:source_ssh_username] == nil
    @input_params[:source_ssh_username] = "root"
  end
  if @input_params[:target_ssh_username] == nil
    @input_params[:target_ssh_username] = "root"
  end
  if @input_params[:source_ssh_keydata] == nil
    raise Exception.new("No Private Key for source region")
  else
    post_message("Checking SSH key for source region...")
    check_ssh_key(@input_params[:source_ssh_keydata])
  end
  if @input_params[:target_ssh_keydata] == nil
    raise Exception.new("No Private Key for target region")
  else
    post_message("Checking SSH key for target region...")
    check_ssh_key(@input_params[:target_ssh_keydata])
  end
  if @input_params[:fs_type] == nil
    @input_params[:fs_type] = "ext3"
  end
  # AWS Name and Description
  if @input_params[:description] == nil || !check_aws_desc(@input_params[:description])
    @input_params[:description] = "Created by CloudyScripts - #{self.class.name}"
  end
  if @input_params[:name] == nil || !check_aws_name(@input_params[:name])
    @input_params[:name] = "Created_by_CloudyScripts/#{self.class.name}_from_#{@input_params[:snapshot_id]}"
  end
  if @input_params[:compression] != nil && (@input_params[:compression] =~ /^on$/i)
    @input_params[:compression] = true
  else
    @input_params[:compression] = false
  end
end

#load_initial_stateObject

Load the initial state for the script. Abstract method to be implemented by extending classes.



136
137
138
# File 'lib/scripts/ec2/copy_mswindows_snapshot.rb', line 136

def load_initial_state()
  CopyMsWindowsSnapshotState.load_state(@input_params)
end