summaryrefslogtreecommitdiffstats
path: root/utils/env_prepare/quota_prepare.py
blob: 8dcdf3d65c47256a8bb9f05b43858a6234f192fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
##############################################################################
# Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


import os
import commands
import utils.logger as log
import utils.infra_setup.heat.manager as client_manager

LOG = log.Logger(__name__).getLogger()

neutron_quota = {"subnet": -1,
                 "network": -1,
                 "floatingip": -1,
                 "subnetpool": -1,
                 "router": -1,
                 "port": -1,
                 "security_group": -1,
                 "security_group_rule": -1,
                 "rbac_policy": -1}

nova_quota = {"ram": -1,
              "cores": -1,
              "instances": -1,
              "key_pairs": -1,
              "fixed_ips": -1,
              "floating_ips": -1,
              "server_groups": -1,
              "injected_files": -1,
              "metadata_items": -1,
              "security_groups": -1,
              "security_group_rules": -1,
              "server_group_members": -1,
              "injected_file_content_bytes": -1,
              "injected_file_path_bytes": -1}


def check_https_enabled():
    LOG.debug("Check if https is enabled in OpenStack")
    os_auth_url = os.getenv('OS_AUTH_URL')
    if os_auth_url.startswith('https'):
        LOG.debug("https is enabled")
        return True
    LOG.debug("https is not enabled")
    return False


def quota_env_prepare():
    https_enabled = check_https_enabled()
    insecure_option = ''
    insecure = os.getenv('OS_INSECURE',)
    if https_enabled:
        LOG.info("https is enabled")
        if insecure:
            if insecure.lower() == "true":
                insecure_option = ' --insecure '
            else:
                LOG.warn("Env variable OS_INSECURE is {}: if https + no "
                         "credential used, it should be set as True."
                         .format(insecure))

    tenant_name = os.getenv("OS_TENANT_NAME")
    cmd = ("openstack {} project list | grep ".format(insecure_option) +
           tenant_name +
           " | awk '{print $2}'")

    result = commands.getstatusoutput(cmd)
    if result[0] == 0 and 'exception' not in result[1]:
        LOG.info("Get %s project id is %s" % (tenant_name, result[1]))
    else:
        LOG.error("can't get openstack project id")
        return 1

    openstack_id = result[1]

    nova_client = client_manager._get_nova_client()
    neutron_client = client_manager._get_neutron_client()

    nova_q = nova_client.quotas.get(openstack_id).to_dict()
    neutron_q = neutron_client.show_quota(openstack_id)
    LOG.info(tenant_name + "tenant nova and neutron quota(previous) :")
    LOG.info(nova_q)
    LOG.info(neutron_q)

    nova_client.quotas.update(openstack_id, **nova_quota)
    neutron_client.update_quota(openstack_id,
                                {'quota': neutron_quota})
    LOG.info("Quota has been changed!")

    nova_q = nova_client.quotas.get(openstack_id).to_dict()
    neutron_q = neutron_client.show_quota(openstack_id)
    LOG.info(tenant_name + "tenant nova and neutron quota(now) :")
    LOG.info(nova_q)
    LOG.info(neutron_q)
    return 0