2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.base import Task
|
|
|
|
from bootstrapvz.common import phases
|
|
|
|
from bootstrapvz.common.exceptions import TaskError
|
|
|
|
from bootstrapvz.common.tools import log_check_call
|
2013-07-13 13:55:12 +02:00
|
|
|
from ebs import Snapshot
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.common.tasks import workspace
|
2013-07-07 20:28:24 +02:00
|
|
|
from connection import Connect
|
2013-12-29 23:21:50 +01:00
|
|
|
from . import assets
|
2013-07-14 23:16:37 +02:00
|
|
|
import os.path
|
|
|
|
|
2013-12-29 23:21:50 +01:00
|
|
|
cert_ec2 = os.path.join(assets, 'certs/cert-ec2.pem')
|
2013-07-07 20:28:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AMIName(Task):
|
|
|
|
description = 'Determining the AMI name'
|
|
|
|
phase = phases.preparation
|
2013-11-21 15:54:42 +01:00
|
|
|
predecessors = [Connect]
|
2013-07-07 20:28:24 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-12-29 16:48:55 +01:00
|
|
|
ami_name = info.manifest.image['name'].format(**info.manifest_vars)
|
|
|
|
ami_description = info.manifest.image['description'].format(**info.manifest_vars)
|
2013-07-07 20:28:24 +02:00
|
|
|
|
|
|
|
images = info.connection.get_all_images()
|
|
|
|
for image in images:
|
|
|
|
if ami_name == image.name:
|
|
|
|
msg = 'An image by the name {ami_name} already exists.'.format(ami_name=ami_name)
|
|
|
|
raise TaskError(msg)
|
|
|
|
info.ami_name = ami_name
|
|
|
|
info.ami_description = ami_description
|
|
|
|
|
|
|
|
|
2013-07-13 15:10:04 +02:00
|
|
|
class BundleImage(Task):
|
|
|
|
description = 'Bundling the image'
|
|
|
|
phase = phases.image_registration
|
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-12-28 13:56:48 +01:00
|
|
|
bundle_name = 'bundle-{id}'.format(id=info.run_id)
|
2013-09-15 16:59:56 +02:00
|
|
|
info.bundle_path = os.path.join(info.workspace, bundle_name)
|
2014-02-23 19:46:50 +01:00
|
|
|
arch = {'i386': 'i386', 'amd64': 'x86_64'}.get(info.manifest.system['architecture'])
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['euca-bundle-image',
|
2013-10-27 17:47:39 +01:00
|
|
|
'--image', info.volume.image_path,
|
2014-02-23 19:46:50 +01:00
|
|
|
'--arch', arch,
|
2013-07-14 23:16:37 +02:00
|
|
|
'--user', info.credentials['user-id'],
|
|
|
|
'--privatekey', info.credentials['private-key'],
|
|
|
|
'--cert', info.credentials['certificate'],
|
|
|
|
'--ec2cert', cert_ec2,
|
|
|
|
'--destination', info.bundle_path,
|
|
|
|
'--prefix', info.ami_name])
|
|
|
|
|
|
|
|
|
|
|
|
class UploadImage(Task):
|
|
|
|
description = 'Uploading the image bundle'
|
|
|
|
phase = phases.image_registration
|
2013-11-21 15:54:42 +01:00
|
|
|
predecessors = [BundleImage]
|
2013-07-14 23:16:37 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-07-14 23:16:37 +02:00
|
|
|
manifest_file = os.path.join(info.bundle_path, info.ami_name + '.manifest.xml')
|
|
|
|
if info.host['region'] == 'us-east-1':
|
|
|
|
s3_url = 'https://s3.amazonaws.com/'
|
2014-02-07 14:07:45 +00:00
|
|
|
elif info.host['region'] == 'cn-north-1':
|
|
|
|
s3_url = 'https://s3.cn-north-1.amazonaws.com.cn'
|
2013-07-14 23:16:37 +02:00
|
|
|
else:
|
|
|
|
s3_url = 'https://s3-{region}.amazonaws.com/'.format(region=info.host['region'])
|
2014-02-08 16:16:02 +00:00
|
|
|
info.manifest.manifest_location = info.manifest.image['bucket'] + '/' + info.ami_name + '.manifest.xml'
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['euca-upload-bundle',
|
2013-07-14 23:16:37 +02:00
|
|
|
'--bucket', info.manifest.image['bucket'],
|
|
|
|
'--manifest', manifest_file,
|
|
|
|
'--access-key', info.credentials['access-key'],
|
|
|
|
'--secret-key', info.credentials['secret-key'],
|
|
|
|
'--url', s3_url,
|
|
|
|
'--region', info.host['region'],
|
|
|
|
'--ec2cert', cert_ec2])
|
2013-07-13 15:10:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RemoveBundle(Task):
|
|
|
|
description = 'Removing the bundle files'
|
|
|
|
phase = phases.cleaning
|
2013-11-21 15:54:42 +01:00
|
|
|
successors = [workspace.DeleteWorkspace]
|
2013-07-13 15:10:04 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-07-13 15:10:04 +02:00
|
|
|
from shutil import rmtree
|
2013-07-14 23:16:37 +02:00
|
|
|
rmtree(info.bundle_path)
|
|
|
|
del info.bundle_path
|
2013-07-13 15:10:04 +02:00
|
|
|
|
|
|
|
|
2013-07-07 20:28:24 +02:00
|
|
|
class RegisterAMI(Task):
|
|
|
|
description = 'Registering the image as an AMI'
|
|
|
|
phase = phases.image_registration
|
2013-11-21 15:54:42 +01:00
|
|
|
predecessors = [Snapshot, UploadImage]
|
2013-07-07 20:28:24 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-12-01 23:50:32 +01:00
|
|
|
registration_params = {'name': info.ami_name,
|
|
|
|
'description': info.ami_description}
|
|
|
|
registration_params['architecture'] = {'i386': 'i386',
|
|
|
|
'amd64': 'x86_64'}.get(info.manifest.system['architecture'])
|
2013-10-27 17:47:39 +01:00
|
|
|
|
2013-12-01 23:50:32 +01:00
|
|
|
if info.manifest.volume['backing'] == 's3':
|
2014-02-08 16:16:02 +00:00
|
|
|
registration_params['image_location'] = info.manifest.manifest_location
|
2013-10-27 17:47:39 +01:00
|
|
|
else:
|
2014-01-05 02:18:59 +01:00
|
|
|
root_dev_name = {'pvm': '/dev/sda',
|
2014-01-05 14:03:04 +01:00
|
|
|
'hvm': '/dev/xvda'}.get(info.manifest.data['virtualization'])
|
2014-01-05 02:18:59 +01:00
|
|
|
registration_params['root_device_name'] = root_dev_name
|
2013-12-01 23:50:32 +01:00
|
|
|
|
|
|
|
from boto.ec2.blockdevicemapping import BlockDeviceType
|
|
|
|
from boto.ec2.blockdevicemapping import BlockDeviceMapping
|
|
|
|
block_device = BlockDeviceType(snapshot_id=info.snapshot.id, delete_on_termination=True,
|
2014-01-19 12:39:07 +01:00
|
|
|
size=info.volume.size.get_qty_in('GiB'))
|
2013-12-01 23:50:32 +01:00
|
|
|
registration_params['block_device_map'] = BlockDeviceMapping()
|
2014-01-05 02:18:59 +01:00
|
|
|
registration_params['block_device_map'][root_dev_name] = block_device
|
2013-12-01 23:50:32 +01:00
|
|
|
|
2014-01-05 14:03:04 +01:00
|
|
|
if info.manifest.data['virtualization'] == 'hvm':
|
2013-12-01 23:50:32 +01:00
|
|
|
registration_params['virtualization_type'] = 'hvm'
|
|
|
|
else:
|
|
|
|
registration_params['virtualization_type'] = 'paravirtual'
|
2014-03-26 16:26:59 +05:30
|
|
|
akis_path = os.path.join(os.path.dirname(__file__), 'ami-akis.json')
|
2014-03-23 23:12:07 +01:00
|
|
|
from bootstrapvz.common.tools import config_get
|
2014-02-23 20:14:23 +01:00
|
|
|
registration_params['kernel_id'] = config_get(akis_path, [info.host['region'],
|
|
|
|
info.manifest.system['architecture']])
|
2013-12-01 23:50:32 +01:00
|
|
|
|
|
|
|
info.image = info.connection.register_image(**registration_params)
|