-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_reservation.py
executable file
·163 lines (140 loc) · 5.22 KB
/
start_reservation.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
import argparse
import pymysql
import pexpect
db = pymsql.connect("localhost","dbuser","passwd","MyDB")
cursor = db.cursor()
def add_filers(filer_name,model, disk_shelf_type,filer_sl_no):
print("Adding new filer to the database MyDB..")
sql = "INSERT INTO Hardware_info(Filer_name, \
Model, Disk_shelf_type, Filer_sl_no, filer_status,user_id,reservation_start,reservation_end) \
VALUES ('%s', '%s', '%s', '%s', '%s', '%d','%s','%s' )" % \
(filer_name,model,disk_shelf_type,filer_sl_no,'Free', None, None,None)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
table_info = cursor.fetchall()
print("Available Hardwares in the table..")
print(table_info)
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
def list_filers():
print("List of available filers for reservation....")
sql = "SELECT * from Hardware_info WHERE filer_status = 'Free'"
try:
# Execute the SQL command
cursor.execute(sql)
filer_list = cursor.fetchall()
for filers in filer_list:
print(filers)
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
def reserve_hardware(userId,filer_name,days):
print("Reserving filer {} for {} days" .format(filer_name,days))
sql = "UPDATE Hardware_info SET user_id = '%d', Reservation_start = GETDATE(),Reservation_end = ADDDATE(GETDATE(),'%d'),\
filer_status = 'RESERVE' WHERE filer_name = '%s'" % (userId,days,filer_name)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
sql = "INSERT INTO History(User_id, Filer_name, Reservation_start = NOW(), Reserved_hours)\
VALUES ('%d', '%s', '%d')" % (userId,filer_name,None)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
def release_filer(filer_name):
print("Released filer {}".format(filer_name))
child = pexpect.spawn('Some command that requires password')
child.expect('Enter password:')
child.sendline('password')
child.expect(pexpect.EOF, timeout=None)
cmd_show_data = child.before
cmd_output = cmd_show_data.split('\r\n')
for data in cmd_output:
print data
status = 'GOOD'
if status == 'GOOD':
sql = "UPDATE Hardware_info SET User_id = 'None',Reservation_start = 'None',Reservation_end = 'None',\
filer_status = 'Free' WHERE filer_name = '%s'" % (filer_name)
else:
sql = "UPDATE Hardware_info SET User_id = 'None', Reservation_start = 'None',Reservation_end = 'None',\
filer_status = 'Bad' WHERE filer_name = '%s'" % (filer_name)
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
sql = "UPDATE History SET Reserved_hours = HOUR(Reservation_start) WHERE Filer_name = '%s'"% filer_name
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
def history(filer_name):
print("History of filer {}".format(filer_name))
sql = "SELECT * FROM HISTORY WHERE Filer_name = '%s'" % filer_name
try:
# Execute the SQL command
cursor.execute(sql)
filer_history = cursor.fetchall()
for history in filer_history:
print(history)
except:
# Rollback in case there is any error
db.rollback()
db.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-A","add", nargs=4,help="Add hardwares/filers",action="store_true")
parser.add_argument("-L","--list", help="List available hardwares/filers",action="store_true")
parser.add_argument("-R","--reserve", nargs=3, help="Reserve hardware/filer")
parser.add_argument("-REL","--release", nargs=1, help="Release reserved hardware/filer")
parser.add_argument("-H","--history", nargs=1, help="History of the selected hardware/filer")
args = parser.parse_args()
if args.add:
filer_name = args.add[0]
model = args.add[1]
disk_shelf_type = args.add[2]
filer_sl_no = args.add[3]
add_filers(filer_name,model,disk_shelf_type,filer_sl_no)
elif args.list:
list_filers()
elif args.reserve:
user_id = args.reserve[0]
filer_name = args.reserve[1]
days_to_reserve = args.reserve[2]
reserve_hardware(user_id,filer_name,days_to_reserve)
elif args.release:
filer_name = args.release[0]
release_filer(filer_name)
elif args.history:
filer_name = args.history[0]
history(filer_name)
else:
pass