From 9c5468b5c14730b2e72fac736115eb2184a402c6 Mon Sep 17 00:00:00 2001 From: "wu.zhihui" Date: Sat, 8 Oct 2016 16:16:33 +0800 Subject: code refactor: create_zones.py 1. simply the process of creating aggregates in create_zones.py. 2. before creating aggregates, it will clean all aggregates firstly. TODO: It should clean up the created aggregates. It needs a big structure modification in args_handler.py. Let's do it step by step. Change-Id: I31e09e917ba83d4676f7f95d9f5186bce8d2a449 Signed-off-by: wu.zhihui --- func/create_zones.py | 108 +++++++++++++++------------------------------------ 1 file changed, 32 insertions(+), 76 deletions(-) (limited to 'func/create_zones.py') diff --git a/func/create_zones.py b/func/create_zones.py index e715dfd4..30b6ef57 100644 --- a/func/create_zones.py +++ b/func/create_zones.py @@ -1,5 +1,5 @@ ############################################################################## -# Copyright (c) 2015 Dell Inc and others. +# 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 @@ -10,14 +10,15 @@ from keystoneclient.auth.identity import v2 from keystoneclient import session from novaclient import client import os -import re -from collections import defaultdict +import random +from utils import logger_utils +logger = logger_utils.QtipLogger('create_zones').get -class create_zones: + +class AvailabilityZone: def __init__(self): - print 'Creating Zones' self._keystone_client = None self._nova_client = None @@ -49,82 +50,37 @@ class create_zones: self._nova_client = client.Client('2', session=keystone) return self._nova_client - @staticmethod - def check_aggregate(nova, agg_name): - list1 = nova.aggregates.list() - agg_name_exist = False - for x in list1: - if x.name == agg_name: - agg_name_exist = True - return agg_name_exist - - @staticmethod - def get_aggregate_id(nova, agg_name): - list1 = nova.aggregates.list() - for x in list1: - if x.name == agg_name: - agg_id = x.id - return agg_id - - @staticmethod - def check_host_added_to_aggregate(nova, agg_id, hostname): - host_added = False - list1 = nova.aggregates.get_details(agg_id) - - nme = str(list1.hosts) - if hostname in nme: - host_added = True - return host_added - - @staticmethod - def del_agg(nova, id, host): - - nova.aggregates.remove_host(id, host) - nova.aggregates.delete(id) - - @staticmethod - def get_compute_num(compute_name): - - num = re.findall(r'\d+', compute_name) - return int(num[0]) - 1 - - def test(self): - nova = self._get_nova_client() - hyper_list = nova.hypervisors.list() - return hyper_list - - def create_agg(self, d): + def clean_all_aggregates(self): + logger.info("clean all aggregates") nova = self._get_nova_client() - hyper_list = nova.hypervisors.list() - host_a = [] - zone_machine = defaultdict(list) - - for x in range(len(hyper_list)): + agg_list = nova.aggregates.list() - host_a.append(hyper_list[x].service['host']) - host_a[x] = str(host_a[x]) + 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) - host_a.sort() - for k in d: + def create_aggs(self, args): + azone_list = list(set(args)) + azone_list.sort() - zone_machine[k].append(' ') + nova = self._get_nova_client() + hyper_list = nova.hypervisors.list() - for x in range(len(zone_machine)): - compute_index = self.get_compute_num(d[x]) - if compute_index > len(hyper_list): - print '\n The specified compute node doesnt exist. using compute 1' - compute_index = 1 - if not self.check_aggregate(nova, host_a[compute_index]): - agg_id_a = nova.aggregates.create(host_a[compute_index], d[x]) - nova.aggregates.add_host(aggregate=agg_id_a, host=host_a[compute_index]) + if len(azone_list) > len(hyper_list): + logger.error("required available zones > compute nodes") + return None - else: - id1 = self.get_aggregate_id(nova, host_a[compute_index]) - self.del_agg(nova, id1, host_a[compute_index]) - nova.aggregates.create(host_a[compute_index], d[x]) - id1 = self.get_aggregate_id(nova, host_a[compute_index]) + compute_nodes = map(lambda x: x.service['host'], hyper_list) + sample_nodes = random.sample(compute_nodes, len(azone_list)) + sample_nodes.sort() - if not self.check_host_added_to_aggregate( - nova, id1, host_a[compute_index]): + for index, item in enumerate(azone_list): + logger.info("create aggregates: %s" % str(item)) + agg_id = nova.aggregates.create(item, item) - nova.aggregates.add_host(aggregate=id1, host=host_a[compute_index]) + logger.info("add host: %s" % sample_nodes[index]) + nova.aggregates.add_host(aggregate=agg_id, host=sample_nodes[index]) -- cgit 1.2.3-korg