blob: 5e95473ec28fa78b1153456882a135d72c28c63c [file] [log] [blame]
Andrew Geissler53be16b2017-09-06 16:23:27 -05001#!/bin/env python
2
3import argparse
4import requests
5import json
6
7import urllib3
8import _sysconfigdata
9urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
10
11
12class 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 Geissler942eca22017-09-08 11:20:17 -050031 raise Exception("Failed to query software: \n" + r.text)
Andrew Geissler53be16b2017-09-06 16:23:27 -050032
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 Geissler942eca22017-09-08 11:20:17 -050042 raise Exception("Failed to get image " + image + ": \n" + r.text)
Andrew Geissler53be16b2017-09-06 16:23:27 -050043
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 Geissler942eca22017-09-08 11:20:17 -050059 def activate_image(self, image_id):
60 r = self.session.put(self.url + "/xyz/openbmc_project/software/" + image_id + "/attr/RequestedActivation",
Andrew Geissler64da2182017-09-08 14:06:42 -050061 json={'data': 'xyz.openbmc_project.Software.Activation.RequestedActivations.Active'},
Andrew Geissler942eca22017-09-08 11:20:17 -050062 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 Geissler53be16b2017-09-06 16:23:27 -050070
71def 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
78def do_view_image(args):
79 s = BMC(server=args.server)
80 print json.dumps(s.get_image(args.image), indent=4)
81
82def do_upload_image(args):
83 s = BMC(server=args.server)
84 s.upload_image(args.image)
85
Andrew Geissler942eca22017-09-08 11:20:17 -050086def do_activate_image(args):
87 s = BMC(server=args.server)
88 s.activate_image(args.image_id)
89
Andrew Geissler53be16b2017-09-06 16:23:27 -050090parser = argparse.ArgumentParser()
91parser.add_argument('--server', help='hostname or IP of BMC', type=str,
92 required=True)
93
94subparsers = parser.add_subparsers()
95list_events = subparsers.add_parser('list', help='List all software images on BMC')
96list_events.set_defaults(func=do_list_sfw)
97
98image_view = subparsers.add_parser('view', help='View info of input image')
99image_view.add_argument('image', help='The image to analyze')
100image_view.set_defaults(func=do_view_image)
101
102image_upload = subparsers.add_parser('upload', help='Upload input image')
103image_upload.add_argument('image', help='The image to upload')
104image_upload.set_defaults(func=do_upload_image)
105
Andrew Geissler942eca22017-09-08 11:20:17 -0500106image_activate = subparsers.add_parser('activate', help='Activate input image id')
107image_activate.add_argument('image_id', help='The image id to activate')
108image_activate.set_defaults(func=do_activate_image)
109
Andrew Geissler53be16b2017-09-06 16:23:27 -0500110args = parser.parse_args()
111
112if 'func' in args:
113 args.func(args)
114else:
115 parser.print_help()