aboutsummaryrefslogtreecommitdiffstats
path: root/func/create_zones.py
blob: 30b6ef5771060dd7897bb3509a42d0c92df34547 (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
##############################################################################
# Copyright (c) 2016 Dell Inc, ZTE 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
##############################################################################
from keystoneclient.auth.identity import v2
from keystoneclient import session
from novaclient import client
import os
import random
from utils import logger_utils

logger = logger_utils.QtipLogger('create_zones').get


class AvailabilityZone:

    def __init__(self):
        self._keystone_client = None
        self._nova_client = None

    def _get_keystone_client(self):
        """returns a keystone client instance"""

        if self._keystone_client is None:
            '''
            self._keystone_client = keystoneclient.v2_0.client.Client(
            auth_url=os.environ.get('OS_AUTH_URL'),
            username=os.environ.get('OS_USERNAME'),
            password=os.environ.get('OS_PASSWORD'),
            tenant_name=os.environ.get('OS_TENANT_NAME'))
            '''
            auth = v2.Password(auth_url=os.environ.get('OS_AUTH_URL'),
                               username=os.environ.get('OS_USERNAME'),
                               password=os.environ.get('OS_PASSWORD'),
                               tenant_name=os.environ.get('OS_TENANT_NAME'))

            sess = session.Session(auth=auth)
        else:
            return self._keystone_client

        return sess

    def _get_nova_client(self):
        if self._nova_client is None:
            keystone = self._get_keystone_client()
            self._nova_client = client.Client('2', session=keystone)
        return self._nova_client

    def clean_all_aggregates(self):
        logger.info("clean all aggregates")
        nova = self._get_nova_client()
        agg_list = nova.aggregates.list()

        for agg in agg_list:
            agg_info = nova.aggregates.get_details(agg.id)
            agg_hosts = agg_info.hosts
            if len(agg_hosts):
                for host in agg_hosts:
                    nova.aggregates.remove_host(agg.id, host)
            nova.aggregates.delete(agg.id)

    def create_aggs(self, args):
        azone_list = list(set(args))
        azone_list.sort()

        nova = self._get_nova_client()
        hyper_list = nova.hypervisors.list()

        if len(azone_list) > len(hyper_list):
            logger.error("required available zones > compute nodes")
            return None

        compute_nodes = map(lambda x: x.service['host'], hyper_list)
        sample_nodes = random.sample(compute_nodes, len(azone_list))
        sample_nodes.sort()

        for index, item in enumerate(azone_list):
            logger.info("create aggregates: %s" % str(item))
            agg_id = nova.aggregates.create(item, item)

            logger.info("add host: %s" % sample_nodes[index])
            nova.aggregates.add_host(aggregate=agg_id, host=sample_nodes[index])