diff --git a/bootstrapvz/providers/ec2/__init__.py b/bootstrapvz/providers/ec2/__init__.py index 30badcb..1cac583 100644 --- a/bootstrapvz/providers/ec2/__init__.py +++ b/bootstrapvz/providers/ec2/__init__.py @@ -69,7 +69,6 @@ def resolve_tasks(taskset, manifest): taskset.update([tasks.host.AddExternalCommands, tasks.packages.DefaultPackages, tasks.connection.GetCredentials, - tasks.host.GetInfo, tasks.ami.AMIName, tasks.connection.Connect, @@ -97,11 +96,13 @@ def resolve_tasks(taskset, manifest): else: taskset.update(task_groups.bootloader_set.get(manifest.system['bootloader'])) - backing_specific_tasks = {'ebs': [tasks.ebs.Create, + backing_specific_tasks = {'ebs': [tasks.host.GetInstanceMetadata, + tasks.ebs.Create, tasks.ebs.Attach, filesystem.FStab, tasks.ebs.Snapshot], 's3': [loopback.AddRequiredCommands, + tasks.host.SetRegion, loopback.Create, volume.Attach, tasks.filesystem.S3FStab, diff --git a/bootstrapvz/providers/ec2/manifest-schema-s3.json b/bootstrapvz/providers/ec2/manifest-schema-s3.json index f9cf5d2..c8012a3 100644 --- a/bootstrapvz/providers/ec2/manifest-schema-s3.json +++ b/bootstrapvz/providers/ec2/manifest-schema-s3.json @@ -22,9 +22,12 @@ "properties": { "bucket": { "type": "string" + }, + "region": { + "$ref": "#/definitions/aws-region" } }, - "required": ["bucket"] + "required": ["bucket", "region"] }, "volume": { "type": "object", @@ -38,5 +41,14 @@ } } }, - "required": ["image"] + "required": ["image"], + "definitions": { + "aws-region": { + "enum": ["ap-northeast-1", "ap-southeast-1", + "ap-southeast-2", "eu-west-1", + "sa-east-1", "us-east-1", + "us-gov-west-1", "us-west-1", + "us-west-2", "cn-north-1"] + } + } } diff --git a/bootstrapvz/providers/ec2/tasks/ami.py b/bootstrapvz/providers/ec2/tasks/ami.py index 5994b21..1d71adf 100644 --- a/bootstrapvz/providers/ec2/tasks/ami.py +++ b/bootstrapvz/providers/ec2/tasks/ami.py @@ -58,12 +58,12 @@ class UploadImage(Task): @classmethod def run(cls, info): manifest_file = os.path.join(info._ec2['bundle_path'], info._ec2['ami_name'] + '.manifest.xml') - if info._ec2['host']['region'] == 'us-east-1': + if info._ec2['region'] == 'us-east-1': s3_url = 'https://s3.amazonaws.com/' - elif info._ec2['host']['region'] == 'cn-north-1': + elif info._ec2['region'] == 'cn-north-1': s3_url = 'https://s3.cn-north-1.amazonaws.com.cn' else: - s3_url = 'https://s3-{region}.amazonaws.com/'.format(region=info._ec2['host']['region']) + s3_url = 'https://s3-{region}.amazonaws.com/'.format(region=info._ec2['region']) info._ec2['manifest_location'] = info.manifest.image['bucket'] + '/' + info._ec2['ami_name'] + '.manifest.xml' log_check_call(['euca-upload-bundle', '--bucket', info.manifest.image['bucket'], @@ -71,7 +71,7 @@ class UploadImage(Task): '--access-key', info.credentials['access-key'], '--secret-key', info.credentials['secret-key'], '--url', s3_url, - '--region', info._ec2['host']['region'], + '--region', info._ec2['region'], '--ec2cert', cert_ec2]) @@ -119,7 +119,7 @@ class RegisterAMI(Task): registration_params['virtualization_type'] = 'paravirtual' akis_path = os.path.join(os.path.dirname(__file__), 'ami-akis.json') from bootstrapvz.common.tools import config_get - registration_params['kernel_id'] = config_get(akis_path, [info._ec2['host']['region'], + registration_params['kernel_id'] = config_get(akis_path, [info._ec2['region'], info.manifest.system['architecture']]) info._ec2['image'] = info._ec2['connection'].register_image(**registration_params) diff --git a/bootstrapvz/providers/ec2/tasks/connection.py b/bootstrapvz/providers/ec2/tasks/connection.py index f17c7a4..cf203e4 100644 --- a/bootstrapvz/providers/ec2/tasks/connection.py +++ b/bootstrapvz/providers/ec2/tasks/connection.py @@ -36,11 +36,11 @@ class GetCredentials(Task): class Connect(Task): description = 'Connecting to EC2' phase = phases.preparation - predecessors = [GetCredentials, host.GetInfo] + predecessors = [GetCredentials, host.GetInstanceMetadata, host.SetRegion] @classmethod def run(cls, info): from boto.ec2 import connect_to_region - info._ec2['connection'] = connect_to_region(info._ec2['host']['region'], + info._ec2['connection'] = connect_to_region(info._ec2['region'], aws_access_key_id=info.credentials['access-key'], aws_secret_access_key=info.credentials['secret-key']) diff --git a/bootstrapvz/providers/ec2/tasks/host.py b/bootstrapvz/providers/ec2/tasks/host.py index 0f8d414..d8bfe09 100644 --- a/bootstrapvz/providers/ec2/tasks/host.py +++ b/bootstrapvz/providers/ec2/tasks/host.py @@ -15,7 +15,7 @@ class AddExternalCommands(Task): info.host_dependencies['euca-upload-bundle'] = 'euca2ools' -class GetInfo(Task): +class GetInstanceMetadata(Task): description = 'Retrieving instance metadata' phase = phases.preparation @@ -26,3 +26,13 @@ class GetInfo(Task): metadata_url = 'http://169.254.169.254/latest/dynamic/instance-identity/document' response = urllib2.urlopen(url=metadata_url, timeout=5) info._ec2['host'] = json.load(response) + info._ec2['region'] = info._ec2['host']['region'] + + +class SetRegion(Task): + description = 'Setting the AWS region' + phase = phases.preparation + + @classmethod + def run(cls, info): + info._ec2['region'] = info.manifest.image['region'] diff --git a/manifests/ec2-s3-debian-official-amd64-pvm-cn-north-1.manifest.json b/manifests/ec2-s3-debian-official-amd64-pvm-cn-north-1.manifest.json index 21acecb..6039091 100644 --- a/manifests/ec2-s3-debian-official-amd64-pvm-cn-north-1.manifest.json +++ b/manifests/ec2-s3-debian-official-amd64-pvm-cn-north-1.manifest.json @@ -15,7 +15,8 @@ "image": { "name": "debian-{system.release}-{system.architecture}-{virtualization}-{%Y}-{%m}-{%d}", "description": "Debian {system.release} {system.architecture} AMI", - "bucket": "debian-amis-cn-north-1" + "bucket": "debian-amis-cn-north-1", + "region": "cn-north-1" }, "system": { "release": "wheezy", @@ -38,10 +39,10 @@ } } }, - "plugins": { - "cloud_init": { - "username": "admin", - "disable_modules": [ "landscape", "byobu", "ssh-import-id" ] - } - } + "plugins": { + "cloud_init": { + "username": "admin", + "disable_modules": [ "landscape", "byobu", "ssh-import-id" ] + } + } } diff --git a/manifests/ec2-s3.manifest.json b/manifests/ec2-s3.manifest.json index f85f78b..fcf7ca2 100644 --- a/manifests/ec2-s3.manifest.json +++ b/manifests/ec2-s3.manifest.json @@ -15,7 +15,8 @@ "image": { "name": "debian-{system.release}-{system.architecture}-{virtualization}-{%y}{%m}{%d}", "description": "Debian {system.release} {system.architecture} AMI", - "bucket": "debian-amis" + "bucket": "debian-amis", + "region": "us-west-1" }, "system": { "release": "wheezy",