From 936cd613567ad51a4cc493b51fcb6722133467ef Mon Sep 17 00:00:00 2001 From: Gonzalo Rafuls Date: Thu, 14 Mar 2019 14:22:21 -0400 Subject: [PATCH] Fix for move and rebuild host. Was wrongly connecting to first interface hence returning wrong vlan ids. Change-Id: I46b5f723bc44d9ee807f06a97c8dac7293f14fbf --- bin/quads-cli | 59 ++++++++++++++++----------- quads/api_v2.py | 6 +-- quads/tools/move_and_rebuild_hosts.py | 10 +++-- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/bin/quads-cli b/bin/quads-cli index 20ac204a0..ea7fdaf76 100755 --- a/bin/quads-cli +++ b/bin/quads-cli @@ -2,6 +2,8 @@ import argparse import datetime import logging +from collections import defaultdict + import requests import subprocess import sys @@ -106,9 +108,9 @@ def main(_args): _cloud_name) ) else: - clouds = quads.get_clouds() - if clouds and "result" not in clouds: - for cloud in clouds: + _clouds = quads.get_clouds() + if _clouds and "result" not in _clouds: + for cloud in _clouds: _cloud_name = cloud["name"] logger.info("%s:" % _cloud_name) _kwargs["cloud"] = _cloud_name @@ -288,28 +290,39 @@ def main(_args): if len(js['result']) == 0: logger.info('Nothing to do.') exit(0) + + _clouds = defaultdict(list) for result in js['result']: - host = result['host'] - current = result['current'] - new = result['new'] - # now we need to know if we should wipe - cloud = quads.get_clouds(name=new) - wipe_value = 1 - if cloud: - wipe_value = int(cloud["wipe"]) - logger.info('Moving %s from %s to %s, wipe = %s' % (host, current, new, wipe_value)) - if not _args.dryrun: - try: - if _args.movecommand == default_move_command: - move_and_rebuild(host, current, new, wipe_value) - else: - if wipe_value: - subprocess.check_call([args.movecommand, host, current, new]) + _clouds[result['new']].append(result) + + for _cloud, results in _clouds.items(): + for result in results: + host = result['host'] + current = result['current'] + new = result['new'] + cloud = quads.get_clouds(name=new) + wipe_value = 1 + if cloud: + wipe_value = int(cloud["wipe"]) + logger.info('Moving %s from %s to %s, wipe = %s' % (host, current, new, wipe_value)) + if not _args.dryrun: + try: + if _args.movecommand == default_move_command: + move_and_rebuild(host, current, new, wipe_value) else: - subprocess.check_call([args.movecommand, host, current, new, "nowipe"]) - except Exception as ex: - logger.error('Move command failed: %s' % ex) - exit(1) + if wipe_value: + subprocess.check_call([args.movecommand, host, current, new]) + else: + subprocess.check_call([args.movecommand, host, current, new, "nowipe"]) + except Exception as ex: + logger.error('Move command failed: %s' % ex) + exit(1) + + if not _args.dryrun: + _new_cloud_obj = Cloud.objects(name=_cloud).first() + _new_cloud_obj.update(released=True) + _old_cloud_obj = Cloud.objects(name=results[0]["current"]).first() + _old_cloud_obj.update(released=False, validated=False, notified=False) exit(0) diff --git a/quads/api_v2.py b/quads/api_v2.py index 26d5782e8..4de3d07be 100644 --- a/quads/api_v2.py +++ b/quads/api_v2.py @@ -131,10 +131,10 @@ def GET(self, **data): _start = _end = datetime.datetime.now() if "start" in data: - _start = datetime.datetime.strptime(data["start"], '%Y-%m-%dT%H:%M:%S') + _start = datetime.datetime.strptime(data["start"], '%Y-%m-%d %H:%M:%S') if "end" in data: - _end = datetime.datetime.strptime(data["end"], '%Y-%m-%dT%H:%M:%S') + _end = datetime.datetime.strptime(data["end"], '%Y-%m-%d %H:%M:%S') available = [] all_hosts = model.Host.objects().all() @@ -264,7 +264,7 @@ class ScheduleMethodHandler(MethodHandlerBase): def GET(self, **data): _args = {} if "date" in data: - date = datetime.datetime.strptime(data["date"], "%Y-%m-%dT%H:%M:%S") + date = datetime.datetime.strptime(data["date"], "%Y-%m-%d %H:%M:%S") _args["date"] = date if "host" in data: host = model.Host.objects(name=data["host"]).first() diff --git a/quads/tools/move_and_rebuild_hosts.py b/quads/tools/move_and_rebuild_hosts.py index ae7dec29c..77b8aed0a 100755 --- a/quads/tools/move_and_rebuild_hosts.py +++ b/quads/tools/move_and_rebuild_hosts.py @@ -39,7 +39,7 @@ def move_and_rebuild(host, old_cloud, new_cloud, rebuild=False): logger.debug("Connecting to switch on: %s" % _host_obj.interfaces[0].ip_address) _public_vlan_obj = Vlan.objects(cloud=_new_cloud_obj).first() for i, interface in enumerate(_host_obj.interfaces): - ssh_helper = SSHHelper(_host_obj.interfaces[0].ip_address, conf["junos_username"]) + ssh_helper = SSHHelper(interface.ip_address, conf["junos_username"]) old_vlan_out = ssh_helper.run_cmd("show configuration interfaces %s" % interface.switch_port) old_vlan = old_vlan_out[0].split(";")[0].split()[1][7:] if not old_vlan: @@ -183,6 +183,8 @@ def move_and_rebuild(host, old_cloud, new_cloud, rebuild=False): return False logger.debug("Updating host: %s") - _host_obj.update(cloud=_new_cloud_obj, last_build=datetime.now()) - _new_cloud_obj.update(released=True) - _old_cloud_obj.update(released=False, validated=False, notified=False) + _host_obj.update(cloud=_new_cloud_obj, build=False, last_build=datetime.now()) + else: + + logger.debug("Updating host: %s") + _host_obj.update(cloud=_new_cloud_obj, build=False, last_build=datetime.now())