-
Notifications
You must be signed in to change notification settings - Fork 0
/
VM_EMOD.py
146 lines (115 loc) · 4.95 KB
/
VM_EMOD.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
from settings_local import EMAIL_PASSWORD, EMAIL_USERNAME, STORAGE_ACCOUNT_NAME, STORAGE_ACCOUNT_KEY, SUBSCRIPTION_ID
__author__ = 'Natalie Sanders'
from azure.servicemanagement import *
from azure.storage import *
from subprocess import call
from os import chdir
import os
import socket
import zipfile
import pickle
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders
global user_info
def delete_vm():
hosted_service = sms.get_hosted_service_properties(service_name=username, embed_detail=True)
if hosted_service.deployments:
deployment = sms.get_deployment_by_name(username, username)
roles = deployment.role_list
for instance in roles:
if vm_name == instance.role_name:
if len(roles) == 1:
sms.delete_deployment(service_name=username, deployment_name=username)
else:
sms.delete_role(service_name=username, deployment_name=username, role_name=vm_name)
break
def send_mail( send_from, send_to, subject, text, files=[], server="localhost", port=587, username='', password='', isTls=True):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = subject
msg.attach( MIMEText(text) )
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload( open(f,"rb").read() )
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f)))
msg.attach(part)
smtp = smtplib.SMTP(server, port)
if isTls: smtp.starttls()
smtp.login(username,password)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()
def upload_results():
####### Upload Final Results ########
# Zip output directory
z = zipfile.ZipFile(user_info["sim"] + '_Results.zip', "w", zipfile.ZIP_DEFLATED)
for result in os.listdir('Output'):
chdir("c:/Users/Public/Sim/Output")
z.write(result)
z.close()
result = 'r-' + vm_name
try:
blob_service.put_block_blob_from_path(container_name, result, 'c:/Users/Public/Sim/' + user_info["sim"] + '_Results.zip')
except:
print "could not upload"
def download_input():
blob_service.get_blob_to_path(container_name, vm_name, 'c:/Users/Public/Sim/Inputs.zip')
chdir("C:/Users/Public/Sim")
z = zipfile.ZipFile('Inputs.zip', 'r')
z.extractall()
z.close()
########################################################################################################################
## MAIN ##
########################################################################################################################
##### Service Management Object #####
vm_name = socket.gethostname()
split = vm_name.split('-')
username = '-'.join(split[:-1])
container_name = '-'.join(split[:-1]).lower()
subscription_id = SUBSCRIPTION_ID
certificate_path = 'CURRENT_USER\\my\\AzureCertificate'
# Import service management certificate
call(['certutil', '-user', '-f', '-p', '1', '-importPFX', 'c:/temp/azure.pfx'])
sms = ServiceManagementService(subscription_id, certificate_path)
###### Redirect stdout to File ######
chdir('C:/Users/Public/Sim')
output = open("Output/stdout.txt", "wb")
####### Download Input Files ########
blob_service = BlobService(
account_name=STORAGE_ACCOUNT_NAME,
account_key=STORAGE_ACCOUNT_KEY)
try:
download_input()
f = "C:/Users/Public/Sim/AzureUserInfo.pickle"
user_info = pickle.load(file(f))
except:
output.write('Could not download input from the cloud.\n')
########### Run Simulation ##########
call(["eradication.exe", "-C", "config.json", "-O", "Output"], stdout=output)
output.close()
try:
########### Upload Results ##########
upload_results()
########### Email Results ###########
send_mail( send_from = '[email protected]',
send_to = user_info["email"],
subject = 'The results for your ' + user_info["sim"] + ' simulation are ready!',
text = 'Hi ' + user_info['username'] + ',\n\nYour ' + user_info["sim"] + ' simulation has '
'finished running. Look for your results below.\n\nThanks for using VecNet Azure '
'resources!\nThe VecNet Team',
files = ['c:/Users/Public/Sim/' + user_info["sim"] + '_Results.zip'],
server = "smtp.gmail.com",
port = 587,
username = EMAIL_USERNAME,
password = EMAIL_PASSWORD,
isTls = True)
print "sent mail"
except:
print "mail failed"
delete_vm()