Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ lbctl restart
lbctl logs --follow --tail 100
```

To inspect a specific managed local deployment, pass its directory to `status`, `doctor`, or `logs`, for example `lbctl status --dir ./langbot`.

Install a new local deployment, or uninstall a deployment while retaining its data:

```sh
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ lbctl restart
lbctl logs --follow --tail 100
```

要查看指定的本机部署,可以给 `status`、`doctor` 或 `logs` 传入部署目录,例如 `lbctl status --dir ./langbot`。

安装新的本机部署,或卸载部署并保留数据:

```sh
Expand Down
28 changes: 27 additions & 1 deletion internal/app/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,21 @@ func (s *Service) Doctor(ctx context.Context, options CheckOptions) (Result, err

func (s *Service) discoverActiveEnvironment(ctx context.Context, options CheckOptions, inspectRuntime bool) (environmentResolution, error) {
resolved := environmentResolution{Environment: newActiveEnvironment()}
if options.LocalDir != "" {
record, diagnostics, err := s.lifecycleTarget(ctx, options)
if err != nil {
return resolved, err
}
timeout, err := localOperationTimeout(options, s.deps.LookupEnv)
if err != nil {
return resolved, err
}
resolved.Diagnostics = &diagnostics
resolved.Connection = config.Connection{Endpoint: record.Endpoint, Timeout: timeout}
resolved.Environment.Connection.Endpoint = record.Endpoint
resolved.Environment.Name = record.DeploymentID
return s.probeEnvironment(ctx, resolved, inspectRuntime)
}
file, err := s.load()
if err != nil {
return resolved, err
Expand Down Expand Up @@ -190,6 +205,11 @@ func (s *Service) discoverActiveEnvironment(ctx context.Context, options CheckOp
return resolved, err
}

return s.probeEnvironment(ctx, resolved, inspectRuntime)
}

func (s *Service) probeEnvironment(ctx context.Context, resolved environmentResolution, inspectRuntime bool) (environmentResolution, error) {
conn := resolved.Connection
callCtx, cancel := context.WithTimeout(ctx, conn.Timeout)
defer cancel()
info, infoErr := s.info(callCtx, conn)
Expand Down Expand Up @@ -342,7 +362,13 @@ func (s *Service) completeRuntime(ctx context.Context, resolved environmentResol

func (s *Service) resolveRuntime(ctx context.Context, environment ActiveEnvironment, identity api.Context, identityVerified, inspect bool, resolved *environmentResolution) (EnvironmentRuntime, error) {
runtimeInfo := EnvironmentRuntime{Location: "unknown", Driver: "unbound", Management: "unavailable", Binding: "none"}
diagnostics, err := s.matchingLocalDiagnostics(environment.Connection.Endpoint, identity, identityVerified)
var diagnostics deploy.Diagnostics
var err error
if resolved.Diagnostics != nil {
diagnostics = *resolved.Diagnostics
} else {
diagnostics, err = s.matchingLocalDiagnostics(environment.Connection.Endpoint, identity, identityVerified)
}
if err != nil {
runtimeInfo.Binding = "unavailable"
var problem *result.Error
Expand Down
21 changes: 21 additions & 0 deletions internal/app/local_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,27 @@ func TestAdoptRequiresLocalEndpointAndMatchingPublishedPort(t *testing.T) {
}
}

func TestDirectoryDiagnosticsUseSelectedDeployment(t *testing.T) {
dir := t.TempDir()
record := deploy.Record{DeploymentID: "selected", Endpoint: "http://127.0.0.1:5331", Port: 5331, Driver: deploy.DriverDockerCompose, ComposeProject: "selected", ComposeFile: filepath.Join(dir, "compose.yaml"), Profile: "basic"}
if err := (deploy.Store{Dir: dir}).Write(context.Background(), record); err != nil {
t.Fatal(err)
}
runner := &lifecycleTestRunner{state: "running", port: record.Port}
service := New(Dependencies{LocalDir: t.TempDir(), LocalRunner: runner, Transport: lifecycleTransport{runner: runner}})
t.Setenv("LANGBOT_ENDPOINT", "https://other.invalid")
t.Setenv("LANGBOT_API_KEY", "other-context-key")
options := CheckOptions{LocalDir: dir}
resolved, err := service.discoverActiveEnvironment(context.Background(), options, false)
if err != nil || resolved.Environment.Runtime.Dir != dir || resolved.Environment.Name != record.DeploymentID || resolved.Connection.Endpoint != record.Endpoint || resolved.Connection.CredentialSource != "" {
t.Fatalf("directory discovery = %+v, err = %v", resolved.Environment, err)
}
options.EndpointSet = true
if _, err := service.discoverActiveEnvironment(context.Background(), options, false); err == nil || result.AsError(err).Kind != "input" {
t.Fatalf("conflicting target error = %v", err)
}
}

func TestFollowLogsUsesJSONLines(t *testing.T) {
port := freeLifecyclePort(t)
root := t.TempDir()
Expand Down
31 changes: 17 additions & 14 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,35 +1342,36 @@ func newContextCheck(service *app.Service, deps Dependencies, flags *globalFlags
}

func newStatusCommand(service *app.Service, deps Dependencies, flags *globalFlags) *cobra.Command {
return &cobra.Command{
var dir string
cmd := &cobra.Command{
Use: "status", Short: i18n.T("Show current environment status"), Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return emitCall(deps, flags, func() (app.Result, error) {
return service.Status(cmd.Context(), app.CheckOptions{
Human: !flags.outputSet,
Context: flags.context, Endpoint: flags.endpoint, Timeout: flags.timeout,
ContextSet: flags.contextSet, EndpointSet: flags.endpointSet, TimeoutSet: flags.timeoutSet,
APIKeyStdin: flags.apiKeyStdin,
})
options := connectionOptions(flags)
options.LocalDir = dir
options.Human = !flags.outputSet
return service.Status(cmd.Context(), options)
})
},
}
cmd.Flags().StringVar(&dir, "dir", "", i18n.T("Local deployment directory"))
return cmd
}

func newDoctorCommand(service *app.Service, deps Dependencies, flags *globalFlags) *cobra.Command {
var region string
var dir, region string
cmd := &cobra.Command{
Use: "doctor", Short: i18n.T("Diagnose the current environment"), Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return emitCall(deps, flags, func() (app.Result, error) {
return service.Doctor(cmd.Context(), app.CheckOptions{
Context: flags.context, Endpoint: flags.endpoint, Timeout: flags.timeout,
ContextSet: flags.contextSet, EndpointSet: flags.endpointSet, TimeoutSet: flags.timeoutSet,
APIKeyStdin: flags.apiKeyStdin, Region: region,
})
options := connectionOptions(flags)
options.LocalDir = dir
options.Region = region
return service.Doctor(cmd.Context(), options)
})
},
}
cmd.Flags().StringVar(&dir, "dir", "", i18n.T("Local deployment directory"))
cmd.Flags().StringVar(&region, "region", "", i18n.T("Region: auto, cn or global"))
return cmd
}
Expand Down Expand Up @@ -1541,7 +1542,7 @@ func newUninstallCommand(service *app.Service, deps Dependencies, flags *globalF
}

func newLocalLogsCommand(service *app.Service, deps Dependencies, flags *globalFlags) *cobra.Command {
var serviceName, since string
var dir, serviceName, since string
var tail int
var follow bool
cmd := &cobra.Command{
Expand All @@ -1554,6 +1555,7 @@ func newLocalLogsCommand(service *app.Service, deps Dependencies, flags *globalF
options := app.LocalLogsOptions{
CheckOptions: connectionOptions(flags), Service: serviceName, Tail: tail, Since: since, Follow: follow, Format: format,
}
options.LocalDir = dir
if !follow {
return emitCall(deps, flags, func() (app.Result, error) { return service.LocalLogs(cmd.Context(), options) })
}
Expand All @@ -1576,6 +1578,7 @@ func newLocalLogsCommand(service *app.Service, deps Dependencies, flags *globalF
return emitCommand(deps, flags, app.Result{}, err)
},
}
cmd.Flags().StringVar(&dir, "dir", "", i18n.T("Local deployment directory"))
cmd.Flags().StringVar(&serviceName, "service", "", i18n.T("Service name: langbot, langbot_plugin_runtime or langbot_box"))
cmd.Flags().IntVar(&tail, "tail", 200, i18n.T("Number of trailing log lines, maximum 5000"))
cmd.Flags().StringVar(&since, "since", "", i18n.T("Read logs only after the specified time"))
Expand Down
Loading