Skip to content

Commit

Permalink
feat: Added --retire option
Browse files Browse the repository at this point in the history
Now if a server needs to be no longer allocated we can mark
it as retired via `--retire --host {hostname}`. This can be
reverted via `--unretire`. We can also list all retired
hosts via `--ls-retired`. Retired hosts will not show on
`ls-hosts` nor `ls-available`. Added logic to `--filter`
for handling boolean kv pairs such as
`--filter "retired==False"`.

Fixes: #364
Fixes: #374

Change-Id: I6d7198532cedc9f539c070f80cd683c34dcf6bb8
  • Loading branch information
grafuls authored and sadsfae committed May 11, 2021
1 parent 3913d48 commit c2d497e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
81 changes: 75 additions & 6 deletions bin/quads-cli
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ def filter_kwargs(filter_args):
except ValueError:
value = v

if type(value) == str:
if value.lower() == "false":
value = False
elif value.lower() == "true":
value = True

if keys[0].strip().lower() in ["disks", "interfaces"]:

key = f"{keys[0].strip()}__match"
Expand Down Expand Up @@ -181,6 +187,13 @@ def main(_args):

exit(0)

if _args.action == "retired":
_hosts = Host.objects(retired=True)
for host in _hosts:
logger.info(host.name)

exit(0)

if _args.action == "interface":
if _args.host is None:
logger.error(
Expand Down Expand Up @@ -334,7 +347,7 @@ def main(_args):
exit(0)

elif _args.action == "host":
kwargs = {}
kwargs = {"retired": False}
if _args.filter:
filter_args = filter_kwargs(_args.filter)
kwargs.update(filter_args)
Expand Down Expand Up @@ -380,14 +393,11 @@ def main(_args):
"There was something wrong constructing the parameters for the query."
)

broken_hosts = Host.objects(broken=True)

for host in all_hosts:
if (
Schedule.is_host_available(
host=host["name"], start=_start, end=_end
)
and host not in broken_hosts
):
if Schedule.current_schedule(host=host):
current.append(host["name"])
Expand Down Expand Up @@ -1620,11 +1630,11 @@ def main(_args):
if host:
if not host.broken:
logger.warning(
"Host %s has already been marked repaired" % (host.name)
f"Host {host.name} has already been marked repaired"
)
else:
host.update(broken=False)
logger.info("Host %s is now marked as repaired" % (host.name))
logger.info(f"Host {host.name} is now marked as repaired")
else:
logger.error("Host not found")
exit(1)
Expand All @@ -1634,6 +1644,46 @@ def main(_args):
logger.error("Missing option. Need --host when using --mark-repaired")
exit(1)

elif _args.retire:
if _args.host:
host = Host.objects(name=_args.host).first()
if host:
if host.retired:
logger.warning(
f"Host {host.name} has already been marked as retired"
)
else:
host.update(retired=True)
logger.info(f"Host {host.name} is now marked as retired")
else:
logger.error(f"Host {_args.host} not found")
exit(1)

exit(0)
else:
logger.error("Missing option. Need --host when using --retire")
exit(1)

elif _args.unretire:
if _args.host:
host = Host.objects(name=_args.host).first()
if host:
if not host.retired:
logger.warning(
f"Host {host.name} has already been marked unretired"
)
else:
host.update(retired=False)
logger.info(f"Host {host.name} is now marked as unretired")
else:
logger.error("Host not found")
exit(1)

exit(0)
else:
logger.error("Missing option. Need --host when using --unretire")
exit(1)

elif _args.host:
if _args.schedstart or _args.schedend:
logger.error(
Expand Down Expand Up @@ -1989,6 +2039,13 @@ if __name__ == "__main__":
const="broken",
help="List all hosts marked as broken",
)
group.add_argument(
"--ls-retired",
dest="action",
action="store_const",
const="retired",
help="List all hosts marked as retired",
)

time_args = parser.add_mutually_exclusive_group()
time_args.add_argument(
Expand Down Expand Up @@ -2313,6 +2370,18 @@ if __name__ == "__main__":
default=False,
help="Mark broken host as repaired",
)
parser.add_argument(
"--retire",
action="store_true",
default=False,
help="Mark host as retired",
)
parser.add_argument(
"--unretire",
action="store_true",
default=False,
help="Mark broken host as back in business",
)
parser.add_argument(
"--debug",
action="store_true",
Expand Down
13 changes: 6 additions & 7 deletions quads/api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,9 @@ def GET(self, **data):

available = []
all_hosts = Host.objects().all()
broken_hosts = Host.objects(broken=True)

for host in all_hosts:
if (
Schedule.is_host_available(
host=host["name"], start=_start, end=_end
)
and host not in broken_hosts
):
if Schedule.is_host_available(host=host["name"], start=_start, end=_end):
available.append(host.name)
return json.dumps(available)

Expand Down Expand Up @@ -206,6 +200,10 @@ def GET(self, **data):

return json.dumps(broken)

if self.name == "retired":
hosts = [host.name for host in self.model.objects(retired=True)]
return json.dumps(hosts)

objs = self.model.objects(**args)
if objs:
return objs.to_json()
Expand Down Expand Up @@ -589,6 +587,7 @@ def __init__(self):
self.wipe = DocumentMethodHandler(Cloud, "wipe")
self.host = DocumentMethodHandler(Host, "host")
self.broken = DocumentMethodHandler(Host, "broken")
self.retired = DocumentMethodHandler(Host, "retired")
self.schedule = ScheduleMethodHandler(Schedule, "schedule")
self.current_schedule = ScheduleMethodHandler(Schedule, "current_schedule")
self.available = DocumentMethodHandler(Schedule, "available")
Expand Down
3 changes: 3 additions & 0 deletions quads/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class Host(Document):
disks = ListField(EmbeddedDocumentField(Disk))
switch_config_applied = BooleanField(default=False)
broken = BooleanField(default=False)
retired = BooleanField(default=False)
meta = {"indexes": [{"fields": ["$name"]}], "strict": False}

@staticmethod
Expand Down Expand Up @@ -272,6 +273,8 @@ def insert_schedule(self, cloud, host, start, end):
@queryset_manager
def is_host_available(self, queryset, host, start, end, exclude=None):
_host = Host.objects(name=host).first()
if _host.broken or _host.retired:
return False
_query = Q(host=_host)
if exclude:
_query = _query & Q(index__ne=exclude)
Expand Down
2 changes: 1 addition & 1 deletion quads/tools/create_input_assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def main():
lines.append("### **%s : %s (%s) -- %s**\n\n" % (name.strip(), cloud["count"], cloud["description"], owner))
lines.extend(print_header())
_cloud_obj = Cloud.objects(name=name).first()
_hosts = sorted(Host.objects(cloud=_cloud_obj), key=lambda x: x.name)
_hosts = sorted(Host.objects(cloud=_cloud_obj, retired=False, broken=False), key=lambda x: x.name)
for host in _hosts:
lines.extend(add_row(host))
lines.append("\n")
Expand Down
3 changes: 0 additions & 3 deletions web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ def available(search):
else:
hosts = Host.objects().all()

broken_hosts = Host.objects(broken=True)

available_hosts = []
start = datetime.combine(search.data["start"], time(hour=22))
end = datetime.combine(search.data["end"], time(hour=22))
Expand All @@ -50,7 +48,6 @@ def available(search):
for host in hosts:
if (
Schedule.is_host_available(host=host["name"], start=start, end=end)
and host not in broken_hosts
):
host_dict = {"name": host.name, "model": host.model}
available_hosts.append(host_dict)
Expand Down

0 comments on commit c2d497e

Please sign in to comment.