Quellcode durchsuchen

do not use createFull and updateFull

Mohammed Nafees vor 4 Jahren
Ursprung
Commit
882b8f4284
6 geänderte Dateien mit 127 neuen und 57 gelöschten Zeilen
  1. 104 40
      cli/cmd/apply.go
  2. 4 7
      cli/cmd/create.go
  3. 3 5
      cli/cmd/deploy.go
  4. 5 4
      cli/cmd/deploy/create.go
  5. 1 1
      go.mod
  6. 10 0
      go.sum

+ 104 - 40
cli/cmd/apply.go

@@ -5,12 +5,15 @@ import (
 	"fmt"
 	"io/ioutil"
 	"os"
+	"path/filepath"
 	"strconv"
 
 	"github.com/cli/cli/git"
+	"github.com/fatih/color"
 	"github.com/mitchellh/mapstructure"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/switchboard/pkg/drivers"
 	"github.com/porter-dev/switchboard/pkg/models"
 	"github.com/porter-dev/switchboard/pkg/parser"
@@ -61,8 +64,8 @@ func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []str
 	}
 
 	worker := worker.NewWorker()
-	worker.RegisterDriver("", NewPorterDriver) // FIXME: workaround for when driver is not present
 	worker.RegisterDriver("porter.deploy", NewPorterDriver)
