From 790c9c02bc5f3ae968188261d1a7ed8b95c38cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sotetsu=20KOYAMADA=EF=BC=88=E5=B0=8F=E5=B1=B1=E7=94=B0?= =?UTF-8?q?=E5=89=B5=E5=93=B2=EF=BC=89?= Date: Mon, 14 Jan 2019 17:20:01 +0900 Subject: [PATCH] fix: use new api for get job info (#62) --- paicli/api.py | 6 ++++-- paicli/main.py | 15 ++++++++------- paicli/ssh.py | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/paicli/api.py b/paicli/api.py index 8ba2cc8..033a23d 100644 --- a/paicli/api.py +++ b/paicli/api.py @@ -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: diff --git a/paicli/main.py b/paicli/main.py index 0a4d0da..3a82bf5 100644 --- a/paicli/main.py +++ b/paicli/main.py @@ -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: @@ -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: @@ -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 @@ -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 diff --git a/paicli/ssh.py b/paicli/ssh.py index 5428017..a7601db 100644 --- a/paicli/ssh.py +++ b/paicli/ssh.py @@ -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") @@ -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