瀏覽代碼

create repo when using pack cache

Mohammed Nafees 4 年之前
父節點
當前提交
88b12058d1

+ 1 - 1
Makefile

@@ -14,7 +14,7 @@ setup-env-files:
 	bash ./scripts/dev-environment/CreateDefaultEnvFiles.sh
 
 build-cli:
-	go build -ldflags="-w -s -X 'github.com/porter-dev/porter/cli/cmd.Version=${VERSION}'" -a -tags cli -o $(BINDIR)/porter ./cli
+	go build -ldflags="-w -s -X 'github.com/porter-dev/porter/cli/cmd/config.Version=${VERSION}'" -a -tags cli -o $(BINDIR)/porter ./cli
 
 build-cli-dev:
 	go build -tags cli -o $(BINDIR)/porter ./cli

+ 1 - 1
cli/cmd/apply.go

@@ -356,7 +356,7 @@ func (d *Driver) applyApplication(resource *models.Resource, client *api.Client,
 
 	if appConfig.Build.UseCache {
 		// set the docker config so that pack caching can use the repo credentials
-		err := setDockerConfig(client)
+		err := config.SetDockerConfig(client)
 
 		if err != nil {
 			return nil, err

+ 205 - 0
cli/cmd/config/docker.go

@@ -0,0 +1,205 @@
+package config
+
+import (
+	"context"
+	"encoding/base64"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/url"
+	"os"
+	"os/exec"
+	"path/filepath"
+	"strings"
+
+	"github.com/docker/cli/cli/config/configfile"
+	"github.com/docker/cli/cli/config/types"
+	"github.com/fatih/color"
+	api "github.com/porter-dev/porter/api/client"
+	"github.com/porter-dev/porter/cli/cmd/github"
+)
+
+func SetDockerConfig(client *api.Client) error {
+	pID := GetCLIConfig().Project
+
+	// get all registries that should be added
+	regToAdd := make([]string, 0)
+
+	// get the list of namespaces
+	resp, err := client.ListRegistries(
+		context.Background(),
+		pID,
+	)
+
+	if err != nil {
+		return err
+	}
+
+	registries := *resp
+
+	for _, registry := range registries {
+		if registry.URL != "" {
+			rURL := registry.URL
+
+			if !strings.Contains(rURL, "http") {
+				rURL = "http://" + rURL
+			}
+
+			// strip the protocol
+			regURL, err := url.Parse(rURL)
+
+			if err != nil {
+				continue
+			}
+
+			regToAdd = append(regToAdd, regURL.Host)
+		}
+	}
+
+	// create a docker dir if it does not exist
+	dockerDir := filepath.Join(home, ".docker")
+
+	if _, err := os.Stat(dockerDir); os.IsNotExist(err) {
+		err = os.Mkdir(dockerDir, 0700)
+
+		if err != nil {
+			return err
+		}
+	}
+
+	dockerConfigFile := filepath.Join(home, ".docker", "config.json")
+
+	// determine if configfile exists
+	if _, err := os.Stat(dockerConfigFile); os.IsNotExist(err) {
+		// if it does not exist, create it
+		err := ioutil.WriteFile(dockerConfigFile, []byte("{}"), 0700)
+
+		if err != nil {
+			return err
+		}
+	}
+
+	// read the file bytes
+	configBytes, err := ioutil.ReadFile(dockerConfigFile)
+
+	if err != nil {
+		return err
+	}
+
+	// check if the docker credential helper exists
+	if !commandExists("docker-credential-porter") {
+		err := downloadCredMatchingRelease()
+
+		if err != nil {
+			color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
+			os.Exit(1)
+		}
+	}
+
+	// otherwise, check the version flag of the binary
+	cmdVersionCred := exec.Command("docker-credential-porter", "--version")
+	writer := &VersionWriter{}
+	cmdVersionCred.Stdout = writer
+
+	err = cmdVersionCred.Run()
+
+	if err != nil || writer.Version != Version {
+		err := downloadCredMatchingRelease()
+
+		if err != nil {
+			color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
+			os.Exit(1)
+		}
+	}
+
+	configFile := &configfile.ConfigFile{
+		Filename: dockerConfigFile,
+	}
+
+	err = json.Unmarshal(configBytes, GetCLIConfig())
+
+	if err != nil {
+		return err
+	}
+
+	if configFile.CredentialHelpers == nil {
+		configFile.CredentialHelpers = make(map[string]string)
+	}
+
+	if configFile.AuthConfigs == nil {
+		configFile.AuthConfigs = make(map[string]types.AuthConfig)
+	}
+
+	for _, regURL := range regToAdd {
+		// if this is a dockerhub registry, see if an auth config has already been generated
+		// for index.docker.io
+		if strings.Contains(regURL, "index.docker.io") {
+			isAuthenticated := false
+
+			for key := range configFile.AuthConfigs {
+				if key == "https://index.docker.io/v1/" {
+					isAuthenticated = true
+				}
+			}
+
+			if !isAuthenticated {
+				// get a dockerhub token from the Porter API
+				tokenResp, err := client.GetDockerhubAuthorizationToken(context.Background(), GetCLIConfig().Project)
+
+				if err != nil {
+					return err
+				}
+
+				decodedToken, err := base64.StdEncoding.DecodeString(tokenResp.Token)
+
+				if err != nil {
+					return fmt.Errorf("Invalid token: %v", err)
+				}
+
+				parts := strings.SplitN(string(decodedToken), ":", 2)
+
+				if len(parts) < 2 {
+					return fmt.Errorf("Invalid token: expected two parts, got %d", len(parts))
+				}
+
+				configFile.AuthConfigs["https://index.docker.io/v1/"] = types.AuthConfig{
+					Auth:     tokenResp.Token,
+					Username: parts[0],
+					Password: parts[1],
+				}
+
+				// since we're using token-based auth, unset the credstore
+				configFile.CredentialsStore = ""
+			}
+		} else {
+			configFile.CredentialHelpers[regURL] = "porter"
+		}
+	}
+
+	return configFile.Save()
+}
+
+func commandExists(cmd string) bool {
+	_, err := exec.LookPath(cmd)
+	return err == nil
+}
+
+func downloadCredMatchingRelease() error {
+	// 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,
+		Downloader: &github.ZIPDownloader{
+			ZipFolderDest:   filepath.Join(home, ".porter"),
+			AssetFolderDest: "/usr/local/bin",
+			ZipName:         "docker-credential-porter_latest.zip",
+		},
+	}
+
+	return z.GetRelease(Version)
+}

+ 16 - 0
cli/cmd/config/version.go

@@ -0,0 +1,16 @@
+package config
+
+import "strings"
+
+// Version will be linked by an ldflag during build
+var Version string = "v0.21.2"
+
+type VersionWriter struct {
+	Version string
+}
+
+func (v *VersionWriter) Write(p []byte) (n int, err error) {
+	v.Version = strings.TrimSpace(string(p))
+
+	return len(p), nil
+}

+ 2 - 1
cli/cmd/create.go

@@ -11,6 +11,7 @@ import (
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/gitutils"
 	"github.com/porter-dev/porter/cli/cmd/utils"
@@ -268,7 +269,7 @@ func createFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 				return err
 			}
 
-			err = setDockerConfig(createAgent.Client)
+			err = config.SetDockerConfig(createAgent.Client)
 
 			if err != nil {
 				return err

+ 2 - 1
cli/cmd/deploy.go

@@ -9,6 +9,7 @@ import (
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/utils"
 	"github.com/spf13/cobra"
@@ -481,7 +482,7 @@ func updateBuildWithAgent(updateAgent *deploy.DeployAgent) error {
 	}
 
 	if useCache {
-		err := setDockerConfig(updateAgent.Client)
+		err := config.SetDockerConfig(updateAgent.Client)
 
 		if err != nil {
 			return err

+ 1 - 200
cli/cmd/docker.go

@@ -1,26 +1,12 @@
 package cmd
 
 import (
-	"context"
-	"encoding/base64"
-	"encoding/json"
-	"fmt"
-	"io/ioutil"
-	"net/url"
 	"os"
-	"os/exec"
-	"path/filepath"
-	"strings"
 
-	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	ptypes "github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/cli/cmd/config"
-	"github.com/porter-dev/porter/cli/cmd/github"
 	"github.com/spf13/cobra"
-
-	"github.com/docker/cli/cli/config/configfile"
-	"github.com/docker/cli/cli/config/types"
 )
 
 var dockerCmd = &cobra.Command{
@@ -47,190 +33,5 @@ func init() {
 }
 
 func dockerConfig(user *ptypes.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
-	return setDockerConfig(client)
-}
-
-func setDockerConfig(client *api.Client) error {
-	pID := cliConf.Project
-
-	// get all registries that should be added
-	regToAdd := make([]string, 0)
-
-	// get the list of namespaces
-	resp, err := client.ListRegistries(
-		context.Background(),
-		pID,
-	)
-
-	if err != nil {
-		return err
-	}
-
-	registries := *resp
-
-	for _, registry := range registries {
-		if registry.URL != "" {
-			rURL := registry.URL
-
-			if !strings.Contains(rURL, "http") {
-				rURL = "http://" + rURL
-			}
-
-			// strip the protocol
-			regURL, err := url.Parse(rURL)
-
-			if err != nil {
-				continue
-			}
-
-			regToAdd = append(regToAdd, regURL.Host)
-		}
-	}
-
-	// create a docker dir if it does not exist
-	dockerDir := filepath.Join(home, ".docker")
-
-	if _, err := os.Stat(dockerDir); os.IsNotExist(err) {
-		err = os.Mkdir(dockerDir, 0700)
-
-		if err != nil {
-			return err
-		}
-	}
-
-	dockerConfigFile := filepath.Join(home, ".docker", "config.json")
-
-	// determine if configfile exists
-	if _, err := os.Stat(dockerConfigFile); os.IsNotExist(err) {
-		// if it does not exist, create it
-		err := ioutil.WriteFile(dockerConfigFile, []byte("{}"), 0700)
-
-		if err != nil {
-			return err
-		}
-	}
-
-	// read the file bytes
-	configBytes, err := ioutil.ReadFile(dockerConfigFile)
-
-	if err != nil {
-		return err
-	}
-
-	// check if the docker credential helper exists
-	if !commandExists("docker-credential-porter") {
-		err := downloadCredMatchingRelease()
-
-		if err != nil {
-			color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
-			os.Exit(1)
-		}
-	}
-
-	// otherwise, check the version flag of the binary
-	cmdVersionCred := exec.Command("docker-credential-porter", "--version")
-	writer := &versionWriter{}
-	cmdVersionCred.Stdout = writer
-
-	err = cmdVersionCred.Run()
-
-	if err != nil || writer.Version != Version {
-		err := downloadCredMatchingRelease()
-
-		if err != nil {
-			color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
-			os.Exit(1)
-		}
-	}
-
-	configFile := &configfile.ConfigFile{
-		Filename: dockerConfigFile,
-	}
-
-	err = json.Unmarshal(configBytes, config.GetCLIConfig())
-
-	if err != nil {
-		return err
-	}
-
-	if configFile.CredentialHelpers == nil {
-		configFile.CredentialHelpers = make(map[string]string)
-	}
-
-	if configFile.AuthConfigs == nil {
-		configFile.AuthConfigs = make(map[string]types.AuthConfig)
-	}
-
-	for _, regURL := range regToAdd {
-		// if this is a dockerhub registry, see if an auth config has already been generated
-		// for index.docker.io
-		if strings.Contains(regURL, "index.docker.io") {
-			isAuthenticated := false
-
-			for key := range configFile.AuthConfigs {
-				if key == "https://index.docker.io/v1/" {
-					isAuthenticated = true
-				}
-			}
-
-			if !isAuthenticated {
-				// get a dockerhub token from the Porter API
-				tokenResp, err := client.GetDockerhubAuthorizationToken(context.Background(), cliConf.Project)
-
-				if err != nil {
-					return err
-				}
-
-				decodedToken, err := base64.StdEncoding.DecodeString(tokenResp.Token)
-
-				if err != nil {
-					return fmt.Errorf("Invalid token: %v", err)
-				}
-
-				parts := strings.SplitN(string(decodedToken), ":", 2)
-
-				if len(parts) < 2 {
-					return fmt.Errorf("Invalid token: expected two parts, got %d", len(parts))
-				}
-
-				configFile.AuthConfigs["https://index.docker.io/v1/"] = types.AuthConfig{
-					Auth:     tokenResp.Token,
-					Username: parts[0],
-					Password: parts[1],
-				}
-
-				// since we're using token-based auth, unset the credstore
-				configFile.CredentialsStore = ""
-			}
-		} else {
-			configFile.CredentialHelpers[regURL] = "porter"
-		}
-	}
-
-	return configFile.Save()
-}
-
-func downloadCredMatchingRelease() error {
-	// 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,
-		Downloader: &github.ZIPDownloader{
-			ZipFolderDest:   filepath.Join(home, ".porter"),
-			AssetFolderDest: "/usr/local/bin",
-			ZipName:         "docker-credential-porter_latest.zip",
-		},
-	}
-
-	return z.GetRelease(Version)
-}
-
-func commandExists(cmd string) bool {
-	_, err := exec.LookPath(cmd)
-	return err == nil
+	return config.SetDockerConfig(client)
 }

+ 52 - 10
cli/cmd/preview/build_image_driver.go

@@ -20,14 +20,14 @@ import (
 
 type BuildDriverConfig struct {
 	Build struct {
-		ForceBuild bool `mapstructure:"force_build"`
-		UseCache   bool `mapstructure:"use_cache"`
-		Method     string
-		Context    string
-		Dockerfile string
-		Builder    string
-		Buildpacks []string
-		Image      string
+		ForceBuild   bool `mapstructure:"force_build"`
+		UsePackCache bool `mapstructure:"use_pack_cache"`
+		Method       string
+		Context      string
+		Dockerfile   string
+		Builder      string
+		Buildpacks   []string
+		Image        string
 	}
 
 	EnvGroups []types.EnvGroupMeta `mapstructure:"env_groups"`
@@ -144,7 +144,7 @@ func (d *BuildDriver) Apply(resource *models.Resource) (*models.Resource, error)
 				LocalDockerfile: d.config.Build.Dockerfile,
 				Method:          deploy.DeployBuildType(d.config.Build.Method),
 				EnvGroups:       d.config.EnvGroups,
-				UseCache:        d.config.Build.UseCache,
+				UseCache:        d.config.Build.UsePackCache,
 			},
 			Kind:        d.source.Name,
 			ReleaseName: d.target.AppName,
@@ -153,11 +153,53 @@ func (d *BuildDriver) Apply(resource *models.Resource) (*models.Resource, error)
 		},
 	}
 
-	_, imageURL, err := createAgent.GetImageRepoURL(d.target.AppName, d.target.Namespace)
+	regID, imageURL, err := createAgent.GetImageRepoURL(d.target.AppName, d.target.Namespace)
 	if err != nil {
 		return nil, err
 	}
 
+	if d.config.Build.UsePackCache {
+		err := config.SetDockerConfig(client)
+
+		if err != nil {
+			return nil, err
+		}
+
+		if d.config.Build.Method == "pack" {
+			repoResp, err := client.ListRegistryRepositories(context.Background(), d.target.Project, regID)
+
+			if err != nil {
+				return nil, err
+			}
+
+			repos := *repoResp
+
+			found := false
+
+			for _, repo := range repos {
+				if repo.URI == imageURL {
+					found = true
+					break
+				}
+			}
+
+			if !found {
+				err = client.CreateRepository(
+					context.Background(),
+					d.target.Project,
+					regID,
+					&types.CreateRegistryRepositoryRequest{
+						ImageRepoURI: imageURL,
+					},
+				)
+
+				if err != nil {
+					return nil, err
+				}
+			}
+		}
+	}
+
 	if d.config.Build.Method != "" {
 		if d.config.Build.Method == string(deploy.DeployBuildTypeDocker) {
 			if d.config.Build.Dockerfile == "" {

+ 1 - 2
cli/cmd/preview/push_image_driver.go

@@ -12,8 +12,7 @@ import (
 
 type PushDriverConfig struct {
 	Push struct {
-		ForcePush bool `mapstructure:"force_build"`
-		UseCache  bool `mapstructure:"use_cache"`
+		ForcePush bool `mapstructure:"force_push"`
 		Image     string
 	}
 }

+ 2 - 2
cli/cmd/root.go

@@ -33,7 +33,7 @@ func Execute() {
 
 	rootCmd.PersistentFlags().AddFlagSet(utils.DefaultFlagSet)
 
-	if Version != "dev" {
+	if config.Version != "dev" {
 		ghClient := github.NewClient(nil)
 		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
 		defer cancel()
@@ -41,7 +41,7 @@ func Execute() {
 		if err == nil {
 			release.GetURL()
 			// we do not care for an error here because we do not want to block the user here
-			constraint, err := semver.NewConstraint(fmt.Sprintf("> %s", strings.TrimPrefix(Version, "v")))
+			constraint, err := semver.NewConstraint(fmt.Sprintf("> %s", strings.TrimPrefix(config.Version, "v")))
 			if err == nil {
 				latestRelease, err := semver.NewVersion(strings.TrimPrefix(release.GetTagName(), "v"))
 				if err == nil {

+ 5 - 15
cli/cmd/server.go

@@ -5,9 +5,9 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
-	"strings"
 
 	"github.com/fatih/color"
+	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/porter-dev/porter/cli/cmd/docker"
 	"github.com/porter-dev/porter/cli/cmd/github"
 	"github.com/porter-dev/porter/cli/cmd/utils"
@@ -182,12 +182,12 @@ func startLocal(
 
 	// otherwise, check the version flag of the binary
 	cmdVersionPorter := exec.Command(cmdPath, "--version")
-	writer := &versionWriter{}
+	writer := &config.VersionWriter{}
 	cmdVersionPorter.Stdout = writer
 
 	err := cmdVersionPorter.Run()
 
-	if err != nil || writer.Version != Version {
+	if err != nil || writer.Version != config.Version {
 		err := downloadMatchingRelease(porterDir)
 
 		if err != nil {
@@ -264,7 +264,7 @@ func downloadMatchingRelease(porterDir string) error {
 		},
 	}
 
-	err := z.GetRelease(Version)
+	err := z.GetRelease(config.Version)
 
 	if err != nil {
 		return err
@@ -285,15 +285,5 @@ func downloadMatchingRelease(porterDir string) error {
 		},
 	}
 
-	return zStatic.GetRelease(Version)
-}
-
-type versionWriter struct {
-	Version string
-}
-
-func (v *versionWriter) Write(p []byte) (n int, err error) {
-	v.Version = strings.TrimSpace(string(p))
-
-	return len(p), nil
+	return zStatic.GetRelease(config.Version)
 }

+ 1 - 0
cli/cmd/utils/docker.go

@@ -0,0 +1 @@
+package utils

+ 2 - 4
cli/cmd/version.go

@@ -3,18 +3,16 @@ package cmd
 import (
 	"fmt"
 
+	"github.com/porter-dev/porter/cli/cmd/config"
 	"github.com/spf13/cobra"
 )
 
-// Version will be linked by an ldflag during build
-var Version string = "v0.19.6"
-
 var versionCmd = &cobra.Command{
 	Use:     "version",
 	Aliases: []string{"v", "--version"},
 	Short:   "Prints the version of the Porter CLI",
 	Run: func(cmd *cobra.Command, args []string) {
-		fmt.Println(Version)
+		fmt.Println(config.Version)
 	},
 }