Merge pull request #562 from mslacken/nodeFileSave
check if new template file was modified
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package edit
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@@ -47,34 +50,69 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
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)
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "# This is a Warewulf Template file.\n")
|
||||
fmt.Fprintf(w, "#\n")
|
||||
fmt.Fprintf(w, "# This file (suffix '.ww') will be automatically rewritten without the suffix\n")
|
||||
fmt.Fprintf(w, "# when the overlay is rendered for the individual nodes. Here are some examples\n")
|
||||
fmt.Fprintf(w, "# of macros and logic which can be used within this file:\n")
|
||||
fmt.Fprintf(w, "#\n")
|
||||
fmt.Fprintf(w, "# Node FQDN = {{.Id}}\n")
|
||||
fmt.Fprintf(w, "# Node Cluster = {{.ClusterName}}\n")
|
||||
fmt.Fprintf(w, "# Network Config = {{.NetDevs.eth0.Ipaddr}}, {{.NetDevs.eth0.Hwaddr}}, etc.\n")
|
||||
fmt.Fprintf(w, "#\n")
|
||||
fmt.Fprintf(w, "# Goto the documentation pages for more information: http://www.hpcng.org/...\n")
|
||||
fmt.Fprintf(w, "\n")
|
||||
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)
|
||||
w.Close()
|
||||
}
|
||||
|
||||
err := util.ExecInteractive(editor, overlayFile)
|
||||
fileDesc1, err := os.OpenFile(overlayFile, os.O_RDWR, os.FileMode(PermMode))
|
||||
if err != nil {
|
||||
wwlog.Warn("Could not open file for editing: %s", err)
|
||||
}
|
||||
defer fileDesc1.Close()
|
||||
_, _ = fileDesc1.Seek(0, 0)
|
||||
hasher := sha256.New()
|
||||
if _, err := io.Copy(hasher, fileDesc1); err != nil {
|
||||
wwlog.Error("Problems getting checksum of file %s\n", err)
|
||||
}
|
||||
sum1 := hex.EncodeToString(hasher.Sum(nil))
|
||||
fileDesc1.Close()
|
||||
err = util.ExecInteractive(editor, overlayFile)
|
||||
if err != nil {
|
||||
wwlog.Error("Editor process existed with non-zero")
|
||||
os.Exit(1)
|
||||
}
|
||||
fileDesc2, err := os.OpenFile(overlayFile, os.O_RDWR, os.FileMode(PermMode))
|
||||
if err != nil {
|
||||
wwlog.Warn("Could not open file for editing: %s", err)
|
||||
}
|
||||
defer fileDesc2.Close()
|
||||
_, _ = fileDesc2.Seek(0, 0)
|
||||
hasher.Reset()
|
||||
if _, err := io.Copy(hasher, fileDesc2); err != nil {
|
||||
wwlog.Error("Problems getting checksum of file %s\n", err)
|
||||
}
|
||||
sum2 := hex.EncodeToString(hasher.Sum(nil))
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user