harness-drone/cmd/drone-controller/main.go

148 lines
3.5 KiB
Go
Raw Normal View History

2019-02-19 23:56:41 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
package main
import (
"context"
"os"
"strconv"
"github.com/drone/drone-runtime/engine"
"github.com/drone/drone-runtime/engine/docker"
"github.com/drone/drone-runtime/engine/kube"
"github.com/drone/drone/cmd/drone-controller/config"
"github.com/drone/drone/operator/manager/rpc"
"github.com/drone/drone/operator/runner"
"github.com/drone/drone/plugin/registry"
"github.com/drone/drone/plugin/secret"
"github.com/drone/signal"
"github.com/sirupsen/logrus"
_ "github.com/joho/godotenv/autoload"
)
func main() {
config, err := config.Environ()
if err != nil {
logrus.WithError(err).Fatalln("invalid configuration")
}
initLogging(config)
ctx := signal.WithContext(
context.Background(),
)
secrets := secret.External(
config.Secrets.Endpoint,
config.Secrets.Password,
config.Secrets.SkipVerify,
)
auths := registry.Combine(
registry.External(
config.Secrets.Endpoint,
config.Secrets.Password,
config.Secrets.SkipVerify,
),
registry.FileSource(
config.Docker.Config,
),
registry.EndpointSource(
config.Registries.Endpoint,
config.Registries.Password,
config.Registries.SkipVerify,
),
)
manager := rpc.NewClient(
config.RPC.Proto+"://"+config.RPC.Host,
config.RPC.Secret,
)
if config.RPC.Debug {
manager.SetDebug(true)
}
if config.Logging.Trace {
manager.SetDebug(true)
}
var engine engine.Engine
if isKubernetes() {
engine, err = kube.NewFile("", "", config.Runner.Machine)
if err != nil {
logrus.WithError(err).
Fatalln("cannot create the kubernetes client")
}
} else {
engine, err = docker.NewEnv()
if err != nil {
logrus.WithError(err).
Fatalln("cannot load the docker engine")
}
}
r := &runner.Runner{
Platform: config.Runner.Platform,
OS: config.Runner.OS,
Arch: config.Runner.Arch,
Kernel: config.Runner.Kernel,
Variant: config.Runner.Variant,
Engine: engine,
Manager: manager,
Registry: auths,
Secrets: secrets,
Volumes: config.Runner.Volumes,
Networks: config.Runner.Networks,
Devices: config.Runner.Devices,
Privileged: config.Runner.Privileged,
Machine: config.Runner.Machine,
Labels: config.Runner.Labels,
Environ: config.Runner.Environ,
Limits: runner.Limits{
MemSwapLimit: int64(config.Runner.Limits.MemSwapLimit),
MemLimit: int64(config.Runner.Limits.MemLimit),
ShmSize: int64(config.Runner.Limits.ShmSize),
CPUQuota: config.Runner.Limits.CPUQuota,
CPUShares: config.Runner.Limits.CPUShares,
CPUSet: config.Runner.Limits.CPUSet,
},
}
id, err := strconv.ParseInt(os.Getenv("DRONE_STAGE_ID"), 10, 64)
if err != nil {
logrus.WithError(err).
Fatalln("cannot parse stage ID")
}
if err := r.Run(ctx, id); err != nil {
logrus.WithError(err).
Warnln("program terminated")
}
}
func isKubernetes() bool {
return os.Getenv("KUBERNETES_SERVICE_HOST") != ""
}
// helper funciton configures the logging.
func initLogging(c config.Config) {
if c.Logging.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
if c.Logging.Trace {
logrus.SetLevel(logrus.TraceLevel)
}
if c.Logging.Text {
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: c.Logging.Color,
DisableColors: !c.Logging.Color,
})
} else {
logrus.SetFormatter(&logrus.JSONFormatter{
PrettyPrint: c.Logging.Pretty,
})
}
}