2022-02-08 18:03:09 +01:00
|
|
|
// 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 exec
|
|
|
|
|
|
|
|
import (
|
2022-07-05 14:53:47 +02:00
|
|
|
"strings"
|
|
|
|
|
2022-02-08 18:03:09 +01:00
|
|
|
"github.com/drone-runners/drone-runner-docker/engine/compiler"
|
|
|
|
"github.com/drone/drone-go/drone"
|
2022-07-05 14:53:47 +02:00
|
|
|
"github.com/joho/godotenv"
|
2022-02-08 18:03:09 +01:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Flags maps
|
|
|
|
type Flags struct {
|
|
|
|
Build *drone.Build
|
|
|
|
Netrc *drone.Netrc
|
|
|
|
Repo *drone.Repo
|
|
|
|
Stage *drone.Stage
|
|
|
|
System *drone.System
|
|
|
|
}
|
|
|
|
|
|
|
|
type execCommand struct {
|
|
|
|
*Flags
|
|
|
|
|
|
|
|
Source string
|
|
|
|
Include []string
|
|
|
|
Exclude []string
|
|
|
|
Privileged []string
|
|
|
|
Networks []string
|
|
|
|
Volumes map[string]string
|
|
|
|
Environ map[string]string
|
|
|
|
Labels map[string]string
|
|
|
|
Secrets map[string]string
|
|
|
|
Resources compiler.Resources
|
|
|
|
Tmate compiler.Tmate
|
|
|
|
Clone bool
|
|
|
|
Config string
|
|
|
|
Pretty bool
|
|
|
|
Procs int64
|
|
|
|
Debug bool
|
|
|
|
Trace bool
|
|
|
|
Dump bool
|
|
|
|
PublicKey string
|
|
|
|
PrivateKey string
|
|
|
|
}
|
|
|
|
|
2022-11-23 19:33:13 +01:00
|
|
|
func mapOldToExecCommand(input *cli.Context) *execCommand {
|
2022-09-06 11:24:13 +02:00
|
|
|
pipelineFile := input.Args().First()
|
|
|
|
if pipelineFile == "" {
|
|
|
|
pipelineFile = ".drone.yml"
|
|
|
|
}
|
2022-11-23 19:33:13 +01:00
|
|
|
return &execCommand{
|
2022-02-08 18:03:09 +01:00
|
|
|
Flags: &Flags{
|
|
|
|
Build: &drone.Build{
|
|
|
|
Event: input.String("event"),
|
|
|
|
Ref: input.String("ref"),
|
|
|
|
Deploy: input.String("deploy-to"),
|
2022-07-05 14:53:47 +02:00
|
|
|
Target: input.String("branch"),
|
2022-02-08 18:03:09 +01:00
|
|
|
},
|
|
|
|
Repo: &drone.Repo{
|
|
|
|
Trusted: input.Bool("trusted"),
|
2022-07-05 14:53:47 +02:00
|
|
|
Timeout: int64(input.Duration("timeout").Seconds()),
|
2022-02-08 18:03:09 +01:00
|
|
|
Branch: input.String("branch"),
|
|
|
|
Name: input.String("name"),
|
|
|
|
},
|
|
|
|
Stage: &drone.Stage{
|
|
|
|
Name: input.String("pipeline"),
|
|
|
|
},
|
|
|
|
Netrc: &drone.Netrc{
|
|
|
|
Machine: input.String("netrc-machine"),
|
|
|
|
Login: input.String("netrc-username"),
|
|
|
|
Password: input.String("netrc-password"),
|
|
|
|
},
|
|
|
|
System: &drone.System{
|
|
|
|
Host: input.String("instance"),
|
|
|
|
},
|
|
|
|
},
|
2022-09-06 11:24:13 +02:00
|
|
|
Source: pipelineFile,
|
2022-02-08 18:03:09 +01:00
|
|
|
Include: input.StringSlice("include"),
|
|
|
|
Exclude: input.StringSlice("exclude"),
|
|
|
|
Clone: input.Bool("clone"),
|
|
|
|
Networks: input.StringSlice("network"),
|
2022-07-05 14:53:47 +02:00
|
|
|
Environ: readParams(input.String("env-file")),
|
|
|
|
Volumes: withVolumeSlice(input.StringSlice("volume")),
|
2022-11-24 11:57:48 +01:00
|
|
|
Secrets: readParams(input.String("secret-file")),
|
2022-07-05 14:53:47 +02:00
|
|
|
Config: input.String("registry"),
|
2022-02-08 18:03:09 +01:00
|
|
|
Privileged: input.StringSlice("privileged"),
|
2022-11-23 19:33:13 +01:00
|
|
|
Pretty: input.BoolT("pretty"),
|
2022-02-08 18:03:09 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 14:53:47 +02:00
|
|
|
|
|
|
|
// WithVolumeSlice is a transform function that adds a set of global volumes to the container that are defined in --volume=host:container format.
|
|
|
|
func withVolumeSlice(volumes []string) (to map[string]string) {
|
|
|
|
to = map[string]string{}
|
|
|
|
for _, s := range volumes {
|
|
|
|
parts := strings.Split(s, ":")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := parts[0]
|
|
|
|
val := parts[1]
|
|
|
|
to[key] = val
|
|
|
|
}
|
|
|
|
return to
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function reads secrets from a key-value file.
|
|
|
|
func readParams(path string) map[string]string {
|
|
|
|
data, _ := godotenv.Read(path)
|
|
|
|
return data
|
|
|
|
}
|