forked from alxhlz/hcloud-failover-keepalived
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhcloud_failover.py
More file actions
executable file
·60 lines (44 loc) · 1.9 KB
/
Copy pathhcloud_failover.py
File metadata and controls
executable file
·60 lines (44 loc) · 1.9 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
#!/usr/bin/env python3
# (c) 2018 Maximilian Siegl
import sys
import json
import os
import requests
from multiprocessing import Process
CONFIG_PATH = os.path.join(os.path.abspath(
os.path.dirname(__file__)), "config.json")
def del_ip(ip_bin_path, floating_ip, interface):
os.system(ip_bin_path + " addr del " + floating_ip + " dev " + interface)
def add_ip(ip_bin_path, floating_ip, interface):
os.system(ip_bin_path + " addr add " + floating_ip + " dev " + interface)
def change_request(endstate, url, header, payload, ip_bin_path, floating_ip, interface):
if endstate == "BACKUP":
del_ip(ip_bin_path, floating_ip, interface)
elif endstate == "FAULT":
del_ip(ip_bin_path, floating_ip, interface)
elif endstate == "MASTER":
add_ip(ip_bin_path, floating_ip, interface)
print("Post request to: " + url)
print("Header: " + str(header))
print("Data: " + str(payload))
r = requests.post(url, data=payload, headers=header)
print("Response:")
print(r.status_code, r.reason)
print(r.text)
else:
print("Error: Endstate not defined!")
def main(arg_type, arg_name, arg_endstate):
with open(CONFIG_PATH, "r") as config_file:
config = json.load(config_file)
header = {
"Content-Type": "application/json",
"Authorization": "Bearer " + config["api-token"]
}
payload = '''{"server": ''' + str(config["server-id"]) + "}"
print("Perform action for transition to " + arg_endstate + " state")
for ips in config["ips"]:
url = config["url"].format(ips["floating-ip-id"])
Process(target=change_request, args=(arg_endstate, url, header, payload,
config["ip_bin_path"], ips["floating-ip"], config["interface"])).start()
if __name__ == "__main__":
main(arg_type=sys.argv[1], arg_name=sys.argv[2], arg_endstate=sys.argv[3])