113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package edit
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/hpcng/warewulf/internal/pkg/overlay"
|
|
"github.com/hpcng/warewulf/internal/pkg/util"
|
|
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func CobraRunE(cmd *cobra.Command, args []string) error {
|
|
editor := os.Getenv("EDITOR")
|
|
var overlaySourceDir string
|
|
|
|
overlayName := args[0]
|
|
fileName := args[1]
|
|
|
|
if editor == "" {
|
|
editor = "/bin/vi"
|
|
}
|
|
|
|
overlaySourceDir = overlay.OverlaySourceDir(overlayName)
|
|
|
|
if !util.IsDir(overlaySourceDir) {
|
|
wwlog.Error("Overlay does not exist: %s", overlayName)
|
|
os.Exit(1)
|
|
}
|
|
|
|
overlayFile := path.Join(overlaySourceDir, fileName)
|
|
|
|
wwlog.Debug("Will edit overlay file: %s", overlayFile)
|
|
|
|
if CreateDirs {
|
|
err := os.MkdirAll(path.Dir(overlayFile), 0755)
|
|
if err != nil {
|
|
wwlog.Error("Could not create directory: %s", path.Dir(overlayFile))
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
if !util.IsDir(path.Dir(overlayFile)) {
|
|
wwlog.Error("Can not create file, parent directory does not exist, try adding the")
|
|
wwlog.Error("'--parents' option to create the directory.")
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
newFile := false
|
|
if !util.IsFile(overlayFile) && filepath.Ext(overlayFile) == ".ww" {
|
|
wwlog.Verbose("This is a new file, creating some default content")
|
|
newFile = true
|
|
w, err := os.OpenFile(overlayFile, os.O_RDWR|os.O_CREATE, os.FileMode(PermMode))
|
|
if err != nil {
|
|
wwlog.Warn("Could not create file for writing: %s", err)
|
|
}
|
|
fmtStr := `# This is a Warewulf Template file.
|
|
#
|
|
# This file (suffix '.ww') will be automatically rewritten without the suffix
|
|
# when the overlay is rendered for the individual nodes. Here are some examples
|
|
# of macros and logic which can be used within this file:
|
|
#
|
|
# Node FQDN = {{.Id}}\n")
|
|
# Node Cluster = {{.ClusterName}}\n")
|
|
# Network Config = {{.NetDevs.eth0.Ipaddr}}, {{.NetDevs.eth0.Hwaddr}}, etc.
|
|
#
|
|
# Goto the documentation pages for more information: http://www.hpcng.org/...
|
|
# Keep the following for better reference
|
|
# This file is autogenerated by warewulf
|
|
# Host: {{.BuildHost}}
|
|
# Time: {{.BuildTime}}
|
|
# Source: {{.BuildSource}}
|
|
`
|
|
fmt.Fprint(w, fmtStr)
|
|
}
|
|
fileDesc, err := os.OpenFile(overlayFile, os.O_RDWR, os.FileMode(PermMode))
|
|
if err != nil {
|
|
wwlog.Warn("Could not open file for editing: %s", err)
|
|
}
|
|
defer fileDesc.Close()
|
|
_, _ = fileDesc.Seek(0, 0)
|
|
hasher := sha256.New()
|
|
if _, err := io.Copy(hasher, fileDesc); err != nil {
|
|
wwlog.Error("Problems getting checksum of file %s\n", err)
|
|
}
|
|
sum1 := hex.EncodeToString(hasher.Sum(nil))
|
|
err = util.ExecInteractive(editor, overlayFile)
|
|
if err != nil {
|
|
wwlog.Error("Editor process existed with non-zero")
|
|
os.Exit(1)
|
|
}
|
|
_, _ = fileDesc.Seek(0, 0)
|
|
hasher.Reset()
|
|
if _, err := io.Copy(hasher, fileDesc); err != nil {
|
|
wwlog.Error("Problems getting checksum of file %s\n", err)
|
|
}
|
|
sum2 := hex.EncodeToString(hasher.Sum(nil))
|
|
fmt.Println("sum1,sum2", sum1, sum2)
|
|
if sum1 == sum2 && newFile {
|
|
wwlog.Verbose("New template %s wasn't modified, deleting it", overlayFile)
|
|
err = os.Remove(overlayFile)
|
|
if err != nil {
|
|
wwlog.Warn("Could not delete %s", overlayFile)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|