示例#1
0
func (j createRepository) Execute(resp jobs.Response) {
	unitName := fmt.Sprintf("job-create-repo-%s.service", j.RequestId.String())
	path := j.Id.HomePath()

	if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {
		resp.Failure(ErrRepositoryAlreadyExists)
		return
	}

	conn := j.Systemd

	if err := conn.Subscribe(); err != nil {
		log.Print("create_repository:", err)
		return
	}
	defer conn.Unsubscribe()

	// make subscription global for efficiency
	changes, errch := conn.SubscribeUnitsCustom(1*time.Second, 2,
		func(s1 *dbus.UnitStatus, s2 *dbus.UnitStatus) bool {
			return true
		},
		func(unit string) bool {
			return unit != unitName
		})

	stdout, err := systemd.ProcessLogsForUnit(unitName)
	if err != nil {
		stdout = utils.EmptyReader
		log.Printf("create_repository: Unable to fetch build logs: %+v", err)
	}
	defer stdout.Close()

	startCmd := []string{
		filepath.Join("/", "usr", "bin", "gear"),
		"init-repo",
		string(j.Id),
	}
	if j.CloneUrl != "" {
		startCmd = append(startCmd, j.CloneUrl)
	}

	status, err := conn.StartTransientUnit(
		unitName,
		"fail",
		dbus.PropExecStart(startCmd, true),
		dbus.PropDescription(fmt.Sprintf("Create repository %s", j.Id)),
		dbus.PropRemainAfterExit(true),
		dbus.PropSlice("githost.slice"),
	)
	if err != nil {
		log.Printf("create_repository: Could not start unit %s: %s", unitName, systemd.SprintSystemdError(err))
		resp.Failure(ErrRepositoryCreateFailed)
		return
	} else if status != "done" {
		log.Printf("create_repository: Unit did not return 'done'")
		resp.Failure(ErrRepositoryCreateFailed)
		return
	}

	w := resp.SuccessWithWrite(jobs.ResponseAccepted, true, false)
	go io.Copy(w, stdout)

wait:
	for {
		select {
		case c := <-changes:
			if changed, ok := c[unitName]; ok {
				if changed.SubState != "running" {
					fmt.Fprintf(w, "Repository created succesfully\n")
					break wait
				}
			}
		case err := <-errch:
			fmt.Fprintf(w, "Error %+v\n", err)
		case <-time.After(10 * time.Second):
			log.Print("create_repository:", "timeout")
			break wait
		}
	}

	stdout.Close()
}
示例#2
0
func (j archiveRepository) Execute(resp jobs.Response) {
	w := resp.SuccessWithWrite(jobs.ResponseOk, false, false)
	if err := writeGitRepositoryArchive(w, j.RepositoryId.RepositoryPathFor(), j.Ref); err != nil {
		log.Printf("job_content: Invalid git repository stream: %v", err)
	}
}