Last active
September 9, 2019 20:19
-
-
Save freyes/03a97438e197c8dfd3ec5532110c69c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# Usage: | |
# ./nodes.py --url http://localhost:5240/MAAS/ --apikey "someapikey" | |
# ./nodes.py --url http://localhost:5240/MAAS/ --username foobar --pasword sekret | |
# | |
# Example output: | |
# node01 Deployed 11 days 2019-08-29 18:56:56+00:00 | |
# node02 Deployed 26 days 2019-08-14 13:19:01+00:00 | |
# node03 Deployed 16 hours 2019-09-09 03:55:11+00:00 | |
# | |
# Author: Felipe Reyes <felipe.reyes@canonical.com> | |
# | |
# TODO: use prettytable module to get sorting and better formatting out of the box. | |
import argparse | |
import datetime | |
import sys | |
try: | |
from maas.client import connect, login as maas_login | |
except ImportError: | |
sys.stderr.write("Cannot import maas.client.\n") | |
sys.stderr.write("apt install python3-libmaas\n") | |
sys.exit(1) | |
try: | |
import humanize | |
except ImportError: | |
sys.stderr.write("apt install python3-humanize\n") | |
sys.exit(1) | |
STATUSES = ["Deployed"] | |
def login(opts): | |
if opts.apikey: | |
return connect(opts.url, apikey=opts.apikey) | |
else: | |
return maas_login(opts.url, username=opts.username, | |
password=opts.password) | |
def setup_opts(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--url', dest="url", | |
default="http://localhost:5240/MAAS/", | |
help='MAAS url') | |
parser.add_argument('-u', '--username', dest='username', required=False, | |
help="User") | |
parser.add_argument('-p', '--password', dest='password', required=False, | |
help='Password') | |
parser.add_argument('--apikey', dest='apikey') | |
args = parser.parse_args() | |
return args | |
def main(): | |
opts = setup_opts() | |
client = login(opts) | |
now = datetime.datetime.now(datetime.timezone.utc) | |
for machine in client.machines.list(): | |
if machine.status_name not in STATUSES: | |
continue | |
for event in client.events.query(system_ids=[machine.system_id], | |
level='INFO'): | |
if event.event_type == "Deployed": | |
print(machine.hostname, event.event_type, | |
humanize.naturaldelta(now - event.created), | |
event.created, | |
sep='\t') | |
break # we only report the latest event | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment