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

Commit 240f7a8

Browse files
gloursJean-Christophe Sirot
authored andcommitted
Add inspect command to display info of running application only
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 3f6a041 commit 240f7a8

File tree

24 files changed

+1347
-23
lines changed

24 files changed

+1347
-23
lines changed

Gopkg.lock

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

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ required = ["github.com/wadey/gocovmerge"]
131131
name = "github.com/jackpal/gateway"
132132
revision = "10816db7ab2f4b71e71dba9420bfa67d5be27b33"
133133

134+
[[constraint]]
135+
name = "github.com/dustin/go-humanize"
136+
version = "1.0.0"
137+
134138
[prune]
135139
non-go = true
136140
unused-packages = true

cmd/cnab-run/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ func inspectAction(instanceName string) error {
2121
}
2222

2323
parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
24-
return appinspect.Inspect(os.Stdout, app, parameters, imageMap)
24+
return appinspect.ImageInspect(os.Stdout, app, parameters, imageMap)
2525
}

cmd/cnab-run/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
"uninstall": uninstallAction,
1818
internal.ActionStatusNameDeprecated: statusAction,
1919
internal.ActionStatusName: statusAction,
20+
internal.ActionStatusJSONName: statusJSONAction,
2021
internal.ActionInspectName: inspectAction,
2122
internal.ActionRenderName: renderAction,
2223
}

cmd/cnab-run/status.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"os"
57

68
"github.com/docker/app/internal"
9+
"github.com/docker/cli/cli/command"
710
"github.com/docker/cli/cli/command/stack"
811
"github.com/docker/cli/cli/command/stack/options"
912
"github.com/docker/cli/opts"
13+
swarmtypes "github.com/docker/docker/api/types/swarm"
1014
"github.com/pkg/errors"
1115
)
1216

