2016-04-20 01:37:53 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-05-23 21:35:58 +00:00
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
2016-04-20 01:37:53 +00:00
|
|
|
"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-04-20 01:37:53 +00:00
|
|
|
"github.com/drone/drone/client"
|
|
|
|
|
|
|
|
"github.com/samalba/dockerclient"
|
|
|
|
)
|
|
|
|
|
2016-04-22 00:10:19 +00:00
|
|
|
type config struct {
|
|
|
|
platform string
|
|
|
|
namespace string
|
|
|
|
privileged []string
|
|
|
|
pull bool
|
2016-05-12 20:24:51 +00:00
|
|
|
logs int64
|
|
|
|
timeout time.Duration
|
2016-04-22 00:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type pipeline struct {
|
|
|
|
drone client.Client
|
|
|
|
docker dockerclient.Client
|
|
|
|
config config
|
2016-04-20 01:37:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 00:10:19 +00:00
|
|
|
func (r *pipeline) run() error {
|
|
|
|
w, err := r.drone.Pull("linux", "amd64")
|
2016-04-20 01:37:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-18 07:06:34 +00:00
|
|
|
running.Add(1)
|
|
|
|
defer func() {
|
|
|
|
running.Done()
|
|
|
|
}()
|
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
|
|
|
// streaming the logs
|
2016-05-23 21:35:58 +00:00
|
|
|
// 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
|
|
|
|
}
|
2016-04-20 01:37:53 +00:00
|
|
|
|
2016-05-11 00:03:24 +00:00
|
|
|
a := agent.Agent{
|
2016-05-23 21:35:58 +00:00
|
|
|
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),
|
2016-05-11 00:03:24 +00:00
|
|
|
Engine: engine,
|
2016-05-12 20:24:51 +00:00
|
|
|
Timeout: r.config.timeout,
|
2016-05-13 16:56:58 +00:00
|
|
|
Platform: r.config.platform,
|
2016-05-11 00:03:24 +00:00
|
|
|
Namespace: r.config.namespace,
|
|
|
|
Escalate: r.config.privileged,
|
|
|
|
Pull: r.config.pull,
|
2016-04-20 01:37:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-11 00:03:24 +00:00
|
|
|
// signal for canceling the build.
|
2016-04-22 00:10:19 +00:00
|
|
|
wait := r.drone.Wait(w.Job.ID)
|
2016-04-22 01:05:54 +00:00
|
|
|
defer wait.Cancel()
|
2016-04-20 01:37:53 +00:00
|
|
|
go func() {
|
2016-04-22 01:05:54 +00:00
|
|
|
if _, err := wait.Done(); err == nil {
|
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-05-11 00:03:24 +00:00
|
|
|
a.Run(w, cancel)
|
2016-04-20 01:37:53 +00:00
|
|
|
|
2016-05-23 21:35:58 +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
|
|
|
return nil
|
|
|
|
}
|