1111"""
1212import os
1313import json
14+ import socket
1415import requests
1516import webbrowser
1617from typing import Dict
1718from getpass import getpass
1819from functools import lru_cache
20+ from dataclasses import dataclass
1921
2022import nbox .utils as U
2123from nbox .utils import join , logger , lo
@@ -46,7 +48,22 @@ class JobDetails(object):
4648 job_id : str
4749 run_id : str
4850
49- # class DeployDetails(object):
51+ class DeployDetails (object ):
52+ deployment_id : str
53+ model_id : str
54+
55+
56+ @dataclass
57+ class AgentDetails ():
58+ group_id : str
59+ instance_id : str
60+ nbx_type : str
61+
62+
63+ NBX_JOB_TYPE = "job"
64+ NBX_DEPLOY_TYPE = "deploy"
65+ NBX_LOCAL_TYPE = "local"
66+
5067
5168class NBXClient :
5269 def __init__ (self , nbx_url = "https://app.nimblebox.ai" ):
@@ -175,30 +192,62 @@ def __call__(self, item, default=None, reload: bool = False):
175192
176193 @property
177194 def workspace_id (self ) -> str :
178- return self .get (AuthConfig .workspace_id ) or self .get (AuthConfig ._workspace_id )
195+ return self .get (AuthConfig .workspace_id , "" ) or self .get (AuthConfig ._workspace_id , "" )
196+
197+ @property
198+ def workspace_name (self ) -> str :
199+ return self (AuthConfig .workspace_name , "" )
179200
180201 @property
181202 def nbx_url (self ) -> str :
182- return self .get (AuthConfig .url )
203+ return self .get (AuthConfig .url , "" )
183204
184205 @property
185206 def access_token (self ) -> str :
186- return self .get (AuthConfig .access_token )
207+ return self .get (AuthConfig .access_token , "" )
187208
188209 @property
189210 def username (self ) -> str :
190- return self .get (AuthConfig .username )
211+ return self .get (AuthConfig .username , "" )
212+
213+ @property
214+ def run_details (self ):
215+ return secret (AuthConfig .nbx_pod_run , {})
191216
192- def get_agent_details (self ) -> Dict [str , str ]:
193- if ConfigString .nbx_pod_run in self .secrets :
217+ @property
218+ def inside_pod (self ) -> bool :
219+ return self .inside_job_pod or self .inside_deploy_pod
220+
221+ @property
222+ def inside_job_pod (self ) -> bool :
223+ return ConfigString .nbx_pod_run in self .secrets
224+
225+ @property
226+ def inside_deploy_pod (self ) -> bool :
227+ return ConfigString .nbx_pod_deploy in self .secrets
228+
229+ def get_agent_details (self ) -> AgentDetails :
230+ if self .inside_job_pod :
194231 run_data = self .secrets [ConfigString .nbx_pod_run ]
195- jd = JobDetails ()
196- jd .job_id = run_data .get ("job_id" , None )
197- jd .run_id = run_data .get ("token" , None )
198- return jd
199- # elif ConfigString.nbx_pod_deploy in self.secrets:
200- # return self.secrets[ConfigString.nbx_pod_deploy]
201- return {}
232+ out = AgentDetails (
233+ group_id = run_data .get ("job_id" , None ),
234+ instance_id = run_data .get ("token" , None ),
235+ nbx_type = NBX_JOB_TYPE
236+ )
237+ elif self .inside_deploy_pod :
238+ deploy_data = self .secrets [ConfigString .nbx_pod_deploy ]
239+ out = AgentDetails (
240+ group_id = deploy_data .get ("deployment_id" , None ),
241+ instance_id = deploy_data .get ("model_id" , None ),
242+ nbx_type = NBX_DEPLOY_TYPE
243+ )
244+ else :
245+ out = AgentDetails (
246+ group_id = f"local-{ socket .gethostname ()} " ,
247+ instance_id = f"{ socket .gethostbyname (socket .gethostname ())} -{ os .getpid ()} " ,
248+ nbx_type = NBX_LOCAL_TYPE
249+ )
250+ return out
202251
203252
204253def init_secret ():
@@ -211,10 +260,13 @@ def init_secret():
211260 logger .info (lo (
212261 f"workspace details" ,
213262 workspace_id = secret .workspace_id ,
214- workspace_name = AuthConfig .workspace_name ,
263+ workspace_name = secret .workspace_name ,
215264 token_present = len (secret .access_token ) > 0 ,
216265 nbx_url = secret .nbx_url ,
217266 ))
267+
268+ if not secret .workspace_id :
269+ raise Exception ("Workspace ID not found. Please run `nbox login` to login to NimbleBox." )
218270 return secret
219271 else :
220272 logger .info (f"Skipping authentication as NBOX_NO_AUTH is set to True" )
@@ -231,10 +283,12 @@ def auth_info_pb():
231283 from nbox .hyperloop .common .common_pb2 import NBXAuthInfo
232284
233285 return NBXAuthInfo (
234- username = secret ( AuthConfig .username ) ,
235- workspace_id = secret ( AuthConfig .workspace_id ) or secret ( AuthConfig . _workspace_id ) ,
236- access_token = secret ( AuthConfig .access_token ) ,
286+ username = secret .username ,
287+ workspace_id = secret .workspace_id ,
288+ access_token = secret .access_token ,
237289 )
238290
239291def inside_pod ():
240- return secret (AuthConfig .nbx_pod_run , False ) or secret (AuthConfig .nbx_pod_deploy , False )
292+ if secret is None :
293+ raise Exception ("Secrets not initialized. Cannot determine where am I." )
294+ return secret .inside_pod
0 commit comments