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': |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 31 | raise Exception("Failed to query software: \n" + r.text) |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 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': |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 42 | raise Exception("Failed to get image " + image + ": \n" + r.text) |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 59 | def activate_image(self, image_id): |
| 60 | r = self.session.put(self.url + "/xyz/openbmc_project/software/" + image_id + "/attr/RequestedActivation", |
Andrew Geissler | 64da218 | 2017-09-08 14:06:42 -0500 | [diff] [blame^] | 61 | json={'data': 'xyz.openbmc_project.Software.Activation.RequestedActivations.Active'}, |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 62 | verify=False) |
| 63 | |
| 64 | j = r.json() |
| 65 | if j['status'] != 'ok': |
| 66 | raise Exception("Failed to activate image " + image_id + ": \n" + r.text) |
| 67 | |
| 68 | return j['data'] |
| 69 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 70 | |
| 71 | def do_list_sfw(args): |
| 72 | s = BMC(server=args.server) |
| 73 | for e in s.list_sfw(): |
| 74 | info = s.get_image(e) |
| 75 | print(e) |
| 76 | print json.dumps(info, indent=4) |
| 77 | |
| 78 | def do_view_image(args): |
| 79 | s = BMC(server=args.server) |
| 80 | print json.dumps(s.get_image(args.image), indent=4) |
| 81 | |
| 82 | def do_upload_image(args): |
| 83 | s = BMC(server=args.server) |
| 84 | s.upload_image(args.image) |
| 85 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 86 | def do_activate_image(args): |
| 87 | s = BMC(server=args.server) |
| 88 | s.activate_image(args.image_id) |
| 89 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 90 | parser = argparse.ArgumentParser() |
| 91 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 92 | required=True) |
| 93 | |
| 94 | subparsers = parser.add_subparsers() |
| 95 | list_events = subparsers.add_parser('list', help='List all software images on BMC') |
| 96 | list_events.set_defaults(func=do_list_sfw) |
| 97 | |
| 98 | image_view = subparsers.add_parser('view', help='View info of input image') |
| 99 | image_view.add_argument('image', help='The image to analyze') |
| 100 | image_view.set_defaults(func=do_view_image) |
| 101 | |
| 102 | image_upload = subparsers.add_parser('upload', help='Upload input image') |
| 103 | image_upload.add_argument('image', help='The image to upload') |
| 104 | image_upload.set_defaults(func=do_upload_image) |
| 105 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 106 | image_activate = subparsers.add_parser('activate', help='Activate input image id') |
| 107 | image_activate.add_argument('image_id', help='The image id to activate') |
| 108 | image_activate.set_defaults(func=do_activate_image) |
| 109 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 110 | args = parser.parse_args() |
| 111 | |
| 112 | if 'func' in args: |
| 113 | args.func(args) |
| 114 | else: |
| 115 | parser.print_help() |