refactor!: drop version, show versions in ps
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

See coop-cloud/organising#526
See coop-cloud/organising#502
This commit is contained in:
decentral1se 2024-07-08 11:42:53 +02:00
parent 72c021c727
commit 5426464092
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
5 changed files with 32 additions and 215 deletions

View File

@ -30,7 +30,6 @@ var AppCommand = cli.Command{
appServicesCommand,
appUndeployCommand,
appUpgradeCommand,
appVersionCommand,
appVolumeCommand,
},
}

View File

@ -50,23 +50,30 @@ var appPsCommand = cli.Command{
log.Fatalf("%s is not deployed?", app.Name)
}
chaosVersion := "false"
statuses, err := appPkg.GetAppStatuses([]appPkg.App{app}, true)
if statusMeta, ok := statuses[app.StackName()]; ok {
if _, exists := statusMeta["chaos"]; !exists {
isChaos, exists := statusMeta["chaos"]
if exists && isChaos == "false" {
if err := app.Recipe.EnsureVersion(deployedVersion); err != nil {
log.Fatal(err)
}
} else {
chaosVersion, err = app.Recipe.ChaosVersion()
if err != nil {
log.Fatal(err)
}
}
}
showPSOutput(app, cl)
showPSOutput(app, cl, deployedVersion, chaosVersion)
return nil
},
}
// showPSOutput renders ps output.
func showPSOutput(app appPkg.App, cl *dockerClient.Client) {
func showPSOutput(app appPkg.App, cl *dockerClient.Client, deployedVersion, chaosVersion string) {
r := recipe.Get(app.Name)
composeFiles, err := r.GetComposeFiles(app.Env)
if err != nil {
@ -101,6 +108,8 @@ func showPSOutput(app appPkg.App, cl *dockerClient.Client) {
var containerStats map[string]string
if len(containers) == 0 {
containerStats = map[string]string{
"version": deployedVersion,
"chaos": chaosVersion,
"service": service.Name,
"image": "unknown",
"created": "unknown",
@ -111,6 +120,8 @@ func showPSOutput(app appPkg.App, cl *dockerClient.Client) {
} else {
container := containers[0]
containerStats = map[string]string{
"version": deployedVersion,
"chaos": chaosVersion,
"service": abraService.ContainerToServiceName(container.Names, app.StackName()),
"image": formatter.RemoveSha(container.Image),
"created": formatter.HumanDuration(container.Created),
@ -123,6 +134,8 @@ func showPSOutput(app appPkg.App, cl *dockerClient.Client) {
allContainerStats[containerStats["service"]] = containerStats
tablerow := []string{
deployedVersion,
chaosVersion,
containerStats["service"],
containerStats["image"],
containerStats["created"],
@ -145,10 +158,11 @@ func showPSOutput(app appPkg.App, cl *dockerClient.Client) {
return
}
tableCol := []string{"service", "image", "created", "status", "state", "ports"}
tableCol := []string{"version", "chaos", "service", "image", "created", "status", "state", "ports"}
table := formatter.CreateTable(tableCol)
for _, row := range tablerows {
table.Append(row)
}
table.SetAutoMergeCellsByColumnIndex([]int{0, 1})
table.Render()
}

View File

@ -1,117 +0,0 @@
package app
import (
"context"
"sort"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/autocomplete"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"coopcloud.tech/abra/pkg/upstream/stack"
"github.com/distribution/reference"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
)
func sortServiceByName(versions [][]string) func(i, j int) bool {
return func(i, j int) bool {
// NOTE(d1): corresponds to the `tableCol` definition below
if versions[i][1] == "app" {
return true
}
return versions[i][1] < versions[j][1]
}
}
// 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)
path = formatter.StripTagMeta(path)
log.Debugf("parsed %s from %s", path, image)
return path, nil
}
var appVersionCommand = cli.Command{
Name: "version",
Aliases: []string{"v"},
ArgsUsage: "<domain>",
Flags: []cli.Flag{
internal.DebugFlag,
internal.NoInputFlag,
internal.OfflineFlag,
},
Before: internal.SubCommandBefore,
Usage: "Show version info of a deployed app",
BashComplete: autocomplete.AppNameComplete,
Action: func(c *cli.Context) error {
app := internal.ValidateApp(c)
stackName := app.StackName()
cl, err := client.New(app.Server)
if err != nil {
log.Fatal(err)
}
log.Debugf("checking whether %s is already deployed", stackName)
isDeployed, deployedVersion, err := stack.IsDeployed(context.Background(), cl, stackName)
if err != nil {
log.Fatal(err)
}
if !isDeployed {
log.Fatalf("%s is not deployed?", app.Name)
}
if deployedVersion == "unknown" {
log.Fatalf("failed to determine version of deployed %s", app.Name)
}
recipeMeta, err := recipe.GetRecipeMeta(app.Recipe.Name, internal.Offline)
if err != nil {
log.Fatal(err)
}
versionsMeta := make(map[string]recipe.ServiceMeta)
for _, recipeVersion := range recipeMeta.Versions {
if currentVersion, exists := recipeVersion[deployedVersion]; exists {
versionsMeta = currentVersion
}
}
if len(versionsMeta) == 0 {
log.Fatalf("could not retrieve deployed version (%s) from recipe catalogue?", deployedVersion)
}
tableCol := []string{"version", "service", "image", "tag"}
table := formatter.CreateTable(tableCol)
var versions [][]string
for serviceName, versionMeta := range versionsMeta {
versions = append(versions, []string{deployedVersion, serviceName, versionMeta.Image, versionMeta.Tag})
}
sort.Slice(versions, sortServiceByName(versions))
for _, version := range versions {
table.Append(version)
}
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.Render()
return nil
},
}

View File

@ -41,8 +41,22 @@ teardown(){
@test "show ps report" {
_deploy_app
latestRelease=$(git -C "$ABRA_DIR/recipes/$TEST_RECIPE" tag -l | tail -n 1)
run $ABRA app ps "$TEST_APP_DOMAIN"
assert_success
assert_output --partial 'app'
assert_output --partial 'healthy'
assert_output --partial "$latestRelease"
assert_output --partial 'false' # not a chaos deploy
}
# bats test_tags=slow
@test "show ps report with chaos deploy" {
run $ABRA app deploy "$TEST_APP_DOMAIN" \
--no-input --no-converge-checks --chaos
assert_success
assert_output --partial "$latestRelease"
assert_output --partial 'true' # is a chaos deploy
}

View File

@ -1,93 +0,0 @@
#!/usr/bin/env bash
setup_file(){
load "$PWD/tests/integration/helpers/common"
_common_setup
_add_server
_new_app
}
teardown_file(){
_rm_app
_rm_server
}
setup(){
load "$PWD/tests/integration/helpers/common"
_common_setup
}
teardown(){
_undeploy_app
_reset_recipe
}
@test "validate app argument" {
run $ABRA app version
assert_failure
assert_output --partial 'no app provided'
run $ABRA app version DOESNTEXIST
assert_failure
assert_output --partial 'cannot find app'
}
@test "error if not deployed" {
run $ABRA app version "$TEST_APP_DOMAIN"
assert_failure
assert_output --partial 'is not deployed'
}
# bats test_tags=slow
@test "error if version unknown" {
run sed -i '/coop-cloud.${STACK_NAME}.version=.*/d' "$ABRA_DIR/recipes/$TEST_RECIPE/compose.yml"
assert_success
run $ABRA app deploy "$TEST_APP_DOMAIN" \
--no-input --no-converge-checks --chaos
assert_success
run $ABRA app version "$TEST_APP_DOMAIN"
assert_failure
assert_output --partial 'failed to determine'
}
# bats test_tags=slow
@test "error if not in catalogue" {
_deploy_app
run $ABRA app version "$TEST_APP_DOMAIN"
assert_failure
assert_output --partial 'does not exist'
}
# bats test_tags=slow
@test "list version" {
appDomain="custom-html.$TEST_SERVER"
run $ABRA app new custom-html \
--no-input \
--server "$TEST_SERVER" \
--domain "$appDomain"
latestVersion=$(git -C "$ABRA_DIR/recipes/custom-html" tag | tail -n 1)
refute [ -z "$latestVersion" ];
run $ABRA app deploy "$appDomain" --no-input --no-converge-checks
assert_success
assert_output --partial "$latestVersion"
run $ABRA app version "$appDomain"
assert_success
assert_output --partial "$latestVersion"
run $ABRA app undeploy "$appDomain" --no-input
assert_success
run $ABRA app volume remove "$appDomain" --no-input
assert_success
run $ABRA app remove "$appDomain" --no-input
assert_success
assert_not_exists "$ABRA_DIR/servers/$TEST_SERVER/$appDomain.env"
}