/
lib_host_gcp.py
210 lines (183 loc) · 6.86 KB
/
lib_host_gcp.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
import json
import logging
import os
from asyncio import get_running_loop
from functools import partial
from pathlib import Path
from libcloud.common.google import (
ResourceNotFoundError, ResourceExistsError, ResourceInUseError,
InvalidRequestError
)
from libcloud.compute.drivers.gce import GCENodeDriver
from libcloud.dns.drivers.google import GoogleDNSDriver
from libcloud.dns.types import RecordDoesNotExistError
from schema import Schema, SchemaError, Use, And, Or, Optional as Opt
from lib_host import HostClean
from lib_util import run_async, read_json
class HostGCP():
def __init__(self, config, gcp_credentials_file):
self.config = config
creds = read_json(gcp_credentials_file)
self._compute = GCENodeDriver(
creds['client_email'],
gcp_credentials_file,
project=creds['project_id'],
datacenter=self.config['gcp_compute_zone'],
timeout=self.config['compute_timeout']
)
self._dns = GoogleDNSDriver(
creds['client_email'],
gcp_credentials_file,
project=creds['project_id']
)
self._dns_zone = self._dns.get_zone(config['gcp_dns_zone'])
self.log = logging.getLogger(__name__ + '.' + self._name)
@property
def _name(self):
return self.config['resources_name']
async def start(self):
self.log.info('start() begins')
try:
await self._start()
except:
self.log.exception('start() failed')
raise
self.log.info('start() finished')
async def _start(self):
try:
self.log.info('Creating external static IP address')
addr = await run_async(self._compute.ex_get_address, self._name)
self.log.info('Exists')
except ResourceNotFoundError:
addr = await run_async(self._compute.ex_create_address, self._name)
self.log.info('Created')
self.log.info('External static IP address: %s', addr.address)
try:
self.log.info('Creating DNS record: %s', self.config['hostname'])
await run_async(
self._dns.create_record, self.config['hostname'],
self._dns_zone, 'A', {
'ttl': self.config['hostname_ttl'],
'rrdatas': [addr.address]
}
)
self.log.info('Created')
except ResourceExistsError:
self.log.info('Exists')
try:
self.log.info('Creating data disk')
data_disk_name = self._name + '-data'
data_disk = await run_async(
self._compute.ex_get_volume, data_disk_name
)
self.log.info('Exists')
except ResourceNotFoundError:
data_disk = await run_async(
self._compute.create_volume,
self.config['data_disk_size'],
data_disk_name,
ex_disk_type=(
'pd-ssd' if self.config['data_disk_ssd'] else 'pd-standard'
)
)
self.log.info('Created')
self.log.info(
'Data disk: %sGB %s', data_disk.size, data_disk.extra['type']
)
try:
self.log.info('Creating host')
host = await run_async(self._compute.ex_get_node, self._name)
self.log.info('Exists')
except ResourceNotFoundError:
host = await run_async(
self._compute.create_node,
self._name,
location=self.config['gcp_compute_zone'],
size=self.config['machine_type'],
image=self.config['boot_image'],
external_ip=addr,
ex_network=self.config['gcp_compute_net'],
ex_subnetwork=self.config['gcp_compute_subnet'],
)
self.log.info('Created')
self.log.info('Host: %s %s', host.extra['image'], host.size)
try:
self.log.info('Attaching data disk')
await run_async(
self._compute.attach_volume,
host,
data_disk,
ex_auto_delete=False
)
self.log.info('Attached')
except ResourceInUseError:
self.log.info('Already attached')
self.log.info('Setting host tags')
await run_async(
self._compute.ex_set_node_tags, host,
self.config['gcp_compute_tags']
)
self.log.info('Setting host metadata')
await run_async(
self._compute.ex_set_node_metadata, host,
self.config['host_metadata']
)
self.log.info('Starting host')
await run_async(self._compute.ex_start_node, host)
self.log.info('Started')
async def stop(self, clean=HostClean.STOP) -> None:
self.log.info('stop(clean=%s) begins', clean.name)
try:
await self._stop(clean)
except:
self.log.exception('stop() failed')
raise
self.log.info('stop() finished')
async def _stop(self, clean) -> None:
try:
self.log.info('Stopping host')
host = await run_async(
self._compute.ex_get_node, self._name,
self.config['gcp_compute_zone']
)
await run_async(self._compute.ex_stop_node, host)
self.log.info('Stopped')
if clean <= HostClean.STOP:
return
self.log.info('Removing host')
await run_async(
self._compute.destroy_node, host, destroy_boot_disk=True
)
self.log.info('Removed')
except ResourceNotFoundError:
self.log.info('Not present')
if clean <= HostClean.HOST:
return
try:
self.log.info('Removing data disk')
data_disk_name = self._name + '-data'
data_disk = await run_async(
self._compute.ex_get_volume, data_disk_name
)
await run_async(self._compute.destroy_volume, data_disk)
self.log.info('Removed')
except ResourceNotFoundError:
self.log.info('Not present')
if clean <= HostClean.DATA:
return
try:
self.log.info('Removing DNS record')
record = await run_async(
self._dns.get_record, self._dns_zone.id,
'A:' + self.config['hostname']
)
await run_async(self._dns.delete_record, record)
self.log.info('Removed')
except RecordDoesNotExistError:
self.log.info('Not present')
try:
self.log.info('Removing external static IP address')
await run_async(self._compute.ex_destroy_address, self._name)
self.log.info('Removed')
except ResourceNotFoundError:
self.log.info('Not present')