summaryrefslogtreecommitdiffstats
path: root/func
diff options
context:
space:
mode:
authorwu.zhihui <wu.zhihui1@zte.com.cn>2016-10-08 16:16:33 +0800
committerwu.zhihui <wu.zhihui1@zte.com.cn>2016-10-09 09:29:03 +0800
commit9c5468b5c14730b2e72fac736115eb2184a402c6 (patch)
tree1565c16d10c8698b8b1b06e483289437bd3eec73 /func
parent62023f676d9801d53dfc1525bebb45f4e88f6fc4 (diff)
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 <wu.zhihui1@zte.com.cn>
Diffstat (limited to 'func')
-rw-r--r--func/create_zones.py108
-rw-r--r--func/spawn_vm.py9
2 files changed, 38 insertions, 79 deletions
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])
diff --git a/func/spawn_vm.py b/func/spawn_vm.py
index b467ab1f..5e2c1a70 100644
--- a/func/spawn_vm.py
+++ b/func/spawn_vm.py
@@ -16,7 +16,7 @@ import heatclient.client
import keystoneclient
from novaclient import client
import time
-from func.create_zones import create_zones
+from func.create_zones import AvailabilityZone
class SpawnVM(Env_setup):
@@ -25,6 +25,7 @@ class SpawnVM(Env_setup):
def __init__(self, vm_info):
print 'SpawnVM Class initiated'
+ print 'vm_info: %s' % vm_info
vm_role_ip_dict = vm_info.copy()
print 'Generating Heat Template\n'
self._keystone_client = None
@@ -32,8 +33,10 @@ class SpawnVM(Env_setup):
self._glance_client = None
self._nova_client = None
self. _get_nova_client()
- azoneobj = create_zones()
- azoneobj.create_agg(vm_info['availability_zone'])
+ self.azone = AvailabilityZone()
+ # TODO: it should clean up aggregates and stack after test case finished.
+ self.azone.clean_all_aggregates()
+ self.azone.create_agg(vm_info['availability_zone'])
installer = self.get_installer_type()
self.Heat_template1 = self.heat_template_vm(vm_info, installer)
self.create_stack(vm_role_ip_dict, self.Heat_template1)