-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
create.go
128 lines (103 loc) · 2.81 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package plugin
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// validateTag checks if the given repoName can be resolved.
func validateTag(rawRepo string) error {
_, err := reference.ParseNormalizedNamed(rawRepo)
return err
}
// 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"))
if err != nil {
return err
}
m := types.PluginConfig{}
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)
if err != nil {
return "", err
}
stat, err := os.Lstat(absContextDir)
if err != nil {
return "", err
}
if !stat.IsDir() {
return "", errors.Errorf("context must be a directory")
}
return absContextDir, nil
}
type pluginCreateOptions struct {
repoName string
context string
compress bool
}
func newCreateCommand(dockerCli command.Cli) *cobra.Command {
options := pluginCreateOptions{}
cmd := &cobra.Command{
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.",
Args: cli.RequiresMinArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
options.repoName = args[0]
options.context = args[1]
return runCreate(dockerCli, options)
},
}
flags := cmd.Flags()
flags.BoolVar(&options.compress, "compress", false, "Compress the context using gzip")
return cmd
}
func runCreate(dockerCli command.Cli, options pluginCreateOptions) error {
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
}
if err := validateConfig(options.context); err != nil {
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
}
ctx := context.Background()
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
}