Class: ManifestMigrator

Inherits:
AMITool show all
Defined in:
lib/ec2/amitools/migratemanifest.rb

Constant Summary

Constants inherited from AMITool

AMITool::BACKOFF_PERIOD, AMITool::MAX_TRIES, AMITool::PROMPT_TIMEOUT

Instance Method Summary collapse

Methods inherited from AMITool

#get_parameters, #handle_early_exit_parameters, #interactive?, #interactive_prompt, #retry_s3, #run, #warn_confirm

Instance Method Details

#backup_manifest(manifest_path, quiet = false) ⇒ Object

—————————————————————————-#



94
95
96
97
98
99
100
101
102
# File 'lib/ec2/amitools/migratemanifest.rb', line 94

def backup_manifest(manifest_path, quiet=false)
  backup_manifest = "#{manifest_path}.bak"
  if File::exists?(backup_manifest)
    raise EC2FatalError.new(2, "Backup file '#{backup_manifest}' already exists. Please delete or rename it and try again.")
  end
  $stdout.puts("Backing up manifest...") unless quiet
  $stdout.puts("Backup manifest at #{backup_manifest}") if @debug
  FileUtils::copy(manifest_path, backup_manifest)
end

#build_migrated_manifest(manifest, user_pk_path, kernel_id = nil, ramdisk_id = nil) ⇒ Object

—————————————————————————-#



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
133
134
135
136
137
# File 'lib/ec2/amitools/migratemanifest.rb', line 106

def build_migrated_manifest(manifest, user_pk_path, kernel_id=nil, ramdisk_id=nil)
  new_manifest = ManifestV20071010.new()
  manifest_params = {
    :name => manifest.name,
    :user => manifest.user,
    :image_type => manifest.image_type,
    :arch => manifest.arch,
    :reserved => nil,
    :parts => manifest.parts.map { |part| [part.filename, part.digest] },
    :size => manifest.size,
    :bundled_size => manifest.bundled_size,
    :user_encrypted_key => manifest.user_encrypted_key,
    :ec2_encrypted_key => manifest.ec2_encrypted_key,
    :cipher_algorithm => manifest.cipher_algorithm,
    :user_encrypted_iv => manifest.user_encrypted_iv,
    :ec2_encrypted_iv => manifest.ec2_encrypted_iv,
    :digest => manifest.digest,
    :digest_algorithm => manifest.digest_algorithm,
    :privkey_filename => user_pk_path,
    :kernel_id => kernel_id,
    :ramdisk_id => ramdisk_id,
    :product_codes => manifest.product_codes,
    :ancestor_ami_ids => manifest.ancestor_ami_ids,
    :block_device_mapping => manifest.block_device_mapping,
    :bundler_name => manifest.bundler_name,
    :bundler_version => manifest.bundler_version,
    :bundler_release => manifest.bundler_release,
    :kernel_name => manifest.kernel_name,
  }
  new_manifest.init(manifest_params)
  new_manifest
end

#check_and_warn(manifest, kernel_id, ramdisk_id) ⇒ Object

—————————————————————————-#



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ec2/amitools/migratemanifest.rb', line 141

def check_and_warn(manifest, kernel_id, ramdisk_id)
  if (manifest.kernel_id and kernel_id.nil?) or (manifest.ramdisk_id and ramdisk_id.nil?)
    message = ["This operation will remove the kernel and/or ramdisk associated with",
               "the AMI. This may cause the AMI to fail to launch unless you specify",
               "an appropriate kernel and ramdisk at launch time.",
              ].join("\n")
    unless warn_confirm(message)
      raise EC2StopExecution.new()
    end
  end
end

#get_manifest(manifest_path, user_cert_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ec2/amitools/migratemanifest.rb', line 40

def get_manifest(manifest_path, user_cert_path)
  unless File::exists?(manifest_path)
    raise BadManifestError.new(manifest_path, "File not found.")
  end
  begin
    manifest = ManifestWrapper.new(File.open(manifest_path).read())
  rescue ManifestWrapper::InvalidManifest => e
    raise BadManifestError.new(manifest_path, e.message)
  end
  unless manifest.authenticate(File.open(user_cert_path))
    raise BadManifestError.new(manifest_path, "Manifest fails authentication.")
  end
  manifest
