-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup_Sim.py
230 lines (192 loc) · 8.12 KB
/
Setup_Sim.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/python
from settings_local import STORAGE_ACCOUNT_KEY, STORAGE_ACCOUNT_KEY, SUBSCRIPTION_ID
__author__ = 'Natalie Sanders'
import argparse
import pickle
import os
from time import sleep
from sys import stderr
from azure import *
from azure.servicemanagement import *
from azure.storage import BlobService
import AzureUserPool
from AzureTools import comp_user
def menu():
"""
Beginning menu which prompts the user to sign in with a previous username, create
an account, or quit the program.
:return:
"""
goto = 0
user_input = raw_input(
"\n########## LOGIN ##########\n"
"(1) Sign in\n"
"(2) New User\n"
"(3) Quit\n"
">> ")
if user_input == '1':
# prompts client for username
goto = users.sign_in()
elif user_input == '2':
# creates cloud service and storage container using client specified username
goto = users.add_user()
pickle.dump(users.user_list, file(f, 'w'))
elif user_input == '3':
quit(0)
else:
print "\nCommand not recognized."
goto = 1
if goto is 1:
menu()
elif goto is 0:
sub_menu(users.curr_user)
def sub_menu(user):
"""
The menu displayed after a client has signed-in or has created an account. Gives the
client the option to run a new simulation, retrieve simulation results for a previously
run simulation, or logout.
:return:
"""
user_input = raw_input(
'\n########### MENU ########### \n'
'(1) Start new EMOD simulation\n'
'(2) Start new Open Malaria simulation\n'
'(3) Check for simulation results\n'
'(4) Delete account\n'
'(5) Logout\n'
'(6) Quit\n'
'>> ')
if user_input == '1':
# creates a VM under client's cloud service, running the EMOD simulation
if user.add_sim('EMOD') is 0:
users.save_user()
pickle.dump(users.user_list, file(f, 'w'))
sub_menu(user)
elif user_input == '2':
# creates a VM under client's cloud service, running the Open Malaria simulation
if user.add_sim('OM') is 0:
users.save_user()
pickle.dump(users.user_list, file(f, 'w'))
sub_menu(user)
elif user_input == '3':
# returns the results of a previously run simulation specified by the client
user.user_requestSimResults()
sub_menu(user)
elif user_input == '4':
# deletes the user's account-- cloud service and storage container
if users.delete_account() is 1:
pickle.dump(users.user_list, file(f, 'w'))
menu()
else:
sub_menu(user)
elif user_input == '5':
users.logout()
menu()
elif user_input == '6':
quit(0)
else:
stderr.write("\nCommand not recognized.")
sleep(0.5)
sub_menu(user)
########################################################################################################################
## MAIN ##
########################################################################################################################
#### GLOBAL VARIABLES ####
# The user pool is imported
global users
global f
f = 'C:/Users/' + comp_user + '/Simulations/AzureUsers.pickle'
if os.path.exists(f):
users = AzureUserPool.AzureUserPool()
users.user_list = pickle.load(file(f))
else:
users = AzureUserPool.AzureUserPool()
# Create service management object
subscription_id = SUBSCRIPTION_ID
certificate_path = 'CURRENT_USER\\my\\AzureCertificate'
sms = ServiceManagementService(subscription_id, certificate_path)
# Create blob service object
blob_service = BlobService(
account_name=STORAGE_ACCOUNT_NAME,
account_key=STORAGE_ACCOUNT_KEY)
# Test the service management object
try:
sms.get_subscription()
except:
stderr.write("An error occurred while connecting to Azure Service Management. Please, check your service "
"management certificate.")
exit(1)
# Check for command line arguments
UI = True
if len(sys.argv) > 1:
use_string = 'Setup_Sim.py [-h] [-new email] username [ -d | [-ncln] -sE INPUT_FOLDER SIMULATION_NAME N_CORES| [-ncln] ' \
'-sOM INPUT_FOLDER SIMULATION_NAME N_CORES | [-ncln] -m INPUT_FOLDER SIMULATION_NAME N_CORES | -r SIMULATION_NAME ]'
parser = argparse.ArgumentParser(usage=use_string)
parser.add_argument("-new", "--new_user", nargs=1, action="store",
help="New user; create an account")
parser.add_argument("username", type=str,
help="Username for simulation account")
parser.add_argument("-ncln", "--cleanup_off", action="store_true",
help="Turn off automatic cleanup of VM instances (VMs not deleted)")
options_group = parser.add_mutually_exclusive_group()
options_group.add_argument("-sOM", "--OpenMalaria", nargs=3, type=str, action="store",
help="Runs new Open Malaria simulation; must provide file path to input folder,"
"a new simulation name, and the number of cores needed.")
options_group.add_argument("-sE", "--EMOD", nargs=3, type=str, action="store",
help="Runs new EMOD simulation; must provide file path to input folder, a new "
"simulation name, and the number of cores needed.")
options_group.add_argument("-r", "--get_results", nargs=1, type=str, action="store",
help="Get simulation results; must provide simulation name")
options_group.add_argument("-d", "--delete", action="store_true",
help="Delete account")
options_group.add_argument("-m", "--mock_model", nargs=3, type=str, action="store",
help="mock model; returns uploaded input")
args = parser.parse_args()
UI = False
# Begin Tasks
if UI:
menu()
else:
# Check for command line errors
if not args.delete and not (args.EMOD or args.OpenMalaria or args.mock_model) and not args.get_results:
stderr.write(use_string + '\nSetup_Sim.py: error: too few arguments.\n')
exit(2)
if args.new_user and args.delete:
stderr.write(use_string + '\nSetup_Sim.py: error: argument -new/--new_user: not allowed with argument '
'-d/--delete')
exit(2)
if args.cleanup_off and args.get_results:
stderr.write(use_string + '\nSetup_Sim.py: error: argument -ncln/--cleanup_off: not allowed with argument '
'-r/--get_results')
exit(2)
if args.cleanup_off and not (args.cleanup_off and (args.EMOD or args.OpenMalaria or args.mock_model)):
stderr.write(use_string + '\nSetup_Sim.py: error: -ncln/--cleanup_off: must use additional argument '
'-sE/--EMOD, -sOM/--OpenMalaria, or -m/--mock_model')
exit(2)
# Start specified tasks
if args.new_user:
print args.new_user
users.add_user(args.username, args.new_user[0], True)
elif args.delete:
users.delete_account(True)
else:
users.sign_in(args.username, True)
if args.cleanup_off:
del_VM = False
else:
del_VM = True
if args.EMOD:
if users.curr_user.add_sim("EMOD", args.EMOD[1], args.EMOD[0], args.EMOD[2], True, del_VM) is 0:
users.save_user()
pickle.dump(users.user_list, file(f, 'w'))
elif args.OpenMalaria:
if users.curr_user.add_sim("OM", args.OpenMalaria[1], args.OpenMalaria[0], args.OpenMalaria[2], True, del_VM) is 0:
users.save_user()
pickle.dump(users.user_list, file(f, 'w'))
elif args.mock_model:
if users.curr_user.add_sim("mock", args.mock_model[1], args.mock_model[0], args.mock_model[2], True, del_VM) is 0:
users.save_user()
pickle.dump(users.user_list, file(f, 'w'))
elif args.get_results:
users.curr_user.user_requestSimResults(True)
pickle.dump(users.user_list,file(f, 'w'))