This repository has been archived on 2026-11-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
w10k/main.go
T

144 lines
3.7 KiB
Go

package main
import (
"context"
"fmt"
"log/slog"
"maps"
"os"
"os/signal"
"path/filepath"
"slices"
"strings"
"syscall"
"time"
"git.unkin.net/unkin/w10k/internal/deploy"
"github.com/spf13/cobra"
)
var version = "dev"
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if err := rootCmd().ExecuteContext(ctx); err != nil {
os.Exit(1)
}
}
func rootCmd() *cobra.Command {
var cfgPath string
var jobs int
var grace time.Duration
var timings bool
var root *cobra.Command
load := func() (*deploy.Deployer, error) {
cfg, err := deploy.LoadConfig(cfgPath)
if err != nil {
return nil, err
}
g := grace
if !root.PersistentFlags().Changed("gc-grace") && cfg.W10k.GCGrace != nil {
g = *cfg.W10k.GCGrace
}
if g < 0 {
return nil, fmt.Errorf("--gc-grace must not be negative")
}
d := deploy.New(cfg, jobs, g)
if timings {
d.Timings = slog.Default()
}
return d, nil
}
live := func() ([][3]string, error) {
d, err := load()
if err != nil {
return nil, err
}
var rows [][3]string
for _, s := range d.Sources() {
live, err := d.Live(s)
if err != nil {
return nil, err
}
for _, e := range slices.Sorted(maps.Keys(live)) {
sha, _, _ := strings.Cut(filepath.Base(live[e]), "-")
rows = append(rows, [3]string{s.Name, e, sha})
}
}
return rows, nil
}
locked := func(fn func(*deploy.Deployer) error) error {
d, err := load()
if err != nil {
return err
}
unlock, err := d.Lock()
if err != nil {
return err
}
defer unlock()
return fn(d)
}
root = &cobra.Command{
Use: "w10k",
Short: "Deploy Puppet environments and Puppetfile modules as sha-pinned git worktrees",
Version: version,
SilenceUsage: true,
}
root.PersistentFlags().StringVarP(&cfgPath, "config", "c", "/etc/puppetlabs/r10k/r10k.yaml", "r10k.yaml path")
root.PersistentFlags().IntVarP(&jobs, "jobs", "j", 8, "maximum concurrent git/forge operations")
root.PersistentFlags().BoolVar(&timings, "timings", false, "log one timing line per deploy phase")
root.PersistentFlags().DurationVar(&grace, "gc-grace", 10*time.Minute, "keep unreferenced worktrees and modules this long after a run marks them (overrides w10k.gc_grace)")
var modules bool
deployCmd := &cobra.Command{
Use: "deploy [env...]",
Short: "Deploy all environments, or only the named ones (env or branch names)",
RunE: func(cmd *cobra.Command, args []string) error {
return locked(func(d *deploy.Deployer) error { return d.Deploy(cmd.Context(), args, modules) })
},
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
rows, _ := live()
var envs []string
for _, r := range rows {
if !slices.Contains(args, r[1]) && !slices.Contains(envs, r[1]) {
envs = append(envs, r[1])
}
}
return envs, cobra.ShellCompDirectiveNoFileComp
},
}
deployCmd.Flags().BoolVarP(&modules, "modules", "m", false, "re-resolve Puppetfile modules for unchanged environments")
listCmd := &cobra.Command{
Use: "list",
Short: "List deployed environments",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
rows, err := live()
if err != nil {
return err
}
for _, r := range rows {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\n", r[0], r[1], r[2])
}
return nil
},
}
gcCmd := &cobra.Command{
Use: "gc",
Short: "Remove worktrees and modules no longer referenced by any environment",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
return locked(func(d *deploy.Deployer) error { return d.GC(cmd.Context()) })
},
}
root.AddCommand(deployCmd, listCmd, gcCmd)
return root
}