2016-10-19 12:22:02 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-19 20:41:11 -05:00
|
|
|
"io"
|
2016-10-19 12:22:02 -04:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-11-03 17:01:54 -04:00
|
|
|
"github.com/docker/docker/opts"
|
2016-11-19 20:41:11 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-11-03 17:01:54 -04:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-11-09 19:59:01 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-10-19 12:22:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
2016-11-03 17:01:54 -04:00
|
|
|
name string
|
2016-11-19 20:41:11 -05:00
|
|
|
file string
|
2016-11-03 17:01:54 -04:00
|
|
|
labels opts.ListOpts
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSecretCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-11-03 17:01:54 -04:00
|
|
|
createOpts := createOptions{
|
2016-12-23 14:09:12 -05:00
|
|
|
labels: opts.NewListOpts(opts.ValidateEnv),
|
2016-11-03 17:01:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2017-01-06 15:06:02 -05:00
|
|
|
Use: "create [OPTIONS] SECRET file|-",
|
2016-11-19 20:41:11 -05:00
|
|
|
Short: "Create a secret from a file or STDIN as content",
|
2017-01-06 15:06:02 -05:00
|
|
|
Args: cli.ExactArgs(2),
|
2016-10-19 12:22:02 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-03 17:01:54 -04:00
|
|
|
createOpts.name = args[0]
|
2017-01-06 15:06:02 -05:00
|
|
|
createOpts.file = args[1]
|
2016-11-03 17:01:54 -04:00
|
|
|
return runSecretCreate(dockerCli, createOpts)
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
}
|
2016-11-03 17:01:54 -04:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.VarP(&createOpts.labels, "label", "l", "Secret labels")
|
|
|
|
|
|
|
|
return cmd
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2016-11-03 17:01:54 -04:00
|
|
|
func runSecretCreate(dockerCli *command.DockerCli, options createOptions) error {
|
2016-10-19 12:22:02 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-11-19 20:41:11 -05:00
|
|
|
var in io.Reader = dockerCli.In()
|
|
|
|
if options.file != "-" {
|
|
|
|
file, err := system.OpenSequential(options.file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
in = file
|
|
|
|
defer file.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
secretData, err := ioutil.ReadAll(in)
|
2016-10-19 12:22:02 -04:00
|
|
|
if err != nil {
|
2016-11-19 20:41:11 -05:00
|
|
|
return fmt.Errorf("Error reading content from %q: %v", options.file, err)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
spec := swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
2016-11-03 17:01:54 -04:00
|
|
|
Name: options.name,
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
Data: secretData,
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := client.SecretCreate(ctx, spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), r.ID)
|
|
|
|
return nil
|
|
|
|
}
|