Initial commit

This commit is contained in:
Mikaël Cluseau
2018-07-06 19:07:37 +11:00
commit 2de2a4d0f6
25 changed files with 2050 additions and 0 deletions

67
pkg/apply/files.go Normal file
View File

@ -0,0 +1,67 @@
package apply
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"novit.nc/direktil/inits/pkg/vars"
"novit.nc/direktil/pkg/config"
dlog "novit.nc/direktil/pkg/log"
)
// Files writes the files from the given config
func Files(cfg *config.Config, log *dlog.Log) (err error) {
err = writeFile(
"/root/.ssh/authorized_keys",
[]byte(strings.Join(cfg.RootUser.AuthorizedKeys, "\n")),
0600, 0700, cfg, log,
)
if err != nil {
return
}
for _, file := range cfg.Files {
mode := file.Mode
if mode == 0 {
mode = 0644
}
content := []byte(file.Content)
err = writeFile(
file.Path,
content,
mode,
0755,
cfg,
log,
)
if err != nil {
return
}
}
return
}
func writeFile(path string, content []byte, fileMode, dirMode os.FileMode,
cfg *config.Config, log *dlog.Log) (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)
}
return
}

32
pkg/vars/boot.go Normal file
View File

@ -0,0 +1,32 @@
package vars
import (
"bytes"
"fmt"
"io/ioutil"
)
var (
bootVarPrefix = []byte("direktil.var.")
)
func BootArgs() [][]byte {
ba, err := ioutil.ReadFile("/proc/cmdline")
if err != nil {
// should not happen
panic(fmt.Errorf("failed to read /proc/cmdline: ", err))
}
return bytes.Split(ba, []byte{' '})
}
func BootArgValue(prefix, defaultValue string) string {
prefixB := []byte("direktil." + prefix + "=")
for _, ba := range BootArgs() {
if bytes.HasPrefix(ba, prefixB) {
return string(ba[len(prefixB):])
}
}
return defaultValue
}

60
pkg/vars/vars.go Normal file
View File

@ -0,0 +1,60 @@
package vars
import (
"bytes"
"novit.nc/direktil/pkg/config"
)
type Var struct {
Template []byte
Value []byte
}
func Vars(cfg *config.Config) []Var {
res := make([]Var, 0)
for _, arg := range BootArgs() {
if !bytes.HasPrefix(arg, bootVarPrefix) {
continue
}
parts := bytes.SplitN(arg[len(bootVarPrefix):], []byte{'='}, 2)
res = append(res, Var{
Template: append(append([]byte("$(var:"), parts[0]...), ')'),
Value: parts[1],
})
}
configVarsLoop:
for _, v := range cfg.Vars {
t := []byte("$(var:" + v.Name + ")")
for _, prev := range res {
if bytes.Equal(prev.Template, t) {
continue configVarsLoop
}
}
res = append(res, Var{t, []byte(v.Default)})
}
return res
}
// Substitute variables in src
func Substitute(src []byte, cfg *config.Config) (dst []byte) {
dst = src
for _, bv := range Vars(cfg) {
if !bytes.Contains(dst, bv.Template) {
continue
}
v := bytes.TrimSpace(bv.Value)
dst = bytes.Replace(dst, bv.Template, v, -1)
}
return
}