Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 4f41507

Browse files
authored
Merge pull request #492 from silvin-lubecki/render-custom-action
Move Render command as a CNAB custom action
2 parents 7d5ceb6 + 4822d4f commit 4f41507

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+628
-453
lines changed

Gopkg.lock

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ required = ["github.com/wadey/gocovmerge"]
4040
name = "github.com/docker/cli"
4141
branch = "master"
4242

43+
# Waiting for https://github.com/deislabs/duffle/pull/664 to be merged
4344
[[override]]
4445
name = "github.com/deislabs/duffle"
45-
branch = "master"
46+
source = "github.com/silvin-lubecki/duffle"
47+
branch = "docker-app"
4648

4749
[[constraint]]
4850
name = "github.com/sirupsen/logrus"

cmd/cnab-run/env.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
)
1313

1414
const (
15-
envVarOchestrator = "DOCKER_STACK_ORCHESTRATOR"
1615
fileDockerContext = "/cnab/app/context.dockercontext"
1716
)
1817

cmd/cnab-run/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/docker/app/internal/packager"
88
)
99

10-
func inspect() error {
10+
func inspectAction(instanceName string) error {
1111
app, err := packager.Extract("")
1212
// todo: merge additional compose file
1313
if err != nil {

cmd/cnab-run/install.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
"github.com/deislabs/duffle/pkg/bundle"
9+
"github.com/docker/app/internal"
910
"github.com/docker/app/internal/packager"
1011
"github.com/docker/app/render"
1112
"github.com/docker/cli/cli/command"
@@ -22,7 +23,7 @@ const (
2223
imageMapFilePath = "/cnab/app/image-map.json"
2324
)
2425

25-
func install(instanceName string) error {
26+
func installAction(instanceName string) error {
2627
cli, err := setupDockerContext()
2728
if err != nil {
2829
return errors.Wrap(err, "unable to restore docker context")
@@ -34,7 +35,7 @@ func install(instanceName string) error {
3435
}
3536
defer app.Cleanup()
3637

37-
orchestratorRaw := os.Getenv(envVarOchestrator)
38+
orchestratorRaw := os.Getenv(internal.DockerStackOrchestratorEnvVar)
3839
orchestrator, err := cli.StackOrchestrator(orchestratorRaw)
3940
if err != nil {
4041
return err
@@ -62,7 +63,7 @@ func install(instanceName string) error {
6263
func getFlagset(orchestrator command.Orchestrator) *pflag.FlagSet {
6364
result := pflag.NewFlagSet("", pflag.ContinueOnError)
6465
if orchestrator == command.OrchestratorKubernetes {
65-
result.String("namespace", os.Getenv("DOCKER_KUBERNETES_NAMESPACE"), "")
66+
result.String("namespace", os.Getenv(internal.DockerKubernetesNamespaceEnvVar), "")
6667
}
6768
return result
6869
}

cmd/cnab-run/main.go

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,42 @@ import (
88
"github.com/docker/app/internal"
99
)
1010

11-
type cnabAction string
12-
13-
const (
14-
cnabActionInstall = cnabAction("install")
15-
cnabActionUninstall = cnabAction("uninstall")
16-
cnabActionUpgrade = cnabAction("upgrade")
17-
cnabActionStatus = cnabAction(internal.Namespace + "status")
18-
cnabActionInspect = cnabAction(internal.Namespace + "inspect")
19-
)
20-
21-
type cnabOperation struct {
22-
action cnabAction
23-
installation string
24-
}
25-
26-
func getCnabAction() (cnabAction, error) {
27-
action, ok := os.LookupEnv("CNAB_ACTION")
28-
if !ok {
29-
return "", errors.New("no CNAB action specified")
11+
type cnabAction func(string) error
12+
13+
var (
14+
cnabActions = map[string]cnabAction{
15+
"install": installAction,
16+
"upgrade": installAction,
17+
"uninstall": uninstallAction,
18+
internal.Namespace + "status": statusAction,
19+
internal.Namespace + "inspect": inspectAction,
20+
internal.Namespace + "render": renderAction,
3021
}
31-
return cnabAction(action), nil
32-
}
22+
)
3323

34-
func getCnabOperation() (cnabOperation, error) {
24+
func getCnabAction() (cnabAction, string, error) {
3525
// CNAB_ACTION should always be set. but in future we want to have
3626
// claim-less actions. So we don't fail if no installation is set
37-
action, err := getCnabAction()
38-
if err != nil {
39-
return cnabOperation{}, err
27+
actionName, ok := os.LookupEnv("CNAB_ACTION")
28+
if !ok {
29+
return nil, "", errors.New("no CNAB action specified")
30+
}
31+
action, ok := cnabActions[actionName]
32+
if !ok {
33+
return nil, "", fmt.Errorf("action %q not supported", actionName)
4034
}
41-
return cnabOperation{
42-
action: action,
43-
installation: os.Getenv("CNAB_INSTALLATION_NAME"),
44-
}, nil
35+
return action, actionName, nil
4536
}
4637

4738
func main() {
48-
op, err := getCnabOperation()
39+
action, actionName, err := getCnabAction()
4940
if err != nil {
5041
fmt.Fprintf(os.Stderr, "Error while parsing cnab operation: %s", err)
5142
os.Exit(1)
5243
}
53-
switch op.action {
54-
case cnabActionInstall:
55-
if err := install(op.installation); err != nil {
56-
fmt.Fprintf(os.Stderr, "Install failed: %s", err)
57-
os.Exit(1)
58-
}
59-
case cnabActionUpgrade:
60-
if err := install(op.installation); err != nil {
61-
fmt.Fprintf(os.Stderr, "Upgrade failed: %s", err)
62-
os.Exit(1)
63-
}
64-
case cnabActionUninstall:
65-
if err := uninstall(op.installation); err != nil {
66-
fmt.Fprintf(os.Stderr, "Uninstall failed: %s", err)
67-
os.Exit(1)
68-
}
69-
case cnabActionStatus:
70-
if err := status(op.installation); err != nil {
71-
fmt.Fprintf(os.Stderr, "Status failed: %s", err)
72-
os.Exit(1)
73-
}
74-
case cnabActionInspect:
75-
if err := inspect(); err != nil {
76-
fmt.Fprintf(os.Stderr, "Inspect failed: %s", err)
77-
os.Exit(1)
78-
}
79-
default:
80-
fmt.Fprintf(os.Stderr, "Action %q is not supported", op.action)
44+
instanceName := os.Getenv("CNAB_INSTALLATION_NAME")
45+
if err := action(instanceName); err != nil {
46+
fmt.Fprintf(os.Stderr, "Action %q failed: %s", actionName, err)
8147
os.Exit(1)
8248
}
8349
}

cmd/cnab-run/render.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/docker/app/internal"
8+
9+
"github.com/docker/app/internal/formatter"
10+
"github.com/docker/app/internal/packager"
11+
"github.com/docker/app/render"
12+
)
13+
14+
func renderAction(instanceName string) error {
15+
app, err := packager.Extract("")
16+
// todo: merge additional compose file
17+
if err != nil {
18+
return err
19+
}
20+
defer app.Cleanup()
21+
22+
imageMap, err := getBundleImageMap()
23+
if err != nil {
24+
return err
25+
}
26+
27+
formatDriver, ok := os.LookupEnv(internal.DockerRenderFormatEnvVar)
28+
if !ok {
29+
return fmt.Errorf("%q is undefined", internal.DockerRenderFormatEnvVar)
30+
}
31+
32+
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
33+
34+
rendered, err := render.Render(app, parameters, imageMap)
35+
if err != nil {
36+
return err
37+
}
38+
res, err := formatter.Format(rendered, formatDriver)
39+
if err != nil {
40+
return err
41+
}
42+
fmt.Printf(res)
43+
44+
return nil
45+
}

cmd/cnab-run/status.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ package main
33
import (
44
"os"
55

6+
"github.com/docker/app/internal"
67
"github.com/docker/cli/cli/command/stack"
78
"github.com/docker/cli/cli/command/stack/options"
89
"github.com/docker/cli/opts"
910
"github.com/pkg/errors"
1011
)
1112

12-
func status(instanceName string) error {
13+
func statusAction(instanceName string) error {
1314
cli, err := setupDockerContext()
1415
if err != nil {
1516
return errors.Wrap(err, "unable to restore docker context")
1617
}
17-
orchestratorRaw := os.Getenv(envVarOchestrator)
18+
orchestratorRaw := os.Getenv(internal.DockerStackOrchestratorEnvVar)
1819
orchestrator, err := cli.StackOrchestrator(orchestratorRaw)
1920
if err != nil {
2021
return err

cmd/cnab-run/uninstall.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package main
33
import (
44
"os"
55

6+
"github.com/docker/app/internal"
67
"github.com/docker/cli/cli/command/stack"
78
"github.com/docker/cli/cli/command/stack/options"
89
"github.com/pkg/errors"
910
)
1011

11-
func uninstall(instanceName string) error {
12+
func uninstallAction(instanceName string) error {
1213
cli, err := setupDockerContext()
1314
if err != nil {
1415
return errors.Wrap(err, "unable to restore docker context")
1516
}
16-
orchestratorRaw := os.Getenv(envVarOchestrator)
17+
orchestratorRaw := os.Getenv(internal.DockerStackOrchestratorEnvVar)
1718
orchestrator, err := cli.StackOrchestrator(orchestratorRaw)
1819
if err != nil {
1920
return err

e2e/cnab_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestCallCustomStatusAction(t *testing.T) {
2828
{
2929
name: "missingCustomStatusAction",
3030
exitCode: 1,
31-
expectedOutput: "Status failed: action not defined for bundle",
31+
expectedOutput: "status failed: action not defined for bundle",
3232
cnab: "cnab-without-status",
3333
},
3434
}

0 commit comments

Comments
 (0)