Repository navigation
Expand file tree
/
Copy pathfunction.py
More file actions
235 lines (216 loc) · 11.2 KB
/
Copy pathfunction.py
File metadata and controls
235 lines (216 loc) · 11.2 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
# coding:utf8
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute.models import DiskCreateOption
import time,base64
def create_credential_object(tenant_id, client_id, client_secret):
print("Create Credential Object")
tenant_id = tenant_id
client_id = client_id
client_secret = client_secret
credential = ServicePrincipalCredentials(tenant=tenant_id, client_id=client_id, secret=client_secret)
return credential
def create_resource_group(subscription_id, credential, tag, location):
print("Create eEsource Group")
credential = credential
resource_client = ResourceManagementClient(credential, subscription_id)
RESOURCE_GROUP_NAME = tag
LOCATION = location
rg_result = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME,
{
"location": LOCATION
}
)
def create_or_update_vm(subscription_id, credential, tag, location, username, password, size, os,rootpwd):
global publisher, offer, sku
compute_client = ComputeManagementClient(credential, subscription_id)
RESOURCE_GROUP_NAME = tag
VNET_NAME = ("vnet-" + tag)
SUBNET_NAME = ("subnet-" + tag)
IP_NAME = ("ip-" + tag)
IP_CONFIG_NAME = ("ipconfig-" + tag)
NIC_NAME = ("nicname-" + tag)
LOCATION = location
VM_NAME = tag
USERNAME = username
PASSWORD = password
ROOT_PWD = rootpwd
SIZE = size
if os == "ubuntu20":
publisher = "Canonical"
offer = "0001-com-ubuntu-server-focal"
sku = "20_04-lts"
version = "latest"
elif os == "ubuntu18":
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
elif os == "ubuntu16":
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
version = "latest"
elif os == "centos":
publisher = "OpenLogic"
offer = "CentOS"
sku = "7.5"
version = "latest"
elif os == "debian10":
publisher = "Debian"
offer = "debian-10"
sku = "10"
version = "latest"
elif os == "windows2019":
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2019-Datacenter-smalldisk"
version = "latest"
elif os == "windows2016":
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter-smalldisk"
version = "latest"
elif os == "windows2012":
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2012-Datacenter-smalldisk"
version = "latest"
else:
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
network_client = NetworkManagementClient(credential, subscription_id)
print("Create VNET")
poller = network_client.virtual_networks.create_or_update(RESOURCE_GROUP_NAME,
VNET_NAME,
{
"location": LOCATION,
"address_space": {
"address_prefixes": ["10.0.0.0/16"]
}
}
)
vnet_result = poller.result()
print("Create Subnets")
poller = network_client.subnets.create_or_update(RESOURCE_GROUP_NAME,
VNET_NAME, SUBNET_NAME,
{"address_prefix": "10.0.0.0/24"}
)
subnet_result = poller.result()
print("Create IP")
poller = network_client.public_ip_addresses.create_or_update(RESOURCE_GROUP_NAME,
IP_NAME,
{
"location": LOCATION,
"sku": {"name": "Basic"},
"public_ip_allocation_method": "Dynamic",
"public_ip_address_version": "IPV4"
}
)
ip_address_result = poller.result()
print("Create Network Interfaces")
poller = network_client.network_interfaces.create_or_update(RESOURCE_GROUP_NAME,
NIC_NAME,
{
"location": LOCATION,
"ip_configurations": [{
"name": IP_CONFIG_NAME,
"subnet": {"id": subnet_result.id},
"public_ip_address": {
"id": ip_address_result.id}
}],
}
)
nic_result = poller.result()
s = "IyEvYmluL2Jhc2gKZWNobyByb290OnBweHdvMTIzIHxzdWRvIGNocGFzc3dkIHJvb3QKc3VkbyBzZWQgLWkgJ3MvXi4qUGVybWl0Um9vdExvZ2luLiovUGVybWl0Um9vdExvZ2luIHllcy9nJyAvZXRjL3NzaC9zc2hkX2NvbmZpZzsKc3VkbyBzZWQgLWkgJ3MvXi4qUGFzc3dvcmRBdXRoZW50aWNhdGlvbi4qL1Bhc3N3b3JkQXV0aGVudGljYXRpb24geWVzL2cnIC9ldGMvc3NoL3NzaGRfY29uZmlnOwpzdWRvIHNlcnZpY2Ugc3NoZCByZXN0YXJ0"
if (ROOT_PWD != ""):
d = base64.b64decode(s).decode('latin-1')
d = d.replace("ppxwo123",ROOT_PWD)
CUSTOM_DATA = base64.b64encode(d.encode("utf-8")).decode('latin-1')
else:
CUSTOM_DATA = s
poller = compute_client.virtual_machines.create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
{
"location": LOCATION,
"storage_profile": {
"osDisk":{
"createOption":"fromImage",
"diskSizeGB":64
},
"image_reference": {
"offer": offer,
"publisher": publisher,
"sku": sku,
"version": version
}
},
"hardware_profile": {
"vm_size": SIZE
},
"os_profile": {
"computer_name": VM_NAME,
"admin_username": USERNAME,
"admin_password": PASSWORD,
"custom_data": CUSTOM_DATA
},
"network_profile": {
"network_interfaces": [{
"id": nic_result.id,
}],
}
}
)
vm_result = poller.result()
print(vm_result)
def start_vm(subscription_id, credential, tag):
compute_client = ComputeManagementClient(credential, subscription_id)
GROUP_NAME = tag
VM_NAME = tag
async_vm_start = compute_client.virtual_machines.start(
GROUP_NAME, VM_NAME)
async_vm_start.wait()
def stop_vm(subscription_id, credential, tag):
compute_client = ComputeManagementClient(credential, subscription_id)
GROUP_NAME = tag
VM_NAME = tag
async_vm_deallocate = compute_client.virtual_machines.deallocate(
GROUP_NAME, VM_NAME)
async_vm_deallocate.wait()
def delete_vm(subscription_id, credential, tag):
resource_client = ResourceManagementClient(credential, subscription_id)
GROUP_NAME = tag
resource_client.resource_groups.delete(GROUP_NAME)
def change_ip(subscription_id, credential, tag):
compute_client = ComputeManagementClient(credential, subscription_id)
GROUP_NAME = tag
VM_NAME = tag
async_vm_deallocate = compute_client.virtual_machines.deallocate(
GROUP_NAME, VM_NAME)
async_vm_deallocate.wait()
time.sleep(10)
async_vm_start = compute_client.virtual_machines.start(
GROUP_NAME, VM_NAME)
async_vm_start.wait()
def list(subscription_id, credential):
network_client = NetworkManagementClient(credential, subscription_id)
info = network_client.public_ip_addresses.list_all()
compute_client = ComputeManagementClient(credential, subscription_id)
info2 = compute_client.virtual_machines.list_all()
iplist = []
taglist = []
for info in info:
info = str(info)
info = str(info).replace("'", "").replace('"', "")
info = info.split(", ")[-7].split(" ")[1]
iplist.append(info)
for info2 in info2:
info2 = str(info2)
info2 = str(info2).replace("'", "").replace('"', "")
info2 = info2.split(", ")[2].split(" ")[1]
taglist.append(info2)
dict = {"ip": iplist, "tag": taglist}
return dict