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
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
from utils import make_request, make_ws_request, wait_on_job
from config import BRICK_PATH, CLUSTER_INFO, DATASET_HIERARCHY
from exceptions import JobTimeOut
JOB_TIMEOUT = 120 # number of seconds to wait on a job to complete
def setup_zpool_and_datasets(ip):
result = {'ERROR': ''}
# query for existing zpool (clean CI run creates a zpool)
print(f'Checking for existing zpools on {ip}')
url = f'http://{ip}/api/v2.0/pool'
ans = make_request('get', url)
if ans.status_code != 200:
result['ERROR'] = f'Invalid status code when checking for existing zpools: {ans.text}'
return result
# get the id of the existing pool and export it
pool = ans.json()
pool = pool[0] if pool else None
if pool:
url = f'http://{ip}/api/v2.0/pool/id/{pool["id"]}/export'
ans = make_request('post', url)
if ans.status_code != 200:
result['ERROR'] = f'Invalid status code when exporting "{pool["name"]}" on {ip}: {ans.text}'
return result
try:
print(f'Waiting on "{pool["name"]}" to be exported on {ip}')
status = wait_on_job(ans.json(), ip, JOB_TIMEOUT)
except JobTimeOut:
result['ERROR'] = f'Timed out waiting on "{pool["name"]}" to be exported on {ip}'
return result
else:
if status['state'] != 'SUCCESS':
result['ERROR'] = f'Exporting "{pool["name"]}" failed on {ip}'
return result
# wipe the disks to clean any remnants of previous zpools
print(f'Wiping "{CLUSTER_INFO["ZPOOL_DISK"]}" on {ip}')
url = f'http://{ip}/api/v2.0/disk/wipe'
payload = {'dev': CLUSTER_INFO['ZPOOL_DISK'], 'mode': 'QUICK'}
ans = make_request('post', url, data=payload)
if ans.status_code != 200:
result['ERROR'] = f'Invalid status code when wiping disk: {ans.status_code}:{ans.text}'
return result
try:
print(f'Waiting for disk "{CLUSTER_INFO["ZPOOL_DISK"]}" on {ip} to be wiped')
status = wait_on_job(ans.json(), ip, JOB_TIMEOUT)
except JobTimeOut:
result['ERROR'] = f'Timed out waiting for disk to be wiped on {ip}'
return result
else:
if status['state'] != 'SUCCESS':
result['ERROR'] = 'Wiping disk {CLUSTER_INFO["ZPOOL_DISK"]} failed on {ip}'
return result
# now create the zpool
print(f'Creating zpool "{CLUSTER_INFO["ZPOOL"]}" on {ip}')
url = f'http://{ip}/api/v2.0/pool'
payload = {
'name': CLUSTER_INFO['ZPOOL'],
'encryption': False,
'topology': {'data': [{'type': 'STRIPE', 'disks': [CLUSTER_INFO['ZPOOL_DISK']]}]},
'allow_duplicate_serials': True,
}
ans = make_request('post', url, data=payload)
if ans.status_code != 200:
result['ERROR'] = f'Failed to create zpool: "{CLUSTER_INFO["ZPOOL"]}" on {ip}:{ans.text}'
return result
try:
print(f'Waiting on zpool "{CLUSTER_INFO["ZPOOL"]}" to be created on {ip}')
status = wait_on_job(ans.json(), ip, JOB_TIMEOUT)
except JobTimeOut:
result['ERROR'] = f'Timed out waiting on zpool to be created on {ip}'
return result
else:
if status['state'] != 'SUCCESS':
result['ERROR'] = f'Creating zpool was a failure: {status["result"]} on {ip}'
return result
# now create the cluster datasets, we have to use websocket here
# because we're creating "internal" datasets that we prevent any
# normal API user from creating using the public API so use websocket
# API to side-step the public API validation
print(f'Creating dataset hierarchy "{DATASET_HIERARCHY}" on {ip}')
payload = {
'msg': 'method',
'method': 'zfs.dataset.create',
'params': [{
'name': DATASET_HIERARCHY,
'type': 'FILESYSTEM',
'create_ancestors': True,
'properties': {'acltype': 'posix'}
}]
}
res = make_ws_request(ip, payload)
if res.get('error', {}):
result['ERROR'] = res['error'].get('reason', 'NO REASON GIVEN')
# libzfs doesn't mount the youngest ancestor when you give it a
# path of ancestors to be created all at once. This means we have
# mount the youngest ancestor
# i.e. cargo/.glusterfs/gvol01/brick0 (brick0 needs to be mounted)
print(f'Mounting dataset hierarchy "{DATASET_HIERARCHY}" on {ip}')
payload = {
'msg': 'method',
'method': 'zfs.dataset.mount',
'params': [DATASET_HIERARCHY],
}
res = make_ws_request(ip, payload)
if res.get('error', {}):
result['ERROR'] = res['error'].get('reason', 'NO REASON GIVEN')
print(f'Creating brick directory "{BRICK_PATH}" on {ip}')
payload = {
'msg': 'method',
'method': 'filesystem.mkdir',
'params': [BRICK_PATH],
}
res = make_ws_request(ip, payload)
if res.get('error', {}):
result['ERROR'] = res['error'].get('reason', 'NO REASON GIVEN')
return result
def setup_network(ip):
# the cluster nodes are assigned an IP
# address via DHCP reservations, however,
# it's a prerequisite that the peers in
# the cluster have static IP addresses
result = {'ERROR': ''}
# setup router/dns/defgw first
print(f'Setting up default gateway and dns on {ip}')
url = f'http://{ip}/api/v2.0/network/configuration'
payload = {'ipv4gateway': CLUSTER_INFO['DEFGW'], 'nameserver1': CLUSTER_INFO['DNS1']}
ans = make_request('put', url, data=payload)
if ans.status_code != 200:
result['ERROR'] = f'Failed to configure gateway on {ip}:{ans.text}'
return result
# setup the static IP address
print(f'Setting up static ip on {ip}')
url = f'http://{ip}/api/v2.0/interface/id/{CLUSTER_INFO["INTERFACE"]}'
payload = {
'ipv4_dhcp': False,
'aliases': [{
'address': ip,
'netmask': CLUSTER_INFO['NETMASK'],
}],
}
ans = make_request('put', url, data=payload)
if ans.status_code != 200:
result['ERROR'] = f'Failed to configure static IP information for {ip}:{ans.text}'
return result
# commit changes
print(f'Commit network changes on {ip}')
url = f'http://{ip}/api/v2.0/interface/commit'
payload = {'rollback': True, 'checkin_timeout': 5}
ans = make_request('post', url, data=payload)
if ans.status_code != 200:
result['ERROR'] = f'Failed to commit static IP information for {ip}:{ans.text}'
return result
# checkin the changes (finalize them)
print(f'Checkin network changes on {ip}')
url = f'http://{ip}/api/v2.0/interface/checkin'
ans = make_request('get', url)
if ans.status_code != 200:
result['ERROR'] = 'Failed to commit static IP information for {ip}:{ans.text}'
return result
print(f'Allowing root SSH {ip}')
res = make_ws_request(ip, {
'msg': 'method',
'method': 'user.update',
'params': [1, {'ssh_password_enabled': True}],
})
if res.get('error', {}):
result['ERROR'] = res['error'].get('reason', 'root user update failed')
return result
print(f'Enabling SSH on boot {ip}')
res = make_ws_request(ip, {
'msg': 'method',
'method': 'service.update',
'params': ['ssh', {'enable': True}]
})
if res.get('error', {}):
result['ERROR'] = res['error'].get('reason', 'SSH enable failed')
return result
print(f'Starting SSH {ip}')
res = make_ws_request(ip, {
'msg': 'method',
'method': 'service.start',
'params': ['ssh', {'silent': False}]
})
if res.get('error', {}):
result['ERROR'] = res['error'].get('reason', 'SSH start failed')
return result
return result
def get_os_version(ip):
# the os versions for all peers need to match
# or we're going to have a bad time
result = {'IP': ip, 'ERROR': '', 'VERS': ''}
print(f'Getting OS version on {ip}')
ans = make_request('get', f'http://{ip}/api/v2.0/system/version')
if ans.status_code == 200:
result['VERS'] = ans.text
else:
result['ERROR'] = f'Got invalid status_code: {ans.status_code} for IP: {ip}'
return result
def init():
with ThreadPoolExecutor() as exc:
nodes_ip_keys = ('NODE_A_IP', 'NODE_B_IP', 'NODE_C_IP', 'NODE_D_IP')
ips = [v for k, v in CLUSTER_INFO.items() if k in nodes_ip_keys]
errors = []
# First, setup the network
futures = [exc.submit(setup_network, ip) for ip in ips]
for fut in as_completed(futures):
res = fut.result()
errors.append(res['ERROR']) if res['ERROR'] else None
if errors:
for error in errors:
# means the network setup failed on at least
# one of the peers
print(error)
sys.exit(2)
# Second, verify the OS versions match
futures = [exc.submit(get_os_version, ip) for ip in ips]
versions = set()
for fut in as_completed(futures):
res = fut.result()
if res['ERROR']:
errors.append(res['ERROR'])
else:
versions.add((res['IP'], res['VERS']))
if errors:
for error in errors:
# means unable to determine OS version betwen all peers
print(error)
sys.exit(2)
elif len(set([i[1] for i in versions])) > 1:
# means OS versions do not match between all peers
print(f'Version of software installed on each peer is not the same: {versions}')
sys.exit(2)
# Finally, setup the zpools and datasets
futures = [exc.submit(setup_zpool_and_datasets, ip) for ip in ips]
for fut in as_completed(futures):
res = fut.result()
errors.append(res['ERROR']) if res['ERROR'] else None
if errors:
for error in errors:
# means the zpool or cluster datasets failed to be setup
print(error)
sys.exit(2)