Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame^] | 1 | #!/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import requests |
| 5 | import json |
| 6 | |
| 7 | import urllib3 |
| 8 | import _sysconfigdata |
| 9 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 10 | |
| 11 | |
| 12 | class BMC: |
| 13 | def __init__(self, server): |
| 14 | self.url = "https://{0}/".format(server) |
| 15 | self.session = requests.Session() |
| 16 | self.login() |
| 17 | |
| 18 | def login(self): |
| 19 | r = self.session.post(self.url + 'login', |
| 20 | json={'data': ['root', '0penBmc']}, |
| 21 | verify=False) |
| 22 | j = r.json() |
| 23 | if j['status'] != 'ok': |
| 24 | raise Exception("Failed to login: \n" + r.text) |
| 25 | |
| 26 | def list_sfw(self): |
| 27 | r = self.session.get(self.url + 'xyz/openbmc_project/software/', |
| 28 | verify=False) |
| 29 | j = r.json() |
| 30 | if j['status'] != 'ok': |
| 31 | raise Exception("Failed to query entries: \n" + r.text) |
| 32 | |
| 33 | events = j['data'] |
| 34 | |
| 35 | return events |
| 36 | |
| 37 | def get_image(self, image): |
| 38 | r = self.session.get(self.url + image, verify=False) |
| 39 | |
| 40 | j = r.json() |
| 41 | if j['status'] != 'ok': |
| 42 | raise Exception("Failed to get event " + image + ": \n" + r.text) |
| 43 | |
| 44 | return j['data'] |
| 45 | |
| 46 | def upload_image(self, image): |
| 47 | |
| 48 | data = open(image,'rb').read() |
| 49 | r = self.session.post(self.url + "/upload/image", |
| 50 | data=data, |
| 51 | headers={'Content-Type': 'application/octet-stream'}, |
| 52 | verify=False) |
| 53 | j = r.json() |
| 54 | if j['status'] != 'ok': |
| 55 | raise Exception("Failed to get event " + image + ": \n" + r.text) |
| 56 | |
| 57 | return j['data'] |
| 58 | |
| 59 | |
| 60 | def do_list_sfw(args): |
| 61 | s = BMC(server=args.server) |
| 62 | for e in s.list_sfw(): |
| 63 | info = s.get_image(e) |
| 64 | print(e) |
| 65 | print json.dumps(info, indent=4) |
| 66 | |
| 67 | def do_view_image(args): |
| 68 | s = BMC(server=args.server) |
| 69 | print json.dumps(s.get_image(args.image), indent=4) |
| 70 | |
| 71 | def do_upload_image(args): |
| 72 | s = BMC(server=args.server) |
| 73 | s.upload_image(args.image) |
| 74 | |
| 75 | parser = argparse.ArgumentParser() |
| 76 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 77 | required=True) |
| 78 | |
| 79 | subparsers = parser.add_subparsers() |
| 80 | list_events = subparsers.add_parser('list', help='List all software images on BMC') |
| 81 | list_events.set_defaults(func=do_list_sfw) |
| 82 | |
| 83 | image_view = subparsers.add_parser('view', help='View info of input image') |
| 84 | image_view.add_argument('image', help='The image to analyze') |
| 85 | image_view.set_defaults(func=do_view_image) |
| 86 | |
| 87 | image_upload = subparsers.add_parser('upload', help='Upload input image') |
| 88 | image_upload.add_argument('image', help='The image to upload') |
| 89 | image_upload.set_defaults(func=do_upload_image) |
| 90 | |
| 91 | args = parser.parse_args() |
| 92 | |
| 93 | if 'func' in args: |
| 94 | args.func(args) |
| 95 | else: |
| 96 | parser.print_help() |