fix: GetService & handling missing services
This commit is contained in:
parent
605e2553b8
commit
95d385c420
|
@ -10,6 +10,7 @@ import (
|
||||||
"coopcloud.tech/abra/pkg/autocomplete"
|
"coopcloud.tech/abra/pkg/autocomplete"
|
||||||
"coopcloud.tech/abra/pkg/client"
|
"coopcloud.tech/abra/pkg/client"
|
||||||
"coopcloud.tech/abra/pkg/config"
|
"coopcloud.tech/abra/pkg/config"
|
||||||
|
"coopcloud.tech/abra/pkg/service"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
dockerClient "github.com/docker/docker/client"
|
dockerClient "github.com/docker/docker/client"
|
||||||
|
@ -48,7 +49,6 @@ func stackLogs(c *cli.Context, stackName string, client *dockerClient.Client) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
// defer after err check as any err returns a nil io.ReadCloser
|
|
||||||
defer logs.Close()
|
defer logs.Close()
|
||||||
|
|
||||||
_, err = io.Copy(os.Stdout, logs)
|
_, err = io.Copy(os.Stdout, logs)
|
||||||
|
@ -57,7 +57,9 @@ func stackLogs(c *cli.Context, stackName string, client *dockerClient.Client) {
|
||||||
}
|
}
|
||||||
}(service.ID)
|
}(service.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,27 +96,21 @@ var appLogsCommand = &cli.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func tailServiceLogs(c *cli.Context, cl *dockerClient.Client, app config.App, serviceName string) error {
|
func tailServiceLogs(c *cli.Context, cl *dockerClient.Client, app config.App, serviceName string) error {
|
||||||
service := fmt.Sprintf("%s_%s", app.StackName(), serviceName)
|
|
||||||
filters := filters.NewArgs()
|
filters := filters.NewArgs()
|
||||||
filters.Add("name", service)
|
filters.Add("name", fmt.Sprintf("%s_%s", app.StackName(), serviceName))
|
||||||
serviceOpts := types.ServiceListOptions{Filters: filters}
|
chosenService, err := service.GetService(c.Context, cl, filters, internal.NoInput)
|
||||||
services, err := cl.ServiceList(c.Context, serviceOpts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(services) != 1 {
|
|
||||||
logrus.Fatalf("expected 1 service but got %v", len(services))
|
|
||||||
}
|
|
||||||
|
|
||||||
if internal.StdErrOnly {
|
if internal.StdErrOnly {
|
||||||
logOpts.ShowStdout = false
|
logOpts.ShowStdout = false
|
||||||
}
|
}
|
||||||
|
|
||||||
logs, err := cl.ServiceLogs(c.Context, services[0].ID, logOpts)
|
logs, err := cl.ServiceLogs(c.Context, chosenService.ID, logOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
logrus.Fatal(err)
|
||||||
}
|
}
|
||||||
// defer after err check as any err returns a nil io.ReadCloser
|
|
||||||
defer logs.Close()
|
defer logs.Close()
|
||||||
|
|
||||||
_, err = io.Copy(os.Stdout, logs)
|
_, err = io.Copy(os.Stdout, logs)
|
||||||
|
|
|
@ -14,8 +14,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetContainer retrieves a container. If prompt is true and the retrievd count
|
// GetContainer retrieves a container. If prompt is true and the retrievd count
|
||||||
// of containers does not match expectedN, then a prompt is presented to let
|
// of containers does not match 1, then a prompt is presented to let the user
|
||||||
// the user choose.
|
// choose. A count of 0 is handled gracefully.
|
||||||
func GetContainer(c context.Context, cl *client.Client, filters filters.Args, prompt bool) (types.Container, error) {
|
func GetContainer(c context.Context, cl *client.Client, filters filters.Args, prompt bool) (types.Container, error) {
|
||||||
containerOpts := types.ContainerListOptions{Filters: filters}
|
containerOpts := types.ContainerListOptions{Filters: filters}
|
||||||
containers, err := cl.ContainerList(c, containerOpts)
|
containers, err := cl.ContainerList(c, containerOpts)
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"coopcloud.tech/abra/pkg/formatter"
|
||||||
|
"github.com/AlecAivazis/survey/v2"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/api/types/swarm"
|
||||||
|
"github.com/docker/docker/client"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetService retrieves a service container. If prompt is true and the retrievd
|
||||||
|
// count of service containers does not match 1, then a prompt is presented to
|
||||||
|
// let the user choose. A count of 0 is handled gracefully.
|
||||||
|
func GetService(c context.Context, cl *client.Client, filters filters.Args, prompt bool) (swarm.Service, error) {
|
||||||
|
serviceOpts := types.ServiceListOptions{Filters: filters}
|
||||||
|
services, err := cl.ServiceList(c, serviceOpts)
|
||||||
|
if err != nil {
|
||||||
|
return swarm.Service{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(services) == 0 {
|
||||||
|
filter := filters.Get("name")[0]
|
||||||
|
return swarm.Service{}, fmt.Errorf("no services matching the %v filter found?", filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(services) != 1 {
|
||||||
|
var servicesRaw []string
|
||||||
|
for _, service := range services {
|
||||||
|
serviceName := service.Spec.Name
|
||||||
|
created := formatter.HumanDuration(service.CreatedAt.Unix())
|
||||||
|
servicesRaw = append(servicesRaw, fmt.Sprintf("%s (created %v)", serviceName, created))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !prompt {
|
||||||
|
err := fmt.Errorf("expected 1 service but found %v: %s", len(services), strings.Join(servicesRaw, " "))
|
||||||
|
return swarm.Service{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Warnf("ambiguous service list received, prompting for input")
|
||||||
|
|
||||||
|
var response string
|
||||||
|
prompt := &survey.Select{
|
||||||
|
Message: "which service are you looking for?",
|
||||||
|
Options: servicesRaw,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := survey.AskOne(prompt, &response); err != nil {
|
||||||
|
return swarm.Service{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chosenService := strings.TrimSpace(strings.Split(response, " ")[0])
|
||||||
|
for _, service := range services {
|
||||||
|
serviceName := strings.ToLower(service.Spec.Name)
|
||||||
|
if serviceName == chosenService {
|
||||||
|
return service, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.Panic("failed to match chosen service")
|
||||||
|
}
|
||||||
|
|
||||||
|
return services[0], nil
|
||||||
|
}
|
Loading…
Reference in New Issue