-
Notifications
You must be signed in to change notification settings - Fork 6
/
ciclic
executable file
·131 lines (95 loc) · 2.81 KB
/
ciclic
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
#!/usr/bin/env python
import os
import sys
import argh
import requests
from argh.decorators import named
try:
from config import *
except ImportError:
PORT="4242"
DOMAIN = "localhost:" + str(PORT)
def request_api(path, domain, verb, data={}, check_return_code=True):
assert verb in ("get", "post", "put", "delete")
https = False
if domain.split(":")[0] not in ("localhost", "127.0.0.1", "0.0.0.0"):
https = True
response = getattr(requests, verb)(
"http%s://%s/api/%s" % ("s" if https else "", domain, path),
headers={"X-Token": open(".admin_token", "r").read().strip()},
json=data,
)
if response.status_code == 403:
print(f"Error: access refused because '{response.json()['status']}'")
sys.exit(1)
if check_return_code:
# TODO: real error message
assert response.status_code == 200, response.content
return response
def shell():
import ipdb; ipdb.set_trace()
def add(name, url_or_path, domain=DOMAIN):
request_api(
path="job",
verb="post",
domain=domain,
data={
"name": name,
"url_or_path": url_or_path,
},
)
@named("list")
def list_(all=False, domain=DOMAIN):
response = request_api(
path="job",
verb="get",
domain=domain,
data={
"all": all,
},
)
for i in response.json():
print(f"{i['id']:4d} - {i['name']} [{i['state']}]")
def app_list(all=False, domain=DOMAIN):
response = request_api(
path="app",
verb="get",
domain=domain,
)
for i in response.json():
print(f"{i['name']} - {i['url']}")
def delete(job_id, domain=DOMAIN):
response = request_api(
path=f"job/{job_id}",
verb="delete",
domain=domain,
check_return_code=False
)
if response.status_code == 404:
print(f"Error: no job with the id '{job_id}'")
sys.exit(1)
assert response.status_code == 200, response.content
def stop(job_id, domain=DOMAIN):
response = request_api(
path=f"job/{job_id}/stop",
verb="post",
domain=domain,
check_return_code=False
)
if response.status_code == 404:
print(f"Error: no job with the id '{job_id}'")
sys.exit(1)
assert response.status_code == 200, response.content
def restart(job_id, domain=DOMAIN):
response = request_api(
path=f"job/{job_id}/restart",
verb="post",
domain=domain,
check_return_code=False
)
if response.status_code == 404:
print(f"Error: no job with the id '{job_id}'")
sys.exit(1)
assert response.status_code == 200, response.content
if __name__ == '__main__':
argh.dispatch_commands([add, list_, delete, stop, restart, app_list, shell])