Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 30eb036

Browse files
committed
Make login and logout be run by click
1 parent a4aee05 commit 30eb036

File tree

6 files changed

+72
-54
lines changed

6 files changed

+72
-54
lines changed

paperspace/cli.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from paperspace import constants, client, config
99
from paperspace.commands import experiments as experiments_commands, deployments as deployments_commands, \
10-
machines as machines_commands
10+
machines as machines_commands, login as login_commands
1111

1212

1313
class ChoiceType(click.Choice):
@@ -1071,3 +1071,36 @@ def wait_for_machine_state(machine_id, state, api_key):
10711071
machines_api = client.API(config.CONFIG_HOST, api_key=api_key)
10721072
command = machines_commands.WaitForMachineStateCommand(api=machines_api)
10731073
command.execute(machine_id, state)
1074+
1075+
1076+
@cli.command("login", help="Log in with email and password")
1077+
@click.option(
1078+
"--email",
1079+
"email",
1080+
required=True,
1081+
callback=validate_email,
1082+
help="Email used to create Paperspace account",
1083+
)
1084+
@click.option(
1085+
"--password",
1086+
"password",
1087+
prompt=True,
1088+
hide_input=True,
1089+
help="Password used to create Paperspace account",
1090+
)
1091+
@click.option(
1092+
"--apiTokenName",
1093+
"api_token_name",
1094+
help="Name of api token used to log in",
1095+
)
1096+
def login(email, password, api_token_name):
1097+
machines_api = client.API(config.CONFIG_HOST)
1098+
command = login_commands.LogInCommand(api=machines_api)
1099+
command.execute(email, password, api_token_name)
1100+
1101+
1102+
@cli.command("logout", help="Log out / remove apiKey from config file")
1103+
def logout():
1104+
machines_api = client.API(config.CONFIG_HOST)
1105+
command = login_commands.LogOutCommand(api=machines_api)
1106+
command.execute()

paperspace/commands/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from paperspace import logger
2+
3+
4+
class CommandBase(object):
5+
def __init__(self, api=None, logger_=logger):
6+
self.api = api
7+
self.logger = logger_

paperspace/commands/deployments.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import terminaltables
44

55
from paperspace import config, version, client, logger
6+
from paperspace.commands import CommandBase
67
from paperspace.utils import get_terminal_lines
78

89
default_headers = {"X-API-Key": config.PAPERSPACE_API_KEY,
@@ -11,11 +12,7 @@
1112
deployments_api = client.API(config.CONFIG_HOST, headers=default_headers)
1213

1314

14-
class _DeploymentCommandBase(object):
15-
def __init__(self, api=deployments_api, logger_=logger):
16-
self.api = api
17-
self.logger = logger_
18-
15+
class _DeploymentCommandBase(CommandBase):
1916
def _log_message(self, response, success_msg_template, error_msg):
2017
if response.ok:
2118
try:

paperspace/commands/login.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from paperspace import login, logout
2+
from paperspace.commands import CommandBase
3+
4+
5+
class LogInCommand(CommandBase):
6+
def execute(self, email, password, api_token_name=None):
7+
login(email, password, api_token_name)
8+
9+
10+
class LogOutCommand(CommandBase):
11+
def execute(self):
12+
logout()
13+
14+

paperspace/commands/machines.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33

44
import terminaltables
55

6-
from paperspace import logger
6+
from paperspace.commands import CommandBase
77
from paperspace.exceptions import BadResponse
88
from paperspace.utils import get_terminal_lines
99

1010

11-
class CommandBase(object):
12-
def __init__(self, api=None, logger_=logger):
13-
self.api = api
14-
self.logger = logger_
15-
11+
class _MachinesCommandBase(CommandBase):
1612
def _log_message(self, response, success_msg_template, error_msg):
1713
if response.ok:
1814
try:
@@ -30,7 +26,7 @@ def _log_message(self, response, success_msg_template, error_msg):
3026
self.logger.log(error_msg)
3127

3228

33-
class CheckAvailabilityCommand(CommandBase):
29+
class CheckAvailabilityCommand(_MachinesCommandBase):
3430
def execute(self, region, machine_type):
3531
params = {"region": region,
3632
"machineType": machine_type}
@@ -40,15 +36,15 @@ def execute(self, region, machine_type):
4036
"Unknown error while checking machine availability")
4137

4238

43-
class CreateMachineCommand(CommandBase):
39+
class CreateMachineCommand(_MachinesCommandBase):
4440
def execute(self, kwargs):
4541
response = self.api.post("/machines/createSingleMachinePublic/", json=kwargs)
4642
self._log_message(response,
4743
"New machine created with id: {id}",
4844
"Unknown error while creating machine")
4945

5046

51-
class UpdateMachineCommand(CommandBase):
47+
class UpdateMachineCommand(_MachinesCommandBase):
5248
def execute(self, machine_id, kwargs):
5349
url = "/machines/{}/updateMachinePublic/".format(machine_id)
5450
response = self.api.post(url, json=kwargs)
@@ -57,7 +53,7 @@ def execute(self, machine_id, kwargs):
5753
"Unknown error while updating machine")
5854

