1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-30 08:06:52 +02:00
woodpecker/drone/agent/exec.go

84 lines
1.9 KiB
Go
Raw Normal View History

2016-04-20 03:37:53 +02:00
package agent
import (
"time"
"github.com/Sirupsen/logrus"
"github.com/drone/drone/agent"
"github.com/drone/drone/build/docker"
2016-09-28 03:30:28 +02:00
"github.com/drone/drone/model"
2016-09-26 10:29:05 +02:00
"github.com/drone/mq/stomp"
2016-04-20 03:37:53 +02:00
"github.com/samalba/dockerclient"
)
2016-04-22 02:10:19 +02:00
type config struct {
platform string
namespace string
privileged []string
pull bool
logs int64
timeout time.Duration
2016-04-22 02:10:19 +02:00
}
type pipeline struct {
2016-09-26 10:29:05 +02:00
drone *stomp.Client
2016-04-22 02:10:19 +02:00
docker dockerclient.Client
config config
2016-04-20 03:37:53 +02:00
}
2016-09-28 03:30:28 +02:00
func (r *pipeline) run(w *model.Work) {
2016-09-26 10:29:05 +02:00
// defer func() {
// // r.drone.Ack(id, opts)
// }()
2016-04-20 03:37:53 +02:00
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)
2016-04-20 03:37:53 +02:00
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,
2016-04-20 03:37:53 +02:00
}
2016-09-26 10:29:05 +02:00
cancelFunc := func(m *stomp.Message) {
defer m.Release()
id := m.Header.GetInt64("job-id")
if id == w.Job.ID {
cancel <- true
2016-04-20 03:37:53 +02:00
logrus.Infof("Cancel build %s/%s#%d.%d",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
}
2016-09-26 10:29:05 +02:00
}
// signal for canceling the build.
2016-09-28 02:33:13 +02:00
sub, err := r.drone.Subscribe("/topic/cancel", stomp.HandlerFunc(cancelFunc))
2016-09-26 10:29:05 +02:00
if err != nil {
2016-09-28 02:33:13 +02:00
logrus.Errorf("Error subscribing to /topic/cancel. %s", err)
2016-09-26 10:29:05 +02:00
}
defer func() {
r.drone.Unsubscribe(sub)
2016-04-20 03:37:53 +02:00
}()
a.Run(w, cancel)
2016-04-20 03:37:53 +02:00
2016-09-26 10:29:05 +02:00
// 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()
2016-04-20 03:37:53 +02:00
logrus.Infof("Finished build %s/%s#%d.%d",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
}