-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
281 lines (247 loc) · 9.7 KB
/
main.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
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
import os
import sys
import json
import time
from fastapi import FastAPI
from typing import Union
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Dict
import pulumi
from pulumi import automation as auto
from pulumi import Output
from pulumi_gcp import storage
from pulumi_gcp import compute
from google.cloud import compute_v1
from google.cloud import bigquery
image_list_origin = {
"win7": "windows-cloud/windows-server-2016-dc-v20220902",
"win8": "windows-cloud/windows-server-2012-r2-dc-v20220902",
"win10": "windows-cloud/windows-server-2016-dc-v20220902",
"win11": "windows-cloud/windows-server-2016-dc-v20220902"
}
def job_status_init(job_name):
client = bigquery.Client()
insert = f"""
INSERT INTO
`gametest.job-status` (job_name,
job_status,
message)
VALUES
('{job_name}', 'starting', 'test job is starting')
""".format(job_name=job_name)
client.query(insert)
print("initialize job status")
def job_stauts_update(job_name):
client = bigquery.Client()
update = f"""
UPDATE
`gametest.job-status`
SET
job_status="on progress", message="test job is on progress"
WHERE
job_name='{job_name}'""".format(job_name=job_name)
client.query(update)
print("update job status")
def job_status_check(job_name):
client = bigquery.Client()
update = f"""
SELECT
*
FROM
`gametest.job-status`
WHERE
job_name = '{job_name}'""".format(job_name=job_name)
query_results = client.query(update)
query_records = [dict(row) for row in query_results]
final_results = json.dumps(str(query_records))
return final_results
def job_result_check(job_name):
client = bigquery.Client()
query = f"""
SELECT
*
FROM
`gametest.job_result`
WHERE
job_name='{job_name}'""".format(job_name=job_name)
query_results = client.query(query)
query_records = [dict(row) for row in query_results]
final_results = json.dumps(str(query_records))
return final_results
def job_status_delete(job_name):
client = bigquery.Client()
update = f"""
UPDATE
`gametest.job-status`
SET
job_status="deleted", message="test job has been deleted"
WHERE
job_name='{job_name}'""".format(job_name=job_name)
client.query(update)
print("update job status")
def instance_status_check(image_list, project_id, zone):
instance_list = []
for image_key in image_list:
instance_list.append(image_key)
client = compute_v1.InstancesClient()
status_running = 0
while (status_running < len(instance_list)):
for instance in instance_list:
instance_obj = client.get(project=project_id, zone=zone, instance=instance)
instance_status = instance_obj.status
if instance_status == "RUNNING":
status_running += 1
else:
pass
print("waiting for all instances become ready")
print("All instances are in running status now")
def job_create(program, job_name, project_id, region, zone):
project_name = job_name
os.environ["PULUMI_CONFIG_PASSPHRASE"] = ""
project_setting = auto.ProjectSettings(name=project_name, runtime="python", backend=auto.ProjectBackend(url="file:///home/pulumidemobackend"))
stack_setting = {
"prd": auto.StackSettings(secrets_provider="default")
}
localworkspace_setting = auto.LocalWorkspaceOptions(work_dir="./",secrets_provider="default" ,project_settings=project_setting, stack_settings=stack_setting)
stack_name = job_name
stack = auto.create_or_select_stack(stack_name=stack_name,
project_name=project_name,
program=program,
opts=localworkspace_setting)
print("initialize pulumi stack")
stack.workspace.install_plugin("gcp", "v6.32.0")
gcp_configmap_setting = {
"gcp:project": auto.ConfigValue(value=project_id),
"gcp:region": auto.ConfigValue(value=region),
"gcp:zone": auto.ConfigValue(value=zone)
}
stack.set_all_config(gcp_configmap_setting)
print("config pulumi stack")
time.sleep(5)
up_res = stack.up(on_output=print)
print("pulumi stack has been created successfully")
def job_delete(program, job_name, project_id, region, zone):
project_name = job_name
os.environ["PULUMI_CONFIG_PASSPHRASE"] = ""
project_setting = auto.ProjectSettings(name=project_name, runtime="python", backend=auto.ProjectBackend(url="file:///home/pulumidemobackend"))
stack_setting = {
"prd": auto.StackSettings(secrets_provider="default")
}
localworkspace_setting = auto.LocalWorkspaceOptions(work_dir="./",secrets_provider="default" ,project_settings=project_setting, stack_settings=stack_setting)
stack_name = job_name
stack = auto.create_or_select_stack(stack_name=stack_name,
project_name=project_name,
program=program,
opts=localworkspace_setting)
print("pulumi stack initialized")
stack.workspace.install_plugin("gcp", "v6.32.0")
gcp_configmap_setting = {
"gcp:project": auto.ConfigValue(value=project_id),
"gcp:region": auto.ConfigValue(value=region),
"gcp:zone": auto.ConfigValue(value=zone)
}
stack.set_all_config(gcp_configmap_setting)
up_destroy = stack.destroy(on_output=print)
print(f'successfully delete job {job_name}'.format(job_name=job_name))
class os_list(BaseModel):
win7: str = None
win8: str = None
win10: str = None
win11: str = None
class job_conf(BaseModel):
job_name: str = None
os_list: os_list
project_id: str = None
region: str = None
zone: str = None
instance_type: str = None
vpc_network: str = None
gcs_bucket: str = None
app = FastAPI()
@app.get("/jobs/{job_name}")
async def get_handle(job_name):
result = job_status_check(job_name=job_name)
return result
@app.post("/jobs/")
async def post_handle(req: job_conf):
job_name = req.job_name
os_list = req.os_list.dict()
project_id = req.project_id
region = req.region
zone = req.zone
instance_type = req.instance_type
vpc_network = req.vpc_network
gcs_bucket = req.gcs_bucket
image_list_final = {}
for image_key in os_list:
if os_list[image_key] is not None:
image_list_final[image_key] = image_list_origin[image_key]
def gcp_resource(image_list=image_list_final):
for image_key in image_list:
instance_dic = {}
instance_dic[image_key] = compute.Instance(image_key,
name = image_key,
machine_type=instance_type,
zone=zone,
boot_disk=compute.InstanceBootDiskArgs(
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
image=image_list[image_key],
),
),
metadata={
"sysprep-specialize-script-cmd": f"echo {gcs_bucket}>C:\\bucketname".format(gcs_bucket=gcs_bucket),
"windows-startup-script-url": f"gs://{gcs_bucket}/inst-py.bat".format(gcs_bucket=gcs_bucket),
},
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
network=vpc_network,
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs()],
)])
pulumi.export(f'instance-{image_key}_status'.format(image_key), instance_dic[image_key].current_status)
job_status_init(job_name=job_name)
job_create(program=gcp_resource, job_name=job_name, project_id=project_id, region=region, zone=zone)
instance_status_check(image_list=image_list_final, project_id=project_id, zone=zone)
job_stauts_update(job_name=job_name)
@app.delete("/jobs/")
async def delete_handle(req: job_conf):
job_name = req.job_name
os_list = req.os_list.dict()
project_id = req.project_id
region = req.region
zone = req.zone
instance_type = req.instance_type
vpc_network = req.vpc_network
gcs_bucket = req.gcs_bucket
image_list_final = {}
for image_key in os_list:
if os_list[image_key] is not None:
image_list_final[image_key] = os_list[image_key]
print(os_list)
print(image_list_final)
def gcp_resource(image_list=image_list_final):
for image_key in image_list:
instance_dic = {}
instance_dic[image_key] = compute.Instance(image_key,
name = image_key,
machine_type=instance_type,
zone=zone,
boot_disk=compute.InstanceBootDiskArgs(
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
image=image_list[image_key],
),
),
metadata={
"sysprep-specialize-script-cmd": f"echo {gcs_bucket}>C:\bucketname".format(gcs_bucket=gcs_bucket),
"windows-startup-script-url": f"gs://{gcs_bucket}/inst-py.bat".format(gcs_bucket=gcs_bucket),
},
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
network=vpc_network,
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs()],
)])
pulumi.export(f'instance-{image_key}_status'.format(image_key), instance_dic[image_key].current_status)
job_delete(program=gcp_resource, job_name=job_name, project_id=project_id, region=region, zone=zone)
job_status_delete(job_name=job_name)
@app.get("/jobresult/{job_name}")
async def get_handle(job_name):
result = job_result_check(job_name=job_name)
return result