171 lines
4.9 KiB
Go
171 lines
4.9 KiB
Go
// Package main implements the IntegraCI vm-deploy Dagger module.
|
|
//
|
|
// Wraps Pyinfra to deploy services to VMs over SSH and a cloud-init helper
|
|
// to bootstrap fresh hosts. Designed to be invoked by IntegraCI's
|
|
// VMDeploymentWorkflow once a VMHost row reaches `active` status.
|
|
//
|
|
// Usage:
|
|
//
|
|
// dagger call deploy --inventory ./inventory.py --deploy ./site.py --ssh-key $KEY
|
|
// dagger call cloud-init-render --address 10.0.0.5 --ssh-pubkey $PUB --packages "nginx,podman"
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"dagger/vm-deploy/internal/dagger"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// VmDeploy is the IntegraCI VM provisioning module.
|
|
type VmDeploy struct{}
|
|
|
|
// PyinfraResult summarises a Pyinfra apply.
|
|
type PyinfraResult struct {
|
|
Hosts []string `json:"hosts"`
|
|
OperationsOK int `json:"operations_ok"`
|
|
OperationsKO int `json:"operations_ko"`
|
|
Output string `json:"output"`
|
|
Success bool `json:"success"`
|
|
}
|
|
|
|
// CloudInitResult holds a generated cloud-init user-data document.
|
|
type CloudInitResult struct {
|
|
UserData string `json:"user_data"`
|
|
Format string `json:"format"`
|
|
}
|
|
|
|
// pyinfraImage returns the base container with pyinfra preinstalled.
|
|
func (m *VmDeploy) pyinfraImage() *dagger.Container {
|
|
return dag.Container().
|
|
From("python:3.12-slim").
|
|
WithExec([]string{"sh", "-c",
|
|
"apt-get update && apt-get install -y --no-install-recommends openssh-client git ca-certificates && rm -rf /var/lib/apt/lists/*",
|
|
}).
|
|
WithExec([]string{"pip", "install", "--no-cache-dir", "pyinfra==3.5"})
|
|
}
|
|
|
|
// Deploy applies a Pyinfra deploy module against the given inventory.
|
|
//
|
|
// `inventory` may be a Python file or a comma-separated host list (handled by
|
|
// passing it as the @hosts CLI form).
|
|
func (m *VmDeploy) Deploy(
|
|
ctx context.Context,
|
|
// Pyinfra inventory file (Python module). Either this or addresses required.
|
|
// +optional
|
|
inventory *dagger.File,
|
|
// Comma-separated list of host addresses (alternative to inventory).
|
|
// +optional
|
|
addresses string,
|
|
// Pyinfra deploy script (Python).
|
|
deploy *dagger.File,
|
|
// Private SSH key as Dagger secret.
|
|
sshKey *dagger.Secret,
|
|
// SSH user.
|
|
// +optional
|
|
// +default="ubuntu"
|
|
sshUser string,
|
|
// Extra pyinfra args (e.g. --limit ip=…).
|
|
// +optional
|
|
extraArgs string,
|
|
) (*PyinfraResult, error) {
|
|
if inventory == nil && addresses == "" {
|
|
return nil, fmt.Errorf("either inventory or addresses must be provided")
|
|
}
|
|
|
|
c := m.pyinfraImage().
|
|
WithMountedFile("/work/deploy.py", deploy).
|
|
WithMountedSecret("/work/id_rsa", sshKey).
|
|
WithExec([]string{"sh", "-c", "chmod 600 /work/id_rsa"})
|
|
|
|
target := "@hosts/" + addresses
|
|
if inventory != nil {
|
|
c = c.WithMountedFile("/work/inventory.py", inventory)
|
|
target = "/work/inventory.py"
|
|
}
|
|
|
|
cmd := strings.Join([]string{
|
|
"cd /work && pyinfra",
|
|
"--user", sshUser,
|
|
"--key", "/work/id_rsa",
|
|
extraArgs,
|
|
target,
|
|
"/work/deploy.py",
|
|
"|| true",
|
|
}, " ")
|
|
c = c.WithExec([]string{"sh", "-c", cmd})
|
|
out, _ := c.Stdout(ctx)
|
|
|
|
res := &PyinfraResult{Output: out}
|
|
res.Hosts = []string{addresses}
|
|
if strings.Contains(out, "completed: ") {
|
|
res.Success = !strings.Contains(strings.ToLower(out), "error: ")
|
|
}
|
|
if strings.Contains(strings.ToLower(out), "error") {
|
|
res.OperationsKO = 1
|
|
} else {
|
|
res.OperationsOK = 1
|
|
res.Success = true
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// CloudInitRender produces a minimal cloud-init `#cloud-config` user-data
|
|
// payload that you can pass to your cloud provider when creating a fresh VM.
|
|
//
|
|
// Real provisioning still happens through your IaaS (Crossplane, Terraform,
|
|
// the cloud console). This module just builds the bootstrap script.
|
|
func (m *VmDeploy) CloudInitRender(
|
|
_ context.Context,
|
|
// Public SSH key authorised on the host
|
|
sshPubkey string,
|
|
// Comma-separated list of packages to install on first boot
|
|
// +optional
|
|
// +default="curl,git,python3,python3-pip,podman"
|
|
packages string,
|
|
// Hostname to set on the new VM
|
|
// +optional
|
|
hostname string,
|
|
// Extra raw runcmd lines (semicolon-separated)
|
|
// +optional
|
|
runcmd string,
|
|
) (*CloudInitResult, error) {
|
|
pkgs := []string{}
|
|
for _, p := range strings.Split(packages, ",") {
|
|
if p = strings.TrimSpace(p); p != "" {
|
|
pkgs = append(pkgs, " - "+p)
|
|
}
|
|
}
|
|
cmds := []string{}
|
|
for _, c := range strings.Split(runcmd, ";") {
|
|
if c = strings.TrimSpace(c); c != "" {
|
|
cmds = append(cmds, " - "+c)
|
|
}
|
|
}
|
|
body := []string{"#cloud-config"}
|
|
if hostname != "" {
|
|
body = append(body, "hostname: "+hostname)
|
|
}
|
|
body = append(body,
|
|
"package_update: true",
|
|
"package_upgrade: false",
|
|
"packages:",
|
|
)
|
|
body = append(body, pkgs...)
|
|
body = append(body,
|
|
"users:",
|
|
" - default",
|
|
" - name: integraci",
|
|
" sudo: ALL=(ALL) NOPASSWD:ALL",
|
|
" groups: sudo",
|
|
" shell: /bin/bash",
|
|
" ssh_authorized_keys:",
|
|
" - "+sshPubkey,
|
|
)
|
|
if len(cmds) > 0 {
|
|
body = append(body, "runcmd:")
|
|
body = append(body, cmds...)
|
|
}
|
|
return &CloudInitResult{UserData: strings.Join(body, "\n") + "\n", Format: "cloud-config"}, nil
|
|
}
|