-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
180 lines (146 loc) · 4.89 KB
/
client.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import click
import requests
import os
"""
User interface
"""
SERVICE_URL = "http://localhost:8080"
token_file = "token.txt"
def get_token():
try:
with open(token_file, "r") as file:
return file.read().strip()
except FileNotFoundError:
click.echo("Token file not found.")
return None
def save_token(token):
with open(token_file, "w") as file:
file.write(token)
def delete_token():
try:
os.remove(token_file)
except FileNotFoundError:
pass
# Jobs command group
@click.group()
def jobs():
"""Manage jobs."""
pass
@jobs.command()
@click.argument('input_name', type=str)
def submit(input_name):
"""Submit a new job."""
token = get_token() # Implement your token retrieval logic here
if not token:
click.echo("You are not logged in.")
return
# Placeholder for other inputs (mapper_func, reducer_func)
mapper_code = ''
reducer_code = ''
# Prepare payload
payload = {
"mapper_func": mapper_code,
"reducer_func": reducer_code,
"input_file": input_name # Assuming input_name is the name of the file to be submitted
}
# Make HTTP POST request to submit job
headers = {"Authorization": token, "Content-Type": "application/json"}
response = requests.post(f"{SERVICE_URL}/jobs/submit", json=payload, headers=headers)
if response.status_code == 200:
click.echo("Job submitted successfully.")
click.echo(response.json())
else:
click.echo(f"Failed to submit job. Status code: {response.status_code}")
click.echo(response.text)
@jobs.command()
@click.argument('job_id')
def status(job_id):
"""View the status of an existing job."""
token = get_token()
if not token:
click.echo("You are not logged in.")
return
headers = {"Authorization": token}
try:
response = requests.get(f"{SERVICE_URL}/jobs/status/{job_id}", headers=headers)
if response.status_code == 200:
job_status = response.json()
sub_status = job_status.get('sub_status', 'N/A')
number_of_chunks = job_status.get('number_of_chunks', 'N/A')
click.echo(f"Job ID: {job_status['job_id']}, Status: {job_status['status']}, Substatus: {sub_status}/{number_of_chunks}")
elif response.status_code == 404:
click.echo("Job not found.")
else:
click.echo(f"Failed to get job status. Status code: {response.status_code}")
click.echo(response.text)
except requests.RequestException as e:
click.echo(f"Failed to connect to server: {e}")
# Admin command group
@click.group()
def admin():
"""Administer the system."""
pass
@admin.command()
@click.argument('username')
def create_user(username):
token = get_token()
if not token:
click.echo("You are not logged in.")
return
"""Create a new user."""
# Prompt for password input
while True:
password1 = click.prompt('Enter password', hide_input=True)
password2 = click.prompt('Confirm password', hide_input=True)
if password1 == password2:
break
else:
click.echo("Passwords do not match. Please try again.")
# Prepare request payload
payload = {"username": username, "password": password1, "email": f"{username}@example.com"}
# Make HTTP POST request to create user
response = requests.post(f"{SERVICE_URL}/admin/create_user", json=payload, headers={"Authorization": token})
if response.status_code == 200:
click.echo(f"User '{username}' created successfully.")
else:
click.echo("Failed to create user.")
click.echo(response.text)
# Logout command
@click.command()
def logout():
"""Log out of the system."""
delete_token()
click.echo("Logged out successfully.")
# Login command
@click.command()
@click.option('--username', prompt=True, help='Your username')
@click.option('--password', prompt=True, hide_input=True, help='Your password')
def login(username, password):
"""Log in to the system."""
token = get_token()
if token:
click.echo("You are already logged in. Please log out first.")
return
# Prepare request payload
payload = {"username": username, "password": password}
# Make HTTP POST request to login
response = requests.post(f"{SERVICE_URL}/login", json=payload)
if response.status_code == 200:
# Save token to a file
save_token(response.json()["access_token"])
click.echo("Login successful.")
else:
click.echo("Login failed.")
click.echo(response.text)
# Define the main CLI group
@click.group()
def cli():
"""Main CLI for job and admin management."""
pass
# Add the command groups to the main CLI group
cli.add_command(jobs)
cli.add_command(admin)
cli.add_command(login)
cli.add_command(logout)
if __name__ == '__main__':
cli()