applyconfig: use dkl/pkg

This commit is contained in:
Mikaël Cluseau
2024-01-20 14:07:11 +01:00
parent 1a84bb4286
commit bad1cf3c23
4 changed files with 74 additions and 105 deletions

View File

@ -1,94 +0,0 @@
package apply
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"novit.nc/direktil/inits/pkg/vars"
"novit.nc/direktil/pkg/config"
)
const (
authorizedKeysPath = "/root/.ssh/authorized_keys"
)
// Files writes the files from the given config
func Files(cfg *config.Config, filters ...string) (err error) {
accept := func(n string) bool { return true }
if len(filters) > 0 {
accept = func(n string) bool {
for _, filter := range filters {
if matched, err := filepath.Match(filter, n); err != nil {
log.Printf("ERROR: bad filter ignored: %q: %v", filter, err)
} else if matched {
return true
}
}
return false
}
}
if cfg.RootUser.AuthorizedKeys != nil && accept(authorizedKeysPath) {
err = writeFile(
authorizedKeysPath,
[]byte(strings.Join(cfg.RootUser.AuthorizedKeys, "\n")),
0600, 0700, cfg,
)
if err != nil {
return
}
}
for _, file := range cfg.Files {
if !accept(file.Path) {
continue
}
mode := file.Mode
if mode == 0 {
mode = 0644
}
content := []byte(file.Content)
err = writeFile(
file.Path,
content,
mode,
0755,
cfg,
)
if err != nil {
log.Print("failed to write file ", file.Path, ": ", err)
continue
}
}
return
}
func writeFile(path string, content []byte, fileMode, dirMode os.FileMode, cfg *config.Config) (err error) {
if err = os.MkdirAll(filepath.Dir(path), dirMode); err != nil {
return
}
content = vars.Substitute(content, cfg)
log.Printf("writing %q, mode %04o, %d bytes", path, fileMode, len(content))
if err = ioutil.WriteFile(path, content, fileMode); err != nil {
err = fmt.Errorf("failed to write %s: %v", path, err)
}
if chmodErr := os.Chmod(path, fileMode); chmodErr != nil {
log.Print("- failed chmod: ", chmodErr)
}
return
}

View File

@ -3,16 +3,17 @@ package applyconfig
import (
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"novit.nc/direktil/inits/pkg/apply"
"novit.nc/direktil/pkg/config"
dlog "novit.nc/direktil/pkg/log"
"novit.tech/direktil/pkg/config"
"novit.tech/direktil/pkg/config/apply"
)
var (
filters []string
log = dlog.Get("dkl")
filters []string
pathPrefix string
)
func Command() (c *cobra.Command) {
@ -26,11 +27,14 @@ func Command() (c *cobra.Command) {
flag := c.Flags()
flag.StringArrayVarP(&filters, "filter", "F", []string{}, "glob filter to select files to apply")
flag.StringVarP(&pathPrefix, "prefix", "P", "", "path prefix")
return c
}
func run(_ *cobra.Command, args []string) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
configPath := args[0]
var (
@ -39,20 +43,19 @@ func run(_ *cobra.Command, args []string) {
)
if configPath == "-" {
log.Print("loading config from stdin")
log.Info().Str("from", "stdin").Msg("loading config")
cfg, err = config.Read(os.Stdin)
} else {
log.Print("loading config from ", configPath)
log.Info().Str("from", configPath).Msg("loading config")
cfg, err = config.Load(configPath)
}
if err != nil {
log.Print("failed to load config: ", err)
log.Fatal().Err(err).Msg("failed to load config")
}
if err = apply.Files(cfg /*log,*/, filters...); err != nil {
log.Taint(dlog.Fatal, "failed to apply files: ", err)
os.Exit(1)
if err = apply.Files(cfg, pathPrefix, filters...); err != nil {
log.Fatal().Err(err).Msg("failed to apply files")
}
}