blob: c808494005cf5e87efb370432ddb800e9fb8a47e [file] [log] [blame]
Andrew Geissler53be16b2017-09-06 16:23:27 -05001#!/bin/env python
2
3import argparse
4import requests
Charles P. Hofer88d15b32017-09-12 11:32:55 -05005import tarfile
6import time
Andrew Geissler53be16b2017-09-06 16:23:27 -05007import json
8
9import urllib3
Charles P. Hofere3ec3572017-09-15 13:30:37 -050010#import _sysconfigdata
Andrew Geissler53be16b2017-09-06 16:23:27 -050011urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
12
13
14class 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 Geissler942eca22017-09-08 11:20:17 -050033 raise Exception("Failed to query software: \n" + r.text)
Andrew Geissler53be16b2017-09-06 16:23:27 -050034
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 Geissler942eca22017-09-08 11:20:17 -050044 raise Exception("Failed to get image " + image + ": \n" + r.text)
Andrew Geissler53be16b2017-09-06 16:23:27 -050045
46 return j['data']
47
48 def upload_image(self, image):
Charles P. Hofer88d15b32017-09-12 11:32:55 -050049
Andrew Geissler53be16b2017-09-06 16:23:27 -050050 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 Geissler942eca22017-09-08 11:20:17 -050061 def activate_image(self, image_id):
Charles P. Hofer88d15b32017-09-12 11:32:55 -050062 r = self.session.put(self.url + "/xyz/openbmc_project/software/" + image_id + "/attr/RequestedActivation",
Andrew Geissler64da2182017-09-08 14:06:42 -050063 json={'data': 'xyz.openbmc_project.Software.Activation.RequestedActivations.Active'},
Andrew Geissler942eca22017-09-08 11:20:17 -050064 verify=False)
Charles P. Hofer88d15b32017-09-12 11:32:55 -050065
Andrew Geissler942eca22017-09-08 11:20:17 -050066 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
Andrew Geisslere1b6a3e2017-09-20 21:57:37 -050072 def delete_image(self, image_id):
73 r = self.session.post(self.url + "/xyz/openbmc_project/software/" + image_id + "/action/delete",
74 headers={'Content-Type': 'application/json'},
75 data='{"data":[]}',
76 verify=False)
77
78 j = r.json()
79 if j['status'] != 'ok':
80 raise Exception("Failed to delete image " + image_id + ": \n" + r.text)
81
82 return j['data']
83
Charles P. Hofer88d15b32017-09-12 11:32:55 -050084 def update_auto(self, image, reboot):
85 image_version = self.__get_image_version(image)
86 self.upload_image(image)
87 image_id = self.__wait_for_image_id(image_version)
88 self.activate_image(image_id)
89 self.__wait_for_activation(image_id)
90 if reboot:
91 self.reboot()
92
93 def reboot(self):
94 r = self.session.post(
95 self.url + '/org/openbmc/control/bmc0/action/warmReset',
96 headers={'Content-Type': 'application/json'},
97 data='{"data":[]}',
98 verify=False)
99
100 j = r.json()
101 if j['status'] != 'ok':
102 raise Exception("Failed to reboot BMC:\n" + r.text)
103
104 def __get_image_version(self, image_file_path):
105 # Open up the manfest file.
106 image_tar = tarfile.open(name=image_file_path, mode='r')
107 manifest = image_tar.extractfile('MANIFEST')
108
109 # Find version line.
110 for line in manifest:
111 if line.startswith('version='):
112 manifest.close()
113 image_tar.close()
114 return line.split('version=', 1)[1].strip()
115
116 # If we didn't find the version line, print an error and return false.
117 manifest.close()
118 image_tar.close()
119 raise Exception("Could not find version line in image manifest")
120
121 def __wait_for_image_id(self, image_version):
122 # Try 8 times, once every 15 seconds.
123 for attempt in range(8):
124 software = self.list_sfw()
125 # Look for our the image with the given version in software
126 for path in software:
127 image = self.get_image(path)
Charles P. Hoferc772b472017-09-20 16:02:48 -0500128 if 'Version' in image and image_version == image['Version']:
Charles P. Hofer88d15b32017-09-12 11:32:55 -0500129 return path.split('/')[-1]
130 time.sleep(15)
131 return False
132
133 def __wait_for_activation(self, image_id):
134 # Keep waiting until the image is active or activation fails.
135 active = False
136 while not active:
137 image = self.get_image("/xyz/openbmc_project/software/" + image_id)
138 if 'xyz.openbmc_project.Software.Activation.Activations.Active' \
139 == image['Activation']:
140 print 'Activation Progress: 100%'
141 active = True
142 elif 'xyz.openbmc_project.Software.Activation.Activations.Activating' \
143 == image['Activation']:
144 print 'Activation Progress: ' + str(image['Progress']) + '%'
145 else:
146 raise Exception("Image activation failed. The BMC has set " \
147 + "the'Activation' property to " + image['Activation'])
148 time.sleep(15)
Andrew Geissler53be16b2017-09-06 16:23:27 -0500149
150def do_list_sfw(args):
151 s = BMC(server=args.server)
152 for e in s.list_sfw():
Andrew Geisslercfc7e4c2017-09-20 14:17:12 -0500153 if (e == '/xyz/openbmc_project/software/active'):
154 continue
Andrew Geissler53be16b2017-09-06 16:23:27 -0500155 info = s.get_image(e)
Andrew Geisslercfc7e4c2017-09-20 14:17:12 -0500156 if (((info['Purpose'] == 'xyz.openbmc_project.Software.Version.VersionPurpose.BMC') and
157 (args.bmc or not args.host)) or \
158 ((info['Purpose'] == 'xyz.openbmc_project.Software.Version.VersionPurpose.Host') and
159 (args.host or not args.bmc))):
160 print(e)
161 print json.dumps(info, indent=4)
Charles P. Hofer88d15b32017-09-12 11:32:55 -0500162
Andrew Geissler53be16b2017-09-06 16:23:27 -0500163def do_view_image(args):
164 s = BMC(server=args.server)
165 print json.dumps(s.get_image(args.image), indent=4)
166
167def do_upload_image(args):
168 s = BMC(server=args.server)
169 s.upload_image(args.image)
170
Andrew Geissler942eca22017-09-08 11:20:17 -0500171def do_activate_image(args):
172 s = BMC(server=args.server)
173 s.activate_image(args.image_id)
174
Charles P. Hofer88d15b32017-09-12 11:32:55 -0500175def do_update_auto(args):
176 s = BMC(server=args.server)
177 s.update_auto(args.image, args.reboot)
178
Andrew Geisslere1b6a3e2017-09-20 21:57:37 -0500179def do_delete_image(args):
180 s = BMC(server=args.server)
181 s.delete_image(args.image_id)
182
Andrew Geissler53be16b2017-09-06 16:23:27 -0500183parser = argparse.ArgumentParser()
184parser.add_argument('--server', help='hostname or IP of BMC', type=str,
185 required=True)
186
187subparsers = parser.add_subparsers()
188list_events = subparsers.add_parser('list', help='List all software images on BMC')
189list_events.set_defaults(func=do_list_sfw)
Andrew Geisslercfc7e4c2017-09-20 14:17:12 -0500190list_events.add_argument(
191 '--bmc',
192 action='store_true',
193 default=False,
194 help='Set if you want to see BMC images')
195list_events.add_argument(
196 '--host',
197 action='store_true',
198 default=False,
199 help='Set if you want to see Host images')
Andrew Geissler53be16b2017-09-06 16:23:27 -0500200
201image_view = subparsers.add_parser('view', help='View info of input image')
202image_view.add_argument('image', help='The image to analyze')
203image_view.set_defaults(func=do_view_image)
204
205image_upload = subparsers.add_parser('upload', help='Upload input image')
206image_upload.add_argument('image', help='The image to upload')
207image_upload.set_defaults(func=do_upload_image)
208
Andrew Geissler942eca22017-09-08 11:20:17 -0500209image_activate = subparsers.add_parser('activate', help='Activate input image id')
210image_activate.add_argument('image_id', help='The image id to activate')
211image_activate.set_defaults(func=do_activate_image)
212
Charles P. Hofer88d15b32017-09-12 11:32:55 -0500213image_update_auto = subparsers.add_parser('update_auto', help='Upload and '
214 + 'activate an image, and then reboot the BMC to apply the update '
215 + 'if necessary')
216image_update_auto.add_argument('image', help='The image to update to')
217image_update_auto.add_argument(
218 '--reboot',
Charles P. Hofer88d15b32017-09-12 11:32:55 -0500219 action='store_true',
220 default=False,
221 help='Set if the BMC should reboot after the update')
Charles P. Hofer88d15b32017-09-12 11:32:55 -0500222image_update_auto.set_defaults(func=do_update_auto)
223
Andrew Geisslere1b6a3e2017-09-20 21:57:37 -0500224image_delete = subparsers.add_parser('delete', help='Delete input image id')
225image_delete.add_argument('image_id', help='The image id to delete')
226image_delete.set_defaults(func=do_delete_image)
227
Andrew Geissler53be16b2017-09-06 16:23:27 -0500228args = parser.parse_args()
229
230if 'func' in args:
231 args.func(args)
232else:
233 parser.print_help()