2023-10-29 21:44:39 +00:00
|
|
|
package hetzner
|
|
|
|
|
|
|
|
import (
|
2023-10-30 21:46:17 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-10-29 21:44:39 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
var USER_DATA_TEMPLATE = `
|
|
|
|
write_files:
|
|
|
|
- content: |
|
|
|
|
# docker-compose.yml
|
|
|
|
version: '3'
|
|
|
|
|
|
|
|
services:
|
|
|
|
|
|
|
|
woodpecker-agent:
|
|
|
|
image: {{ .Image }}
|
|
|
|
command: agent
|
|
|
|
restart: always
|
|
|
|
volumes:
|
|
|
|
- /var/run/docker.sock:/var/run/docker.sock
|
|
|
|
environment:
|
|
|
|
{{- range $key, $val := .EnvConfig }}
|
|
|
|
- {{ $key }}: {{ $val }}
|
|
|
|
{{- end }}
|
|
|
|
path: /root/docker-compose.yml
|
|
|
|
runcmd:
|
|
|
|
- [ sh, -xc, "cd /root; docker run --rm --privileged multiarch/qemu-user-static --reset -p yes; docker compose up -d" ]
|
|
|
|
`
|
|
|
|
|
|
|
|
type UserDataConfig struct {
|
|
|
|
Image string
|
|
|
|
EnvConfig map[string]string
|
|
|
|
}
|
|
|
|
|
2023-10-30 21:46:17 +00:00
|
|
|
func generateConfig(cfg *config.Config) (string, error) {
|
2023-10-29 21:44:39 +00:00
|
|
|
envConfig := map[string]string{}
|
|
|
|
envConfig["WOODPECKER_SERVER"] = cfg.WoodpeckerInstance
|
|
|
|
envConfig["WOODPECKER_AGENT_SECRET"] = cfg.WoodpeckerAgentSecret
|
|
|
|
envConfig["WOODPECKER_FILTER_LABELS"] = cfg.LabelSelector
|
|
|
|
config := UserDataConfig{
|
|
|
|
Image: "woodpeckerci/woodpecker-agent:latest",
|
|
|
|
EnvConfig: envConfig,
|
|
|
|
}
|
2023-10-30 21:46:17 +00:00
|
|
|
tmpl, err := template.New("userdata").Parse(USER_DATA_TEMPLATE)
|
2023-10-29 21:44:39 +00:00
|
|
|
if err != nil {
|
2023-10-30 21:46:17 +00:00
|
|
|
return "", errors.New(fmt.Sprintf("Errors in userdata template: %s", err.Error()))
|
2023-10-29 21:44:39 +00:00
|
|
|
}
|
2023-10-30 21:46:17 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = tmpl.Execute(&buf, &config)
|
2023-10-29 21:44:39 +00:00
|
|
|
if err != nil {
|
2023-10-30 21:46:17 +00:00
|
|
|
return "", errors.New(fmt.Sprintf("Could not render userdata template: %s", err.Error()))
|
2023-10-29 21:44:39 +00:00
|
|
|
}
|
2023-10-30 21:46:17 +00:00
|
|
|
|
|
|
|
return buf.String(), nil
|
2023-10-29 21:44:39 +00:00
|
|
|
}
|