create.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/docker/api/types"
  10. "github.com/docker/docker/cli"
  11. "github.com/docker/docker/cli/command"
  12. "github.com/docker/docker/pkg/archive"
  13. "github.com/docker/docker/reference"
  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.ParseNamed(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. stat, err := os.Lstat(absContextDir)
  37. if err != nil {
  38. return "", err
  39. }
  40. if !stat.IsDir() {
  41. return "", fmt.Errorf("context must be a directory")
  42. }
  43. return absContextDir, nil
  44. }
  45. type pluginCreateOptions struct {
  46. repoName string
  47. context string
  48. compress bool
  49. }
  50. func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
  51. options := pluginCreateOptions{}
  52. cmd := &cobra.Command{
  53. Use: "create [OPTIONS] PLUGIN[:tag] PATH-TO-ROOTFS(rootfs + config.json)",
  54. Short: "Create a plugin from a rootfs and config",
  55. Args: cli.RequiresMinArgs(2),
  56. RunE: func(cmd *cobra.Command, args []string) error {
  57. options.repoName = args[0]
  58. options.context = args[1]
  59. return runCreate(dockerCli, options)
  60. },
  61. }
  62. flags := cmd.Flags()
  63. flags.BoolVar(&options.compress, "compress", false, "Compress the context using gzip")
  64. return cmd
  65. }
  66. func runCreate(dockerCli *command.DockerCli, options pluginCreateOptions) error {
  67. var (
  68. createCtx io.ReadCloser
  69. err error
  70. )
  71. if err := validateTag(options.repoName); err != nil {
  72. return err
  73. }
  74. absContextDir, err := validateContextDir(options.context)
  75. if err != nil {
  76. return err
  77. }
  78. if err := validateConfig(options.context); err != nil {
  79. return err
  80. }
  81. compression := archive.Uncompressed
  82. if options.compress {
  83. logrus.Debugf("compression enabled")
  84. compression = archive.Gzip
  85. }
  86. createCtx, err = archive.TarWithOptions(absContextDir, &archive.TarOptions{
  87. Compression: compression,
  88. })
  89. if err != nil {
  90. return err
  91. }
  92. ctx := context.Background()
  93. createOptions := types.PluginCreateOptions{RepoName: options.repoName}
  94. if err = dockerCli.Client().PluginCreate(ctx, createCtx, createOptions); err != nil {
  95. return err
  96. }
  97. fmt.Fprintln(dockerCli.Out(), options.repoName)
  98. return nil
  99. }