33
44import terminaltables
55
6- from paperspace import logger
6+ from paperspace . commands import CommandBase
77from paperspace .exceptions import BadResponse
88from 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 :
0 commit comments