2016-10-04 15:01:19 -04:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-10-04 15:01:19 -04:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-08-30 18:36:58 -04:00
|
|
|
"github.com/distribution/reference"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-05-12 07:18:48 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2016-10-04 15:01:19 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2017-08-07 05:52:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-10-04 15:01:19 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validateTag checks if the given repoName can be resolved.
|
|
|
|
func validateTag(rawRepo string) error {
|
2017-01-11 16:54:52 -05:00
|
|
|
_, err := reference.ParseNormalizedNamed(rawRepo)
|
2016-10-04 15:01:19 -04:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:51:47 -05:00
|
|
|
// validateConfig ensures that a valid config.json is available in the given path
|
|
|
|
func validateConfig(path string) error {
|
|
|
|
dt, err := os.Open(filepath.Join(path, "config.json"))
|
2016-10-04 15:01:19 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:51:47 -05:00
|
|
|
m := types.PluginConfig{}
|
2016-10-04 15:01:19 -04:00
|
|
|
err = json.NewDecoder(dt).Decode(&m)
|
|
|
|
dt.Close()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateContextDir validates the given dir and returns abs path on success.
|
|
|
|
func validateContextDir(contextDir string) (string, error) {
|
|
|
|
absContextDir, err := filepath.Abs(contextDir)
|
2016-11-29 03:17:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-10-04 15:01:19 -04:00
|
|
|
stat, err := os.Lstat(absContextDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
2017-03-09 13:23:45 -05:00
|
|
|
return "", errors.Errorf("context must be a directory")
|
2016-10-04 15:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return absContextDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type pluginCreateOptions struct {
|
|
|
|
repoName string
|
|
|
|
context string
|
|
|
|
compress bool
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:18:27 -04:00
|
|
|
func newCreateCommand(dockerCli command.Cli) *cobra.Command {
|
2016-10-04 15:01:19 -04:00
|
|
|
options := pluginCreateOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-12-12 18:05:53 -05:00
|
|
|
Use: "create [OPTIONS] PLUGIN PLUGIN-DATA-DIR",
|
|
|
|
Short: "Create a plugin from a rootfs and configuration. Plugin data directory must contain config.json and rootfs directory.",
|
2016-10-04 15:01:19 -04:00
|
|
|
Args: cli.RequiresMinArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
options.repoName = args[0]
|
|
|
|
options.context = args[1]
|
2023-09-09 18:27:44 -04:00
|
|
|
return runCreate(cmd.Context(), dockerCli, options)
|
2016-10-04 15:01:19 -04:00
|
|
|
},
|
2022-05-12 07:18:48 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-10-04 15:01:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.BoolVar(&options.compress, "compress", false, "Compress the context using gzip")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runCreate(ctx context.Context, dockerCli command.Cli, options pluginCreateOptions) error {
|
2016-10-04 15:01:19 -04:00
|
|
|
var (
|
|
|
|
createCtx io.ReadCloser
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := validateTag(options.repoName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
absContextDir, err := validateContextDir(options.context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-07 21:51:47 -05:00
|
|
|
if err := validateConfig(options.context); err != nil {
|
2016-10-04 15:01:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
compression := archive.Uncompressed
|
|
|
|
if options.compress {
|
|
|
|
logrus.Debugf("compression enabled")
|
|
|
|
compression = archive.Gzip
|
|
|
|
}
|
|
|
|
|
|
|
|
createCtx, err = archive.TarWithOptions(absContextDir, &archive.TarOptions{
|
|
|
|
Compression: compression,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
createOptions := types.PluginCreateOptions{RepoName: options.repoName}
|
|
|
|
if err = dockerCli.Client().PluginCreate(ctx, createCtx, createOptions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Out(), options.repoName)
|
|
|
|
return nil
|
|
|
|
}
|