bottlenecks offiline support
[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 from neutronclient.v2_0.client import Client as NeutronClient
15
16
17 def _get_glance_client():
18     sess = op_utils.get_session()
19     return GlanceClient(
20         op_utils.get_glance_api_version(),
21         session=sess)
22
23
24 def _get_nova_client():
25     sess = op_utils.get_session()
26
27     return NovaClient(
28         op_utils.get_nova_api_version(),
29         session=sess)
30
31
32 def _get_neutron_client():
33     sess = op_utils.get_session()
34     neutron_client = NeutronClient(session=sess)
35     return neutron_client
36
37
38 def create_images(
39         imagefile=None,
40         image_name="bottlenecks_image"):
41     print "========== Create image in OS =========="
42
43     if imagefile is None:
44         print "imagefile not set/found"
45         return False
46
47     glance = _get_glance_client()
48     image = glance.images.create(
49         name=image_name,
50         disk_format="qcow2",
51         container_format="bare")
52     with open(imagefile) as fimage:
53         glance.images.upload(image.id, fimage)
54
55     timeInQueue = 0
56     img_status = image.status
57     while img_status == "queued" and timeInQueue < 30:
58         print "  image's status: " + img_status
59         time.sleep(1)
60         timeInQueue = timeInQueue + 1
61         img_status = glance.images.get(image.id).status
62
63     print "After %d seconds,image status is [%s]" % (timeInQueue, img_status)
64     return True if img_status == "active" else False
65
66
67 def stack_create_keypairs(key_path, name="bottlenecks_keypair"):
68     print "========== Add keypairs in OS =========="
69     nova = _get_nova_client()
70     with open(key_path) as pkey:
71         nova.keypairs.create(name=name, public_key=pkey.read())
72
73
74 def stack_create_flavors(
75         name="bottlenecks_flavor",
76         ram=4096,
77         vcpus=2,
78         disk=10):
79     print "========== Create flavors in OS =========="
80     nova = _get_nova_client()
81     nova.flavors.create(name=name, ram=ram, vcpus=vcpus, disk=disk)