-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcreateRemoteClusterConnection.py
More file actions
225 lines (180 loc) · 6.46 KB
/
createRemoteClusterConnection.py
File metadata and controls
225 lines (180 loc) · 6.46 KB
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
#!/usr/bin/python3
import requests
import urllib3
import json
import time
import asyncio
import time
import os
import subprocess
import base64
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
CLUSTER_0_IP = os.environ["SOURCECLUSTER"]
CLUSTER_1_IP = os.environ["TARGETCLUSTER"]
CLUSTER_0_PATH = f"https://{CLUSTER_0_IP}/rest/v2"
CLUSTER_1_PATH = f"https://{CLUSTER_1_IP}/rest/v2"
acceptJSONHeader = {"accept": "application/json"}
contentTypeJSONHeader = {"Content-Type": "application/json"}
authString = f"{os.environ["USERNAME"]}:{os.environ["PASSWORD"]}"
basicAuthHeader = {
"Authorization": f"Basic {base64.b64encode(authString.encode("utf-8")).decode("utf-8")}"
}
nonAuthHeaders = acceptJSONHeader | contentTypeJSONHeader
defaultHeaders = nonAuthHeaders | basicAuthHeader
class WaitTimeoutError(Exception):
pass
class TaskFailedError(Exception):
def __init__(self, taskState):
self.taskState = taskState
super().__init__(f"Task {taskState['taskTag']} is in state Error")
async def waitUntilTaskComplete(basePath, taskTag, timeout=600, period=1):
if taskTag == "":
return
start = time.monotonic()
end = start + timeout
while time.monotonic() < end:
print("task wait: " + basePath + "/TaskTag")
tasks = requests.get(
basePath + "/TaskTag",
params={"taskTag": str(taskTag), "limitSize": 1},
headers=defaultHeaders,
verify=False,
)
if not tasks.ok:
raise Exception(f"Failed to fetch task: {tasks.status_code} - {tasks.text}")
tasks = tasks.json()
for task in tasks:
if task["state"] == "ERROR":
raise TaskFailedError(task)
elif task["state"] == "COMPLETE":
return
await asyncio.sleep(period)
raise WaitTimeoutError(f"Timed out waiting for task {taskTag} to complete")
async def create_remote_cluster_connection():
print("Performing Host Key Exhange")
CLUSTER_1_HOSTKEYS = requests.get(
f"{CLUSTER_1_PATH}/AuthenticationServerHostKey",
headers=defaultHeaders,
verify=False,
).json()
CLUSTER_1_HOSTKEYS = {"keys": CLUSTER_1_HOSTKEYS}
response = requests.post(
f"{CLUSTER_0_PATH}/AuthenticationHostKey/Accept",
headers=defaultHeaders,
data=json.dumps(CLUSTER_1_HOSTKEYS),
verify=False,
)
print("generating authentication key on source and accepting on target")
response = requests.post(
f"{CLUSTER_0_PATH}/AuthenticationKey",
headers=defaultHeaders,
verify=False,
)
remote_cluster_connection_create_body = {
"authInfo": {
"connectionTimeoutSeconds": 300,
"receiveTimeoutSeconds": 300,
"ipAddress": CLUSTER_1_IP,
"secureConnection": True,
},
"compression": True,
"createOptions": {"cleanupOnFailure": True},
}
print("Creating remote cluster connection")
response = requests.post(
f"{CLUSTER_0_PATH}/RemoteClusterConnection",
headers=defaultHeaders,
data=json.dumps(remote_cluster_connection_create_body),
verify=False,
)
remoteConnectionUUID = response.json()["createdUUID"]
remoteConnectionCreateTaskTag = response.json()["taskTag"]
response = requests.get(
f"{CLUSTER_0_PATH}/AuthenticationKey",
headers=defaultHeaders,
verify=False,
)
CLUSTER_0_AUTHENTICATION_KEYS = {"keys": response.json()}
response = requests.post(
f"{CLUSTER_1_PATH}/AuthenticationKey/Accept",
headers=defaultHeaders,
data=json.dumps(CLUSTER_0_AUTHENTICATION_KEYS),
verify=False,
)
await waitUntilTaskComplete(
CLUSTER_0_PATH, remoteConnectionCreateTaskTag, timeout=600
)
print("Remote Cluster Connection initialization Complete")
blockDevs = [
{"type": "IDE_CDROM", "capacity": 10**9},
{"type": "VIRTIO_DISK", "capacity": 10**9},
]
vmDesc = {
"name": "Grey Goo",
"description": "I exist to replicate",
"numVCPU": 2,
"mem": 8675309000,
"blockDevs": blockDevs,
}
vm_json = {"dom": vmDesc, "options": {"attachGuestToolsISO": True}}
print("creating vm for replication")
response = requests.post(
f"{CLUSTER_0_PATH}/VirDomain",
headers=defaultHeaders,
verify=False,
data=json.dumps(vm_json),
)
vmUUID = response.json().get("createdUUID")
vmCreateTaskTag = response.json().get("taskTag")
await waitUntilTaskComplete(CLUSTER_0_PATH, vmCreateTaskTag)
print("Setting up replication for VM")
replicationSetupJSON = {
"connectionUUID": remoteConnectionUUID,
"enable": True,
"label": "Grey Goo Replication",
"sourceDomainUUID": vmUUID,
}
replicationSetupResponse = requests.post(
f"{CLUSTER_0_PATH}/VirDomainReplication",
headers=defaultHeaders,
verify=False,
data=json.dumps(replicationSetupJSON),
)
await waitUntilTaskComplete(
CLUSTER_0_PATH, replicationSetupResponse.json()["taskTag"]
)
print("taking snapshot with replication enabled")
snapInfo = {"domainUUID": vmUUID, "label": "Track the spread of the grey goo"}
snapResp = requests.post(
f"{CLUSTER_0_PATH}/VirDomainSnapshot",
headers=defaultHeaders,
verify=False,
data=json.dumps(snapInfo),
)
snapResp.raise_for_status()
await waitUntilTaskComplete(CLUSTER_0_PATH, snapResp.json()["taskTag"])
cleanup = False
if cleanup:
print("Deleting remote cluster connection")
response = requests.delete(
f"{CLUSTER_0_PATH}/RemoteClusterConnection/{remoteConnectionUUID}",
headers=defaultHeaders,
verify=False,
)
await waitUntilTaskComplete(CLUSTER_0_PATH, response.json()["taskTag"])
print("deleting vm")
response = requests.delete(
f"{CLUSTER_0_PATH}/VirDomain/{vmUUID}",
headers=defaultHeaders,
verify=False,
)
response.raise_for_status()
await waitUntilTaskComplete(CLUSTER_0_PATH, response.json()["taskTag"])
async def main():
try:
print("Running create_remote_cluster_connection")
await asyncio.wait_for(create_remote_cluster_connection(), timeout=600)
except asyncio.TimeoutError:
print("create_remote_cluster_connection timed out!")
if __name__ == "__main__":
asyncio.run(main())