docker.go 2.3 KB

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