WIP abra app version <app> implementation

This commit is contained in:
decentral1se 2021-08-25 13:06:49 +02:00
parent 3211994b2e
commit ed11634abf
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
4 changed files with 101 additions and 0 deletions

View File

@ -30,5 +30,6 @@ scaling apps up and spinning them down.
appRollbackCommand,
appSecretCommand,
appVolumeCommand,
appVersionCommand,
},
}

73
cli/app/version.go Normal file
View File

@ -0,0 +1,73 @@
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"
)
// 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)
}
tableCol := []string{"Name", "Image", "Version", "Digest"}
table := abraFormatter.CreateTable(tableCol)
for _, service := range compose.Services {
label := fmt.Sprintf("coop-cloud.%s.%s.version", appEnv.StackName(), service.Name)
status := stack.GetDeployedServicesByLabel(app.Server, label)
for _, serviceStatus := range status.Services {
version, digest := parseVersionLabel(serviceStatus.Spec.Labels[label])
img, err := reference.ParseNormalizedNamed(service.Image)
if err != nil {
logrus.Fatal(err)
}
image := reference.Path(img)
if strings.Contains(image, "library") {
image = strings.Split(image, "/")[1]
}
serviceName := fmt.Sprintf("%s_%s", appEnv.StackName(), service.Name)
table.Append([]string{serviceName, image, version, digest})
}
}
table.Render()
return nil
},
}

View File

@ -47,6 +47,28 @@ func getStackServices(ctx context.Context, apiclient client.APIClient, namespace
return apiclient.ServiceList(ctx, types.ServiceListOptions{Filters: getStackServiceFilter(namespace)})
}
// GetDeployedServicesByLabel filters services by label
func GetDeployedServicesByLabel(contextName string, label string) StackStatus {
cl, err := abraClient.NewClientWithContext(contextName)
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
// No local context found, bail out gracefully
return StackStatus{[]swarm.Service{}, nil}
}
return StackStatus{[]swarm.Service{}, err}
}
ctx := context.Background()
filters := filters.NewArgs()
filters.Add("label", label)
services, err := cl.ServiceList(ctx, types.ServiceListOptions{Filters: filters})
if err != nil {
return StackStatus{[]swarm.Service{}, err}
}
return StackStatus{services, nil}
}
func GetAllDeployedServices(contextName string) StackStatus {
cl, err := abraClient.NewClientWithContext(contextName)
if err != nil {
@ -56,11 +78,13 @@ func GetAllDeployedServices(contextName string) StackStatus {
}
return StackStatus{[]swarm.Service{}, err}
}
ctx := context.Background()
services, err := cl.ServiceList(ctx, types.ServiceListOptions{Filters: getAllStacksFilter()})
if err != nil {
return StackStatus{[]swarm.Service{}, err}
}
return StackStatus{services, nil}
}

View File

@ -228,6 +228,9 @@ func GetAppStatuses(appFiles AppFiles) (map[string]string, error) {
return statuses, nil
}
// GetAppComposeFiles retrieves a compose specification for a recipe. This
// specification is the result of a merge of all the compose.**.yml files in
// the recipe repository.
func GetAppComposeFiles(recipe string) (*composetypes.Config, error) {
pattern := fmt.Sprintf("%s/%s/compose**yml", APPS_DIR, recipe)
composeFiles, err := filepath.Glob(pattern)