@@ -243,7 +243,6 @@ var (
243243
244244func createAgentClient (ctx context.Context , cmd * cli.Command ) (context.Context , error ) {
245245 var err error
246- var lkConfig * config.LiveKitTOML
247246
248247 if _ , err := requireProject (ctx , cmd ); err != nil {
249248 return ctx , err
@@ -256,22 +255,22 @@ func createAgentClient(ctx context.Context, cmd *cli.Command) (context.Context,
256255 // If a project has been manually selected that conflicts with the agent's config,
257256 // or if the config file is malformed, this is an error. If the config does not exist,
258257 // we assume it gets created later.
259- lkConfig , configExists , err := config . LoadTOMLFile (workingDir , tomlFilename )
260- if ! errors .Is (err , os .ErrNotExist ) {
261- return nil , err
258+ configExists , err := requireConfig (workingDir , tomlFilename )
259+ if err != nil && ! errors .Is (err , os .ErrNotExist ) {
260+ return ctx , err
262261 }
263262 if configExists {
264263 projectSubdomainMatch := subdomainPattern .FindStringSubmatch (project .URL )
265264 if len (projectSubdomainMatch ) < 2 {
266- return nil , fmt .Errorf ("invalid project URL [%s]" , project .URL )
265+ return ctx , fmt .Errorf ("invalid project URL [%s]" , project .URL )
267266 }
268267 if projectSubdomainMatch [1 ] != lkConfig .Project .Subdomain {
269- return nil , fmt .Errorf ("project does not match agent subdomain [%s]" , lkConfig .Project .Subdomain )
268+ return ctx , fmt .Errorf ("project does not match agent subdomain [%s]" , lkConfig .Project .Subdomain )
270269 }
271270 }
272271
273272 agentsClient , err = lksdk .NewAgentClient (project .URL , project .APIKey , project .APISecret )
274- return nil , err
273+ return ctx , err
275274}
276275
277276func createAgent (ctx context.Context , cmd * cli.Command ) error {
@@ -296,11 +295,22 @@ func createAgent(ctx context.Context, cmd *cli.Command) error {
296295 if _ , err := selectProject (ctx , cmd ); err != nil {
297296 return err
298297 }
298+ var err error
299+ // Recreate the client with the new project
300+ agentsClient , err = lksdk .NewAgentClient (project .URL , project .APIKey , project .APISecret )
301+ if err != nil {
302+ return err
303+ }
304+ // Re-parse the project URL to get the subdomain
305+ subdomainMatches = subdomainPattern .FindStringSubmatch (project .URL )
306+ if len (subdomainMatches ) < 2 {
307+ return fmt .Errorf ("invalid project URL [%s]" , project .URL )
308+ }
299309 }
300310 }
301311
302312 logger .Debugw ("Creating agent" , "working-dir" , workingDir )
303- lkConfig , configExists , err := config . LoadTOMLFile (workingDir , tomlFilename )
313+ configExists , err := requireConfig (workingDir , tomlFilename )
304314 if err != nil && configExists {
305315 return err
306316 }
@@ -500,7 +510,7 @@ func createAgentConfig(ctx context.Context, cmd *cli.Command) error {
500510}
501511
502512func deployAgent (ctx context.Context , cmd * cli.Command ) error {
503- lkConfig , configExists , err := config . LoadTOMLFile (workingDir , tomlFilename )
513+ configExists , err := requireConfig (workingDir , tomlFilename )
504514 if err != nil {
505515 return err
506516 }
@@ -616,7 +626,7 @@ func getAgentStatus(ctx context.Context, cmd *cli.Command) error {
616626}
617627
618628func updateAgent (ctx context.Context , cmd * cli.Command ) error {
619- lkConfig , configExists , err := config . LoadTOMLFile (workingDir , tomlFilename )
629+ configExists , err := requireConfig (workingDir , tomlFilename )
620630 if err != nil && configExists {
621631 return err
622632 }
@@ -913,7 +923,7 @@ func updateAgentSecrets(ctx context.Context, cmd *cli.Command) error {
913923func getAgentName (cmd * cli.Command , agentDir string , tomlFileName string ) (string , error ) {
914924 agentName := cmd .String ("name" )
915925 if agentName == "" {
916- lkConfig , configExists , err := config . LoadTOMLFile (agentDir , tomlFileName )
926+ configExists , err := requireConfig (agentDir , tomlFileName )
917927 if err != nil && configExists {
918928 return "" , err
919929 }
@@ -998,27 +1008,54 @@ func requireDockerfile(ctx context.Context, cmd *cli.Command, workingDir string)
9981008 }
9991009
10001010 if ! dockerfileExists {
1011+ var clientSettingsResponse * lkproto.ClientSettingsResponse
1012+ var innerErr error
1013+
10011014 if ! cmd .Bool ("silent" ) {
1002- fmt .Println ("Creating Dockerfile" )
1015+ if err := util .Await (
1016+ "Loading client settings..." ,
1017+ func () {
1018+ clientSettingsResponse , err = agentsClient .GetClientSettings (ctx , & lkproto.ClientSettingsRequest {})
1019+ },
1020+ ); err != nil {
1021+ return err
1022+ }
1023+ } else {
1024+ clientSettingsResponse , err = agentsClient .GetClientSettings (ctx , & lkproto.ClientSettingsRequest {})
10031025 }
10041026
1005- clientSettingsResponse , err := agentsClient .GetClientSettings (ctx , & lkproto.ClientSettingsRequest {})
1006- if err != nil {
1007- if twerr , ok := err .(twirp.Error ); ok {
1027+ if innerErr != nil {
1028+ if twerr , ok := innerErr .(twirp.Error ); ok {
10081029 if twerr .Code () == twirp .PermissionDenied {
10091030 return fmt .Errorf ("agent hosting is disabled for this project -- join the beta program here [%s]" , cloudAgentsBetaSignupURL )
10101031 }
10111032 }
1012- return err
1033+ return innerErr
10131034 }
10141035
10151036 settingsMap := make (map [string ]string )
10161037 for _ , setting := range clientSettingsResponse .Params {
10171038 settingsMap [setting .Name ] = setting .Value
10181039 }
10191040
1020- if err := agentfs .CreateDockerfile (workingDir , settingsMap ); err != nil {
1021- return err
1041+ if ! cmd .Bool ("silent" ) {
1042+ var innerErr error
1043+ if err := util .Await (
1044+ "Creating Dockerfile..." ,
1045+ func () {
1046+ innerErr = agentfs .CreateDockerfile (workingDir , settingsMap )
1047+ },
1048+ ); err != nil {
1049+ return err
1050+ }
1051+ if innerErr != nil {
1052+ return innerErr
1053+ }
1054+ fmt .Println ("Created [" + util .Accented ("Dockerfile" ) + "]" )
1055+ } else {
1056+ if err := agentfs .CreateDockerfile (workingDir , settingsMap ); err != nil {
1057+ return err
1058+ }
10221059 }
10231060 } else {
10241061 if ! cmd .Bool ("silent" ) {
@@ -1028,3 +1065,14 @@ func requireDockerfile(ctx context.Context, cmd *cli.Command, workingDir string)
10281065
10291066 return nil
10301067}
1068+
1069+ func requireConfig (workingDir , tomlFilename string ) (bool , error ) {
1070+ if lkConfig != nil {
1071+ return true , nil
1072+ }
1073+
1074+ var exists bool
1075+ var err error
1076+ lkConfig , exists , err = config .LoadTOMLFile (workingDir , tomlFilename )
1077+ return exists , err
1078+ }
0 commit comments