-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcreate_instance.py
235 lines (183 loc) · 8.29 KB
/
create_instance.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
"""The following script:
- Starts specified instance
- watches if it has been preempted or not
- shuts down after specified timeout
- restarts in case of preemption
"""
import os
import ConfigParser
import argparse
import googleapiclient.discovery
import logging
import string
import sys
import time
from datetime import datetime, timedelta
logging.basicConfig(
level=logging.CRITICAL,
format='[%(asctime)s] %(levelname)s - %(message)s',
stream=sys.stdout
)
logger = logging.getLogger('rasp')
logger.setLevel(logging.INFO)
def get_operations(compute, project_id, zone, target_id):
return compute.zoneOperations().list(project=project_id, zone=zone, filter='targetId=%s' % target_id).execute()
def get_status(compute, project_id, name, zone):
return compute.instances().get(project=project_id, zone=zone, instance=name).execute()['status']
def stop_instance(compute, project_id, name, zone):
logger.info('Stopping instance (%s/%s/%s)', project_id, zone, name)
op = compute.instances().stop(project=project_id, zone=zone, instance=name).execute()
wait_for_operation(compute, project_id, zone, op['name'])
def start_instance(compute, project_id, name, zone):
logger.info('Starting instance (%s/%s/%s)', project_id, zone, name)
assert get_status(compute, project_id, name, zone) == 'TERMINATED', 'Instance (%s/%s/%s) is already running' % (
project_id, zone, name)
instance = compute.instances().start(project=project_id, zone=zone, instance=name).execute()
instance['startTime'] = datetime.utcnow()
wait_for_operation(compute, project_id, zone, instance['name'])
assert get_status(compute, project_id, name, zone) != 'TERMINATED', 'Instance (%s/%s/%s) must have been started' % (
project_id, zone, name)
return instance
def wait_for_termination(instance_data, compute, project_id, name, zone, timeout):
logger.info("Waiting for %s to terminate", name)
force_shutdown_time = datetime.utcnow() + timedelta(minutes=timeout)
while get_status(compute, project_id, name, zone) != 'TERMINATED':
if datetime.utcnow() > force_shutdown_time:
logger.error('Instance (%s/%s/%s) timeout!', project_id, zone, name)
stop_instance(compute, project_id, name, zone)
return False
time.sleep(60)
ops = get_operations(compute, project_id, zone, instance_data['targetId'])
ops = [o['operationType'] for o in ops['items'] if dt_parse(o['endTime']) > instance_data['startTime']]
return 'compute.instances.preempted' in ops
def wait_for_operation(compute, project, zone, operation):
logger.info('Waiting for operation %s to finish...', operation)
while True:
time.sleep(3)
result = compute.zoneOperations().get(project=project, zone=zone, operation=operation).execute()
if result['status'] == 'DONE':
if 'error' in result:
raise Exception(result['error'])
return result
def populate_template(template, params):
for k, v in params.iteritems():
template = template.replace("${%s}" % k.upper(), v)
return template
def create_instance(compute, project, zone, name, params, ssh_private_key_path, machine_type):
# Get the latest Debian Jessie image.
image_response = compute.images().getFromFamily(
project='cos-cloud', family='cos-stable').execute()
source_disk_image = image_response['selfLink']
# Configure the machine
ssh_private_key = open(
os.path.join(
os.path.dirname(__file__), ssh_private_key_path), 'r').read()
startup_script = open(
os.path.join(
os.path.dirname(__file__), 'user_data_template.yaml'), 'r').read()
startup_script = populate_template(startup_script, params)
config = {
'name': name,
'machineType': "zones/%s/machineTypes/%s" % (zone, machine_type),
# Specify the boot disk and the image to use as a source.
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
}
}
],
# Specify a network interface with NAT to access the public
# internet.
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
# Allow the instance to access cloud storage and logging.
'serviceAccounts': [{
'email': 'default',
'scopes': [
'https://www.googleapis.com/auth/devstorage.read_write',
'https://www.googleapis.com/auth/logging.write'
]
}],
# Preemptible image
'scheduling': {
'preemptible': True
},
# Metadata is readable from the instance and allows you to
# pass configuration from deployment scripts to instances.
'metadata': {
'items': [{
'key': 'user-data',
'value': startup_script
}, {
'key': 'private-key',
'value': ssh_private_key
}]
}
}
instance = compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
instance['startTime'] = datetime.utcnow()
wait_for_operation(compute, project, zone, instance['name'])
assert get_status(compute, project, name, zone) != 'TERMINATED', 'Instance (%s/%s/%s) must have been started' % (
project_id, zone, name)
return instance
def delete_instance(compute, project, zone, name):
logger.info('Deleting instance %s', name)
return compute.instances().delete(
project=project,
zone=zone,
instance=name).execute()
def main(project_id, name, zone, timeout, max_restarts, ssh_private_key_path, params, machine_type):
compute = googleapiclient.discovery.build('compute', 'v1')
instance = create_instance(compute, project_id, zone, name, params, ssh_private_key_path, machine_type)
while max_restarts > 0:
preempted = wait_for_termination(instance, compute, project_id, name, zone, timeout)
if not preempted:
logger.info('Instance (%s/%s/%s) terminated normally', project_id, zone, name)
break
else:
max_restarts -= 1
if max_restarts > 0:
logger.info('Restarting preempted instance (%s/%s/%s)', project_id, zone, name)
instance = start_instance(compute, project_id, name, zone)
else:
logger.warning('Instance (%s/%s/%s) has been preempted and will not be restarted', project_id, zone,
name)
delete_instance(compute, project_id, zone, name)
def dt_parse(t):
ret = datetime.strptime(t[:23], '%Y-%m-%dT%H:%M:%S.%f')
if t[23] == '+':
ret -= timedelta(hours=int(t[24:26]), minutes=int(t[27:]))
elif t[23] == '-':
ret += timedelta(hours=int(t[24:26]), minutes=int(t[27:]))
return ret
def str_generator(size=6, chars=string.ascii_lowercase):
return ''.join(random.choice(chars) for _ in range(size))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('config', help='Configuration file for the instance.')
parser.add_argument('day', help='Day of the forecast. 0 for today, 1 for today+1, etc.')
parser.add_argument('name', help='Instance name.')
parser.add_argument('--timeout', default='60', help='Maximum time instance allowed to run in minutes.')
parser.add_argument('--max-restarts', default='3', help='Maximum number of restarts for preempted instance.')
args = parser.parse_args()
config = ConfigParser.RawConfigParser()
config.read(args.config)
params = {
'day': args.day
}
ssh_private_key_path = config.get('Instance', 'ssh_private_key_path')
machine_type = config.get('Instance', 'machine_type')
for key in ['host', 'rasp_dir', 'rasp_name', 'docker_image']:
params[key] = config.get('Instance', key)
main(config.get('Instance', 'project_id'), args.name, config.get('Instance', 'zone'),
int(args.timeout), int(args.max_restarts), ssh_private_key_path, params, machine_type)