+	worker.SetDefaultDriver("porter.deploy")
 
 	return worker.Apply(resGroup, &switchboardTypes.ApplyOpts{
 		BasePath: basePath,
@@ -132,13 +135,15 @@ func (d *Driver) ShouldApply(resource *models.Resource) bool {
 
 func (d *Driver) Apply(resource *models.Resource) (*models.Resource, error) {
 	client := GetAPIClient(config)
+	d.output = make(map[string]interface{})
 
-	// TODO: use source.repo, source.version
-	config.SetProject(d.target.Project)
-	config.SetCluster(d.target.Cluster)
+	if resource.Name == "" {
+		return nil, fmt.Errorf("empty app name")
+	}
+	resource.Name = fmt.Sprintf("preview-%s", resource.Name)
 
-	namespace = d.target.Namespace
-	existingNamespaces, err := client.GetK8sNamespaces(context.Background(), config.Project, config.Cluster)
+	namespace := d.target.Namespace
+	existingNamespaces, err := client.GetK8sNamespaces(context.Background(), d.target.Project, d.target.Cluster)
 	if err != nil {
 		return nil, err
 	}
@@ -151,60 +156,113 @@ func (d *Driver) Apply(resource *models.Resource) (*models.Resource, error) {
 	}
 	if !namespaceFound {
 		_, err := client.CreateNewK8sNamespace(
-			context.Background(), config.Project, config.Cluster, namespace)
+			context.Background(), d.target.Project, d.target.Cluster, namespace)
 		if err != nil {
 			return nil, err
 		}
 	}
 
-	method = d.config.Build.Method
+	method := d.config.Build.Method
 	if method == "" {
 		return nil, fmt.Errorf("method should either be \"docker\" or \"pack\"")
-	} else if method == "docker" {
-		dockerfile = d.config.Build.Dockerfile
 	}
 
-	localPath = d.config.Build.Context
-	source = "local"
-	valuesObj = d.config.Values
+	fullPath, err := filepath.Abs(d.config.Build.Context)
+	if err != nil {
+		return nil, err
+	}
 
-	if resource.Name == "" {
-		return nil, fmt.Errorf("empty app name")
+	tag := os.Getenv("PORTER_TAG")
+	if tag == "" {
+		commit, err := git.LastCommit()
+		if err != nil {
+			return nil, err
+		}
+		tag = commit.Sha
+	}
+	if tag == "" {
+		return nil, fmt.Errorf("could not find commit SHA to tag the image")
 	}
-	resource.Name = fmt.Sprintf("preview-%s", resource.Name)
 
-	_, err = client.GetRelease(context.Background(), config.Project, config.Cluster, d.target.Namespace, resource.Name)
+	_, err = client.GetRelease(context.Background(), d.target.Project,
+		d.target.Cluster, d.target.Namespace, resource.Name)
 	if err != nil {
-		// create new app
-		name = resource.Name
+		// create new release
+		color.New(color.FgGreen).Printf("Creating %s release: %s\n", d.source.Name, resource.Name)
 
-		regList, err := client.ListRegistries(context.Background(), config.Project)
+		regList, err := client.ListRegistries(context.Background(), d.target.Project)
 		if err != nil {
 			return nil, err
 		}
-		if len(*regList) > 0 {
+		var registryURL string
+		if len(*regList) == 0 {
+			return nil, fmt.Errorf("no registry found")
+		} else {
 			registryURL = (*regList)[0].URL
 		}
 
-		err = createFull(nil, client, []string{d.source.Name})
-		if err != nil {
-			return nil, err
+		createAgent := &deploy.CreateAgent{
+			Client: client,
+			CreateOpts: &deploy.CreateOpts{
+				SharedOpts: &deploy.SharedOpts{
+					ProjectID:       d.target.Project,
+					ClusterID:       d.target.Cluster,
+					Namespace:       namespace,
+					LocalPath:       fullPath,
+					LocalDockerfile: d.config.Build.Dockerfile,
+					Method:          deploy.DeployBuildType(method),
+				},
+				Kind:        d.source.Name,
+				ReleaseName: resource.Name,
+				RegistryURL: registryURL,
+			},
 		}
+
+		subdomain, err := createAgent.CreateFromDocker(d.config.Values, tag)
+
+		return resource, handleSubdomainCreate(subdomain, err)
 	}
-	app = resource.Name
-	tag = os.Getenv("PORTER_TAG")
-	if tag == "" {
-		commit, err := git.LastCommit()
-		if err != nil {
-			return nil, err
-		}
-		tag = commit.Sha
+
+	// update an existing release
+	color.New(color.FgGreen).Println("Deploying app:", resource.Name)
+
+	updateAgent, err := deploy.NewDeployAgent(client, resource.Name, &deploy.DeployOpts{
+		SharedOpts: &deploy.SharedOpts{
+			ProjectID:       d.target.Project,
+			ClusterID:       d.target.Cluster,
+			Namespace:       namespace,
+			LocalPath:       fullPath,
+			LocalDockerfile: d.config.Build.Dockerfile,
+			OverrideTag:     tag,
+			Method:          deploy.DeployBuildType(method),
+		},
+		Local: true,
+	})
+	if err != nil {
+		return nil, err
 	}
-	if tag == "" {
-		return nil, fmt.Errorf("could not find commit SHA to tag the image")
+
+	buildEnv, err := updateAgent.GetBuildEnv()
+	if err != nil {
+		return nil, err
+	}
+
+	err = updateAgent.SetBuildEnv(buildEnv)
+	if err != nil {
+		return nil, err
+	}
+
+	err = updateAgent.Build()
+	if err != nil {
+		return nil, err
+	}
+
+	err = updateAgent.Push()
+	if err != nil {
+		return nil, err
 	}
 
-	err = updateFull(nil, client, []string{})
+	err = updateAgent.UpdateImageAndValues(d.config.Values)
 	if err != nil {
 		return nil, err
 	}
@@ -234,6 +292,13 @@ func getSource(genericSource map[string]interface{}) (*Source, error) {
 			source.Name = nameVal
 		}
 	}
+	if source.Name == "" {
+		return nil, fmt.Errorf("source name required")
+	}
+	if _, ok := supportedKinds[source.Name]; !ok {
+		return nil, fmt.Errorf("%s is not a supported source name: specify web, job, or worker", source.Name)
+	}
+
 	if source.Repo == "" {
 		if repo, ok := genericSource["repo"]; ok {
 			repoVal, ok := repo.(string)
@@ -254,12 +319,11 @@ func getSource(genericSource map[string]interface{}) (*Source, error) {
 	}
 
 	// lastly, just put in the defaults
-	if source.Name == "" || source.Repo == "" || source.Version == "" {
-		// default to these values when any one of the source values are empty
-		// this makes sense because it might save us from version mismatches and other mishaps
-		source.Name = "web"
+	if source.Repo == "" {
 		source.Repo = "https://charts.getporter.dev"
-		source.Version = "v0.13.0"
+	}
+	if source.Version == "" {
+		source.Version = "latest"
 	}
 
 	return source, nil

+ 4 - 7
cli/cmd/create.go

@@ -76,7 +76,6 @@ var values string
 var source string
 var image string
 var registryURL string
-var valuesObj map[string]interface{}
 
 func init() {
 	rootCmd.AddCommand(createCmd)
@@ -160,11 +159,9 @@ func createFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 	var err error
 
 	// read the values if necessary
-	if valuesObj == nil {
-		valuesObj, err = readValuesFile()
-		if err != nil {
-			return err
-		}
+	valuesObj, err := readValuesFile()
+	if err != nil {
+		return err
 	}
 
 	color.New(color.FgGreen).Printf("Creating %s release: %s\n", args[0], name)
@@ -201,7 +198,7 @@ func createFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 	}
 
 	if source == "local" {
-		subdomain, err := createAgent.CreateFromDocker(valuesObj)
+		subdomain, err := createAgent.CreateFromDocker(valuesObj, "default")
 
 		return handleSubdomainCreate(subdomain, err)
 	} else if source == "github" {

+ 3 - 5
cli/cmd/deploy.go

@@ -526,11 +526,9 @@ func updateUpgradeWithAgent(updateAgent *deploy.DeployAgent) error {
 	var err error
 
 	// read the values if necessary
-	if valuesObj == nil {
-		valuesObj, err = readValuesFile()
-		if err != nil {
-			return err
-		}
+	valuesObj, err := readValuesFile()
+	if err != nil {
+		return err
 	}
 
 	if err != nil {

+ 5 - 4
cli/cmd/deploy/create.go

@@ -213,6 +213,7 @@ func (c *CreateAgent) CreateFromRegistry(
 // container image, and then deploys it onto Porter.
 func (c *CreateAgent) CreateFromDocker(
 	overrideValues map[string]interface{},
+	imageTag string,
 ) (string, error) {
 	opts := c.CreateOpts
 
@@ -256,7 +257,7 @@ func (c *CreateAgent) CreateFromDocker(
 
 	mergedValues["image"] = map[string]interface{}{
 		"repository": imageURL,
-		"tag":        "latest",
+		"tag":        imageTag,
 	}
 
 	// create docker agen
@@ -281,9 +282,9 @@ func (c *CreateAgent) CreateFromDocker(
 	}
 
 	if opts.Method == DeployBuildTypeDocker {
-		err = buildAgent.BuildDocker(agent, opts.LocalPath, opts.LocalPath, opts.LocalDockerfile, "latest", "")
+		err = buildAgent.BuildDocker(agent, opts.LocalPath, opts.LocalPath, opts.LocalDockerfile, imageTag, "")
 	} else {
-		err = buildAgent.BuildPack(agent, opts.LocalPath, "latest", nil)
+		err = buildAgent.BuildPack(agent, opts.LocalPath, imageTag, nil)
 	}
 
 	if err != nil {
@@ -304,7 +305,7 @@ func (c *CreateAgent) CreateFromDocker(
 		return "", err
 	}
 
-	err = agent.PushImage(fmt.Sprintf("%s:%s", imageURL, "latest"))
+	err = agent.PushImage(fmt.Sprintf("%s:%s", imageURL, imageTag))
 
 	if err != nil {
 		return "", err

+ 1 - 1
go.mod

@@ -44,7 +44,7 @@ require (
 	github.com/opencontainers/image-spec v1.0.1
 	github.com/pelletier/go-toml v1.9.4 // indirect
 	github.com/pkg/errors v0.9.1
-	github.com/porter-dev/switchboard v0.0.0-20211203120508-691813a27c1b
+	github.com/porter-dev/switchboard v0.0.0-20211206152649-b6792e4dc331
 	github.com/rs/zerolog v1.26.0
 	github.com/segmentio/backo-go v0.0.0-20200129164019-23eae7c10bd3 // indirect
 	github.com/sendgrid/rest v2.6.3+incompatible // indirect

+ 10 - 0
go.sum

@@ -138,6 +138,7 @@ github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqR
 github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
 github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
 github.com/acomagu/bufpipe v1.0.3/go.mod h1:mxdxdup/WdsKVreO5GpW4+M/1CE2sMG4jeGJ2sYmHc4=
+github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
 github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
 github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
 github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
@@ -163,7 +164,9 @@ github.com/apex/logs v1.0.0/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDw
 github.com/aphistic/golf v0.0.0-20180712155816-02c07f170c5a/go.mod h1:3NqKYiepwy8kCu4PNA+aP7WUV72eXWJeP9/r3/K9aLE=
 github.com/aphistic/sweet v0.2.0/go.mod h1:fWDlIh/isSE9n6EPsRmC0det+whmX6dJid3stzu0Xys=
 github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM=
+github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
 github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk=
+github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
 github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
 github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
 github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
@@ -745,18 +748,22 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b
 github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
 github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
 github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
+github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
 github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
 github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
 github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
 github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
 github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
+github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 h1:PFfGModn55JA0oBsvFghhj0v93me+Ctr3uHC/UmFAls=
 github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80/go.mod h1:Cxv+IJLuBiEhQ7pBYGEuORa0nr4U994pE8mYLuFd7v0=
 github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
 github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
 github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
 github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
+github.com/hashicorp/terraform-exec v0.15.0 h1:cqjh4d8HYNQrDoEmlSGelHmg2DYDh5yayckvJ5bV18E=
 github.com/hashicorp/terraform-exec v0.15.0/go.mod h1:H4IG8ZxanU+NW0ZpDRNsvh9f0ul7C0nHP+rUR/CHs7I=
+github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniyEi5CM+FWGY=
 github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
 github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
 github.com/heroku/color v0.0.6 h1:UTFFMrmMLFcL3OweqP1lAdp8i1y/9oHqkeHjQ/b/Ny0=
@@ -1166,6 +1173,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/porter-dev/switchboard v0.0.0-20211203120508-691813a27c1b h1:xcM7l5Bzhi4xQsMEGr5MELPWMesVBMQdMqD6AUh5wiw=
 github.com/porter-dev/switchboard v0.0.0-20211203120508-691813a27c1b/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
+github.com/porter-dev/switchboard v0.0.0-20211206152649-b6792e4dc331 h1:winqj0G5++gjklyo/lZtxqEMtQbj2eiIrwOzEESaFGI=
+github.com/porter-dev/switchboard v0.0.0-20211206152649-b6792e4dc331/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
 github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
 github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
 github.com/prometheus/client_golang v0.0.0-20180209125602-c332b6f63c06/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
@@ -1402,6 +1411,7 @@ github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f h1
 github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
 github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
 github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
+github.com/zclconf/go-cty v1.9.1 h1:viqrgQwFl5UpSxc046qblj78wZXVDFnSOufaOTER+cc=
 github.com/zclconf/go-cty v1.9.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
 github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
 github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=