2021-08-25 11:06:49 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
abraFormatter "coopcloud.tech/abra/cli/formatter"
|
|
|
|
"coopcloud.tech/abra/cli/internal"
|
|
|
|
"coopcloud.tech/abra/client/stack"
|
|
|
|
"coopcloud.tech/abra/config"
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
2021-08-25 12:06:42 +00:00
|
|
|
// getImagePath returns the image name
|
|
|
|
func getImagePath(image string) (string, error) {
|
|
|
|
img, err := reference.ParseNormalizedNamed(image)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
path := reference.Path(img)
|
|
|
|
if strings.Contains(image, "library") {
|
|
|
|
image = strings.Split(image, "/")[1]
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:30:55 +00:00
|
|
|
// parseVersionLabel parses a $STACK_NAME_$SERVICE_NAME service label
|
|
|
|
func parseServiceName(label string) string {
|
|
|
|
idx := strings.LastIndex(label, "_")
|
|
|
|
return label[idx+1:]
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:06:49 +00:00
|
|
|
// parseVersionLabel parses a $VERSION-$DIGEST service label
|
|
|
|
func parseVersionLabel(label string) (string, string) {
|
|
|
|
// versions may look like v4.2-abcd or v4.2-alpine-abcd
|
|
|
|
idx := strings.LastIndex(label, "-")
|
|
|
|
return label[:idx], label[idx+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
var appVersionCommand = &cli.Command{
|
|
|
|
Name: "version",
|
|
|
|
Usage: "show version of all services in app",
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
appName := c.Args().First()
|
|
|
|
if appName == "" {
|
|
|
|
internal.ShowSubcommandHelpAndError(c, errors.New("no app name provided"))
|
|
|
|
}
|
|
|
|
appFiles, err := config.LoadAppFiles("")
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
appEnv, err := config.GetApp(appFiles, appName)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app := appFiles[appName]
|
|
|
|
|
|
|
|
compose, err := config.GetAppComposeFiles(appEnv.Type)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:30:55 +00:00
|
|
|
ch := make(chan stack.StackStatus, len(compose.Services))
|
|
|
|
for _, service := range compose.Services {
|
|
|
|
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
|
|
|
|
go func(s string, l string) {
|
|
|
|
ch <- stack.GetDeployedServicesByLabel(s, l)
|
|
|
|
}(app.Server, label)
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:06:49 +00:00
|
|
|
tableCol := []string{"Name", "Image", "Version", "Digest"}
|
|
|
|
table := abraFormatter.CreateTable(tableCol)
|
|
|
|
|
2021-08-25 12:06:42 +00:00
|
|
|
statuses := make(map[string]stack.StackStatus)
|
2021-08-25 11:30:55 +00:00
|
|
|
for range compose.Services {
|
|
|
|
status := <-ch
|
2021-08-25 12:06:42 +00:00
|
|
|
if len(status.Services) > 0 {
|
|
|
|
serviceName := parseServiceName(status.Services[0].Spec.Name)
|
|
|
|
statuses[serviceName] = status
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range compose.Services {
|
|
|
|
if status, ok := statuses[service.Name]; ok {
|
|
|
|
statusService := status.Services[0]
|
|
|
|
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
|
|
|
|
version, digest := parseVersionLabel(statusService.Spec.Labels[label])
|
|
|
|
image, err := getImagePath(statusService.Spec.Labels["com.docker.stack.image"])
|
2021-08-25 11:06:49 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
2021-08-25 12:06:42 +00:00
|
|
|
table.Append([]string{service.Name, image, version, digest})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
image, err := getImagePath(service.Image)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Fatal(err)
|
2021-08-25 11:06:49 +00:00
|
|
|
}
|
2021-08-25 12:06:42 +00:00
|
|
|
table.Append([]string{service.Name, image, "?", "?"})
|
2021-08-25 11:06:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table.Render()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|