1317
func statusAction(instanceName string) error {
18+
cli, err := getCli()
19+
if err != nil {
20+
return err
21+
}
22+
services, _ := runningServices(cli, instanceName)
23+
fmt.Fprintln(cli.Out(), services)
24+
return nil
25+
}
26+
27+
func statusJSONAction(instanceName string) error {
28+
cli, err := getCli()
29+
if err != nil {
30+
return err
31+
}
32+
services, _ := runningServices(cli, instanceName)
33+
js, err := json.MarshalIndent(services, "", " ")
34+
if err != nil {
35+
return err
36+
}
37+
fmt.Fprintln(cli.Out(), string(js))
38+
return nil
39+
}
40+
41+
func getCli() (command.Cli, error) {
1442
cli, err := setupDockerContext()
1543
if err != nil {
16-
return errors.Wrap(err, "unable to restore docker context")
44+
return nil, errors.Wrap(err, "unable to restore docker context")
1745
}
46+
return cli, nil
47+
}
48+
49+
func runningServices(cli command.Cli, instanceName string) ([]swarmtypes.Service, error) {
1850
orchestratorRaw := os.Getenv(internal.DockerStackOrchestratorEnvVar)
1951
orchestrator, err := cli.StackOrchestrator(orchestratorRaw)
2052
if err != nil {
21-
return err
53+
return nil, err
2254
}
23-
return stack.RunServices(cli, getFlagset(orchestrator), orchestrator, options.Services{
24-
Namespace: instanceName,
55+
return stack.GetServices(cli, getFlagset(orchestrator), orchestrator, options.Services{
2556
Filter: opts.NewFilterOpt(),
57+
Namespace: instanceName,
2658
})
2759
}

internal/commands/inspect.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/deislabs/cnab-go/action"
9+
"github.com/docker/app/internal"
10+
"github.com/docker/app/internal/cnab"
11+
"github.com/docker/app/internal/inspect"
12+
"github.com/docker/cli/cli"
13+
"github.com/docker/cli/cli/command"
14+
"github.com/docker/cli/cli/command/stack"
15+
"github.com/docker/cli/cli/command/stack/options"
16+
"github.com/docker/cli/opts"
17+
"github.com/spf13/cobra"
18+
"github.com/spf13/pflag"
19+
)
20+
21+
type inspectOptions struct {
22+
credentialOptions
23+
pretty bool
24+
orchestrator string
25+
kubeNamespace string
26+
}
27+
28+
func inspectCmd(dockerCli command.Cli) *cobra.Command {
29+
var opts inspectOptions
30+
cmd := &cobra.Command{
31+
Use: "inspect [OPTIONS] RUNNING_APP",
32+
Short: "Shows installation and application metadata, parameters and the containers list of a running application",
33+
Example: `$ docker app inspect my-running-app
34+
$ docker app inspect my-running-app:1.0.0`,
35+
Args: cli.RequiresMaxArgs(1),
36+
Hidden: true,
37+
RunE: func(cmd *cobra.Command, args []string) error {
38+
return runInspect(dockerCli, firstOrEmpty(args), opts)
39+
},
40+
}
41+
cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Pretty print the output")
42+
cmd.Flags().StringVar(&opts.orchestrator, "orchestrator", "", "Orchestrator where the App is running on (swarm, kubernetes)")
43+
cmd.Flags().StringVar(&opts.kubeNamespace, "namespace", "default", "Kubernetes namespace in which to find the App")
44+
opts.credentialOptions.addFlags(cmd.Flags())
45+
return cmd
46+
}
47+
48+
func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOptions) error {
49+
orchestrator, err := getContextOrchestrator(dockerCli, inspectOptions.orchestrator)
50+
if err != nil {
51+
return err
52+
}
53+
services, err := stack.GetServices(dockerCli, pflag.NewFlagSet("", pflag.ContinueOnError), orchestrator, options.Services{
54+
Filter: opts.NewFilterOpt(),
55+
Namespace: inspectOptions.kubeNamespace,
56+
})
57+
if err != nil {
58+
return err
59+
}
60+
println(services)
61+
62+
inspectOptions.SetDefaultTargetContext(dockerCli)
63+
defer muteDockerCli(dockerCli)()
64+
_, installationStore, credentialStore, err := prepareStores(inspectOptions.targetContext)
65+
if err != nil {
66+
return err
67+
}
68+
installation, err := installationStore.Read(appName)
69+
if err != nil {
70+
return err
71+
}
72+
73+
orchestratorName, ok := installation.Parameters[internal.ParameterOrchestratorName].(string)
74+
if !ok || orchestratorName == "" {
75+
orchestratorName = string(orchestrator)
76+
}
77+
78+
format := "json"
79+
actionName := internal.ActionStatusJSONName
80+
if inspectOptions.pretty {
81+
format = "pretty"
82+
actionName = internal.ActionStatusName
83+
}
84+
85+
if err := inspect.Inspect(os.Stdout, installation.Claim, format, orchestratorName); err != nil {
86+
return err
87+
}
88+
89+
var statusAction bool
90+
for key := range installation.Bundle.Actions {
91+
if strings.HasPrefix(key, "io.cnab.status") {
92+
statusAction = true
93+
}
94+
}
95+
if !statusAction {
96+
return nil
97+
}
98+
99+
bind, err := cnab.RequiredBindMount(inspectOptions.targetContext, orchestratorName, dockerCli.ContextStore())
100+
if err != nil {
101+
return err
102+
}
103+
104+
driverImpl, errBuf := cnab.PrepareDriver(dockerCli, bind, nil)
105+
a := &action.RunCustom{
106+
Action: actionName,
107+
Driver: driverImpl,
108+
}
109+
110+
creds, err := prepareCredentialSet(installation.Bundle, inspectOptions.CredentialSetOpts(dockerCli, credentialStore)...)
111+
if err != nil {
112+
return err
113+
}
114+
115+
installation.SetParameter(internal.ParameterInspectFormatName, format)
116+
println()
117+
if err := a.Run(&installation.Claim, creds, nil); err != nil {
118+
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
119+
}
120+
return nil
121+
}
122+
123+
func getContextOrchestrator(dockerCli command.Cli, orchestratorFlag string) (command.Orchestrator, error) {
124+
var orchestrator command.Orchestrator
125+
orchestrator, _ = command.NormalizeOrchestrator(orchestratorFlag)
126+
if string(orchestrator) == "" {
127+
orchestrator, err := dockerCli.StackOrchestrator("")
128+
if err != nil {
129+
return "", err
130+
}
131+
return orchestrator, nil
132+
}
133+
return orchestrator, nil
134+
}

internal/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
5959
pullCmd(dockerCli),
6060
image.Cmd(dockerCli),
6161
build.Cmd(dockerCli),
62+
inspectCmd(dockerCli),
6263
)
6364
}
6465

0 commit comments

Comments
 (0)