func (j *ContainerLogRequest) Execute(resp jobs.Response) { if _, err := os.Stat(j.Id.UnitPathFor()); err != nil { resp.Failure(ErrContainerNotFound) return } w := resp.SuccessWithWrite(jobs.ResponseOk, true, false) err := systemd.WriteLogsTo(w, j.Id.UnitNameFor(), 30, time.After(30*time.Second)) if err != nil { log.Printf("job_container_log: Unable to fetch journal logs: %s\n", err.Error()) } }
func (j *StoppedContainerStateRequest) Execute(resp jobs.Response) { unitName := j.Id.UnitNameFor() inState, tooSoon := inStateOrTooSoon(j.Id, unitName, false, false, rateLimitChanges) if inState { w := resp.SuccessWithWrite(jobs.ResponseAccepted, true, false) fmt.Fprintf(w, "Container %s is stopped\n", j.Id) return } if tooSoon { resp.Failure(ErrStopRequestThrottled) return } if errs := csystemd.SetUnitStartOnBoot(j.Id, false); errs != nil { log.Print("alter_container_state: Unable to persist whether the unit is started on boot: ", errs) resp.Failure(ErrContainerStopFailed) return } w := resp.SuccessWithWrite(jobs.ResponseAccepted, true, false) done := make(chan time.Time) ioerr := make(chan error) go func() { ioerr <- systemd.WriteLogsTo(w, unitName, 0, done) }() joberr := make(chan error) go func() { status, err := systemd.Connection().StopUnit(unitName, "replace") if err == nil && status != "done" { err = errors.New(fmt.Sprintf("Job status 'done' != %s", status)) } joberr <- err }() var err error select { case err = <-ioerr: log.Printf("alter_container_state: Client hung up") close(ioerr) case err = <-joberr: log.Printf("alter_container_state: Stop job done") case <-time.After(15 * time.Second): log.Printf("alter_container_state: Timeout waiting for stop completion") } close(done) select { case <-ioerr: } switch { case systemd.IsNoSuchUnit(err): if _, err := os.Stat(j.Id.UnitPathFor()); err == nil { fmt.Fprintf(w, "Container %s is stopped\n", j.Id) } else { fmt.Fprintf(w, "No such container %s\n", j.Id) } case err != nil: fmt.Fprintf(w, "Could not stop container: %s\n", err.Error()) default: fmt.Fprintf(w, "Container %s is stopped\n", j.Id) } }