예제 #1
0
func handlePostRelease(s api.FluxService) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var (
			inst    = getInstanceID(r)
			vars    = mux.Vars(r)
			service = vars["service"]
			image   = vars["image"]
			kind    = vars["kind"]
		)
		serviceSpec, err := flux.ParseServiceSpec(service)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			fmt.Fprintf(w, errors.Wrapf(err, "parsing service spec %q", service).Error())
			return
		}
		imageSpec := flux.ParseImageSpec(image)
		releaseKind, err := flux.ParseReleaseKind(kind)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			fmt.Fprintf(w, errors.Wrapf(err, "parsing release kind %q", kind).Error())
			return
		}

		var excludes []flux.ServiceID
		for _, ex := range r.URL.Query()["exclude"] {
			s, err := flux.ParseServiceID(ex)
			if err != nil {
				w.WriteHeader(http.StatusBadRequest)
				fmt.Fprintf(w, errors.Wrapf(err, "parsing excluded service %q", ex).Error())
				return
			}
			excludes = append(excludes, s)
		}

		id, err := s.PostRelease(inst, jobs.ReleaseJobParams{
			ServiceSpec: serviceSpec,
			ImageSpec:   imageSpec,
			Kind:        releaseKind,
			Excludes:    excludes,
		})
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintf(w, err.Error())
			return
		}

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		if err := json.NewEncoder(w).Encode(postReleaseResponse{
			Status:    "Queued.",
			ReleaseID: id,
		}); err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			fmt.Fprintf(w, err.Error())
			return
		}
	})
}
예제 #2
0
func (opts *serviceReleaseOpts) RunE(cmd *cobra.Command, args []string) error {
	if len(args) != 0 {
		return errorWantedNoArgs
	}

	if err := checkExactlyOne("--update-image=<image>, --update-all-images, or --no-update", opts.image != "", opts.allImages, opts.noUpdate); err != nil {
		return err
	}

	if err := checkExactlyOne("--service=<service>, or --all", opts.service != "", opts.allServices); err != nil {
		return err
	}

	service, err := parseServiceOption(opts.service) // will be "" iff opts.allServices
	if err != nil {
		return err
	}

	var image flux.ImageSpec
	switch {
	case opts.image != "":
		image = flux.ParseImageSpec(opts.image)
	case opts.allImages:
		image = flux.ImageSpecLatest
	case opts.noUpdate:
		image = flux.ImageSpecNone
	}

	var kind flux.ReleaseKind = flux.ReleaseKindExecute
	if opts.dryRun {
		kind = flux.ReleaseKindPlan
	}

	var excludes []flux.ServiceID
	for _, exclude := range opts.exclude {
		s, err := flux.ParseServiceID(exclude)
		if err != nil {
			return err
		}
		excludes = append(excludes, s)
	}

	if opts.dryRun {
		fmt.Fprintf(os.Stdout, "Submitting dry-run release job...\n")
	} else {
		fmt.Fprintf(os.Stdout, "Submitting release job...\n")
	}

	id, err := opts.API.PostRelease(noInstanceID, jobs.ReleaseJobParams{
		ServiceSpec: service,
		ImageSpec:   image,
		Kind:        kind,
		Excludes:    excludes,
	})
	if err != nil {
		return err
	}

	fmt.Fprintf(os.Stdout, "Release job submitted, ID %s\n", id)
	if opts.noFollow {
		fmt.Fprintf(os.Stdout, "To check the status of this release job, run\n")
		fmt.Fprintf(os.Stdout, "\n")
		fmt.Fprintf(os.Stdout, "\tfluxctl check-release --release-id=%s\n", id)
		fmt.Fprintf(os.Stdout, "\n")
		return nil
	}

	// This is a bit funny, but works.
	return (&serviceCheckReleaseOpts{
		serviceOpts: opts.serviceOpts,
		releaseID:   string(id),
		noFollow:    false,
		noTty:       opts.noTty,
	}).RunE(cmd, nil)
}