Skip to content

Commit 6760eff

Browse files
authored
fix(agents): re-create agent client after project selection (#573)
1 parent 4b7b63a commit 6760eff

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed

cmd/lk/agent.go

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ var (
243243

244244
func 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

277276
func 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

502512
func 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

618628
func 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 {
913923
func 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+
}

cmd/lk/app.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
destinationFile string
4444
exampleFile string
4545
project *config.ProjectConfig
46+
lkConfig *config.LiveKitTOML
4647
AppCommands = []*cli.Command{
4748
{
4849
Name: "app",
@@ -145,10 +146,11 @@ func requireProject(ctx context.Context, cmd *cli.Command) (context.Context, err
145146
return ctx, nil
146147
}
147148
if _, err = loadProjectConfig(ctx, cmd); err != nil {
148-
// something is wrong with config file
149+
// something is wrong with CLI config file
149150
return nil, err
150151
}
151152
if project, err = loadProjectDetails(cmd); err != nil {
153+
// something is wrong with project config file
152154
if errors.Is(err, config.ErrInvalidConfig) {
153155
return ctx, err
154156
}
@@ -177,6 +179,7 @@ func selectProject(ctx context.Context, cmd *cli.Command) (context.Context, erro
177179
Run(); err != nil {
178180
return nil, err
179181
}
182+
fmt.Println("Using project [" + util.Accented(project.Name) + "]")
180183
} else {
181184
shouldAuth := true
182185
if err = huh.NewConfirm().
@@ -487,14 +490,8 @@ func doPostCreate(ctx context.Context, _ *cli.Command, rootPath string, verbose
487490
return nil
488491
}
489492

490-
var cmdErr error
491-
if err := util.Await(
492-
"Cleaning up...",
493-
func() { cmdErr = task() },
494-
); err != nil {
495-
return err
496-
}
497-
return cmdErr
493+
fmt.Println("Cleaning up...")
494+
return task()
498495
}
499496

500497
func doInstall(ctx context.Context, task bootstrap.KnownTask, rootPath string, verbose bool) error {

cmd/lk/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func loadProjectDetails(c *cli.Command, opts ...loadOption) (*config.ProjectConf
253253
}
254254

255255
// load from config file
256-
lkConfig, _, err := config.LoadTOMLFile(workingDir, tomlFilename)
256+
_, err := requireConfig(workingDir, tomlFilename)
257257
if errors.Is(err, config.ErrInvalidConfig) {
258258
return nil, err
259259
}

pkg/util/action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
// Call an action and show a spinner while waiting for it to finish.
2222
func Await(title string, action func()) error {
2323
return spinner.New().
24-
Title(title).
24+
Title(" " + title).
2525
Action(action).
26+
Type(spinner.Pulse).
2627
Style(Theme.Focused.Title).
2728
Run()
2829
}

0 commit comments

Comments
 (0)