create default TestVM for Functest 01/30401/3
authorSerenaFeng <feng.xiaowei@zte.com.cn>
Tue, 14 Mar 2017 08:48:22 +0000 (16:48 +0800)
committerSerenaFeng <feng.xiaowei@zte.com.cn>
Thu, 16 Mar 2017 01:14:25 +0000 (09:14 +0800)
Change-Id: I505a0819d5f1a4350e82ceaa9e5cbee285c8fba0
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
deploy/post/execute.py
deploy/post/glance.py [new file with mode: 0644]

index d54b2df..b9665e1 100644 (file)
@@ -6,6 +6,9 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
+import os
+
+import glance
 import neutron
 import nova
 
@@ -57,11 +60,48 @@ def _create_flavor_m1_micro():
         print ('Use existing m1.micro flavor')
 
 
+def _prepare_cirros():
+    url = 'http://download.cirros-cloud.net'
+    version = '0.3.5'
+    name = 'cirros-{}-x86_64-disk.img'.format(version)
+    img = os.path.join(os.path.abspath(os.path.dirname(__file__)), name)
+    if not os.path.isfile(img):
+        cmd = "wget %(url)s/%(version)s/%(name)s -O %(path)s" % {
+            'url': url,
+            'version': version,
+            'name': name,
+            'path': img}
+        try:
+            print ('Downloading cirros: {}'.format(cmd))
+            os.system(cmd)
+        except Exception as error:
+            print ('Download cirros failed: {}'.format(str(error)))
+            img = None
+
+    return img
+
+
+def _create_image_TestVM():
+    glanceclient = glance.Glance()
+    image = 'TestVM'
+    if not glanceclient.get_by_name(image):
+        img = _prepare_cirros()
+        if img:
+            try:
+                glanceclient.create(image, img)
+            except Exception as error:
+                print ('Create image failed: {}'.format(str(error)))
+    else:
+        print ('Use existing TestVM image')
+
+
 def main():
     neutronclient = neutron.Neutron()
     nid = neutronclient.create_network(*(_config_admin_external_network()))
     neutronclient.create_subnet(_config_admin_external_subnet(nid))
     _create_flavor_m1_micro()
+    _create_image_TestVM()
+
 
 if __name__ == '__main__':
     main()
diff --git a/deploy/post/glance.py b/deploy/post/glance.py
new file mode 100644 (file)
index 0000000..2422c7a
--- /dev/null
@@ -0,0 +1,37 @@
+import os
+
+import glanceclient
+
+import keystoneauth
+
+
+class Glance(keystoneauth.Keystoneauth):
+    def __init__(self, version='2', openrc=None):
+        super(Glance, self).__init__(openrc)
+        self.client = glanceclient.Client(version, session=self.session)
+        self.controller = self.client.images
+
+    def create(self, name, path,
+               disk_format="qcow2",
+               container_format="bare",
+               visibility="public"):
+        if not os.path.isfile(path):
+            raise Exception('Error: file {} not exist'.format(path))
+        image = self.controller.create(name=name,
+                                       visibility=visibility,
+                                       disk_format=disk_format,
+                                       container_format=container_format)
+        id = image.id
+        with open(path) as data:
+            self.controller.upload(id, data)
+        return id
+
+    def get_by_name(self, name):
+        for image in self.list():
+            if image.name == name:
+                return image.id
+
+        return None
+
+    def list(self):
+        return self.controller.list()