diff --git a/command/daemon/daemon.go b/command/daemon/daemon.go index e5437ff..a5876ac 100644 --- a/command/daemon/daemon.go +++ b/command/daemon/daemon.go @@ -262,6 +262,11 @@ func setupLogger(config Config) { // Register the daemon command. func Register(app *kingpin.Application) { + registerDaemon(app) + registerProcess(app) +} + +func registerDaemon(app *kingpin.Application) { c := new(daemonCommand) cmd := app.Command("daemon", "starts the runner daemon"). diff --git a/command/daemon/process.go b/command/daemon/process.go new file mode 100644 index 0000000..77e62bd --- /dev/null +++ b/command/daemon/process.go @@ -0,0 +1,144 @@ +// Copyright 2019 Drone.IO Inc. All rights reserved. +// Use of this source code is governed by the Polyform License +// that can be found in the LICENSE file. + +package daemon + +import ( + "github.com/drone-runners/drone-runner-docker/engine" + "github.com/drone-runners/drone-runner-docker/engine/compiler" + "github.com/drone-runners/drone-runner-docker/engine/linter" + "github.com/drone-runners/drone-runner-docker/engine/resource" + + "github.com/drone/runner-go/client" + "github.com/drone/runner-go/environ/provider" + "github.com/drone/runner-go/logger" + "github.com/drone/runner-go/pipeline/reporter/remote" + "github.com/drone/runner-go/pipeline/runtime" + "github.com/drone/runner-go/registry" + "github.com/drone/runner-go/secret" + + "github.com/sirupsen/logrus" + "gopkg.in/alecthomas/kingpin.v2" +) + +type processCommand struct { + stage int64 +} + +func (c *processCommand) run(*kingpin.ParseContext) error { + // load the configuration from the environment + config, err := fromEnviron() + if err != nil { + return err + } + + // setup the global logrus logger. + setupLogger(config) + + cli := client.New( + config.Client.Address, + config.Client.Secret, + config.Client.SkipVerify, + ) + if config.Client.Dump { + cli.Dumper = logger.StandardDumper( + config.Client.DumpBody, + ) + } + cli.Logger = logger.Logrus( + logrus.NewEntry( + logrus.StandardLogger(), + ), + ) + + opts := engine.Opts{ + HidePull: !config.Docker.Stream, + } + engine, err := engine.NewEnv(opts) + if err != nil { + logrus.WithError(err). + Fatalln("cannot load the docker engine") + } + + remote := remote.New(cli) + + runner := &runtime.Runner{ + Client: cli, + Machine: config.Runner.Name, + Reporter: remote, + Lookup: resource.Lookup, + Lint: linter.New().Lint, + Match: nil, + Compiler: &compiler.Compiler{ + Clone: config.Runner.Clone, + Privileged: append(config.Runner.Privileged, compiler.Privileged...), + Networks: config.Runner.Networks, + Volumes: config.Runner.Volumes, + Resources: compiler.Resources{ + Memory: config.Resources.Memory, + MemorySwap: config.Resources.MemorySwap, + CPUQuota: config.Resources.CPUQuota, + CPUPeriod: config.Resources.CPUPeriod, + CPUShares: config.Resources.CPUShares, + CPUSet: config.Resources.CPUSet, + ShmSize: config.Resources.ShmSize, + }, + Environ: provider.Combine( + provider.Static(config.Runner.Environ), + provider.External( + config.Environ.Endpoint, + config.Environ.Token, + config.Environ.SkipVerify, + ), + ), + Registry: registry.Combine( + registry.File( + config.Docker.Config, + ), + registry.External( + config.Registry.Endpoint, + config.Registry.Token, + config.Registry.SkipVerify, + ), + ), + Secret: secret.Combine( + secret.StaticVars( + config.Runner.Secrets, + ), + secret.External( + config.Secret.Endpoint, + config.Secret.Token, + config.Secret.SkipVerify, + ), + ), + }, + Exec: runtime.NewExecer( + remote, + remote, + engine, + config.Runner.Procs, + ).Exec, + } + + err = runner.RunAccepted(nocontext, c.stage) + if err != nil { + // TODO should this return an error and fail the + // command? How does this impact Nomad? + logrus.WithError(err). + Errorln("pipeline execution failed") + } + return nil +} + +func registerProcess(app *kingpin.Application) { + c := new(processCommand) + + cmd := app.Command("process", "processes a pipeline by id"). + Action(c.run). + Hidden() + + cmd.Arg("id", "pipeline id"). + Required(). + Int64Var(&c.stage) +} diff --git a/go.mod b/go.mod index 185ee6d..5de7b3c 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/drone/drone-go v1.2.1-0.20200326064413-195394da1018 github.com/drone/drone-runtime v1.1.0 github.com/drone/envsubst v1.0.2 - github.com/drone/runner-go v1.6.1-0.20200415215637-a82f0982f1be + github.com/drone/runner-go v1.6.1-0.20200506182602-d2e6327ade15 github.com/drone/signal v1.0.0 github.com/ghodss/yaml v1.0.0 github.com/golang/mock v1.3.1 diff --git a/go.sum b/go.sum index 89d3976..15075dc 100644 --- a/go.sum +++ b/go.sum @@ -83,6 +83,8 @@ github.com/drone/runner-go v1.6.1-0.20200415161953-7df9235cfbdb h1:Z6sPkIuKY8rCp github.com/drone/runner-go v1.6.1-0.20200415161953-7df9235cfbdb/go.mod h1:+Zc4z9/xqlqkFkAcqOtuYjYS77d2PeuWh0PxxibHfNs= github.com/drone/runner-go v1.6.1-0.20200415215637-a82f0982f1be h1:OrDS3TyMcXgj40bT5OctlNiDFRF3d5a/BF30okxfoyA= github.com/drone/runner-go v1.6.1-0.20200415215637-a82f0982f1be/go.mod h1:+Zc4z9/xqlqkFkAcqOtuYjYS77d2PeuWh0PxxibHfNs= +github.com/drone/runner-go v1.6.1-0.20200506182602-d2e6327ade15 h1:+oj5a9GdF1DeQ3+i1pxARZBCd2wjYPPZveerAcF+WZk= +github.com/drone/runner-go v1.6.1-0.20200506182602-d2e6327ade15/go.mod h1:+Zc4z9/xqlqkFkAcqOtuYjYS77d2PeuWh0PxxibHfNs= github.com/drone/signal v1.0.0 h1:NrnM2M/4yAuU/tXs6RP1a1ZfxnaHwYkd0kJurA1p6uI= github.com/drone/signal v1.0.0/go.mod h1:S8t92eFT0g4WUgEc/LxG+LCuiskpMNsG0ajAMGnyZpc= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=