2021-01-11 20:54:49 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2021-01-22 11:00:14 +00:00
|
|
|
"time"
|
2021-01-11 20:54:49 +00:00
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2023-02-08 09:13:28 +00:00
|
|
|
"golang.org/x/sys/execabs"
|
2021-01-11 20:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// helper function to create the docker login command.
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandLogin(login Login) *execabs.Cmd {
|
2021-01-11 20:54:49 +00:00
|
|
|
if login.Email != "" {
|
|
|
|
return commandLoginEmail(login)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"login",
|
2021-01-11 20:54:49 +00:00
|
|
|
"-u", login.Username,
|
|
|
|
"-p", login.Password,
|
|
|
|
login.Registry,
|
2023-02-08 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return execabs.Command(
|
|
|
|
dockerBin, args...,
|
2021-01-11 20:54:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandLoginEmail(login Login) *execabs.Cmd {
|
|
|
|
args := []string{
|
|
|
|
"login",
|
2021-01-11 20:54:49 +00:00
|
|
|
"-u", login.Username,
|
|
|
|
"-p", login.Password,
|
|
|
|
"-e", login.Email,
|
|
|
|
login.Registry,
|
2023-02-08 09:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return execabs.Command(
|
|
|
|
dockerBin, args...,
|
2021-01-11 20:54:49 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to create the docker info command.
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandVersion() *execabs.Cmd {
|
|
|
|
return execabs.Command(dockerBin, "version")
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to create the docker info command.
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandInfo() *execabs.Cmd {
|
|
|
|
return execabs.Command(dockerBin, "info")
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandBuilder(daemon Daemon) *execabs.Cmd {
|
2021-07-08 07:00:51 +00:00
|
|
|
args := []string{
|
2021-07-25 12:28:33 +00:00
|
|
|
"buildx",
|
|
|
|
"create",
|
2021-07-08 07:00:51 +00:00
|
|
|
"--use",
|
|
|
|
}
|
|
|
|
|
|
|
|
if daemon.BuildkitConfig != "" {
|
2021-07-25 12:28:33 +00:00
|
|
|
args = append(args, "--config", buildkitConfig)
|
2021-07-08 07:00:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
return execabs.Command(dockerBin, args...)
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandBuildx() *execabs.Cmd {
|
|
|
|
return execabs.Command(dockerBin, "buildx", "ls")
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to create the docker build command.
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandBuild(build Build, dryrun bool) *execabs.Cmd {
|
2021-01-11 20:54:49 +00:00
|
|
|
args := []string{
|
|
|
|
"buildx",
|
|
|
|
"build",
|
|
|
|
"--rm=true",
|
|
|
|
"-f", build.Dockerfile,
|
|
|
|
}
|
|
|
|
|
2021-01-22 11:57:32 +00:00
|
|
|
defaultBuildArgs := []string{
|
|
|
|
fmt.Sprintf("DOCKER_IMAGE_CREATED=%s", time.Now().Format(time.RFC3339)),
|
|
|
|
}
|
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
args = append(args, build.Context)
|
2022-01-27 20:53:46 +00:00
|
|
|
if !dryrun && build.Output == "" && len(build.Tags.Value()) > 0 {
|
2021-07-02 16:58:55 +00:00
|
|
|
args = append(args, "--push")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if build.Compress {
|
|
|
|
args = append(args, "--compress")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if build.Pull {
|
|
|
|
args = append(args, "--pull=true")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if build.NoCache {
|
|
|
|
args = append(args, "--no-cache")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2022-10-31 19:10:18 +00:00
|
|
|
for _, arg := range build.CacheFrom {
|
2021-01-11 20:54:49 +00:00
|
|
|
args = append(args, "--cache-from", arg)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2022-08-08 11:36:23 +00:00
|
|
|
if build.CacheTo != "" {
|
|
|
|
args = append(args, "--cache-to", build.CacheTo)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
for _, arg := range build.ArgsEnv.Value() {
|
|
|
|
addProxyValue(&build, arg)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-22 11:57:32 +00:00
|
|
|
for _, arg := range append(defaultBuildArgs, build.Args.Value()...) {
|
2021-01-11 20:54:49 +00:00
|
|
|
args = append(args, "--build-arg", arg)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
for _, host := range build.AddHost.Value() {
|
|
|
|
args = append(args, "--add-host", host)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if build.Target != "" {
|
|
|
|
args = append(args, "--target", build.Target)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if build.Quiet {
|
|
|
|
args = append(args, "--quiet")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2022-01-27 20:53:46 +00:00
|
|
|
if build.Output != "" {
|
|
|
|
args = append(args, "--output", build.Output)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2022-07-24 10:48:02 +00:00
|
|
|
for _, arg := range build.NamedContext.Value() {
|
|
|
|
args = append(args, "--build-context", arg)
|
|
|
|
}
|
2021-01-11 20:54:49 +00:00
|
|
|
|
|
|
|
if len(build.Platforms.Value()) > 0 {
|
2022-04-25 10:51:42 +00:00
|
|
|
args = append(args, "--platform", strings.Join(build.Platforms.Value(), ","))
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 16:58:55 +00:00
|
|
|
for _, arg := range build.Tags.Value() {
|
|
|
|
args = append(args, "-t", fmt.Sprintf("%s:%s", build.Repo, arg))
|
2021-07-25 12:28:33 +00:00
|
|
|
}
|
2021-07-02 16:58:55 +00:00
|
|
|
|
2022-10-09 12:05:05 +00:00
|
|
|
for _, arg := range build.ExtraTags.Value() {
|
|
|
|
args = append(args, "-t", arg)
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:35:29 +00:00
|
|
|
for _, arg := range build.Labels.Value() {
|
|
|
|
args = append(args, "--label", arg)
|
|
|
|
}
|
|
|
|
|
2023-01-12 10:28:22 +00:00
|
|
|
if build.Provenance != "" {
|
|
|
|
args = append(args, "--provenance", build.Provenance)
|
|
|
|
}
|
|
|
|
|
2023-03-08 14:51:24 +00:00
|
|
|
if build.SBOM != "" {
|
|
|
|
args = append(args, "--sbom", build.SBOM)
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
return execabs.Command(dockerBin, args...)
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:13:28 +00:00
|
|
|
// helper function to add proxy values from the environment.
|
2021-01-11 20:54:49 +00:00
|
|
|
func addProxyBuildArgs(build *Build) {
|
|
|
|
addProxyValue(build, "http_proxy")
|
|
|
|
addProxyValue(build, "https_proxy")
|
|
|
|
addProxyValue(build, "no_proxy")
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to add the upper and lower case version of a proxy value.
|
|
|
|
func addProxyValue(build *Build, key string) {
|
|
|
|
value := getProxyValue(key)
|
|
|
|
|
|
|
|
if len(value) > 0 && !hasProxyBuildArg(build, key) {
|
2021-01-11 21:06:24 +00:00
|
|
|
build.Args = *cli.NewStringSlice(append(build.Args.Value(), fmt.Sprintf("%s=%s", key, value))...)
|
|
|
|
build.Args = *cli.NewStringSlice(append(build.Args.Value(), fmt.Sprintf("%s=%s", strings.ToUpper(key), value))...)
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to get a proxy value from the environment.
|
|
|
|
//
|
|
|
|
// assumes that the upper and lower case versions of are the same.
|
|
|
|
func getProxyValue(key string) string {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
|
|
|
|
if len(value) > 0 {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Getenv(strings.ToUpper(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function that looks to see if a proxy value was set in the build args.
|
|
|
|
func hasProxyBuildArg(build *Build, key string) bool {
|
|
|
|
keyUpper := strings.ToUpper(key)
|
|
|
|
|
|
|
|
for _, s := range build.Args.Value() {
|
|
|
|
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to create the docker daemon command.
|
2023-02-08 09:13:28 +00:00
|
|
|
func commandDaemon(daemon Daemon) *execabs.Cmd {
|
2021-01-11 20:54:49 +00:00
|
|
|
args := []string{
|
|
|
|
"--data-root", daemon.StoragePath,
|
|
|
|
"--host=unix:///var/run/docker.sock",
|
|
|
|
}
|
|
|
|
|
|
|
|
if daemon.StorageDriver != "" {
|
|
|
|
args = append(args, "-s", daemon.StorageDriver)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if daemon.Insecure && daemon.Registry != "" {
|
|
|
|
args = append(args, "--insecure-registry", daemon.Registry)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if daemon.IPv6 {
|
|
|
|
args = append(args, "--ipv6")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if len(daemon.Mirror) != 0 {
|
|
|
|
args = append(args, "--registry-mirror", daemon.Mirror)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if len(daemon.Bip) != 0 {
|
|
|
|
args = append(args, "--bip", daemon.Bip)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
for _, dns := range daemon.DNS.Value() {
|
|
|
|
args = append(args, "--dns", dns)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
for _, dnsSearch := range daemon.DNSSearch.Value() {
|
|
|
|
args = append(args, "--dns-search", dnsSearch)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if len(daemon.MTU) != 0 {
|
|
|
|
args = append(args, "--mtu", daemon.MTU)
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
2021-01-11 20:54:49 +00:00
|
|
|
if daemon.Experimental {
|
|
|
|
args = append(args, "--experimental")
|
|
|
|
}
|
2023-02-08 09:13:28 +00:00
|
|
|
|
|
|
|
return execabs.Command(dockerdBin, args...)
|
2021-01-11 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// trace writes each command to stdout with the command wrapped in an xml
|
|
|
|
// tag so that it can be extracted and displayed in the logs.
|
2023-02-08 09:13:28 +00:00
|
|
|
func trace(cmd *execabs.Cmd) {
|
2021-01-11 20:54:49 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "+ %s\n", strings.Join(cmd.Args, " "))
|
|
|
|
}
|