Exemplo n.º 1
0
func (r *pipeline) run(w *model.Work) {

	// defer func() {
	// 	// r.drone.Ack(id, opts)
	// }()

	logrus.Infof("Starting build %s/%s#%d.%d",
		w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)

	cancel := make(chan bool, 1)
	engine := docker.NewClient(r.docker)

	a := agent.Agent{
		Update:    agent.NewClientUpdater(r.drone),
		Logger:    agent.NewClientLogger(r.drone, w.Job.ID, r.config.logs),
		Engine:    engine,
		Timeout:   r.config.timeout,
		Platform:  r.config.platform,
		Namespace: r.config.namespace,
		Escalate:  r.config.privileged,
		Pull:      r.config.pull,
	}

	cancelFunc := func(m *stomp.Message) {
		defer m.Release()

		id := m.Header.GetInt64("job-id")
		if id == w.Job.ID {
			cancel <- true
			logrus.Infof("Cancel build %s/%s#%d.%d",
				w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
		}
	}

	// signal for canceling the build.
	sub, err := r.drone.Subscribe("/topic/cancel", stomp.HandlerFunc(cancelFunc))
	if err != nil {
		logrus.Errorf("Error subscribing to /topic/cancel. %s", err)
	}
	defer func() {
		r.drone.Unsubscribe(sub)
	}()

	a.Run(w, cancel)

	// if err := r.drone.LogPost(w.Job.ID, ioutil.NopCloser(&buf)); err != nil {
	// 	logrus.Errorf("Error sending logs for %s/%s#%d.%d",
	// 		w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
	// }
	// stream.Close()

	logrus.Infof("Finished build %s/%s#%d.%d",
		w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
}
Exemplo n.º 2
0
func exec(c *cli.Context) error {
	sigterm := make(chan os.Signal, 1)
	cancelc := make(chan bool, 1)
	signal.Notify(sigterm, os.Interrupt)
	go func() {
		<-sigterm
		cancelc <- true
	}()

	path := c.Args().First()
	if path == "" {
		path = ".drone.yml"
	}
	path, _ = filepath.Abs(path)
	dir := filepath.Dir(path)

	file, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}

	engine, err := docker.New(
		c.String("docker-host"),
		c.String("docker-cert-path"),
		c.Bool("docker-tls-verify"),
	)
	if err != nil {
		return err
	}

	a := agent.Agent{
		Update:    agent.NoopUpdateFunc,
		Logger:    agent.TermLoggerFunc,
		Engine:    engine,
		Timeout:   c.Duration("timeout.inactivity"),
		Platform:  "linux/amd64",
		Namespace: c.String("namespace"),
		Disable:   c.StringSlice("plugin"),
		Escalate:  c.StringSlice("privileged"),
		Netrc:     []string{},
		Local:     dir,
		Pull:      c.Bool("pull"),
	}

	payload := &model.Work{
		Yaml:     string(file),
		Verified: c.BoolT("yaml.verified"),
		Signed:   c.BoolT("yaml.signed"),
		Repo: &model.Repo{
			FullName:  c.String("repo.fullname"),
			Owner:     c.String("repo.owner"),
			Name:      c.String("repo.name"),
			Kind:      c.String("repo.type"),
			Link:      c.String("repo.link"),
			Branch:    c.String("repo.branch"),
			Avatar:    c.String("repo.avatar"),
			Timeout:   int64(c.Duration("timeout").Minutes()),
			IsPrivate: c.Bool("repo.private"),
			IsTrusted: c.Bool("repo.trusted"),
			Clone:     c.String("remote.url"),
		},
		System: &model.System{
			Link: c.GlobalString("server"),
		},
		Secrets: getSecrets(c),
		Netrc: &model.Netrc{
			Login:    c.String("netrc.username"),
			Password: c.String("netrc.password"),
			Machine:  c.String("netrc.machine"),
		},
		Build: &model.Build{
			Commit:  c.String("commit.sha"),
			Branch:  c.String("commit.branch"),
			Ref:     c.String("commit.ref"),
			Link:    c.String("commit.link"),
			Message: c.String("commit.message"),
			Author:  c.String("commit.author.name"),
			Email:   c.String("commit.author.email"),
			Avatar:  c.String("commit.author.avatar"),
			Number:  c.Int("build.number"),
			Event:   c.String("build.event"),
			Deploy:  c.String("build.deploy"),
		},
		BuildLast: &model.Build{
			Number: c.Int("prev.build.number"),
			Status: c.String("prev.build.status"),
			Commit: c.String("prev.commit.sha"),
		},
	}

	if len(c.StringSlice("matrix")) > 0 {
		p := *payload
		p.Job = &model.Job{
			Environment: getMatrix(c),
		}
		return a.Run(&p, cancelc)
	}

	axes, err := yaml.ParseMatrix(file)
	if err != nil {
		return err
	}

	if len(axes) == 0 {
		axes = append(axes, yaml.Axis{})
	}

	var jobs []*model.Job
	count := 0
	for _, axis := range axes {
		jobs = append(jobs, &model.Job{
			Number:      count,
			Environment: axis,
		})
		count++
	}

	for _, job := range jobs {
		fmt.Printf("Running Matrix job #%d\n", job.Number)
		p := *payload
		p.Job = job
		if err := a.Run(&p, cancelc); err != nil {
			return err
		}
	}

	return nil
}
Exemplo n.º 3
0
Arquivo: exec.go Projeto: Ablu/drone
func (r *pipeline) run() error {
	w, err := r.drone.Pull("linux", "amd64")
	if err != nil {
		return err
	}

	logrus.Infof("Starting build %s/%s#%d.%d",
		w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)

	cancel := make(chan bool, 1)
	engine := docker.NewClient(r.docker)

	// streaming the logs
	// rc, wc := io.Pipe()
	// defer func() {
	// 	wc.Close()
	// 	rc.Close()
	// }()

	var buf bytes.Buffer

	stream, err := r.drone.LogStream(w.Job.ID)
	if err != nil {
		return err
	}

	a := agent.Agent{
		Update: agent.NewClientUpdater(r.drone),
		// Logger:    agent.NewClientLogger(r.drone, w.Job.ID, rc, wc, r.config.logs),
		Logger:    agent.NewStreamLogger(stream, &buf, r.config.logs),
		Engine:    engine,
		Timeout:   r.config.timeout,
		Platform:  r.config.platform,
		Namespace: r.config.namespace,
		Escalate:  r.config.privileged,
		Pull:      r.config.pull,
	}

	// signal for canceling the build.
	wait := r.drone.Wait(w.Job.ID)
	defer wait.Cancel()
	go func() {
		if _, err := wait.Done(); err == nil {
			cancel <- true
			logrus.Infof("Cancel build %s/%s#%d.%d",
				w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
		}
	}()

	a.Run(w, cancel)

	if err := r.drone.LogPost(w.Job.ID, ioutil.NopCloser(&buf)); err != nil {
		logrus.Errorf("Error sending logs for %s/%s#%d.%d",
			w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
	}
	stream.Close()

	logrus.Infof("Finished build %s/%s#%d.%d",
		w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)

	return nil
}