This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.py
100 lines (98 loc) · 4.33 KB
/
worker.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
import json
import random
import string
_IMAGE = open("image.txt", "r").read().strip()
def generate_config(context):
zone = context.properties["zone"]
region = zone.rsplit("-", 1)[0]
controller_deployment = context.properties["controller-deployment-name"]
deployment = context.env["deployment"]
network_name = controller_deployment + "-network"
instance_template_id = "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(4))
instance_template_name = deployment + "-template-" + instance_template_id
instance_group_manager_name = deployment + "-group"
return {
"resources": [
# Worker Group Instance Template
{
"name": instance_template_name,
"type": "compute.v1.instanceTemplate",
"properties": {
"properties": {
"zone": zone,
"machineType": context.properties["virtual-machine-size"],
"disks": [
{
"deviceName": "boot",
"type": "PERSISTENT",
"boot": True,
"autoDelete": True,
"initializeParams": {
"diskType": "pd-ssd",
"sourceImage": _IMAGE,
},
},
],
"tags": {
"items": [
"lgtm-worker",
],
},
"networkInterfaces": [
{
"network": "projects/" + context.env["project"] + "/global/networks/" + network_name,
"accessConfigs": [
{
"type": "ONE_TO_ONE_NAT",
},
],
},
],
"metadata": {
"items": [
{
"key": "controller-hostname",
"value": controller_deployment + "." + zone + ".c." + context.env["project"] + ".internal",
},
{
"key": "worker-credentials",
"value": context.properties["worker-credentials"],
},
{
"key": "n-general",
"value": context.properties["general-workers"],
},
{
"key": "n-on-demand",
"value": context.properties["on-demand-workers"],
},
{
"key": "n-query",
"value": context.properties["query-workers"],
},
{
"key": "environment",
"value": json.dumps(context.properties["worker-environment"]),
},
{
"key": "manifest-password",
"value": context.properties["manifest-password"],
},
],
},
},
},
},
# Worker Group Instance Group Manager
{
"name": instance_group_manager_name,
"type": "compute.v1.instanceGroupManager",
"properties": {
"zone": zone,
"baseInstanceName": deployment,
"instanceTemplate": "$(ref." + instance_template_name + ".selfLink)",
"targetSize": context.properties["copies"],
},
},
],
}