]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Beginings of Volumes and VolumeAttachments
authorAngus Salkeld <asalkeld@redhat.com>
Mon, 2 Apr 2012 13:04:35 +0000 (23:04 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Mon, 2 Apr 2012 13:04:46 +0000 (23:04 +1000)
Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
heat/engine/resources.py
templates/WordPress_Single_Instance.template
templates/WordPress_Single_Instance_With_EBS_Volume.template [new file with mode: 0644]

index 630992e0082ef5f182f5304873fb431890f51c40..8010f69c5f7512c58b0036b94e5c4f10fd177037 100644 (file)
@@ -47,6 +47,7 @@ class Resource(object):
         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 @@ class Resource(object):
         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 @@ class Volume(Resource):
         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 @@ class VolumeAttachment(Resource):
             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 @@ class Instance(Resource):
         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 @@ class Instance(Resource):
             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)
index 9e6f8f2f5c8bfd2ca52a25ceb05f10656a4aadc3..57e8c9fac65b9d667649efbe3e632412837bc7c6 100644 (file)
@@ -1,7 +1,7 @@
 {\r
   "AWSTemplateFormatVersion" : "2010-09-09",\r
 \r
-  "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.",\r
+  "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.",\r
 \r
   "Parameters" : {\r
 \r
@@ -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" ],\r
       "ConstraintDescription" : "must be a valid EC2 instance type."\r
     },\r
-\r
+
     "DBName": {\r
       "Default": "wordpress",\r
       "Description" : "The WordPress database name",\r
diff --git a/templates/WordPress_Single_Instance_With_EBS_Volume.template b/templates/WordPress_Single_Instance_With_EBS_Volume.template
new file mode 100644 (file)
index 0000000..1f1415c
--- /dev/null
@@ -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" ],\r
+      "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"
+      }
+    }
+  }
+}