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