k3s/pkg/deploy/stage.go

47 lines
1.0 KiB
Go
Raw Normal View History

//go:build !no_stage
2019-01-22 21:14:58 +00:00
package deploy
import (
"bytes"
2019-01-22 21:14:58 +00:00
"os"
"path/filepath"
"strings"
2019-03-25 04:54:52 +00:00
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
2019-01-22 21:14:58 +00:00
)
2020-01-29 23:40:49 +00:00
func Stage(dataDir string, templateVars map[string]string, skips map[string]bool) error {
staging:
2019-01-22 21:14:58 +00:00
for _, name := range AssetNames() {
nameNoExtension := strings.TrimSuffix(name, filepath.Ext(name))
if skips[name] || skips[nameNoExtension] {
continue staging
}
namePath := strings.Split(name, string(os.PathSeparator))
for i := 1; i < len(namePath); i++ {
subPath := filepath.Join(namePath[0:i]...)
if skips[subPath] {
continue staging
}
}
2019-01-22 21:14:58 +00:00
content, err := Asset(name)
if err != nil {
return err
}
for k, v := range templateVars {
content = bytes.Replace(content, []byte(k), []byte(v), -1)
}
2019-01-22 21:14:58 +00:00
p := filepath.Join(dataDir, name)
os.MkdirAll(filepath.Dir(p), 0700)
2019-01-22 21:14:58 +00:00
logrus.Info("Writing manifest: ", p)
if err := os.WriteFile(p, content, 0600); err != nil {
2019-01-22 21:14:58 +00:00
return errors.Wrapf(err, "failed to write to %s", name)
}
}
return nil
}