forked from toolshed/abra
WIP: add first run at app rollback command
See coop-cloud/organising#146.
This commit is contained in:
@ -1,7 +1,13 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"coopcloud.tech/abra/pkg/client/stack"
|
||||
"coopcloud.tech/abra/pkg/config"
|
||||
apiclient "github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
// Get retrieves an app
|
||||
@ -18,3 +24,45 @@ func Get(appName string) (config.App, error) {
|
||||
|
||||
return app, nil
|
||||
}
|
||||
|
||||
// deployedServiceSpec represents a deployed service of an app.
|
||||
type deployedServiceSpec struct {
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
// VersionSpec represents a deployed app and associated metadata.
|
||||
type VersionSpec map[string]deployedServiceSpec
|
||||
|
||||
// DeployedVersions lists metadata (e.g. versions) for deployed
|
||||
func DeployedVersions(ctx context.Context, cl *apiclient.Client, app config.App) (VersionSpec, bool, error) {
|
||||
services, err := stack.GetStackServices(ctx, cl, app.StackName())
|
||||
if err != nil {
|
||||
return VersionSpec{}, false, err
|
||||
}
|
||||
|
||||
appSpec := make(VersionSpec)
|
||||
for _, service := range services {
|
||||
serviceName := ParseServiceName(service.Spec.Name)
|
||||
label := fmt.Sprintf("coop-cloud.%s.%s.version", app.StackName(), serviceName)
|
||||
if deployLabel, ok := service.Spec.Labels[label]; ok {
|
||||
version, _ := ParseVersionLabel(deployLabel)
|
||||
appSpec[serviceName] = deployedServiceSpec{Name: serviceName, Version: version}
|
||||
}
|
||||
}
|
||||
|
||||
return appSpec, len(services) > 0, nil
|
||||
}
|
||||
|
||||
// ParseVersionLabel parses a $VERSION-$DIGEST app 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:]
|
||||
}
|
||||
|
||||
// ParseVersionName parses a $STACK_NAME_$SERVICE_NAME service label.
|
||||
func ParseServiceName(label string) string {
|
||||
idx := strings.LastIndex(label, "_")
|
||||
return label[idx+1:]
|
||||
}
|
||||
|
Reference in New Issue
Block a user