blob: 16a98f4cfcaffc546954956ee98f07d2e1c5b756 [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':
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
60def 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
67def do_view_image(args):
68 s = BMC(server=args.server)
69 print json.dumps(s.get_image(args.image), indent=4)
70
71def do_upload_image(args):
72 s = BMC(server=args.server)
73 s.upload_image(args.image)
74
75parser = argparse.ArgumentParser()
76parser.add_argument('--server', help='hostname or IP of BMC', type=str,
77 required=True)
78
79subparsers = parser.add_subparsers()
80list_events = subparsers.add_parser('list', help='List all software images on BMC')
81list_events.set_defaults(func=do_list_sfw)
82
83image_view = subparsers.add_parser('view', help='View info of input image')
84image_view.add_argument('image', help='The image to analyze')
85image_view.set_defaults(func=do_view_image)
86
87image_upload = subparsers.add_parser('upload', help='Upload input image')
88image_upload.add_argument('image', help='The image to upload')
89image_upload.set_defaults(func=do_upload_image)
90
91args = parser.parse_args()
92
93if 'func' in args:
94 args.func(args)
95else:
96 parser.print_help()