docker.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/url"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/porter-dev/porter/cli/cmd/api"
  11. "github.com/porter-dev/porter/cli/cmd/github"
  12. "github.com/spf13/cobra"
  13. "github.com/docker/cli/cli/config/configfile"
  14. )
  15. var dockerCmd = &cobra.Command{
  16. Use: "docker",
  17. Short: "Commands to configure Docker for a project",
  18. }
  19. var configureCmd = &cobra.Command{
  20. Use: "configure",
  21. Short: "Configures the host's Docker instance",
  22. Run: func(cmd *cobra.Command, args []string) {
  23. err := checkLoginAndRun(args, dockerConfig)
  24. if err != nil {
  25. os.Exit(1)
  26. }
  27. },
  28. }
  29. func init() {
  30. rootCmd.AddCommand(dockerCmd)
  31. dockerCmd.AddCommand(configureCmd)
  32. }
  33. func dockerConfig(user *api.AuthCheckResponse, client *api.Client, args []string) error {
  34. pID := getProjectID()
  35. // get all registries that should be added
  36. regToAdd := make([]string, 0)
  37. // get the list of namespaces
  38. registries, err := client.ListRegistries(
  39. context.Background(),
  40. pID,
  41. )
  42. if err != nil {
  43. return err
  44. }
  45. for _, registry := range registries {
  46. if registry.URL != "" {
  47. rURL := registry.URL
  48. if !strings.Contains(rURL, "http") {
  49. rURL = "http://" + rURL
  50. }
  51. // strip the protocol
  52. regURL, err := url.Parse(rURL)
  53. if err != nil {
  54. continue
  55. }
  56. regToAdd = append(regToAdd, regURL.Host)
  57. }
  58. }
  59. dockerConfigFile := filepath.Join(home, ".docker", "config.json")
  60. // determine if configfile exists
  61. if info, err := os.Stat(dockerConfigFile); info.IsDir() || os.IsNotExist(err) {
  62. // if it does not exist, create it
  63. err := ioutil.WriteFile(dockerConfigFile, []byte("{}"), 0700)
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. // read the file bytes
  69. configBytes, err := ioutil.ReadFile(dockerConfigFile)
  70. if err != nil {
  71. return err
  72. }
  73. // download the porter cred helper
  74. z := &github.ZIPReleaseGetter{
  75. AssetName: "docker-credential-porter",
  76. AssetFolderDest: "/usr/local/bin",
  77. ZipFolderDest: filepath.Join(home, ".porter"),
  78. ZipName: "docker-credential-porter_latest.zip",
  79. EntityID: "porter-dev",
  80. RepoName: "porter",
  81. IsPlatformDependent: true,
  82. }
  83. err = z.GetLatestRelease()
  84. if err != nil {
  85. return err
  86. }
  87. config := &configfile.ConfigFile{
  88. Filename: dockerConfigFile,
  89. }
  90. err = json.Unmarshal(configBytes, config)
  91. if err != nil {
  92. return err
  93. }
  94. for _, regURL := range regToAdd {
  95. config.CredentialHelpers[regURL] = "porter"
  96. }
  97. return config.Save()
  98. }