diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-08-18 13:09:29 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2020-02-17 17:31:09 +0100 |
commit | 367c391a61091817cf34475f91a23ca286eec334 (patch) | |
tree | 616ee3d89d031032681844f5825c9d41eec8e9c0 /python | |
parent | 64d41da73cce0005757ca0f7dd88151f9ba5cf4a (diff) | |
download | Puppet-367c391a61091817cf34475f91a23ca286eec334.tar.gz Puppet-367c391a61091817cf34475f91a23ca286eec334.tar.zst Puppet-367c391a61091817cf34475f91a23ca286eec334.zip |
Add hetzner server scripts
Diffstat (limited to 'python')
-rw-r--r-- | python/get_initial_configuration_hetzner_server.py | 21 | ||||
-rw-r--r-- | python/hetzner_helper.py | 59 | ||||
-rw-r--r-- | python/list_servers.py | 6 | ||||
-rw-r--r-- | python/reboot_hetzner_server.py | 18 |
4 files changed, 104 insertions, 0 deletions
diff --git a/python/get_initial_configuration_hetzner_server.py b/python/get_initial_configuration_hetzner_server.py new file mode 100644 index 0000000..71583ff --- /dev/null +++ b/python/get_initial_configuration_hetzner_server.py | |||
@@ -0,0 +1,21 @@ | |||
1 | import sys | ||
2 | import json | ||
3 | import hetzner_helper | ||
4 | |||
5 | instance = sys.argv[-1] | ||
6 | instance = hetzner_helper.get("servers/{}".format(instance))[1]["server"] | ||
7 | |||
8 | infos = {} | ||
9 | infos["ips"] = { | ||
10 | "v4": { | ||
11 | "ipAddress": instance["public_net"]["ipv4"]["ip"], | ||
12 | "gateway": "172.31.1.1", | ||
13 | }, | ||
14 | "v6": { | ||
15 | "ipAddress": instance["public_net"]["ipv6"]["ip"].split("/")[0], | ||
16 | "gateway": "fe80::1", | ||
17 | "mask": instance["public_net"]["ipv6"]["ip"].split("/")[1], | ||
18 | } | ||
19 | } | ||
20 | |||
21 | print(json.dumps(infos)) | ||
diff --git a/python/hetzner_helper.py b/python/hetzner_helper.py new file mode 100644 index 0000000..496e14d --- /dev/null +++ b/python/hetzner_helper.py | |||
@@ -0,0 +1,59 @@ | |||
1 | import json | ||
2 | import requests | ||
3 | import os | ||
4 | |||
5 | from configparser import RawConfigParser, NoSectionError, NoOptionError | ||
6 | |||
7 | class AuthenticationException(Exception): | ||
8 | pass | ||
9 | |||
10 | class RateLimitExceeded(Exception): | ||
11 | pass | ||
12 | |||
13 | class InternalServer(Exception): | ||
14 | pass | ||
15 | |||
16 | class HetznerConfig: | ||
17 | def __init__(self): | ||
18 | config = RawConfigParser() | ||
19 | config.read([os.path.expanduser('~/.hetzner.conf')]) | ||
20 | |||
21 | self.api_key = config.get("default", "api_key") | ||
22 | |||
23 | config = HetznerConfig() | ||
24 | |||
25 | def call(endpoint, url_params=None, body=None, method="GET"): | ||
26 | api = "https://api.hetzner.cloud/v1/{}".format(endpoint) | ||
27 | headers = {"Authorization": "Bearer {}".format(config.api_key)} | ||
28 | data = json.dumps(body) if body is not None else None | ||
29 | |||
30 | if method == "GET": | ||
31 | request = requests.get(api, headers=headers, params=url_params) | ||
32 | elif method == "POST" or (method == "GET" and body is not None): | ||
33 | request = requests.post(api, data=data, headers=headers, params=url_params) | ||
34 | elif method == "DELETE": | ||
35 | request = requests.delete(api, headers=headers) | ||
36 | elif method == "PUT": | ||
37 | request = requests.put(api, headers=headers, data=data) | ||
38 | |||
39 | if request.status_code == 401 or request.status_code == 403: | ||
40 | raise AuthenticationException() | ||
41 | |||
42 | if request.status_code == 429: | ||
43 | raise RateLimitExceeded() | ||
44 | |||
45 | if request.status_code == 500: | ||
46 | raise InternalServer(request.text) | ||
47 | |||
48 | if not request.text: | ||
49 | return request.status_code, "" | ||
50 | |||
51 | js = request.json() | ||
52 | |||
53 | return request.status_code, js | ||
54 | |||
55 | def get(*args, **kwargs): | ||
56 | return call(*args, method="GET", **kwargs) | ||
57 | |||
58 | def post(*args, **kwargs): | ||
59 | return call(*args, method="POST", **kwargs) | ||
diff --git a/python/list_servers.py b/python/list_servers.py index e7bd2af..6d22a56 100644 --- a/python/list_servers.py +++ b/python/list_servers.py | |||
@@ -21,3 +21,9 @@ print("OVH VPS SSD servers:") | |||
21 | for vps in vps_list: | 21 | for vps in vps_list: |
22 | print("\t{}".format(vps)) | 22 | print("\t{}".format(vps)) |
23 | 23 | ||
24 | import hetzner_helper | ||
25 | |||
26 | print("Hetzner VPS servers:") | ||
27 | return_code, json = hetzner_helper.call("servers") | ||
28 | for server in json["servers"]: | ||
29 | print("\t{}: {}".format(server["name"], server["id"])) | ||
diff --git a/python/reboot_hetzner_server.py b/python/reboot_hetzner_server.py new file mode 100644 index 0000000..7452afe --- /dev/null +++ b/python/reboot_hetzner_server.py | |||
@@ -0,0 +1,18 @@ | |||
1 | import sys | ||
2 | import hetzner_helper | ||
3 | |||
4 | instance = sys.argv[-1] | ||
5 | actions = [] | ||
6 | if "--rescue" in sys.argv: | ||
7 | actions.append("enable_rescue") | ||
8 | elif "--local" in sys.argv: | ||
9 | actions.append("disable_rescue") | ||
10 | |||
11 | if "--hard" in sys.argv: | ||
12 | actions.append("reset") | ||
13 | else: | ||
14 | actions.append("reboot") | ||
15 | |||
16 | for action in actions: | ||
17 | result = hetzner_helper.post("servers/{}/actions/{}".format(instance, action)) | ||
18 | print(result) | ||