From e354226876fd64a28672841efedf6d86880f249f Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Mon, 2 Apr 2012 23:04:35 +1000 Subject: [PATCH] Beginings of Volumes and VolumeAttachments Signed-off-by: Angus Salkeld --- heat/engine/resources.py | 83 +++++++----- templates/WordPress_Single_Instance.template | 4 +- ...s_Single_Instance_With_EBS_Volume.template | 125 ++++++++++++++++++ 3 files changed, 179 insertions(+), 33 deletions(-) create mode 100644 templates/WordPress_Single_Instance_With_EBS_Volume.template diff --git a/heat/engine/resources.py b/heat/engine/resources.py index 630992e00..8010f69c5 100644 --- a/heat/engine/resources.py +++ b/heat/engine/resources.py @@ -47,6 +47,7 @@ def __init__(self, name, json_snippet, stack): self.stack = stack self.name = name self.instance_id = None + self._nova = None if not self.t.has_key('Properties'): # make a dummy entry to prevent having to check all over the # place for it. @@ -55,6 +56,19 @@ def __init__(self, name, json_snippet, stack): stack.resolve_static_refs(self.t) stack.resolve_find_in_map(self.t) + def nova(self): + if self._nova: + return self._nova + + username = self.stack.creds['username'] + password = self.stack.creds['password'] + tenant = self.stack.creds['tenant'] + auth_url = self.stack.creds['auth_url'] + + self._nova = client.Client(username, password, tenant, auth_url, + service_type='compute', service_name='nova') + return self._nova + def start(self): for c in self.depends_on: #print '%s->%s.start()' % (self.name, self.stack.resources[c].name) @@ -177,16 +191,25 @@ def __init__(self, name, json_snippet, stack): super(Volume, self).__init__(name, json_snippet, stack) def start(self): - if self.state != None: return self.state_set(self.CREATE_IN_PROGRESS) super(Volume, self).start() - # TODO start the volume here - # of size -> self.t['Properties']['Size'] - # and set self.instance_id to the volume id - logger.info('$ euca-create-volume -s %s -z nova' % self.t['Properties']['Size']) - self.instance_id = 'vol-4509854' + + vol = self.nova().volumes.create(self.t['Properties']['Size'], + display_name=self.name, + display_description=self.name) + self.instance_id = vol.id + + def stop(self): + if self.state == self.DELETE_IN_PROGRESS or self.state == self.DELETE_COMPLETE: + return + self.state_set(self.DELETE_IN_PROGRESS) + Resource.stop(self) + + if self.instance_id != None: + self.nova().volumes.delete(self.instance_id) + self.state_set(self.DELETE_COMPLETE) class VolumeAttachment(Resource): def __init__(self, name, json_snippet, stack): @@ -198,15 +221,23 @@ def start(self): return self.state_set(self.CREATE_IN_PROGRESS) super(VolumeAttachment, self).start() - # TODO attach the volume with an id of: - # self.t['Properties']['VolumeId'] - # to the vm of instance: - # self.t['Properties']['InstanceId'] - # and make sure that the mountpoint is: - # self.t['Properties']['Device'] - logger.info('$ euca-attach-volume %s -i %s -d %s' % (self.t['Properties']['VolumeId'], - self.t['Properties']['InstanceId'], - self.t['Properties']['Device'])) + + att = self.nova().volumes.create_server_volume(self.t['Properties']['InstanceId'], + self.t['Properties']['VolumeId'], + self.t['Properties']['Device']) + self.instance_id = att.id + self.state_set(self.CREATE_COMPLETE) + + def stop(self): + if self.state == self.DELETE_IN_PROGRESS or self.state == self.DELETE_COMPLETE: + return + self.state_set(self.DELETE_IN_PROGRESS) + Resource.stop(self) + + if self.instance_id == None: + self.nova().volumes.delete_server_volume(self.t['Properties']['InstanceId'], + self.instance_id) + self.state_set(self.DELETE_COMPLETE) class Instance(Resource): @@ -290,23 +321,19 @@ def _null_callback(p, n, out): key_name = self.t['Properties']['KeyName'] image_name = self.t['Properties']['ImageId'] - username = self.stack.creds['username'] - password = self.stack.creds['password'] - tenant = self.stack.creds['tenant'] - auth_url = self.stack.creds['auth_url'] - - nova_client = client.Client(username, password, tenant, auth_url, service_type='compute', service_name='nova') - image_list = nova_client.images.list() + image_list = self.nova().images.list() for o in image_list: if o.name == image_name: image_id = o.id - flavor_list = nova_client.flavors.list() + flavor_list = self.nova().flavors.list() for o in flavor_list: if o.name == flavor: flavor_id = o.id - server = nova_client.servers.create(name=self.name, image=image_id, flavor=flavor_id, key_name=key_name, userdata=self.FnBase64(userdata)) + server = self.nova().servers.create(name=self.name, image=image_id, + flavor=flavor_id, key_name=key_name, + userdata=self.FnBase64(userdata)) while server.status == 'BUILD': server.get() time.sleep(0.1) @@ -327,13 +354,7 @@ def stop(self): self.state_set(self.DELETE_COMPLETE) return - username = self.stack.creds['username'] - password = self.stack.creds['password'] - tenant = self.stack.creds['tenant'] - auth_url = self.stack.creds['auth_url'] - - nova_client = client.Client(username, password, tenant, auth_url, service_type='compute', service_name='nova') - server = nova_client.servers.get(self.instance_id) + server = self.nova().servers.get(self.instance_id) server.delete() self.instance_id = None self.state_set(self.DELETE_COMPLETE) diff --git a/templates/WordPress_Single_Instance.template b/templates/WordPress_Single_Instance.template index 9e6f8f2f5..57e8c9fac 100644 --- a/templates/WordPress_Single_Instance.template +++ b/templates/WordPress_Single_Instance.template @@ -1,7 +1,7 @@ { "AWSTemplateFormatVersion" : "2010-09-09", - "Description" : "AWS CloudFormation Sample Template WordPress_Single_Instance: WordPress is web software you can use to create a beautiful website or blog. This template installs a single-instance WordPress deployment using a local MySQL database to store the data. It demonstrates using the AWS CloudFormation bootstrap scripts to install packages and files at instance launch time. **WARNING** This template creates an Amazon EC2 instance. You will be billed for the AWS resources used if you create a stack from this template.", + "Description" : "AWS CloudFormation Sample Template WordPress_Single_Instance: WordPress is web software you can use to create a beautiful website or blog. This template installs a single-instance WordPress deployment using a local MySQL database to store the data.", "Parameters" : { @@ -17,7 +17,7 @@ "AllowedValues" : [ "t1.micro", "m1.small", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge" ], "ConstraintDescription" : "must be a valid EC2 instance type." }, - + "DBName": { "Default": "wordpress", "Description" : "The WordPress database name", diff --git a/templates/WordPress_Single_Instance_With_EBS_Volume.template b/templates/WordPress_Single_Instance_With_EBS_Volume.template new file mode 100644 index 000000000..1f1415c49 --- /dev/null +++ b/templates/WordPress_Single_Instance_With_EBS_Volume.template @@ -0,0 +1,125 @@ +{ + "AWSTemplateFormatVersion" : "2010-09-09", + + "Description" : "AWS CloudFormation Sample Template WordPress_Single_Instance: WordPress is web software you can use to create a beautiful website or blog. This template installs a single-instance WordPress deployment using a local MySQL database to store the data.", + + "Parameters" : { + + "KeyName" : { + "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances", + "Type" : "String" + }, + + "InstanceType" : { + "Description" : "WebServer EC2 instance type", + "Type" : "String", + "Default" : "m1.large", + "AllowedValues" : [ "t1.micro", "m1.small", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge" ], + "ConstraintDescription" : "must be a valid EC2 instance type." + }, + + "VolumeSize" : { + "Description" : "WebServer EC2 instance type", + "Type" : "Number", + "Default" : "1", + "MinValue" : "1", + "MaxValue" : "1024", + "ConstraintDescription" : "must be between 1 and 1024 Gb." + }, + + "LinuxDistribution": { + "Default": "F16", + "Description" : "Distribution of choice", + "Type": "String", + "AllowedValues" : [ "F16", "F17", "U10", "RHEL-6.1", "RHEL-6.2", "RHEL-6.3" ] + } + }, + + "Mappings" : { + "AWSInstanceType2Arch" : { + "t1.micro" : { "Arch" : "32" }, + "m1.small" : { "Arch" : "32" }, + "m1.large" : { "Arch" : "64" }, + "m1.xlarge" : { "Arch" : "64" }, + "m2.xlarge" : { "Arch" : "64" }, + "m2.2xlarge" : { "Arch" : "64" }, + "m2.4xlarge" : { "Arch" : "64" }, + "c1.medium" : { "Arch" : "32" }, + "c1.xlarge" : { "Arch" : "64" }, + "cc1.4xlarge" : { "Arch" : "64" } + }, + "DistroArch2AMI": { + "F16" : { "32" : "F16-i686", "64" : "F16-x86_64" }, + "F17" : { "32" : "F17-i686", "64" : "F17-x86_64" }, + "U10" : { "32" : "U10-i686", "64" : "U10-x86_64" }, + "RHEL-6.1" : { "32" : "rhel61-i686", "64" : "rhel61-x86_64" }, + "RHEL-6.2" : { "32" : "rhel62-i686", "64" : "rhel62-x86_64" }, + "RHEL-6.3" : { "32" : "rhel63-i686", "64" : "rhel63-x86_64" } + } + }, + + "Resources" : { + + "WebServer": { + "Type": "AWS::EC2::Instance", + "Metadata" : { + "AWS::CloudFormation::Init" : { + "config" : { + "packages" : { + "yum" : { + "httpd" : [], + "mysql" : [], + "mysql-server" : [], + "wordpress" : [] + } + }, + "services" : { + "systemd" : { + "httpd" : { "enabled" : "true", "ensureRunning" : "true" }, + "mysqld" : { "enabled" : "true", "ensureRunning" : "true" } + } + } + } + } + }, + "Properties": { + "ImageId" : { "Fn::FindInMap" : [ "DistroArch2AMI", { "Ref" : "LinuxDistribution" }, + { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] }, + "InstanceType" : { "Ref" : "InstanceType" }, + "KeyName" : { "Ref" : "KeyName" }, + "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ + "#!/bin/bash -v\n", + "# Wait for the EBS volume to show up\n", + "while [ ! -e /dev/sdh ]; do echo Waiting for EBS volume to attach; sleep 5; done\n", + + "# Format the EBS volume and mount it\n", + "mkdir /var/wikidata\n", + "/sbin/mkfs -t ext3 /dev/sdh1\n", + "mount /dev/sdh1 /var/wikidata\n", + + "# Initialize the wiki and fire up the server\n", + "cd /var/wikidata\n", + "touch /var/wikidata/hello\n" + ]]}} + } + }, + + "DataVolume" : { + "Type" : "AWS::EC2::Volume", + "Properties" : { + "Size" : { "Ref" : "VolumeSize" }, + "AvailabilityZone" : { "Fn::GetAtt" : [ "WebServer", "AvailabilityZone" ]}, + "Tags" : [{ "Key" : "Usage", "Value" : "Wiki Data Volume" }] + } + }, + + "MountPoint" : { + "Type" : "AWS::EC2::VolumeAttachment", + "Properties" : { + "InstanceId" : { "Ref" : "WebServer" }, + "VolumeId" : { "Ref" : "DataVolume" }, + "Device" : "/dev/sdh" + } + } + } +}