create.go 2.8 KB

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