Automatically generate buildkit config with registry CA file (#17)

Fixes #14

Buildkit config is actually TOML file not JSON - https://docs.docker.com/engine/reference/commandline/buildx_create/#config

Tested using `lafriks/plugin-docker-buildx:latest` image built with these changes

Co-authored-by: Lauris BH <lauris@nix.lv>
Reviewed-on: https://codeberg.org/woodpecker-plugins/plugin-docker-buildx/pulls/17
Reviewed-by: 6543 <6543@obermui.de>
Co-authored-by: Lauris BH <lafriks@noreply.codeberg.org>
Co-committed-by: Lauris BH <lafriks@noreply.codeberg.org>
This commit is contained in:
Lauris BH
2022-09-27 22:33:05 +02:00
committed by 6543
parent 64aed54d14
commit 8a0424c7a5
3 changed files with 46 additions and 11 deletions

View File

@@ -5,10 +5,13 @@ import (
"os"
)
const dockerExe = "/usr/local/bin/docker"
const dockerdExe = "/usr/local/bin/dockerd"
const dockerHome = "/root/.docker/"
const buildkitConfig = "/tmp/buildkit.json"
const (
dockerExe = "/usr/local/bin/docker"
dockerdExe = "/usr/local/bin/dockerd"
dockerHome = "/root/.docker/"
buildkitConfig = "/tmp/buildkit.toml"
buildkitConfigTemplate = "[registry.\"%s\"]\n ca=[\"%s\"]\n"
)
func (p Plugin) startDaemon() {
cmd := commandDaemon(p.settings.Daemon)

View File

@@ -2,6 +2,7 @@ package plugin
import (
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
@@ -101,6 +102,32 @@ func (p *Plugin) Validate() error {
return nil
}
func (p *Plugin) writeBuildkitConfig() error {
if p.settings.Daemon.BuildkitConfig == "" && p.settings.Daemon.Registry != "" {
registry := p.settings.Daemon.Registry
u, err := url.Parse(registry)
if err == nil && u.Host != "" {
registry = u.Host
}
caPath := fmt.Sprintf("/etc/docker/certs.d/%s/ca.crt", registry)
ca, err := os.Open(caPath)
if err != nil && !os.IsNotExist(err) {
logrus.Warnf("error reading %s: %w", caPath, err)
} else if err == nil {
ca.Close()
p.settings.Daemon.BuildkitConfig = fmt.Sprintf(buildkitConfigTemplate, registry, caPath)
}
}
if p.settings.Daemon.BuildkitConfig != "" {
err := os.WriteFile(buildkitConfig, []byte(p.settings.Daemon.BuildkitConfig), 0o600)
if err != nil {
return fmt.Errorf("error writing buildkit.toml: %s", err)
}
}
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
// start the Docker daemon server
@@ -139,11 +166,8 @@ func (p *Plugin) Execute() error {
}
}
if p.settings.Daemon.BuildkitConfig != "" {
err := os.WriteFile(buildkitConfig, []byte(p.settings.Daemon.BuildkitConfig), 0o600)
if err != nil {
return fmt.Errorf("error writing buildkit.json: %s", err)
}
if err := p.writeBuildkitConfig(); err != nil {
return err
}
switch {