-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·148 lines (134 loc) · 5.16 KB
/
cli.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
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
#!/usr/bin/env python3
from lib import *
__author__ = "Samuel Monroe & Rémy Voet"
@click.group()
def main():
"""
Simple Splay CLI for querying the Backend Service
"""
pass
@main.command()
@click.argument('username', nargs=1)
@click.argument('password', nargs=1)
def start_session(username, password):
"""Init a session by providing <username> and <password>"""
endpoint = BASE_URL + 'sessions'
data = request_body('session', {'username': username, 'password': password})
response = requests.post(endpoint, data=json.dumps(data))
check_response(response)
store_session(response.json()['token'])
@main.command()
def list_users():
"""List Splay Users, must be admin and started a session."""
endpoint = BASE_URL + 'users'
headers = authentified_headers()
response = requests.get(endpoint, headers=headers)
check_response(response)
click.echo(response.json()['users'])
@main.command()
@click.argument('username', nargs=1)
@click.argument('email', nargs=1)
@click.argument('password', nargs=1)
@click.argument('password_conf', nargs=1)
def new_user(username, email, password, password_conf):
"""Creates user with <username>, <email>, <password> and <password_conf>"""
endpoint = BASE_URL + 'users'
# headers = authentified_headers()
data = request_body('user', {
'username': username, 'password': password, 'email': email, 'password_confirmation': password_conf
})
response = requests.post(endpoint, data=json.dumps(data))
check_response(response)
store_session(response.json()['token'])
@main.command()
@click.argument('user_id', nargs=1)
def remove_user(user_id):
"""Removes user referred by <user_id>, admin only."""
endpoint = BASE_URL + 'users/' + user_id
headers = authentified_headers()
response = requests.delete(endpoint, headers=headers)
check_response(response)
click.echo(response.json())
@main.command()
def list_splayds():
"""List Splay Daemons, called Splayds"""
endpoint = BASE_URL + 'splayds'
headers = authentified_headers()
response = requests.get(endpoint, headers=headers)
check_response(response)
for item in response.json()['splayds']:
click.echo("* [Splayd id : " + str(item['id']) + "]")
click.echo("\t # IP " + item['ip'])
click.echo("\t # Status " + item['status'])
click.echo("\t # Key " + item['key'])
@main.command()
def list_jobs():
"""List Splay Jobs"""
endpoint = BASE_URL + 'jobs'
headers = authentified_headers()
response = requests.get(endpoint, headers=headers)
check_response(response)
click.echo(response.json())
@main.command()
@click.argument('job_id', nargs=1)
def kill_job(job_id):
"""Kill Splay Job referred by <job_id>"""
endpoint = BASE_URL + 'jobs/' + job_id
headers = authentified_headers()
response = requests.delete(endpoint, headers=headers)
check_response(response)
click.echo("Success kill")
@main.command()
@click.argument('job_id', nargs=1)
def get_job_code(job_id):
"""Get Splay Job Code referred by <job_id>"""
endpoint = BASE_URL + 'jobs/' + job_id
headers = authentified_headers()
response = requests.get(endpoint, headers=headers)
check_response(response)
click.echo(response.json()['job']['code'])
@main.command()
@click.argument('job_id', nargs=1)
def get_job_details(job_id):
"""Get Splay Job details referred by <job_id>"""
endpoint = BASE_URL + 'jobs/' + job_id
headers = authentified_headers()
response = requests.get(endpoint, headers=headers)
check_response(response)
click.echo(response.json()['job'])
@main.command()
@click.option('--name', '-n', help='Name of the job.')
@click.option('--description', '-d', help='Description of the job.')
@click.option('--nb_splayds', '-s', default=1, help='Number of splayds.')
@click.option('--topo_filename', '-t', help='Xml topology filename')
@click.argument('filename', nargs=1)
def submit_job(name, description, nb_splayds, topo_filename, filename):
"""Submit new job, specifying a name of lua code file and optional args."""
endpoint = BASE_URL + 'jobs'
headers = authentified_headers()
print("Submit new job, specif")
data = request_body('user', { 'code': fetch_file(filename) })
if name:
data['data']['attributes']['name'] = name
if description:
data['data']['attributes']['description'] = description
if nb_splayds:
data['data']['attributes']['nb_splayds'] = nb_splayds
if topo_filename:
data['data']['attributes']['topology'] = fetch_file(topo_filename)
response = requests.post(endpoint, headers=headers, data=json.dumps(data))
check_response(response)
click.echo("Job submitted")
click.echo("Job ID : " + str(response.json()['job']['id']))
click.echo("Job reference : " + response.json()['job']['ref'])
@main.command()
@click.argument('job_id', nargs=1)
def get_job_logs(job_id):
"""Get Splay Job logs referred by <job_id>"""
endpoint = BASE_URL + 'logs/' + job_id
headers = authentified_headers()
response = requests.get(endpoint, headers=headers)
check_response(response)
click.echo(response.json()['logs'])
if __name__ == "__main__":
main()