Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 1 | #!/bin/env python |
| 2 | |
| 3 | import argparse |
| 4 | import requests |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 5 | import tarfile |
| 6 | import time |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 7 | import json |
| 8 | |
| 9 | import urllib3 |
Charles P. Hofer | e3ec357 | 2017-09-15 13:30:37 -0500 | [diff] [blame] | 10 | #import _sysconfigdata |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 11 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 12 | |
| 13 | |
| 14 | class BMC: |
| 15 | def __init__(self, server): |
| 16 | self.url = "https://{0}/".format(server) |
| 17 | self.session = requests.Session() |
| 18 | self.login() |
| 19 | |
| 20 | def login(self): |
| 21 | r = self.session.post(self.url + 'login', |
| 22 | json={'data': ['root', '0penBmc']}, |
| 23 | verify=False) |
| 24 | j = r.json() |
| 25 | if j['status'] != 'ok': |
| 26 | raise Exception("Failed to login: \n" + r.text) |
| 27 | |
| 28 | def list_sfw(self): |
| 29 | r = self.session.get(self.url + 'xyz/openbmc_project/software/', |
| 30 | verify=False) |
| 31 | j = r.json() |
| 32 | if j['status'] != 'ok': |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 33 | raise Exception("Failed to query software: \n" + r.text) |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 34 | |
| 35 | events = j['data'] |
| 36 | |
| 37 | return events |
| 38 | |
| 39 | def get_image(self, image): |
| 40 | r = self.session.get(self.url + image, verify=False) |
| 41 | |
| 42 | j = r.json() |
| 43 | if j['status'] != 'ok': |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 44 | raise Exception("Failed to get image " + image + ": \n" + r.text) |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 45 | |
| 46 | return j['data'] |
| 47 | |
| 48 | def upload_image(self, image): |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 49 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 50 | data = open(image,'rb').read() |
| 51 | r = self.session.post(self.url + "/upload/image", |
| 52 | data=data, |
| 53 | headers={'Content-Type': 'application/octet-stream'}, |
| 54 | verify=False) |
| 55 | j = r.json() |
| 56 | if j['status'] != 'ok': |
| 57 | raise Exception("Failed to get event " + image + ": \n" + r.text) |
| 58 | |
| 59 | return j['data'] |
| 60 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 61 | def activate_image(self, image_id): |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 62 | 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] | 63 | json={'data': 'xyz.openbmc_project.Software.Activation.RequestedActivations.Active'}, |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 64 | verify=False) |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 65 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 66 | j = r.json() |
| 67 | if j['status'] != 'ok': |
| 68 | raise Exception("Failed to activate image " + image_id + ": \n" + r.text) |
| 69 | |
| 70 | return j['data'] |
| 71 | |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 72 | def update_auto(self, image, reboot): |
| 73 | image_version = self.__get_image_version(image) |
| 74 | self.upload_image(image) |
| 75 | image_id = self.__wait_for_image_id(image_version) |
| 76 | self.activate_image(image_id) |
| 77 | self.__wait_for_activation(image_id) |
| 78 | if reboot: |
| 79 | self.reboot() |
| 80 | |
| 81 | def reboot(self): |
| 82 | r = self.session.post( |
| 83 | self.url + '/org/openbmc/control/bmc0/action/warmReset', |
| 84 | headers={'Content-Type': 'application/json'}, |
| 85 | data='{"data":[]}', |
| 86 | verify=False) |
| 87 | |
| 88 | j = r.json() |
| 89 | if j['status'] != 'ok': |
| 90 | raise Exception("Failed to reboot BMC:\n" + r.text) |
| 91 | |
| 92 | def __get_image_version(self, image_file_path): |
| 93 | # Open up the manfest file. |
| 94 | image_tar = tarfile.open(name=image_file_path, mode='r') |
| 95 | manifest = image_tar.extractfile('MANIFEST') |
| 96 | |
| 97 | # Find version line. |
| 98 | for line in manifest: |
| 99 | if line.startswith('version='): |
| 100 | manifest.close() |
| 101 | image_tar.close() |
| 102 | return line.split('version=', 1)[1].strip() |
| 103 | |
| 104 | # If we didn't find the version line, print an error and return false. |
| 105 | manifest.close() |
| 106 | image_tar.close() |
| 107 | raise Exception("Could not find version line in image manifest") |
| 108 | |
| 109 | def __wait_for_image_id(self, image_version): |
| 110 | # Try 8 times, once every 15 seconds. |
| 111 | for attempt in range(8): |
| 112 | software = self.list_sfw() |
| 113 | # Look for our the image with the given version in software |
| 114 | for path in software: |
| 115 | image = self.get_image(path) |
Charles P. Hofer | c772b47 | 2017-09-20 16:02:48 -0500 | [diff] [blame] | 116 | if 'Version' in image and image_version == image['Version']: |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 117 | return path.split('/')[-1] |
| 118 | time.sleep(15) |
| 119 | return False |
| 120 | |
| 121 | def __wait_for_activation(self, image_id): |
| 122 | # Keep waiting until the image is active or activation fails. |
| 123 | active = False |
| 124 | while not active: |
| 125 | image = self.get_image("/xyz/openbmc_project/software/" + image_id) |
| 126 | if 'xyz.openbmc_project.Software.Activation.Activations.Active' \ |
| 127 | == image['Activation']: |
| 128 | print 'Activation Progress: 100%' |
| 129 | active = True |
| 130 | elif 'xyz.openbmc_project.Software.Activation.Activations.Activating' \ |
| 131 | == image['Activation']: |
| 132 | print 'Activation Progress: ' + str(image['Progress']) + '%' |
| 133 | else: |
| 134 | raise Exception("Image activation failed. The BMC has set " \ |
| 135 | + "the'Activation' property to " + image['Activation']) |
| 136 | time.sleep(15) |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 137 | |
| 138 | def do_list_sfw(args): |
| 139 | s = BMC(server=args.server) |
| 140 | for e in s.list_sfw(): |
Andrew Geissler | cfc7e4c | 2017-09-20 14:17:12 -0500 | [diff] [blame] | 141 | if (e == '/xyz/openbmc_project/software/active'): |
| 142 | continue |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 143 | info = s.get_image(e) |
Andrew Geissler | cfc7e4c | 2017-09-20 14:17:12 -0500 | [diff] [blame] | 144 | if (((info['Purpose'] == 'xyz.openbmc_project.Software.Version.VersionPurpose.BMC') and |
| 145 | (args.bmc or not args.host)) or \ |
| 146 | ((info['Purpose'] == 'xyz.openbmc_project.Software.Version.VersionPurpose.Host') and |
| 147 | (args.host or not args.bmc))): |
| 148 | print(e) |
| 149 | print json.dumps(info, indent=4) |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 150 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 151 | def do_view_image(args): |
| 152 | s = BMC(server=args.server) |
| 153 | print json.dumps(s.get_image(args.image), indent=4) |
| 154 | |
| 155 | def do_upload_image(args): |
| 156 | s = BMC(server=args.server) |
| 157 | s.upload_image(args.image) |
| 158 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 159 | def do_activate_image(args): |
| 160 | s = BMC(server=args.server) |
| 161 | s.activate_image(args.image_id) |
| 162 | |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 163 | def do_update_auto(args): |
| 164 | s = BMC(server=args.server) |
| 165 | s.update_auto(args.image, args.reboot) |
| 166 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 167 | parser = argparse.ArgumentParser() |
| 168 | parser.add_argument('--server', help='hostname or IP of BMC', type=str, |
| 169 | required=True) |
| 170 | |
| 171 | subparsers = parser.add_subparsers() |
| 172 | list_events = subparsers.add_parser('list', help='List all software images on BMC') |
| 173 | list_events.set_defaults(func=do_list_sfw) |
Andrew Geissler | cfc7e4c | 2017-09-20 14:17:12 -0500 | [diff] [blame] | 174 | list_events.add_argument( |
| 175 | '--bmc', |
| 176 | action='store_true', |
| 177 | default=False, |
| 178 | help='Set if you want to see BMC images') |
| 179 | list_events.add_argument( |
| 180 | '--host', |
| 181 | action='store_true', |
| 182 | default=False, |
| 183 | help='Set if you want to see Host images') |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 184 | |
| 185 | image_view = subparsers.add_parser('view', help='View info of input image') |
| 186 | image_view.add_argument('image', help='The image to analyze') |
| 187 | image_view.set_defaults(func=do_view_image) |
| 188 | |
| 189 | image_upload = subparsers.add_parser('upload', help='Upload input image') |
| 190 | image_upload.add_argument('image', help='The image to upload') |
| 191 | image_upload.set_defaults(func=do_upload_image) |
| 192 | |
Andrew Geissler | 942eca2 | 2017-09-08 11:20:17 -0500 | [diff] [blame] | 193 | image_activate = subparsers.add_parser('activate', help='Activate input image id') |
| 194 | image_activate.add_argument('image_id', help='The image id to activate') |
| 195 | image_activate.set_defaults(func=do_activate_image) |
| 196 | |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 197 | image_update_auto = subparsers.add_parser('update_auto', help='Upload and ' |
| 198 | + 'activate an image, and then reboot the BMC to apply the update ' |
| 199 | + 'if necessary') |
| 200 | image_update_auto.add_argument('image', help='The image to update to') |
| 201 | image_update_auto.add_argument( |
| 202 | '--reboot', |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 203 | action='store_true', |
| 204 | default=False, |
| 205 | help='Set if the BMC should reboot after the update') |
Charles P. Hofer | 88d15b3 | 2017-09-12 11:32:55 -0500 | [diff] [blame] | 206 | image_update_auto.set_defaults(func=do_update_auto) |
| 207 | |
Andrew Geissler | 53be16b | 2017-09-06 16:23:27 -0500 | [diff] [blame] | 208 | args = parser.parse_args() |
| 209 | |
| 210 | if 'func' in args: |
| 211 | args.func(args) |
| 212 | else: |
| 213 | parser.print_help() |