-
Notifications
You must be signed in to change notification settings - Fork 0
/
hetzner_failover.py
31 lines (27 loc) · 1.67 KB
/
hetzner_failover.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
import requests
import json
class HetznerFailover():
def __init__(self,failoverIp,login,password):
self.failoverIp = failoverIp
self.login = login
self.password = password
def getActiveServer(self):
response = requests.get('https://robot-ws.your-server.de/failover/{}'.format(self.failoverIp), auth=(self.login,self.password))
responseContent = json.loads(response.content.decode())
if response.status_code == 401 or response.status_code == 404:
raise RuntimeError('{}: {}'.format(responseContent['error']['code'],responseContent['error']['message']))
return responseContent['failover']['active_server_ip']
def setActiveServer(self, ip):
response = requests.post('https://robot-ws.your-server.de/failover/{}'.format(self.failoverIp), auth=(self.login,self.password), data={'active_server_ip': '{}'.format(ip)})
responseContent = json.loads(response.content.decode())
if response.status_code == 400 or response.status_code == 401 or response.status_code == 404 or response.status_code == 500:
raise RuntimeError('{}: {}'.format(responseContent['error']['code'],responseContent['error']['message']))
elif response.status_code == 409:
if responseContent['error']['code'] == 'FAILOVER_LOCKED':
raise RuntimeError('{}: {}'.format(responseContent['error']['code'],responseContent['error']['message']))
elif responseContent['error']['code'] == 'FAILOVER_ALREADY_ROUTED':
return True
if responseContent['failover']['active_server_ip'] == ip:
return True
else:
return False