5955

60-
class StartMachineCommand(CommandBase):
56+
class StartMachineCommand(_MachinesCommandBase):
6157
def execute(self, machine_id):
6258
url = "/machines/{}/start/".format(machine_id)
6359
response = self.api.post(url)
@@ -66,7 +62,7 @@ def execute(self, machine_id):
6662
"Unknown error while starting the machine")
6763

6864

69-
class StopMachineCommand(CommandBase):
65+
class StopMachineCommand(_MachinesCommandBase):
7066
def execute(self, machine_id):
7167
url = "/machines/{}/stop/".format(machine_id)
7268
response = self.api.post(url)
@@ -75,7 +71,7 @@ def execute(self, machine_id):
7571
"Unknown error while stopping the machine")
7672

7773

78-
class RestartMachineCommand(CommandBase):
74+
class RestartMachineCommand(_MachinesCommandBase):
7975
def execute(self, machine_id):
8076
url = "/machines/{}/restart/".format(machine_id)
8177
response = self.api.post(url)
@@ -84,7 +80,7 @@ def execute(self, machine_id):
8480
"Unknown error while restarting the machine")
8581

8682

87-
class ShowMachineCommand(CommandBase):
83+
class ShowMachineCommand(_MachinesCommandBase):
8884
def execute(self, machine_id):
8985
params = {"machineId": machine_id}
9086
response = self.api.get("/machines/getMachinePublic/", params=params)
@@ -142,7 +138,7 @@ def make_details_table(machine):
142138
return table_string
143139

144140

145-
class ListMachinesCommand(CommandBase):
141+
class ListMachinesCommand(_MachinesCommandBase):
146142
def execute(self, kwargs):
147143
json_ = {"params": kwargs} if kwargs else None
148144
response = self.api.get("/machines/getMachines/", json=json_)
@@ -186,7 +182,7 @@ def _make_machines_list_table(machines):
186182
return table_string
187183

188184

189-
class DestroyMachineCommand(CommandBase):
185+
class DestroyMachineCommand(_MachinesCommandBase):
190186
def execute(self, machine_id, release_public_ip):
191187
json_ = {"releasePublicIp": release_public_ip} if release_public_ip else None
192188
url = "/machines/{}/destroyMachine/".format(machine_id)
@@ -196,7 +192,7 @@ def execute(self, machine_id, release_public_ip):
196192
"Unknown error while destroying the machine")
197193

198194

199-
class ShowMachineUtilisationCommand(CommandBase):
195+
class ShowMachineUtilisationCommand(_MachinesCommandBase):
200196
def execute(self, machine_id, billing_month):
201197
params = {"machineId": machine_id,
202198
"billingMonth": billing_month}
@@ -227,7 +223,7 @@ def make_details_table(machine):
227223
return table_string
228224

229225

230-
class WaitForMachineStateCommand(CommandBase):
226+
class WaitForMachineStateCommand(_MachinesCommandBase):
231227
def execute(self, machine_id, state, interval=5):
232228
while True:
233229
try:

paperspace/main.py

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
from .cli import cli
55
from .jobs import run, print_json_pretty
6-
from .login import login, logout, set_apikey
6+
from .login import set_apikey
77
from .version import version
88

99

1010
def main():
11-
if len(sys.argv) >= 2 and sys.argv[1] in ('experiments', 'deployments', 'machines'):
11+
if len(sys.argv) >= 2 and sys.argv[1] in ('experiments', 'deployments', 'machines', 'login', 'logout'):
1212
cli(sys.argv[1:])
1313

1414
args = sys.argv[:]
@@ -30,35 +30,6 @@ def main():
3030
vers(prog)
3131
sys.exit(0)
3232

33-
if cmd == 'login':
34-
email = None
35-
password = None
36-
apiToken = None
37-
while args:
38-
opt = args.pop(0)
39-
if opt in help_opts:
40-
print('usage: %s' % login_usage(prog))
41-
sys.exit(0)
42-
elif opt == '--email':
43-
email = args.pop(0) if args else None
44-
elif opt == '--password':
45-
password = args.pop(0) if args else None
46-
elif opt == '--apiToken':
47-
apiToken = args.pop(0) if args else None
48-
elif not email:
49-
email = opt
50-
elif not password:
51-
password = opt
52-
elif not apiToken:
53-
apiToken = opt
54-
return not login(email, password, apiToken)
55-
56-
if cmd == 'logout':
57-
if args:
58-
print('usage: %s logout' % prog)
59-
sys.exit(not (args[0] in help_opts))
60-
return not logout()
61-
6233
if cmd == 'apikey' or cmd == 'apiKey':
6334
if not args or args[0] in help_opts:
6435
print('usage: %s' % apikey_usage(prog))

0 commit comments

Comments
 (0)