-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
170 lines (124 loc) · 5.56 KB
/
setup.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
__author__ = 'Natalie'
import os
import contextlib
import shutil
from subprocess import Popen
from getpass import getuser
import Tkinter as tk
import sys
from threading import Lock
class subscriptionDialog:
def __init__(self):
# Acquire Lock
mutex.acquire();
self.root = tk.Tk()
self.nameLbl = tk.Label(self.root, text='Azure Subscription Name:')
self.nameLbl.pack()
self.nameEntry = tk.Entry(self.root)
self.nameEntry.pack()
self.idLbl = tk.Label(self.root, text='Azure Subscription ID:')
self.idLbl.pack()
self.idEntry = tk.Entry(self.root)
self.idEntry.pack()
self.submitBttn = tk.Button(self.root, text='Submit', command=self.send)
self.submitBttn.pack()
self.root.mainloop()
def send(self):
subscription_name = self.nameEntry.get()
subscription_id = self.idEntry.get()
if subscription_name and subscription_id:
upload = Popen(['powershell', 'upload_cert.ps1'])
r = upload.wait()
if r < 0:
sys.stderr.write("ERROR: Certificate upload to Windows Azure failed\n")
success = False
mutex.release()
self.root.destroy()
@contextlib.contextmanager
def stdout_redirect(where):
sys.stdout = where
try:
yield where
finally:
sys.stdout = sys.__stdout__
if __name__ == "__main__":
global subscription_name, subscription_id, success, mutex
mutex = Lock()
subscription_name = None
subscription_id = None
success = True
############################################ Create Simulations Folder #############################################
sim_folder = "C:/Users/" + getuser() + "/Simulations/"
if not os.path.isdir(sim_folder):
print "Creating VecNet Simulations folder..."
os.mkdir(sim_folder)
############################################# Create Azure certificate #############################################
print "\nCreating Windows Azure certificate..."
os.chdir("C:/Program Files/Microsoft SDKs/Windows/v7.1/Bin/")
create = Popen(['makecert.exe', '-sky', 'exchange', '-r', '-n', 'CN=AzureCertificate',
'-pe', '-a', 'sha1', '-len', '2048', '-ss', 'VecNet',
'D:\\Naki\'s Data\\My Documents\\CRC Internship\\vecnetCert2.cer'])
r = create.wait()
if r < 0:
sys.stderr.write("ERROR: Microsoft Azure certificate creation failed\n")
success = False
############################################# Install Azure certificate ############################################
print "\nInstall Windows Azure certificate..."
os.chdir("D:/Naki's Data/My Documents/CRC Internship/")
install = Popen(['certutil', '-user', '-f', '-addstore', 'VecNet', 'vecnetCert2.cer'])
r = install.wait()
if r < 0:
sys.stderr.write("ERROR: Installation of Microsoft Azure certificate failed.\n")
success = False
########################### Run powershell script to upload Azure Certificate to Azure #############################
dialog = subscriptionDialog()
print "\nUpload certificate to Windows Azure..."
f = open("upload_cert.ps1", "w")
f.write("Set-AzureSubscription -SubscriptionName \"" + subscription_name + #
"\" -SubscriptionId \"" + subscription_id + # 15fee040-58a2-4882-8c49-126902f64612
"\" -Certificate \"vecnetCert.cer\"")
f.close()
########################################### Run diskpart script to create VHD ######################################
print "\nCreating VHD data disk for VecNet..."
f = open("create_datadisk.txt", "w")
f.write("create vdisk file=vecnetDataDisk.vhd MAXIMUM=100\n")
f.write("select vdisk file=vecnetDataDisk.vhd\n")
f.write("attach vdisk\n")
f.write("convert mbr\n")
f.write("create partition primary\n")
f.write("format fs=ntfs label=\"VecNet Data\" quick\n")
f.write("assign letter=v\n")
f.close()
diskpart1 = Popen(['diskpart', '/s', 'create_datadisk.txt'])
r = diskpart1.wait()
if r < 1:
print "VecNet data disk creation failed.\n"
############################################## Copy certificate onto VHD ###########################################
shutil.copy("D:\\Naki\'s Data\\My Documents\\CRC Internship\\vecnetCert.cer", "V:\\vecnetCert.cer")
######################################## Run diskpart script to detach VHD #########################################
f = open("detach_datadisk.txt", "w")
f.write("select vdisk file=vecnetDataDisk.vhd\n")
f.write("detach vdisk\n")
diskpart2 = Popen(['diskpart', '/s', 'detach_datadisk.txt'])
r = diskpart2.wait()
if r < 1:
print "VecNet data disk unsuccessfully detached"
print "VecNet Azure configuration SUCCESSFUL"
'''
# Export Azure certificate to new data drive
print "\nExport certificate to new data disk..."
with stdout_redirect(StringIO()) as store_contents:
store = sys('certutil', '-store', '-user', '-f', '\"VecNet\"')
r = store.wait()
if r < 1:
print "Azure certificate export failed.\n"
store_contents.seek(0)
cert_list = split("^={16} Certificate \d ={16}$", store_contents)
serial_num = None
for cert in cert_list:
if "Subject: AzureCertificate" in cert:
serial_num = split(split("\n", cert)[0])[1]
export = sys.call('certutil', '-exportPFX', '-p', '\"1\"', 'my', serial_num, 'V:\\vecnetCert.pfx')
r = export.wait()
break
'''