120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"sort"
|
|
"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 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")
|
|
}
|
|
return deploy.New(cfg, jobs, g), 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().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",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return locked(func(d *deploy.Deployer) error { return d.Deploy(cmd.Context(), args, modules) })
|
|
},
|
|
}
|
|
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 {
|
|
d, err := load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, s := range d.Sources() {
|
|
live, err := d.Live(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
envs := make([]string, 0, len(live))
|
|
for e := range live {
|
|
envs = append(envs, e)
|
|
}
|
|
sort.Strings(envs)
|
|
for _, e := range envs {
|
|
sha, _, _ := strings.Cut(filepath.Base(live[e]), "-")
|
|
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\t%s\t%s\n", s.Name, e, sha)
|
|
}
|
|
}
|
|
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
|
|
}
|