Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Add inspect command to display info of running application only
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <guillaume.lours@docker.com>
  • Loading branch information
glours committed Nov 4, 2019
1 parent bb47af9 commit 269b4f0
Show file tree
Hide file tree
Showing 24 changed files with 1,347 additions and 23 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ required = ["github.com/wadey/gocovmerge"]
name = "github.com/wadey/gocovmerge"
branch = "master"

[[constraint]]
name = "github.com/dustin/go-humanize"
version = "1.0.0"

[prune]
non-go = true
unused-packages = true
Expand Down
2 changes: 1 addition & 1 deletion cmd/cnab-run/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func inspectAction(instanceName string) error {
}

parameters := packager.ExtractCNABParametersValues(packager.ExtractCNABParameterMapping(app.Parameters()), os.Environ())
return appinspect.Inspect(os.Stdout, app, parameters, imageMap)
return appinspect.ImageInspect(os.Stdout, app, parameters, imageMap)
}
1 change: 1 addition & 0 deletions cmd/cnab-run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
"uninstall": uninstallAction,
internal.ActionStatusNameDeprecated: statusAction,
internal.ActionStatusName: statusAction,
internal.ActionStatusJSONName: statusJSONAction,
internal.ActionInspectName: inspectAction,
internal.ActionRenderName: renderAction,
}
Expand Down
40 changes: 36 additions & 4 deletions cmd/cnab-run/status.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/docker/app/internal"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/opts"
swarmtypes "github.com/docker/docker/api/types/swarm"
"github.com/pkg/errors"
)

func statusAction(instanceName string) error {
cli, err := getCli()
if err != nil {
return err
}
services, _ := runningServices(cli, instanceName)
fmt.Fprintln(cli.Out(), services)
return nil
}

func statusJSONAction(instanceName string) error {
cli, err := getCli()
if err != nil {
return err
}
services, _ := runningServices(cli, instanceName)
js, err := json.MarshalIndent(services, "", " ")
if err != nil {
return err
}
fmt.Fprintln(cli.Out(), string(js))
return nil
}

func getCli() (command.Cli, error) {
cli, err := setupDockerContext()
if err != nil {
return errors.Wrap(err, "unable to restore docker context")
return nil, errors.Wrap(err, "unable to restore docker context")
}
return cli, nil
}

func runningServices(cli command.Cli, instanceName string) ([]swarmtypes.Service, error) {
orchestratorRaw := os.Getenv(internal.DockerStackOrchestratorEnvVar)
orchestrator, err := cli.StackOrchestrator(orchestratorRaw)
if err != nil {
return err
return nil, err
}
return stack.RunServices(cli, getFlagset(orchestrator), orchestrator, options.Services{
Namespace: instanceName,
return stack.GetServices(cli, getFlagset(orchestrator), orchestrator, options.Services{
Filter: opts.NewFilterOpt(),
Namespace: instanceName,
})
}
134 changes: 134 additions & 0 deletions internal/commands/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package commands

import (
"fmt"
"os"
"strings"

"github.com/deislabs/cnab-go/action"
"github.com/docker/app/internal"
"github.com/docker/app/internal/cnab"
"github.com/docker/app/internal/inspect"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/opts"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

type inspectOptions struct {
credentialOptions
pretty bool
orchestrator string
kubeNamespace string
}

func inspectCmd(dockerCli command.Cli) *cobra.Command {
var opts inspectOptions
cmd := &cobra.Command{
Use: "inspect [OPTIONS] RUNNING_APP",
Short: "Shows installation and application metadata, parameters and the containers list of a running application",
Example: `$ docker app inspect my-running-app
$ docker app inspect my-running-app:1.0.0`,
Args: cli.RequiresMaxArgs(1),
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runInspect(dockerCli, firstOrEmpty(args), opts)
},
}
cmd.Flags().BoolVar(&opts.pretty, "pretty", false, "Pretty print the output")
cmd.Flags().StringVar(&opts.orchestrator, "orchestrator", "", "Orchestrator where the App is running on (swarm, kubernetes)")
cmd.Flags().StringVar(&opts.kubeNamespace, "namespace", "default", "Kubernetes namespace in which to find the App")
opts.credentialOptions.addFlags(cmd.Flags())
return cmd
}

func runInspect(dockerCli command.Cli, appName string, inspectOptions inspectOptions) error {
orchestrator, err := getContextOrchestrator(dockerCli, inspectOptions.orchestrator)
if err != nil {
return err
}
services, err := stack.GetServices(dockerCli, pflag.NewFlagSet("", pflag.ContinueOnError), orchestrator, options.Services{
Filter: opts.NewFilterOpt(),
Namespace: inspectOptions.kubeNamespace,
})
if err != nil {
return err
}
println(services)

inspectOptions.SetDefaultTargetContext(dockerCli)
defer muteDockerCli(dockerCli)()
_, installationStore, credentialStore, err := prepareStores(inspectOptions.targetContext)
if err != nil {
return err
}
installation, err := installationStore.Read(appName)
if err != nil {
return err
}

orchestratorName, ok := installation.Parameters[internal.ParameterOrchestratorName].(string)
if !ok || orchestratorName == "" {
orchestratorName = string(orchestrator)
}

format := "json"
actionName := internal.ActionStatusJSONName
if inspectOptions.pretty {
format = "pretty"
actionName = internal.ActionStatusName
}

if err := inspect.Inspect(os.Stdout, installation.Claim, format, orchestratorName); err != nil {
return err
}

var statusAction bool
for key := range installation.Bundle.Actions {
if strings.HasPrefix(key, "io.cnab.status") {
statusAction = true
}
}
if !statusAction {
return nil
}

bind, err := cnab.RequiredBindMount(inspectOptions.targetContext, orchestratorName, dockerCli.ContextStore())
if err != nil {
return err
}

driverImpl, errBuf := cnab.PrepareDriver(dockerCli, bind, nil)
a := &action.RunCustom{
Action: actionName,
Driver: driverImpl,
}

creds, err := prepareCredentialSet(installation.Bundle, inspectOptions.CredentialSetOpts(dockerCli, credentialStore)...)
if err != nil {
return err
}

installation.SetParameter(internal.ParameterInspectFormatName, format)
println()
if err := a.Run(&installation.Claim, creds, nil); err != nil {
return fmt.Errorf("inspect failed: %s\n%s", err, errBuf)
}
return nil
}

func getContextOrchestrator(dockerCli command.Cli, orchestratorFlag string) (command.Orchestrator, error) {
var orchestrator command.Orchestrator
orchestrator, _ = command.NormalizeOrchestrator(orchestratorFlag)
if string(orchestrator) == "" {
orchestrator, err := dockerCli.StackOrchestrator("")
if err != nil {
return "", err
}
return orchestrator, nil
}
return orchestrator, nil
}
1 change: 1 addition & 0 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
pullCmd(dockerCli),
image.Cmd(dockerCli),
build.Cmd(dockerCli),
inspectCmd(dockerCli),
)
}

Expand Down
Loading

0 comments on commit 269b4f0

Please sign in to comment.