Skip to content

Commit

Permalink
Merge "Fix for move and rebuild host."
Browse files Browse the repository at this point in the history
  • Loading branch information
sadsfae authored and gerritforge-ltd committed Mar 15, 2019
2 parents 9babe1f + 936cd61 commit c72d9b2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
59 changes: 36 additions & 23 deletions bin/quads-cli
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import argparse
import datetime
import logging
from collections import defaultdict

import requests
import subprocess
import sys
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions quads/api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions quads/tools/move_and_rebuild_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())

0 comments on commit c72d9b2

Please sign in to comment.