bug-fix: idenity env not defined
[bottlenecks.git] / utils / env_prepare / quota_prepare.py
1 #!/usr/bin/env python
2 ##############################################################################
3 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11
12 import os
13 import commands
14 import utils.logger as log
15 import utils.infra_setup.heat.manager as client_manager
16
17 LOG = log.Logger(__name__).getLogger()
18
19 neutron_quota = {"subnet": -1,
20                  "network": -1,
21                  "floatingip": -1,
22                  "subnetpool": -1,
23                  "router": -1,
24                  "port": -1,
25                  "security_group": -1,
26                  "security_group_rule": -1,
27                  "rbac_policy": -1}
28
29 nova_quota = {"ram": -1,
30               "cores": -1,
31               "instances": -1,
32               "key_pairs": -1,
33               "fixed_ips": -1,
34               "floating_ips": -1,
35               "server_groups": -1,
36               "injected_files": -1,
37               "metadata_items": -1,
38               "security_groups": -1,
39               "security_group_rules": -1,
40               "server_group_members": -1,
41               "injected_file_content_bytes": -1,
42               "injected_file_path_bytes": -1}
43
44
45 def check_https_enabled():
46     LOG.debug("Check if https is enabled in OpenStack")
47     os_auth_url = os.getenv('OS_AUTH_URL')
48     if os_auth_url.startswith('https'):
49         LOG.debug("https is enabled")
50         return True
51     LOG.debug("https is not enabled")
52     return False
53
54
55 def quota_env_prepare():
56     https_enabled = check_https_enabled()
57     insecure_option = ''
58     insecure = os.getenv('OS_INSECURE',)
59     if https_enabled:
60         LOG.info("https is enabled")
61         if insecure:
62             if insecure.lower() == "true":
63                 insecure_option = ' --insecure '
64             else:
65                 LOG.warn("Env variable OS_INSECURE is {}: if https + no "
66                          "credential used, it should be set as True."
67                          .format(insecure))
68
69     quota_name = os.getenv("OS_PROJECT_NAME")
70     if quota_name:
71         cmd = ("openstack {} project list | grep ".format(insecure_option) +
72                quota_name +
73                " | awk '{print $2}'")
74         result = commands.getstatusoutput(cmd)
75         if result[0] == 0 and 'exception' not in result[1]:
76             LOG.info("Get %s project name is %s" % (quota_name, result[1]))
77         else:
78             LOG.error("can't get openstack project name")
79             return 1
80     else:
81         quota_name = os.getenv("OS_TENANT_NAME")
82         cmd = ("openstack {} tenant list | grep ".format(insecure_option) +
83                quota_name +
84                " | awk '{print $2}'")
85         result = commands.getstatusoutput(cmd)
86         if result[0] == 0 and 'exception' not in result[1]:
87             LOG.info("Get %s tenant name is %s" % (quota_name, result[1]))
88         else:
89             LOG.error("can't get openstack tenant name")
90             return 1
91
92     openstack_id = result[1]
93
94     nova_client = client_manager._get_nova_client()
95     neutron_client = client_manager._get_neutron_client()
96
97     nova_q = nova_client.quotas.get(openstack_id).to_dict()
98     neutron_q = neutron_client.show_quota(openstack_id)
99     LOG.info(quota_name + " nova and neutron quotas (previous) :")
100     LOG.info(nova_q)
101     LOG.info(neutron_q)
102
103     nova_client.quotas.update(openstack_id, **nova_quota)
104     neutron_client.update_quota(openstack_id,
105                                 {'quota': neutron_quota})
106     LOG.info("Quota has been changed!")
107
108     nova_q = nova_client.quotas.get(openstack_id).to_dict()
109     neutron_q = neutron_client.show_quota(openstack_id)
110     LOG.info(quota_name + " nova and neutron quotas (now) :")
111     LOG.info(nova_q)
112     LOG.info(neutron_q)
113     return 0