From 74f68f4f7e1a141e1ad5bdccfec385b4019cdbf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paco=20G=C3=B3mez?= Date: Mon, 5 Feb 2018 09:12:53 -0600 Subject: [PATCH] template preparation fix and cluster info (#69) - fix #67 Ubuntu template preparation - fix #68 node list - mask password in debug logging - set password using guest operations - update tests - update dependencies - pika logging set to warning Signed-off-by: Paco Gomez --- ChangeLog | 3 + container_service_extension/broker.py | 59 ++++++---------- container_service_extension/cluster.py | 97 ++++++++++++++++---------- container_service_extension/service.py | 1 + container_service_extension/utils.py | 3 +- requirements.txt | 2 +- scripts/cust-ubuntu-16.04.sh | 9 ++- tests/add-nodes.sh | 9 +++ tests/create-clusters.sh | 2 +- tests/info-clusters.sh | 9 +++ 10 files changed, 115 insertions(+), 79 deletions(-) create mode 100755 tests/add-nodes.sh create mode 100755 tests/info-clusters.sh diff --git a/ChangeLog b/ChangeLog index 7b381f2ed..8b2ae5912 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ CHANGES ======= +* template preparation fix and cluster info +* updated docs + 0.4.0 ----- diff --git a/container_service_extension/broker.py b/container_service_extension/broker.py index 7caf18263..b80606710 100644 --- a/container_service_extension/broker.py +++ b/container_service_extension/broker.py @@ -333,12 +333,22 @@ def get_cluster_info(self, name, headers, body): for vm in vms: node_info = { 'name': vm.get('name'), - 'numberOfCpus': vm.VmSpecSection.NumCpus.text, - 'memoryMB': - vm.VmSpecSection.MemoryResourceMb.Configured.text, + 'numberOfCpus': '', + 'memoryMB': '', 'status': VCLOUD_STATUS_MAP.get(int(vm.get('status'))), - 'ipAddress': vapp.get_primary_ip(vm.get('name')) + 'ipAddress': '' } + if hasattr(vm, 'VmSpecSection'): + node_info['numberOfCpus'] = vm.VmSpecSection.NumCpus.text + node_info[ + 'memoryMB'] = \ + vm.VmSpecSection.MemoryResourceMb.Configured.text + try: + node_info['ipAddress'] = vapp.get_primary_ip( + vm.get('name')) + except Exception: + LOGGER.debug( + 'cannot get ip address for node %s' % vm.get('name')) if vm.get('name').startswith(TYPE_MASTER): node_info['node_type'] = 'master' clusters[0].get('master_nodes').append(node_info) @@ -430,17 +440,8 @@ def create_cluster_thread(self): message='Creating master node for %s(%s)' % (self.cluster_name, self.cluster_id)) vapp.reload() - add_nodes( - 1, - template, - TYPE_MASTER, - self.config, - self.client_tenant, - org, - vdc, - vapp, - self.body, - wait=True) + add_nodes(1, template, TYPE_MASTER, self.config, + self.client_tenant, org, vdc, vapp, self.body) self.update_task( TaskStatus.RUNNING, message='Initializing cluster %s(%s)' % (self.cluster_name, @@ -457,17 +458,9 @@ def create_cluster_thread(self): message='Creating %s node(s) for %s(%s)' % (self.body['node_count'], self.cluster_name, self.cluster_id)) - add_nodes( - self.body['node_count'], - template, - TYPE_NODE, - self.config, - self.client_tenant, - org, - vdc, - vapp, - self.body, - wait=True) + add_nodes(self.body['node_count'], template, TYPE_NODE, + self.config, self.client_tenant, org, vdc, vapp, + self.body) self.update_task( TaskStatus.RUNNING, message='Adding %s node(s) to %s(%s)' % @@ -601,17 +594,9 @@ def create_nodes_thread(self): TaskStatus.RUNNING, message='Creating %s node(s) for %s(%s)' % (self.body['node_count'], self.cluster_name, self.cluster_id)) - new_nodes = add_nodes( - self.body['node_count'], - template, - TYPE_NODE, - self.config, - self.client_tenant, - org, - vdc, - vapp, - self.body, - wait=True) + new_nodes = add_nodes(self.body['node_count'], template, TYPE_NODE, + self.config, self.client_tenant, org, vdc, + vapp, self.body) self.update_task( TaskStatus.RUNNING, message='Adding %s node(s) to %s(%s)' % diff --git a/container_service_extension/cluster.py b/container_service_extension/cluster.py index 09dfc43ae..36e506066 100644 --- a/container_service_extension/cluster.py +++ b/container_service_extension/cluster.py @@ -4,6 +4,7 @@ import logging import random +import re import string import time @@ -97,16 +98,7 @@ def load_from_metadata(client, name=None, cluster_id=None): return list(clusters_dict.values()) -def add_nodes(qty, - template, - node_type, - config, - client, - org, - vdc, - vapp, - body, - wait=True): +def add_nodes(qty, template, node_type, config, client, org, vdc, vapp, body): if qty < 1: return None specs = [] @@ -122,10 +114,7 @@ def add_nodes(qty, if [ x$1=x"postcustomization" ]; then """ # NOQA - cust_script_common = \ -""" -echo "root:{password}" | chpasswd -""".format(password=template['admin_password']) # NOQA + cust_script_common = '' if 'ssh_key' in body: cust_script_common += \ """ @@ -167,10 +156,9 @@ def add_nodes(qty, else: reconfigure_hw = False task = vapp.add_vms(specs, power_on=not reconfigure_hw) - if wait: - # TODO(get details of the exception like not enough resources avail) - client.get_task_monitor().wait_for_status(task) - if wait and reconfigure_hw: + # TODO(get details of the exception like not enough resources avail) + client.get_task_monitor().wait_for_status(task) + if reconfigure_hw: vapp.reload() for spec in specs: vm_resource = vapp.get_vm(spec['target_vm_name']) @@ -184,8 +172,24 @@ def add_nodes(qty, client.get_task_monitor().wait_for_status(task) vm = VM(client, resource=vm_resource) task = vm.power_on() - if wait: - client.get_task_monitor().wait_for_status(task) + client.get_task_monitor().wait_for_status(task) + password = source_vapp.get_admin_password(source_vm) + vapp.reload() + for spec in specs: + vm_resource = vapp.get_vm(spec['target_vm_name']) + script = \ +"""#!/usr/bin/env bash +echo "root:{password}" | chpasswd +""".format(password=template['admin_password']) # NOQA + nodes = [vm_resource] + execute_script_in_nodes( + config, + vapp, + password, + script, + nodes, + check_tools=True, + wait=False) return {'task': task, 'specs': specs} @@ -313,11 +317,17 @@ def execute_script_in_nodes(config, password, script, nodes, - check_tools=True): + check_tools=True, + wait=True): all_results = [] for node in nodes: + if 'chpasswd' in script: + p = re.compile(':.*\"') + debug_script = p.sub(':***\"', script) + else: + debug_script = script LOGGER.debug('will try to execute script on %s:\n%s' % - (node.get('name'), script)) + (node.get('name'), debug_script)) vs = get_vsphere(config, vapp, node.get('name')) vs.connect() moid = vapp.get_vm_moid(node.get('name')) @@ -327,20 +337,35 @@ def execute_script_in_nodes(config, vs.wait_until_tools_ready( vm, sleep=5, callback=wait_for_tools_ready_callback) wait_until_ready_to_exec(vs, vm, password) - LOGGER.debug('about to execute script on %s' % node.get('name')) - result = vs.execute_script_in_guest( - vm, - 'root', - password, - script, - target_file=None, - wait_for_completion=True, - wait_time=10, - get_output=True, - delete_script=True, - callback=wait_for_guest_execution_callback) - result_stdout = result[1].content.decode() - result_stderr = result[2].content.decode() + LOGGER.debug('about to execute script on %s, wait=%s' % + (node.get('name'), wait)) + if wait: + result = vs.execute_script_in_guest( + vm, + 'root', + password, + script, + target_file=None, + wait_for_completion=True, + wait_time=10, + get_output=True, + delete_script=True, + callback=wait_for_guest_execution_callback) + result_stdout = result[1].content.decode() + result_stderr = result[2].content.decode() + else: + result = vs.execute_script_in_guest( + vm, + 'root', + password, + script, + target_file=None, + wait_for_completion=False, + get_output=False, + delete_script=False, + callback=wait_for_guest_execution_callback) + result_stdout = '' + result_stderr = '' LOGGER.debug(result[0]) LOGGER.debug(result_stderr) LOGGER.debug(result_stdout) diff --git a/container_service_extension/service.py b/container_service_extension/service.py index 599c0895e..613eee46f 100644 --- a/container_service_extension/service.py +++ b/container_service_extension/service.py @@ -172,6 +172,7 @@ def run(self): '%(levelname)-8s %(message)s', datefmt='%m-%d %H:%M:%S', handlers=(handler, )) + logging.getLogger("pika").setLevel(logging.WARNING) message = """ Container Service Extension for vCloud Director running diff --git a/container_service_extension/utils.py b/container_service_extension/utils.py index 3fb29cb0a..e78b24b87 100644 --- a/container_service_extension/utils.py +++ b/container_service_extension/utils.py @@ -76,7 +76,8 @@ def get_vsphere(config, vapp, vm_name): break cache[vm_id] = cache_item else: - LOGGER.debug('retrieved from cache:\n%s' % cache[vm_id]) + LOGGER.debug('vCenter retrieved from cache: %s / %s' % + (vm_id, cache[vm_id]['hostname'])) v = VSphere(cache[vm_id]['hostname'], cache[vm_id]['username'], cache[vm_id]['password'], cache[vm_id]['port']) diff --git a/requirements.txt b/requirements.txt index 65698aec5..09e8a5de3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,4 @@ humanfriendly >= 4.8 pika >= 0.11.2 pyvcloud >= 19.1.1 vcd-cli >= 20.1.1 -vsphere-guest-run >= 0.0.3 +vsphere-guest-run >= 0.0.4 diff --git a/scripts/cust-ubuntu-16.04.sh b/scripts/cust-ubuntu-16.04.sh index 3ba63857e..9e7a8af88 100644 --- a/scripts/cust-ubuntu-16.04.sh +++ b/scripts/cust-ubuntu-16.04.sh @@ -2,14 +2,17 @@ set -e +echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf +echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> /etc/sysctl.conf +echo 'net.ipv6.conf.lo.disable_ipv6 = 1' >> /etc/sysctl.conf echo 'nameserver 8.8.8.8' >> /etc/resolvconf/resolv.conf.d/tail resolvconf -u systemctl restart networking.service -growpart /dev/sda 1 -resize2fs /dev/sda1 +growpart /dev/sda 1 || : +resize2fs /dev/sda1 || : -echo 'installing kuberentes' +echo 'installing kubernetes' export DEBIAN_FRONTEND=noninteractive apt-get -q update apt-get -q install -y apt-transport-https ca-certificates curl software-properties-common diff --git a/tests/add-nodes.sh b/tests/add-nodes.sh new file mode 100755 index 000000000..63a8b18eb --- /dev/null +++ b/tests/add-nodes.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +vcd cse cluster list + +for i in `seq 1 3`; +do + echo cluster$i + vcd cse node create cluster$i --network $NETWORK --template $TEMPLATE --nodes 1 +done diff --git a/tests/create-clusters.sh b/tests/create-clusters.sh index 3eb5a8f3c..717f79be6 100755 --- a/tests/create-clusters.sh +++ b/tests/create-clusters.sh @@ -2,7 +2,7 @@ for i in `seq 1 3`; do - vcd --no-wait cse cluster create cluster$i --network $NETWORK --ssh-key ~/.ssh/id_rsa.pub + vcd --no-wait cse cluster create cluster$i --network $NETWORK --ssh-key ~/.ssh/id_rsa.pub --template $TEMPLATE done watch vcd task list running diff --git a/tests/info-clusters.sh b/tests/info-clusters.sh new file mode 100755 index 000000000..eaa3392ea --- /dev/null +++ b/tests/info-clusters.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +vcd cse cluster list + +for i in `seq 1 3`; +do + echo cluster$i + vcd cse cluster info cluster$i +done