end

#get_manualObject

——————————————————————————# Overrides ——————————————————————————#



198
199
200
# File 'lib/ec2/amitools/migratemanifest.rb', line 198

def get_manual()
  MIGRATE_MANIFEST_MANUAL
end

#get_mappings(*args) ⇒ Object

—————————————————————————-#



57
58
59
# File 'lib/ec2/amitools/migratemanifest.rb', line 57

def get_mappings(*args)
  KernelMappings.new(*args)
end

#get_nameObject



202
203
204
# File 'lib/ec2/amitools/migratemanifest.rb', line 202

def get_name()
  MIGRATE_MANIFEST_NAME
end

#main(p) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ec2/amitools/migratemanifest.rb', line 206

def main(p)
  migrate_manifest(p.manifest_path,
                   p.user_pk_path,
                   p.user_cert_path,
                   p.user,
                   p.pass,
                   p.use_mapping,
                   p.kernel_id,
                   p.ramdisk_id,
                   p.region)
end

#map_identifiers(manifest, user, pass, region, kernel_id = nil, ramdisk_id = nil) ⇒ Object

—————————————————————————-#



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
# File 'lib/ec2/amitools/migratemanifest.rb', line 63

def map_identifiers(manifest, user, pass, region, kernel_id=nil, ramdisk_id=nil)
  if region.nil?
    raise EC2FatalError.new(1, "No region provided, cannot map automatically.")
  end
  if manifest.kernel_id.nil? and manifest.ramdisk_id.nil?
    # Exit early if we have nothing to do
    return [kernel_id, ramdisk_id]
  end

  begin
    mappings = get_mappings(user, pass, [manifest.kernel_id, manifest.ramdisk_id].compact, region)
  rescue KernelMappings::MappingError => e
    raise EC2FatalError.new(7, e.message)
  end

  begin
    if manifest.kernel_id
      kernel_id ||= mappings[manifest.kernel_id]
    end
    if manifest.ramdisk_id
      ramdisk_id ||= mappings[manifest.ramdisk_id]
    end
    warn_about_mappings(mappings.find_missing_targets([kernel_id, ramdisk_id].compact))
  rescue KernelMappings::MappingError => e
    raise EC2FatalError.new(6, e.message)
  end
  [kernel_id, ramdisk_id]
end

#migrate_manifest(manifest_path, user_pk_path, user_cert_path, user = nil, pass = nil, use_mapping = true, kernel_id = nil, ramdisk_id = nil, region = nil, quiet = false) ⇒ Object

—————————————————————————-#



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
# File 'lib/ec2/amitools/migratemanifest.rb', line 167

def migrate_manifest(manifest_path,
                     user_pk_path,
                     user_cert_path,
                     user=nil,
                     pass=nil,
                     use_mapping=true,
                     kernel_id=nil,
                     ramdisk_id=nil,
                     region=nil,
                     quiet=false)
  manifest = get_manifest(manifest_path, user_cert_path)
  backup_manifest(manifest_path, quiet)
  if use_mapping
    kernel_id, ramdisk_id = map_identifiers(manifest,
                                            user,
                                            pass,
                                            region,
                                            kernel_id,
                                            ramdisk_id)
  end
  check_and_warn(manifest, kernel_id, ramdisk_id)
  new_manifest = build_migrated_manifest(manifest, user_pk_path, kernel_id, ramdisk_id)
  File.open(manifest_path, 'w') { |f| f.write(new_manifest.to_s) }
  $stdout.puts("Successfully migrated #{manifest_path}") unless quiet
  $stdout.puts("It is now suitable for use in #{region}.") unless quiet
end

#warn_about_mappings(problems) ⇒ Object

—————————————————————————-#



155
156
157
158
159
160
161
162
163
# File 'lib/ec2/amitools/migratemanifest.rb', line 155

def warn_about_mappings(problems)
  return if problems.nil?
  message = ["The following identifiers do not exist in the target region:",
             "  " + problems.inspect.gsub(/['"]/, '')
            ].join("\n")
  unless warn_confirm(message)
    raise EC2StopExecution.new()
  end
end