Skip to content
This repository has been archived by the owner on May 30, 2021. It is now read-only.

Commit

Permalink
fix: use new api for get job info (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
sotetsuk authored Jan 14, 2019
1 parent 4fc0992 commit 790c9c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
6 changes: 4 additions & 2 deletions paicli/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ def get_jobs(self, username=""):
else:
res.raise_for_status()

def get_jobs_jobname(self, jobname):
url = "{}/api/{}/jobs/{}".format(self.config.api_uri, self.config.api_version, jobname)
def get_user_username_jobs_jobname(self, username, jobname):
url = "{}/api/{}/user/{}/jobs/{}".format(
self.config.api_uri, self.config.api_version, username, jobname
)
res = requests.get(url)

if res.ok:
Expand Down
15 changes: 8 additions & 7 deletions paicli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def sshcmd(jobname, task_name, task_index, username, command, dryrun, profile):
config = Config(profile)
_load(config)
api = API(config)
if not username:
username = config.username

if not jobname:
if not username:
username = config.username
jobs = Jobs(api, username)
jobs.filter({'state': ['RUNNING']})
if len(jobs) == 0:
Expand All @@ -102,8 +102,6 @@ def sshcmd(jobname, task_name, task_index, username, command, dryrun, profile):
try:
content = json.loads(api.get_user_username_jobs_jobname_ssh(username, _jobname))
except requests.HTTPError as e:
# This method is duplicated in the latest API
# So this nested try-catch should be removed in the near future
try:
content = json.loads(api.get_user_username_jobs_jobname_ssh(config.username, _jobname))
except requests.HTTPError as e:
Expand All @@ -118,7 +116,7 @@ def sshcmd(jobname, task_name, task_index, username, command, dryrun, profile):
exit(1)

try:
run_ssh(api, _jobname, task_name, task_index, config, content, command, dryrun)
run_ssh(api, username, _jobname, task_name, task_index, config, content, command, dryrun)
except KeyError: # TODO: raise/catch original error
print(colored("SSH failed.", "red"))
print("There is no match task.\n") # TODO: give more information
Expand Down Expand Up @@ -244,17 +242,20 @@ def stopcmd(jobname, profile):

@click.command(name='host', help="Show host information of a job.")
@click.argument('jobname', type=str)
@click.option('--username', '-u', type=str, default='')
@click.option('--profile', '-p', type=str, default='default', help="Use a specified profile.")
def hostcmd(jobname, profile):
def hostcmd(jobname, username, profile):
config = Config(profile)
_load(config)
api = API(config)
if not username:
username = config.username

tab = PrettyTable()
tab.field_names = ["task role", "ip", "port label", "port"]

try:
ret = api.get_jobs_jobname(jobname)
ret = api.get_user_username_jobs_jobname(username, jobname)
tasks = json.loads(ret)['taskRoles']
for k, v in tasks.items():
task_name = k
Expand Down
4 changes: 2 additions & 2 deletions paicli/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .intereactive import select_choices_interactively


def run_ssh(api, jobname, task_name, task_index, config, content, command="", dryrun=False):
def run_ssh(api, username, jobname, task_name, task_index, config, content, command="", dryrun=False):
sshkey = _download_sshkey(content)
path_to_sshkey = os.path.join(config.path_to_configdir, ".tmpkey")

Expand All @@ -27,7 +27,7 @@ def run_ssh(api, jobname, task_name, task_index, config, content, command="", dr
# if the job has >= 2 tasks
else:
choices = []
ret = api.get_jobs_jobname(jobname) # TODO: Error handling
ret = api.get_user_username_jobs_jobname(username, jobname) # TODO: Error handling
tasks = json.loads(ret)['taskRoles']
for k, v in tasks.items():
_task_name = k
Expand Down

0 comments on commit 790c9c0

Please sign in to comment.