harness-drone/cmd/drone-agent/main.go
2019-02-27 23:07:13 -08:00

125 lines
2.9 KiB
Go

// 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.
// +build !oss
package main
import (
"context"
"github.com/drone/drone-runtime/engine/docker"
"github.com/drone/drone/cmd/drone-agent/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)
}
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,
},
}
if err := r.Start(ctx, config.Runner.Capacity); err != nil {
logrus.WithError(err).
Warnln("program terminated")
}
}
// 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,
})
}
}