소스 검색

<wip> add create options

Alexander Belanger 5 년 전
부모
커밋
5f47cc8587
7개의 변경된 파일89개의 추가작업 그리고 33개의 파일을 삭제
  1. 2 1
      cli/cmd/api/deploy.go
  2. 0 0
      cli/cmd/create.go
  3. 23 0
      cli/cmd/create/create.go
  4. 7 7
      cli/cmd/deploy/deploy.go
  5. 35 5
      cli/cmd/job.go
  6. 21 19
      server/api/release_handler.go
  7. 1 1
      server/router/router.go

+ 2 - 1
cli/cmd/api/deploy.go

@@ -84,6 +84,7 @@ type UpdateBatchImageRequest struct {
 func (c *Client) UpdateBatchImage(
 	ctx context.Context,
 	projID, clusterID uint,
+	namespace string,
 	updateImageReq *UpdateBatchImageRequest,
 ) error {
 	data, err := json.Marshal(updateImageReq)
@@ -96,7 +97,7 @@ func (c *Client) UpdateBatchImage(
 		"POST",
 		fmt.Sprintf("%s/projects/%d/releases/image/update/batch?"+url.Values{
 			"cluster_id": []string{fmt.Sprintf("%d", clusterID)},
-			"namespace":  []string{"default"},
+			"namespace":  []string{namespace},
 			"storage":    []string{"secret"},
 		}.Encode(), c.BaseURL, projID),
 		strings.NewReader(string(data)),

+ 0 - 0
cli/cmd/create.go


+ 23 - 0
cli/cmd/create/create.go

@@ -1,6 +1,10 @@
 package create
 
 import (
+	"fmt"
+	"os"
+	"path/filepath"
+
 	"github.com/porter-dev/porter/cli/cmd/api"
 	"github.com/porter-dev/porter/cli/cmd/docker"
 )
@@ -20,6 +24,7 @@ type CreateOpts struct {
 }
 
 func (c *CreateAgent) CreateFromDocker() error {
+
 	// read values from local file
 
 	// overwrite with docker image repository and tag
@@ -28,3 +33,21 @@ func (c *CreateAgent) CreateFromDocker() error {
 
 	return nil
 }
+
+type CreateConfig struct {
+	DockerfilePath string
+}
+
+func (c *CreateAgent) DetectConfig(buildPath string) (*CreateConfig, error) {
+	// detect if there is a dockerfile at the path `./Dockerfile`
+	dockerFilePath := filepath.Join(buildPath, "./Dockerfile")
+
+	if info, err := os.Stat(dockerFilePath); !os.IsNotExist(err) && !info.IsDir() {
+		// path/to/whatever does not exist
+		return &CreateConfig{
+			DockerfilePath: dockerFilePath,
+		}, nil
+	}
+
+	return nil, fmt.Errorf("no supported build configuration detected")
+}

+ 7 - 7
cli/cmd/deploy/deploy.go

@@ -21,10 +21,10 @@ type DeployBuildType string
 
 const (
 	// uses local Docker daemon to build and push images
-	deployBuildTypeDocker DeployBuildType = "docker"
+	DeployBuildTypeDocker DeployBuildType = "docker"
 
 	// uses cloud-native build pack to build and push images
-	deployBuildTypePack DeployBuildType = "pack"
+	DeployBuildTypePack DeployBuildType = "pack"
 )
 
 // DeployAgent handles the deployment and redeployment of an application on Porter
@@ -94,18 +94,18 @@ func NewDeployAgent(client *api.Client, app string, opts *DeployOpts) (*DeployAg
 			// if the git action config exists, and dockerfile path is not empty, build type
 			// is docker
 			if release.GitActionConfig.DockerfilePath != "" {
-				deployAgent.opts.Method = deployBuildTypeDocker
+				deployAgent.opts.Method = DeployBuildTypeDocker
 			}
 
 			// otherwise build type is pack
-			deployAgent.opts.Method = deployBuildTypePack
+			deployAgent.opts.Method = DeployBuildTypePack
 		} else {
 			// if the git action config does not exist, we use pack by default
-			deployAgent.opts.Method = deployBuildTypePack
+			deployAgent.opts.Method = DeployBuildTypePack
 		}
 	}
 
-	if deployAgent.opts.Method == deployBuildTypeDocker {
+	if deployAgent.opts.Method == DeployBuildTypeDocker {
 		if release.GitActionConfig != nil {
 			deployAgent.dockerfilePath = release.GitActionConfig.DockerfilePath
 		}
@@ -234,7 +234,7 @@ func (d *DeployAgent) Build() error {
 		d.imageExists = false
 	}
 
-	if d.opts.Method == deployBuildTypeDocker {
+	if d.opts.Method == DeployBuildTypeDocker {
 		return d.BuildDocker(dst, d.tag)
 	}
 

+ 35 - 5
cli/cmd/job.go

@@ -2,6 +2,7 @@ package cmd
 
 import (
 	"context"
+	"fmt"
 	"os"
 
 	"github.com/fatih/color"
@@ -10,8 +11,28 @@ import (
 )
 
 var batchImageUpdateCmd = &cobra.Command{
-	Use:   "update-image",
-	Short: "Blerp.",
+	Use:   "job update-images",
+	Short: "Updates the image tag of all jobs in a namespace which use a specific image.",
+	Long: fmt.Sprintf(`
+%s 
+
+Updates the image tag of all jobs in a namespace which use a specific image. Note that for all
+jobs with version <= v0.4.0, this will trigger a new run of a manual job. However, for versions
+>= v0.5.0, this will not create a new run of the job. 
+
+Example commands:
+
+  %s
+
+This command is namespace-scoped and uses the default namespace. To specify a different namespace, 
+use the --namespace flag:
+
+  %s
+`,
+		color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter job update-images\":"),
+		color.New(color.FgGreen, color.Bold).Sprintf("porter job update-images --image-repo-uri my-image.registry.io --tag newtag"),
+		color.New(color.FgGreen, color.Bold).Sprintf("porter job update-images --namespace custom-namespace --image-repo-uri my-image.registry.io --tag newtag"),
+	),
 	Run: func(cmd *cobra.Command, args []string) {
 		err := checkLoginAndRun(args, batchImageUpdate)
 
@@ -30,10 +51,15 @@ func init() {
 		&tag,
 		"tag",
 		"",
-		"Tag",
+		"The new image tag to use.",
 	)
 
-	batchImageUpdateCmd.MarkPersistentFlagRequired("app")
+	batchImageUpdateCmd.PersistentFlags().StringVar(
+		&namespace,
+		"namespace",
+		"",
+		"The namespace of the jobs.",
+	)
 
 	batchImageUpdateCmd.PersistentFlags().StringVarP(
 		&imageRepoURI,
@@ -42,15 +68,19 @@ func init() {
 		"",
 		"Image repo uri",
 	)
+
+	batchImageUpdateCmd.MarkPersistentFlagRequired("image-repo-uri")
+	batchImageUpdateCmd.MarkPersistentFlagRequired("tag")
 }
 
 func batchImageUpdate(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
-	color.New(color.FgGreen).Println("Update releases with image:", imageRepoURI)
+	color.New(color.FgGreen).Println("Updating all jobs which use the image:", imageRepoURI)
 
 	return client.UpdateBatchImage(
 		context.TODO(),
 		config.Project,
 		config.Cluster,
+		namespace,
 		&api.UpdateBatchImageRequest{
 			ImageRepoURI: imageRepoURI,
 			Tag:          tag,

+ 21 - 19
server/api/release_handler.go

@@ -975,7 +975,7 @@ func (app *App) HandleReleaseDeployWebhook(w http.ResponseWriter, r *http.Reques
 }
 
 // HandleReleaseJobUpdateImage
-func (app *App) HandleReleaseBatchUpdateImage(w http.ResponseWriter, r *http.Request) {
+func (app *App) HandleReleaseUpdateJobImages(w http.ResponseWriter, r *http.Request) {
 	vals, err := url.ParseQuery(r.URL.RawQuery)
 
 	if err != nil {
@@ -1051,26 +1051,28 @@ func (app *App) HandleReleaseBatchUpdateImage(w http.ResponseWriter, r *http.Req
 				mu.Unlock()
 			}
 
-			image := map[string]interface{}{}
-			image["repository"] = releases[index].ImageRepoURI
-			image["tag"] = form.Tag
-			rel.Config["image"] = image
-			rel.Config["paused"] = true
-
-			conf := &helm.UpgradeReleaseConfig{
-				Name:       releases[index].Name,
-				Cluster:    form.ReleaseForm.Cluster,
-				Repo:       *app.Repo,
-				Registries: registries,
-				Values:     rel.Config,
-			}
+			if rel.Chart.Name() == "job" {
+				image := map[string]interface{}{}
+				image["repository"] = releases[index].ImageRepoURI
+				image["tag"] = form.Tag
+				rel.Config["image"] = image
+				rel.Config["paused"] = true
+
+				conf := &helm.UpgradeReleaseConfig{
+					Name:       releases[index].Name,
+					Cluster:    form.ReleaseForm.Cluster,
+					Repo:       *app.Repo,
+					Registries: registries,
+					Values:     rel.Config,
+				}
 
-			_, err = agent.UpgradeReleaseByValues(conf, app.DOConf)
+				_, err = agent.UpgradeReleaseByValues(conf, app.DOConf)
 
-			if err != nil {
-				mu.Lock()
-				errors = append(errors, err.Error())
-				mu.Unlock()
+				if err != nil {
+					mu.Lock()
+					errors = append(errors, err.Error())
+					mu.Unlock()
+				}
 			}
 		}()
 	}

+ 1 - 1
server/router/router.go

@@ -1474,7 +1474,7 @@ func New(a *api.App) *chi.Mux {
 				"/projects/{project_id}/releases/image/update/batch",
 				auth.DoesUserHaveProjectAccess(
 					auth.DoesUserHaveClusterAccess(
-						requestlog.NewHandler(a.HandleReleaseBatchUpdateImage, l),
+						requestlog.NewHandler(a.HandleReleaseUpdateJobImages, l),
 						mw.URLParam,
 						mw.QueryParam,
 					),