|
| 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 | +} |
0 commit comments