harness-drone/drone/agent/agent.go

179 lines
3.9 KiB
Go
Raw Normal View History

2016-04-20 01:37:53 +00:00
package agent
import (
"sync"
"time"
"github.com/drone/drone/client"
"github.com/samalba/dockerclient"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
)
// AgentCmd is the exported command for starting the drone agent.
var AgentCmd = cli.Command{
Name: "agent",
Usage: "starts the drone agent",
Action: start,
Flags: []cli.Flag{
cli.StringFlag{
EnvVar: "DOCKER_HOST",
Name: "docker-host",
Usage: "docker deamon address",
Value: "unix:///var/run/docker.sock",
},
cli.BoolFlag{
EnvVar: "DOCKER_TLS_VERIFY",
Name: "docker-tls-verify",
Usage: "docker daemon supports tlsverify",
},
cli.StringFlag{
EnvVar: "DOCKER_CERT_PATH",
Name: "docker-cert-path",
Usage: "docker certificate directory",
Value: "",
},
cli.IntFlag{
EnvVar: "DOCKER_MAX_PROCS",
Name: "docker-max-procs",
Usage: "limit number of running docker processes",
Value: 2,
},
2016-04-22 00:10:19 +00:00
cli.StringFlag{
EnvVar: "DOCKER_OS",
Name: "docker-os",
Usage: "docker operating system",
Value: "linux",
},
cli.StringFlag{
EnvVar: "DOCKER_ARCH",
Name: "docker-arch",
Usage: "docker architecture system",
Value: "amd64",
},
2016-04-20 01:37:53 +00:00
cli.StringFlag{
EnvVar: "DRONE_SERVER",
Name: "drone-server",
Usage: "drone server address",
Value: "http://localhost:8000",
},
cli.StringFlag{
EnvVar: "DRONE_TOKEN",
Name: "drone-token",
Usage: "drone authorization token",
},
cli.DurationFlag{
2016-04-21 08:18:20 +00:00
EnvVar: "DRONE_BACKOFF",
Name: "backoff",
2016-04-20 01:37:53 +00:00
Usage: "drone server backoff interval",
Value: time.Second * 15,
},
cli.BoolFlag{
2016-04-21 08:18:20 +00:00
EnvVar: "DRONE_DEBUG",
2016-04-20 01:37:53 +00:00
Name: "debug",
Usage: "start the agent in debug mode",
},
cli.BoolFlag{
2016-04-21 08:18:20 +00:00
EnvVar: "DRONE_EXPERIMENTAL",
2016-04-20 01:37:53 +00:00
Name: "experimental",
Usage: "start the agent with experimental features",
},
2016-04-21 08:18:20 +00:00
cli.StringSliceFlag{
2016-04-22 00:10:19 +00:00
EnvVar: "DRONE_PLUGIN_NETRC",
2016-04-21 08:18:20 +00:00
Name: "netrc-plugin",
Usage: "plugins that receive the netrc file",
2016-04-23 11:27:28 +00:00
Value: &cli.StringSlice{
"git",
"git:*",
"hg",
"hg:*",
"plugins/hg",
"plugins/hg:*",
"plugins/git",
"plugins/git:*",
},
2016-04-21 08:18:20 +00:00
},
cli.StringSliceFlag{
2016-04-22 00:10:19 +00:00
EnvVar: "DRONE_PLUGIN_PRIVILEGED",
Name: "privileged",
2016-04-21 08:18:20 +00:00
Usage: "plugins that require privileged mode",
2016-04-22 00:10:19 +00:00
Value: &cli.StringSlice{
"plugins/docker",
"plugins/docker:*",
2016-05-02 17:22:04 +00:00
"plugins/gcr",
"plugins/gcr:*",
2016-04-22 00:10:19 +00:00
"plugins/ecr",
"plugins/ecr:*",
},
},
cli.BoolFlag{
EnvVar: "DRONE_PLUGIN_PULL",
Name: "pull",
Usage: "always pull latest plugin images",
},
cli.StringFlag{
EnvVar: "DRONE_PLUGIN_NAMESPACE",
Name: "namespace",
Value: "plugins",
Usage: "default plugin image namespace",
},
cli.StringSliceFlag{
EnvVar: "DRONE_PLUGIN_WHITELIST",
Name: "whitelist",
Usage: "plugins that are permitted to run on the host",
Value: &cli.StringSlice{"plugins/*"},
2016-04-21 08:18:20 +00:00
},
2016-04-20 01:37:53 +00:00
},
}
func start(c *cli.Context) {
// debug level if requested by user
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.WarnLevel)
}
client := client.NewClientToken(
c.String("drone-server"),
c.String("drone-token"),
)
tls, _ := dockerclient.TLSConfigFromCertPath(c.String("docker-cert-path"))
if c.Bool("docker-host") {
tls.InsecureSkipVerify = true
}
docker, err := dockerclient.NewDockerClient(c.String("docker-host"), tls)
if err != nil {
logrus.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < c.Int("docker-max-procs"); i++ {
wg.Add(1)
go func() {
2016-04-22 00:10:19 +00:00
r := pipeline{
drone: client,
docker: docker,
config: config{
whitelist: c.StringSlice("whitelist"),
namespace: c.String("namespace"),
privileged: c.StringSlice("privileged"),
netrc: c.StringSlice("netrc-plugin"),
pull: c.Bool("pull"),
},
}
2016-04-20 01:37:53 +00:00
for {
2016-04-22 00:10:19 +00:00
if err := r.run(); err != nil {
2016-04-21 08:18:20 +00:00
dur := c.Duration("backoff")
2016-04-22 00:10:19 +00:00
logrus.Warnf("Attempting to reconnect in %v", dur)
2016-04-20 01:37:53 +00:00
time.Sleep(dur)
}
}
}()
}
wg.Wait()
}