forked from jaimelightfoot/HardwareCheckout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddDevice.py
executable file
·107 lines (89 loc) · 3.35 KB
/
addDevice.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
#!/usr/bin/env python3
from HardwareCheckout import create_app
from HardwareCheckout.models import DeviceQueue, Role, DeviceType
from HardwareCheckout.config import db_path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from werkzeug.security import generate_password_hash
from argparse import ArgumentParser
import os
import configparser
parser = ArgumentParser()
parser.add_argument('-u', '--username', dest='username', required=False)
parser.add_argument('-p','--password', dest='password', required=False)
parser.add_argument('-t', '--type', dest='devtype', required=False)
parser.add_argument('-i', '--ini', help='Ini file containing list of users', dest='inifile', required=False)
args = parser.parse_args()
# parser.add_argument("Roles", nargs='+')
session = sessionmaker(bind=create_engine(db_path))
s = session()
def deviceAdd(username, password, devtype):
device = s.query(Role).filter_by(name="Device").first()
typeID = s.query(DeviceType).filter_by(name=devtype).first()
if not typeID:
print("Invalid type")
exit(1)
s.add(
DeviceQueue(
name=username,
password=generate_password_hash(
password,
method="pbkdf2:sha256:45000"
),
state="want-provision",
type=typeID.id
)
)
s.commit()
def iniParse(confPath, devType):
config = configparser.ConfigParser()
config.read(confPath)
result = []
for item in config.sections():
innerList = []
innerList.append(config[item]["username"])
innerList.append(config[item]["password"])
innerList.append(devType)
result.append(innerList)
return result
def csvParse(csvPath):
with open(csvPath, 'r') as ulist:
try:
users = list(filter(bool, ulist.read().split('\n')))
result = []
for user in users:
components = user.split(",")
if len(components) < 2:
print("Parameter missing in line {}".format(user))
exit(1)
else:
result.append(components)
except:
print("Couldn't read {}, {}".format(args.inifile))
exit(1)
return result
def printHelp():
print("Adding multiple devices:")
print("python3 addDevice.py -i <path/to/inifile> -t <devicetype>")
print()
print("Add a single device:")
print("python3 addDevice.py -u <devicename> -p <password> -t <devicetype>")
exit(1)
def main():
if args.inifile and (args.username or args.password) and not args.devtype:
print ("You cannot define username, password, but must define device type when you define a ini file!!")
elif args.username and args.password and args.devtype:
deviceAdd(args.username,args.password,args.devtype)
else:
if not args.inifile or not args.devtype:
printHelp()
if not os.path.isfile(args.inifile):
print ("Ini file {} doesn't exist!".format(args.inifile))
exit(1)
# for parsing csv files replace this with
# csvParse(csvPath) --> Note that csvParse expects device type in the file
users = iniParse(args.inifile, args.devtype)
for user in users:
deviceAdd(user[0],user[1],user[2])
if __name__ == '__main__':
main()