2016-04-20 03:37:53 +02:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-11 02:03:24 +02:00
|
|
|
"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 {
|
2016-11-17 03:08:25 +02:00
|
|
|
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)
|
|
|
|
|
2016-05-11 02:03:24 +02:00
|
|
|
cancel := make(chan bool, 1)
|
|
|
|
engine := docker.NewClient(r.docker)
|
2016-04-20 03:37:53 +02:00
|
|
|
|
2016-05-11 02:03:24 +02:00
|
|
|
a := agent.Agent{
|
2016-11-17 03:08:25 +02:00
|
|
|
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 {
|
2016-05-11 02:03:24 +02:00
|
|
|
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
|
|
|
}()
|
|
|
|
|
2016-05-11 02:03:24 +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)
|
2016-04-22 03:05:54 +02:00
|
|
|
}
|