forked from rauc/rauc-hawkbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbloader.py
187 lines (133 loc) · 3.96 KB
/
hbloader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/env python3
import sys
import asyncio
import aiohttp
import json
import re
import pathlib
from getpass import getpass
from lib.hbclient import HBClient
from lib.ddi.client import DDIClient
from lib.ddi.client import (ConfigStatusExecution, ConfigStatusResult)
import logging
HBLCFG = 'hblcfg.json'
def result_callback(result):
print("Result: {}".format('SUCCESSFUL' if result == 0 else 'FAILED' ))
def step_callback(percentage, message):
print("Progress: {:>3}% - {}".format(percentage, message))
async def main():
config = load_config()
logfmt= '%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d-%(funcName)s] %(message)s'
datefmt= '%Y-%m-%d %H:%M:%S'
logging.basicConfig(level=logging.DEBUG, format=logfmt, datefmt=datefmt)
async with aiohttp.ClientSession() as session:
client = HBClient(session, result_callback, step_callback, **config)
await client.run_ddi()
await client.start_polling()
def ask_parameters(config):
'''
Running first time
'''
''' get host ip '''
ip = input('Enter IP address (default 127.0.0.1): ')
if not ip:
ip = '127.0.0.1'
config['ip'] = ip
''' get host port '''
while True:
port = input ('Enter port (default 443): ')
if not port:
port = '443'
if port.isdecimal():
p = int(port)
if (p in range(1023, 65535)) or (p in (80, 443)):
break
print('Invalid port number')
config['port'] = port
''' target name '''
while True:
target_name = input('Enter device name (for humans): ')
if target_name:
break
config['target_name'] = target_name
''' controller_id '''
while True:
controller_id = input('Enter controller ID (for computers): ')
if controller_id:
break
config['controller_id'] = controller_id
''' get tenant id '''
tenant_id = input('Enter tenant id: ')
if not tenant_id:
tenant_id = 'default'
config['tenant_id'] = tenant_id
''' management login '''
login = input('Login: ')
if not login:
login = 'admin'
config['login'] = login
config['password'] = getpass('Password: ')
''' SSH mode '''
while True:
yn = input('SSL mode (y/n): ')
if yn in ('y', 'Y', ''):
ssl = 'True'
break
if yn in ('n', 'N'):
ssl = 'False'
break
print('Wrong input')
config['ssl'] = ssl
'''run_as_service'''
while True:
yn = input('Run installed as service ? (Yes/No/Ask)')
if yn in ('y', 'Y', ''):
run_as_service = 'yes'
break
if yn in ('n', 'N'):
run_as_service = 'no'
break
if yn in ('a', 'A'):
run_as_service = 'ask'
break
print('Wrong input')
config['run_as_service'] = run_as_service
def load_config():
'''
Load configuration params
If file exists load it as params,
if not create it with default parameters
'''
config = {
"ssl" : False,
"host" :"127.0.0.1",
"tenant_id" : "default",
"target_name" : "",
"login" : "admin",
"password" : "admin",
"auth_token" : "",
"attributes" : {"MAC": ""},
"loglevel" : "DEBUG",
"run_as_service" : "yes",
"port" : "443"
}
'''
if config file exist and contains host ip
return this config
'''
if pathlib.Path(HBLCFG).exists():
with open(HBLCFG, "r") as config_file:
config = json.load(config_file)
if config["ip"]:
return config
'''
else ask to input parameters manually
save and rerun updated config
'''
ask_parameters(config)
with open(HBLCFG, "w") as config_file:
json.dump(config, config_file, indent=4)
return config
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())