3a270ac1e2853f89018cc40184d33070f545e6a0
[bottlenecks.git] / utils / infra_setup / heat / manager.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import time
11 import common as op_utils
12 from glanceclient.client import Client as GlanceClient
13 from novaclient.client import Client as NovaClient
14
15
16 def _get_glance_client():
17     sess = op_utils.get_session()
18     return GlanceClient(
19         op_utils.get_glance_api_version(),
20         session=sess)
21
22
23 def _get_nova_client():
24     sess = op_utils.get_session()
25
26     return NovaClient(
27         op_utils.get_nova_api_version(),
28         session=sess)
29
30
31 def stack_create_images(
32         imagefile=None,
33         image_name="bottlenecks_image"):
34     print "========== Create image in OS =========="
35
36     if imagefile is None:
37         print "imagefile not set/found"
38         return False
39
40     glance = _get_glance_client()
41     image = glance.images.create(
42         name=image_name,
43         disk_format="qcow2",
44         container_format="bare")
45     with open(imagefile) as fimage:
46         glance.images.upload(image.id, fimage)
47
48     timeInQueue = 0
49     img_status = image.status
50     while img_status == "queued" and timeInQueue < 30:
51         print "  image's status: " + img_status
52         time.sleep(1)
53         timeInQueue = timeInQueue + 1
54         img_status = glance.images.get(image.id).status
55
56     print "After %d seconds,image status is [%s]" % (timeInQueue, img_status)
57     return True if img_status == "active" else False
58
59
60 def stack_create_keypairs(key_path, name="bottlenecks_keypair"):
61     print "========== Add keypairs in OS =========="
62     nova = _get_nova_client()
63     with open(key_path) as pkey:
64         nova.keypairs.create(name=name, public_key=pkey.read())
65
66
67 def stack_create_flavors(
68         name="bottlenecks_flavor",
69         ram=4096,
70         vcpus=2,
71         disk=10):
72     print "========== Create flavors in OS =========="
73     nova = _get_nova_client()
74     nova.flavors.create(name=name, ram=ram, vcpus=vcpus, disk=disk)