create.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package plugin
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "github.com/Sirupsen/logrus"
  9. "github.com/docker/distribution/reference"
  10. "github.com/docker/docker/api/types"
  11. "github.com/docker/docker/cli"
  12. "github.com/docker/docker/cli/command"
  13. "github.com/docker/docker/pkg/archive"
  14. "github.com/pkg/errors"
  15. "github.com/spf13/cobra"
  16. "golang.org/x/net/context"
  17. )
  18. // validateTag checks if the given repoName can be resolved.
  19. func validateTag(rawRepo string) error {
  20. _, err := reference.ParseNormalizedNamed(rawRepo)
  21. return err
  22. }
  23. // validateConfig ensures that a valid config.json is available in the given path
  24. func validateConfig(path string) error {
  25. dt, err := os.Open(filepath.Join(path, "config.json"))
  26. if err != nil {
  27. return err
  28. }
  29. m := types.PluginConfig{}
  30. err = json.NewDecoder(dt).Decode(&m)
  31. dt.Close()
  32. return err
  33. }
  34. // validateContextDir validates the given dir and returns abs path on success.
  35. func validateContextDir(contextDir string) (string, error) {
  36. absContextDir, err := filepath.Abs(contextDir)
  37. if err != nil {
  38. return "", err
  39. }
  40. stat, err := os.Lstat(absContextDir)
  41. if err != nil {
  42. return "", err
  43. }
  44. if !stat.IsDir() {
  45. return "", errors.Errorf("context must be a directory")
  46. }
  47. return absContextDir, nil
  48. }
  49. type pluginCreateOptions struct {
  50. repoName string
  51. context string
  52. compress bool
  53. }
  54. func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
  55. options := pluginCreateOptions{}
  56. cmd := &cobra.Command{
  57. Use: "create [OPTIONS] PLUGIN PLUGIN-DATA-DIR",
  58. Short: "Create a plugin from a rootfs and configuration. Plugin data directory must contain config.json and rootfs directory.",
  59. Args: cli.RequiresMinArgs(2),
  60. RunE: func(cmd *cobra.Command, args []string) error {
  61. options.repoName = args[0]
  62. options.context = args[1]
  63. return runCreate(dockerCli, options)
  64. },
  65. }
  66. flags := cmd.Flags()
  67. flags.BoolVar(&options.compress, "compress", false, "Compress the context using gzip")
  68. return cmd
  69. }
  70. func runCreate(dockerCli *command.DockerCli, options pluginCreateOptions) error {
  71. var (
  72. createCtx io.ReadCloser
  73. err error
  74. )
  75. if err := validateTag(options.repoName); err != nil {
  76. return err
  77. }
  78. absContextDir, err := validateContextDir(options.context)
  79. if err != nil {
  80. return err
  81. }
  82. if err := validateConfig(options.context); err != nil {
  83. return err
  84. }
  85. compression := archive.Uncompressed
  86. if options.compress {
  87. logrus.Debugf("compression enabled")
  88. compression = archive.Gzip
  89. }
  90. createCtx, err = archive.TarWithOptions(absContextDir, &archive.TarOptions{
  91. Compression: compression,
  92. })
  93. if err != nil {
  94. return err
  95. }
  96. ctx := context.Background()
  97. createOptions := types.PluginCreateOptions{RepoName: options.repoName}
  98. if err = dockerCli.Client().PluginCreate(ctx, createCtx, createOptions); err != nil {
  99. return err
  100. }
  101. fmt.Fprintln(dockerCli.Out(), options.repoName)
  102. return nil
  103. }