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 } }) }
func (opts *serviceAutomateOpts) RunE(_ *cobra.Command, args []string) error { if len(args) > 0 { return errorWantedNoArgs } if opts.service == "" { return newUsageError("-s, --service is required") } serviceID, err := flux.ParseServiceID(opts.service) if err != nil { return err } return opts.API.Automate(noInstanceID, serviceID) }
func (s *Server) History(inst flux.InstanceID, spec flux.ServiceSpec) (res []flux.HistoryEntry, err error) { defer func(begin time.Time) { s.metrics.HistoryDuration.With( "service_spec", fmt.Sprint(spec), "success", fmt.Sprint(err == nil), ).Observe(time.Since(begin).Seconds()) }(time.Now()) helper, err := s.instancer.Get(inst) if err != nil { return nil, errors.Wrapf(err, "getting instance") } var events []history.Event if spec == flux.ServiceSpecAll { events, err = helper.AllEvents() if err != nil { return nil, errors.Wrap(err, "fetching all history events") } } else { id, err := flux.ParseServiceID(string(spec)) if err != nil { return nil, errors.Wrapf(err, "parsing service ID from spec %s", spec) } namespace, service := id.Components() events, err = helper.EventsForService(namespace, service) if err != nil { return nil, errors.Wrapf(err, "fetching history events for %s", id) } } res = make([]flux.HistoryEntry, len(events)) for i, event := range events { res[i] = flux.HistoryEntry{ Stamp: event.Stamp, Type: "v0", Data: fmt.Sprintf("%s: %s", event.Service, event.Msg), } } return res, nil }
func (a *Automator) Handle(j *jobs.Job, _ jobs.JobUpdater) error { logger := log.NewContext(a.cfg.Logger).With("job", j.ID) params := j.Params.(jobs.AutomatedServiceJobParams) serviceID, err := flux.ParseServiceID(string(params.ServiceSpec)) if err != nil { // I don't see how we could ever expect this to work, so let's not // reschedule. return errors.Wrapf(err, "parsing service ID from spec %s", params.ServiceSpec) } config, err := a.cfg.InstanceDB.GetConfig(j.Instance) if err != nil { if err2 := a.reschedule(j); err2 != nil { logger.Log("err", err2) // abnormal } return errors.Wrap(err, "getting instance config") } s := config.Services[serviceID] if !s.Automated { // Job is not automated, don't reschedule return nil } if s.Locked { return a.reschedule(j) } if _, err := a.cfg.Jobs.PutJob(j.Instance, jobs.Job{ Method: jobs.ReleaseJob, Priority: jobs.PriorityBackground, Params: jobs.ReleaseJobParams{ ServiceSpec: params.ServiceSpec, ImageSpec: flux.ImageSpecLatest, Kind: flux.ReleaseKindExecute, }, }); err != nil { logger.Log("err", errors.Wrap(err, "put automated release job")) // abnormal } return a.reschedule(j) }
func handleUnlock(s api.FluxService) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { inst := getInstanceID(r) service := mux.Vars(r)["service"] id, err := flux.ParseServiceID(service) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, errors.Wrapf(err, "parsing service ID %q", id).Error()) return } if err = s.Unlock(inst, id); err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, err.Error()) return } w.WriteHeader(http.StatusOK) }) }
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) }
func (r *Releaser) Handle(job *jobs.Job, updater jobs.JobUpdater) (err error) { spec := job.Params.(jobs.ReleaseJobParams) releaseType := "unknown" defer func(begin time.Time) { r.metrics.ReleaseDuration.With( "release_type", releaseType, "release_kind", fmt.Sprint(spec.Kind), "success", fmt.Sprint(err == nil), ).Observe(time.Since(begin).Seconds()) }(time.Now()) inst, err := r.instancer.Get(job.Instance) if err != nil { return err } inst.Logger = log.NewContext(inst.Logger).With("job", job.ID) updateJob := func(format string, args ...interface{}) { status := fmt.Sprintf(format, args...) job.Status = status job.Log = append(job.Log, status) updater.UpdateJob(*job) } exclude := flux.ServiceIDSet{} exclude.Add(spec.Excludes) locked, err := lockedServices(inst) if err != nil { return err } exclude.Add(locked) updateJob("Calculating release actions.") switch { case spec.ServiceSpec == flux.ServiceSpecAll && spec.ImageSpec == flux.ImageSpecLatest: releaseType = "release_all_to_latest" return r.releaseImages(releaseType, "Release latest images to all services", inst, spec.Kind, allServicesExcept(exclude), allLatestImages, updateJob) case spec.ServiceSpec == flux.ServiceSpecAll && spec.ImageSpec == flux.ImageSpecNone: releaseType = "release_all_without_update" return r.releaseWithoutUpdate(releaseType, "Apply latest config to all services", inst, spec.Kind, allServicesExcept(exclude), updateJob) case spec.ServiceSpec == flux.ServiceSpecAll: releaseType = "release_all_for_image" imageID := flux.ParseImageID(string(spec.ImageSpec)) return r.releaseImages(releaseType, fmt.Sprintf("Release %s to all services", imageID), inst, spec.Kind, allServicesExcept(exclude), exactlyTheseImages([]flux.ImageID{imageID}), updateJob) case spec.ImageSpec == flux.ImageSpecLatest: releaseType = "release_one_to_latest" serviceID, err := flux.ParseServiceID(string(spec.ServiceSpec)) if err != nil { return errors.Wrapf(err, "parsing service ID from spec %s", spec.ServiceSpec) } services := flux.ServiceIDs([]flux.ServiceID{serviceID}).Without(exclude) return r.releaseImages(releaseType, fmt.Sprintf("Release latest images to %s", serviceID), inst, spec.Kind, exactlyTheseServices(services), allLatestImages, updateJob) case spec.ImageSpec == flux.ImageSpecNone: releaseType = "release_one_without_update" serviceID, err := flux.ParseServiceID(string(spec.ServiceSpec)) if err != nil { return errors.Wrapf(err, "parsing service ID from spec %s", spec.ServiceSpec) } services := flux.ServiceIDs([]flux.ServiceID{serviceID}).Without(exclude) return r.releaseWithoutUpdate(releaseType, fmt.Sprintf("Apply latest config to %s", serviceID), inst, spec.Kind, exactlyTheseServices(services), updateJob) default: releaseType = "release_one" serviceID, err := flux.ParseServiceID(string(spec.ServiceSpec)) if err != nil { return errors.Wrapf(err, "parsing service ID from spec %s", spec.ServiceSpec) } services := flux.ServiceIDs([]flux.ServiceID{serviceID}).Without(exclude) imageID := flux.ParseImageID(string(spec.ImageSpec)) return r.releaseImages(releaseType, fmt.Sprintf("Release %s to %s", imageID, serviceID), inst, spec.Kind, exactlyTheseServices(services), exactlyTheseImages([]flux.ImageID{imageID}), updateJob) } }