-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureUser.py
More file actions
426 lines (352 loc) · 16.3 KB
/
AzureUser.py
File metadata and controls
426 lines (352 loc) · 16.3 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
__author__ = 'Natalie'
import os
from re import search
from time import sleep
from sys import stderr
from copy import copy
import AzureTools
import AzureSimulation
from AzureTools import comp_user
from AzureSimulation import sms, blob_service
class AzureUser:
def __init__(self):
self.simulations = {}
self.username = ""
self.curr_sim = AzureSimulation.AzureSimulation()
self.email = ""
self.user_info = {}
def add_sim(self, sim_type="mock", proj_name="", user_input="", num_cores=1, ARG=False, DEL=False):
"""
Adds simulation to the user's list of projects. Calls setup_sim and curr_sim.simulation to configure the new
project and actually deploy the simulation
:param sim_type:
:param proj_name:
:param user_input:
:param ARG:
:param DEL:
:return:
"""
###### Create Unique VM ID ######
self.curr_sim.generate_vm_id(self.username)
##### If accessed through command-line #####
if ARG:
self.curr_sim.name = proj_name
self.curr_sim.input = user_input
self.curr_sim.cores = num_cores
self.curr_sim.tstamp = AzureTools.timestamp()
## Check if Input Exits ##
if(not os.path.exists(user_input)):
stderr.write("Could not find " + user_input + "\n")
## Check Project Name is Unique and Valid ###
valid = self.curr_sim.check_name(proj_name)
unique = self.check_sim(proj_name)
##### Update Simulation File ######
# The Simulation name has invalid syntax...
if not valid:
stderr.write("\nSimulation name has invalid syntax.")
exit(1)
elif not unique:
stderr.write("Simulation name already exists.")
exit(1)
else:
self.user_info["username"] = self.username
self.user_info["email"] = self.email
self.user_info["sim"] = self.curr_sim.name
self.user_info["sim_type"] = sim_type
self.user_info["delete"] = DEL
self.curr_sim.upload_input(self.user_info)
success = self.curr_sim.simulation(self.username, sim_type, ARG, DEL)
return success
else:
# Get user input for simulation name and check if name is unique
if self.user_giveSimName() is 1:
return 1
# Get user input for input files
if self.user_giveSimInput() is 1:
return 1
# Get user input for core count
self.user_giveSimCores()
# Generate timestamp
self.curr_sim.tstamp = AzureTools.timestamp()
# Run simulation
success = self.curr_sim.simulation(self.username, sim_type)
return success
def save_curr_sim(self):
"""
Saves the simulation in the user's list of projects
:return:
"""
# Add new, configured simulation to the user's simulation list
key = copy(self.curr_sim.name)
val = copy(self.curr_sim)
self.simulations[key] = val
def user_giveSimCores(self):
"""
User assigns a number of cores to the simulation VM
:return:
"""
user_input = int(raw_input("\nHow many cores? (Options: 1, 2, 4, 8, 16): "))
while user_input not in (1, 2, 4, 8, 16):
print('Not a valid core size.\n')
user_input = int(raw_input("\nHow many cores? (Options: 1, 2, 4, 8, 16): "))
self.cores = user_input
def user_giveSimName(self):
"""
User gives the new simulation a name; checks validity and uniqueness respectively with calls to
curr_sim.check_name and check_sim
:return:
"""
######## Choose Simulation Name ########
sim_name = "1"
while sim_name == '1':
sim_name = raw_input("\nRestrictions: Cannot contain spaces or the following reserved characters \ / < "
"> : \" | ? * \nPress 1 to list existing simulations or enter a name for your new "
"simulation: ")
if sim_name == '1':
self.list_projects()
# Check that project name doesn't exist and has valid syntax
valid = self.curr_sim.check_name(sim_name)
unique = self.check_sim(sim_name)
# The Simulation name is not valid or unique...
while not unique or not valid:
# The Simulation name has invalid syntax...
if not valid:
stderr.write("\nSimulation name has invalid syntax.")
sleep(0.5)
# The Simulation name already exists...
elif not unique:
stderr.write("\nSimulation name already exists.")
sleep(0.5)
option = None
while option not in ['1', '2', '3']:
option = raw_input("\n(1) Re-enter simulation name\n"
"(2) Back to menu\n"
"(3) Quit\n"
">> ")
if option not in ['1', '2', '3']:
stderr.write("\nInput not recognized.\n")
sleep(0.5)
if option == '1':
sim_name = "1"
while sim_name == "1":
sim_name = raw_input("\nRestrictions: Cannot contain spaces or the following reserved "
"characters \ / < > : \" | ? * \nPress 1 to list existing "
"simulations or enter a name for your new simulation: ")
if sim_name == "1":
self.list_projects()
print sim_name
# Check that project name doesn't exist and has valid syntax
valid = self.curr_sim.check_name(sim_name)
unique = self.check_sim(sim_name)
elif option == '2':
return 1
elif option == '3':
quit(0)
# Once valid, update current simulation's name
self.curr_sim.name = sim_name
def user_giveSimInput(self):
"""
Asks the user for the path to their input necessary to run the simulation. Calls curr_sim.upload_input to
upload the input files.
:return:
"""
i = raw_input("\nEnter path to your input folder/file: ")
# Convert path to Windows format
norm_inputs = os.path.normpath(i) # C:/Users/SomeName/InputFiles
# Check that path exists
while not os.path.exists(i):
stderr.write("\nCould not find file.\n")
sleep(0.5)
option = None
while option not in ['1', '2', '3']:
option = raw_input("(1) Re-enter file path\n"
"(2) Back to menu\n"
"(3) Quit\n"
">> ")
if option not in ['1', '2', '3']:
stderr.write("\nInput not recognized.\n")
sleep(0.5)
if option == "1":
i = raw_input("\nEnter path to your input folder/file: ")
elif option == "2":
return 1
elif option == "3":
quit(0)
# Once valid, update current simulation's input files
self.curr_sim.input = i
# Upload files
self.user_info["username"] = self.username
self.user_info["email"] = self.email
self.user_info["sim"] = self.curr_sim.name
self.curr_sim.upload_input(self.user_info)
def user_requestSimResults(self, ARG=False):
"""
Checks if the specified simulation is done running and if its results file has been uploaded
to the client's storage container. If the simulation has finished, the results are downloaded
to the clients computer.
:param ARG:
:return:
"""
if ARG:
print "Checking for " + self.curr_sim.name + " results..."
# Check if project is listed in client's simulation file
not_listed = self.check_sim(self.curr_sim.name)
######## Get Results if Ready ########
if not_listed:
stderr.write("\nProject does not exist.")
exit(1)
else:
# Retrieve names of all user's files in their container
try:
blobs = blob_service.list_blobs(container_name=self.username.lower())
except:
stderr.write('An error occurred while accessing your storage.')
exit(1)
sim_results = 'r-' + self.curr_sim.vm_id
results_in = False
# Check if results are in
for uploaded_file in blobs:
if uploaded_file.name == sim_results:
# Download results file
file_path = 'c:/Users/' + comp_user + '/Simulations/' + self.username + '/' + self.curr_sim.name + \
'_results.zip'
try:
blob_service.get_blob_to_path(self.username.lower(), sim_results, file_path)
except:
stderr.write('An error occurred while downloading your results.')
exit(1)
AzureTools.extract_files(self.curr_sim.name + '_results', self.username)
results_in = True
break
if results_in:
print "\nYour results are in! Check C:/Users/" + comp_user + "/Simulations/" + self.username + " for " \
"the " + self.curr_sim.name + "_results folder."
else:
# If the results are not in and the VM is still running...
try:
sms.get_role(self.username, self.username, self.curr_sim.vm_id)
# The simulation is still running
print "\nThe simulation is still running. Check back later to retrieve results."
# If the results are not in but the VM is already deleted...
except:
# The results did not upload
print "Your results were unable to be uploaded. Try running the simulation again."
elif not ARG:
######## Get Simulation Name #########
requested_sim = "1"
while requested_sim == '1':
requested_sim = raw_input("\nSelect a project to check for results.\n"
"Press '1' to list projects or enter project name: ")
if requested_sim == '1':
self.list_projects()
######## Get Results if Ready ########
while self.check_sim(requested_sim) == True:
print "\nProject does not exist."
option = None
while option not in ['1', '2', '3']:
option = raw_input("(1) Check another project\n"
"(2) Back to menu\n"
"(3) Quit\n"
">> ")
if option not in ['1', '2', '3']:
stderr.write("\nInput not recognized.\n")
sleep(0.5)
if option == 1:
requested_sim = "1"
while requested_sim == '1':
requested_sim = raw_input("\nSelect a project to check for results.\n"
"Press '1' to list projects or enter project name: ")
if requested_sim == '1':
self.list_projects()
elif option == '2':
return
elif option == '3':
quit(0)
# Retrieve all user's files from their container
try:
blobs = blob_service.list_blobs(container_name=self.username.lower())
except:
stderr.write('An error occurred while trying to access your storage.')
exit(1)
sim_results = 'r-' + self.curr_sim.vm_id
results_in = False
# Check if results are in
for uploaded_file in blobs:
if uploaded_file.name == sim_results:
# Download results file
file_path = 'c:/Users/' + comp_user + '/Simulations/' + self.username + '/' + requested_sim + \
'_results.zip'
try:
blob_service.get_blob_to_path(self.username.lower(), sim_results, file_path)
except:
stderr.write('An error occurred when trying to download your results.')
exit(1)
# Extract results
AzureTools.extract_files(requested_sim + '_results', self.username)
results_in = True
break
if results_in:
print "\nYour results are in! Check C:/Users/" + comp_user + "/Simulations/" + self.username + " for " \
"the " + requested_sim + "_results folder."
return
else:
# If the results are not in and the VM is still running...
try:
sms.get_role(self.username, self.username, self.curr_sim.vm_id)
# The simulation is still running
print "\nThe simulation is still running. Check back later to retrieve results."
# If the results are not in but the VM is already deleted...
except:
# The results did not upload
print "Your results were unable to be uploaded. Try running the simulation again."
return
def check_username(self):
"""
Checks that the syntax in the client chosen username is valid. If the username is valid, a cloud service will
be created for the client and a boolean 'True' will be returned
:return: is_valid
"""
is_valid = True
# Check username for invalid characters
if not search('^[a-zA-Z0-9-]+$', self.username):
is_valid = False
stderr.write('\nUsername can only contain numbers, letters, and dashes.\n')
# Check username length
elif len(self.username) > 10 or len(self.username) < 3:
is_valid = False
stderr.write('\nUsername must be between 3 and 10 characters.\n')
# Check username starting/ending characters
elif not search('^[a-zA-Z].*\w$', self.username):
is_valid = False
stderr.write('\nUsername must start with a letter and end with a letter or a number.\n')
# Check if username (cloud service) already exists in Azure
elif not sms.check_hosted_service_name_availability(self.username).result:
is_valid = False
stderr.write('\nUsername is not available.\n')
sleep(0.5)
return is_valid
def check_sim(self, sim_name):
"""
Checks that the project name chosen by the user is unique within their account.
:return: unique
"""
unique = True
# Check if the project name already exists under this account
if sim_name in self.simulations.keys():
unique = False
return unique
def remove_proj(self, del_proj):
"""
Removes specified project from the user's simulation file
:param del_proj:
:return:
"""
self.simulations.pop(del_proj)
def list_projects(self):
"""
Lists all the client's simulations and their time stamps.
:return:
"""
print '\n ' + self.username + '\'s Projects:'
for proj in self.simulations:
print " " + self.simulations[proj].tstamp + " " + self.simulations[proj].name