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