Explorar el Código

docker configure command without checks

Alexander Belanger hace 5 años
padre
commit
9b05b15f02

+ 54 - 27
cli/cmd/docker.go

@@ -1,12 +1,15 @@
 package cmd
 
 import (
+	"context"
 	"encoding/json"
 	"io/ioutil"
+	"net/url"
 	"os"
 	"path/filepath"
 
-	"github.com/fatih/color"
+	"github.com/porter-dev/porter/cli/cmd/api"
+	"github.com/porter-dev/porter/cli/cmd/github"
 	"github.com/spf13/cobra"
 
 	"github.com/docker/cli/cli/config/configfile"
@@ -21,8 +24,9 @@ var configureCmd = &cobra.Command{
 	Use:   "configure",
 	Short: "Configures the host's Docker instance",
 	Run: func(cmd *cobra.Command, args []string) {
-		if err := dockerConfig(); err != nil {
-			color.New(color.FgRed).Println("Configuring Docker unsuccessful:", err.Error())
+		err := checkLoginAndRun(args, dockerConfig)
+
+		if err != nil {
 			os.Exit(1)
 		}
 	},
@@ -34,7 +38,35 @@ func init() {
 	dockerCmd.AddCommand(configureCmd)
 }
 
-func dockerConfig() error {
+func dockerConfig(user *api.AuthCheckResponse, client *api.Client, args []string) error {
+	pID := getProjectID()
+
+	// get all registries that should be added
+	regToAdd := make([]string, 0)
+
+	// get the list of namespaces
+	registries, err := client.ListRegistries(
+		context.Background(),
+		pID,
+	)
+
+	if err != nil {
+		return err
+	}
+
+	for _, registry := range registries {
+		if registry.URL != "" {
+			// strip the protocol
+			regURL, err := url.Parse(registry.URL)
+
+			if err != nil {
+				continue
+			}
+
+			regToAdd = append(regToAdd, regURL.Host)
+		}
+	}
+
 	dockerConfigFile := filepath.Join(home, ".docker", "config.json")
 
 	// check that a compatible version of docker is installed
@@ -50,41 +82,36 @@ func dockerConfig() error {
 		return err
 	}
 
-	config := &configfile.ConfigFile{
-		Filename: dockerConfigFile,
+	// download the porter cred helper
+	z := &github.ZIPReleaseGetter{
+		AssetName:           "docker-credential-porter",
+		AssetFolderDest:     "/usr/local/bin",
+		ZipFolderDest:       filepath.Join(home, ".porter"),
+		ZipName:             "docker-credential-porter_latest.zip",
+		EntityID:            "porter-dev",
+		RepoName:            "porter",
+		IsPlatformDependent: true,
 	}
 
-	err = json.Unmarshal(configBytes, config)
+	err = z.GetLatestRelease()
 
 	if err != nil {
 		return err
 	}
 
-	config.CredentialHelpers["393629051022.dkr.ecr.us-east-2.amazonaws.com"] = "porter"
+	config := &configfile.ConfigFile{
+		Filename: dockerConfigFile,
+	}
 
-	err = config.Save()
+	err = json.Unmarshal(configBytes, config)
 
 	if err != nil {
 		return err
 	}
 
-	return nil
-
-	// z := &github.ZIPReleaseGetter{
-	// 	AssetName:           "docker-credential-porter",
-	// 	AssetFolderDest:     "/usr/local/bin",
-	// 	ZipFolderDest:       filepath.Join(home, ".porter"),
-	// 	ZipName:             "docker-credential-porter_latest.zip",
-	// 	EntityID:            "porter-dev",
-	// 	RepoName:            "porter",
-	// 	IsPlatformDependent: true,
-	// }
-
-	// err = z.GetLatestRelease()
-
-	// if err != nil {
-	// 	return err
-	// }
+	for _, regURL := range regToAdd {
+		config.CredentialHelpers[regURL] = "porter"
+	}
 
-	// return nil
+	return config.Save()
 }

+ 3 - 3
cli/cmd/registry.go

@@ -120,15 +120,15 @@ func listRegistries(user *api.AuthCheckResponse, client *api.Client, args []stri
 	w := new(tabwriter.Writer)
 	w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
 
-	fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "NAME", "SERVICE")
+	fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "URL", "SERVICE")
 
 	currRegistryID := getRegistryID()
 
 	for _, registry := range registries {
 		if currRegistryID == registry.ID {
-			color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s (current registry)\n", registry.ID, registry.Name, registry.Service)
+			color.New(color.FgGreen).Fprintf(w, "%d\t%s\t%s (current registry)\n", registry.ID, registry.URL, registry.Service)
 		} else {
-			fmt.Fprintf(w, "%d\t%s\t%s\n", registry.ID, registry.Name, registry.Service)
+			fmt.Fprintf(w, "%d\t%s\t%s\n", registry.ID, registry.URL, registry.Service)
 		}
 	}
 

+ 1 - 0
internal/forms/registry.go

@@ -10,6 +10,7 @@ import (
 type CreateRegistry struct {
 	Name             string `json:"name" form:"required"`
 	ProjectID        uint   `json:"project_id" form:"required"`
+	URL              string `json:"url"`
 	GCPIntegrationID uint   `json:"gcp_integration_id"`
 	AWSIntegrationID uint   `json:"aws_integration_id"`
 }

+ 7 - 0
internal/models/registry.go

@@ -13,6 +13,9 @@ type Registry struct {
 	// Name of the registry
 	Name string `json:"name"`
 
+	// URL of the registry
+	URL string `json:"url"`
+
 	// The project that this integration belongs to
 	ProjectID uint `json:"project_id"`
 
@@ -37,6 +40,9 @@ type RegistryExternal struct {
 	// Name of the registry
 	Name string `json:"name"`
 
+	// URL of the registry
+	URL string `json:"url"`
+
 	// The integration service for this registry
 	Service integrations.IntegrationService `json:"service"`
 }
@@ -55,6 +61,7 @@ func (r *Registry) Externalize() *RegistryExternal {
 		ID:        r.ID,
 		ProjectID: r.ProjectID,
 		Name:      r.Name,
+		URL:       r.URL,
 		Service:   serv,
 	}
 }

+ 28 - 0
server/api/registry_handler.go

@@ -48,6 +48,34 @@ func (app *App) HandleCreateRegistry(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	// if the registry is ECR and URL is not set, get the registry url
+	if registry.URL == "" && registry.AWSIntegrationID != 0 {
+		awsInt, err := app.Repo.AWSIntegration.ReadAWSIntegration(registry.AWSIntegrationID)
+
+		if err != nil {
+			app.handleErrorDataRead(err, w)
+			return
+		}
+
+		sess, err := awsInt.GetSession()
+
+		if err != nil {
+			app.handleErrorDataRead(err, w)
+			return
+		}
+
+		ecrSvc := ecr.New(sess)
+
+		output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
+
+		if err != nil {
+			app.handleErrorDataRead(err, w)
+			return
+		}
+
+		registry.URL = *output.AuthorizationData[0].ProxyEndpoint
+	}
+
 	// handle write to the database
 	registry, err = app.Repo.Registry.CreateRegistry(registry)