Last bit of updates for basic overlay support

This commit is contained in:
Gregory Kurtzer
2020-11-18 20:37:18 -08:00
parent 27441f77d3
commit 0bc53ea483
6 changed files with 145 additions and 3 deletions

View File

@@ -0,0 +1,78 @@
package chmod
import (
"github.com/hpcng/warewulf/internal/pkg/assets"
"github.com/hpcng/warewulf/internal/pkg/config"
"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"
"os"
"path"
"strconv"
)
func CobraRunE(cmd *cobra.Command, args []string) error {
config := config.New()
var overlaySourceDir string
overlayName := args[0]
fileName := args[2]
permissionMode, err := strconv.ParseInt(args[1], 8, 32)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not convert requested mode: %s\n", err)
os.Exit(1)
}
if SystemOverlay == true {
overlaySourceDir = config.SystemOverlaySource(overlayName)
} else {
overlaySourceDir = config.RuntimeOverlaySource(overlayName)
}
if util.IsDir(overlaySourceDir) == false {
wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s\n", overlayName)
os.Exit(1)
}
overlayFile := path.Join(overlaySourceDir, fileName)
if util.IsFile(overlayFile) == false {
wwlog.Printf(wwlog.ERROR, "File does not exist within overlay: %s:%s\n", overlayName, fileName)
os.Exit(1)
}
err = os.Chmod(overlayFile, os.FileMode(permissionMode))
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not set permission: %s\n", err)
os.Exit(1)
}
if NoOverlayUpdate == false {
nodes, err := assets.FindAllNodes()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Cloud not get nodeList: %s\n", err)
os.Exit(1)
}
var updateNodes []assets.NodeInfo
for _, node := range nodes {
if SystemOverlay == true && node.SystemOverlay == overlayName {
updateNodes = append(updateNodes, node)
} else if node.RuntimeOverlay == overlayName {
updateNodes = append(updateNodes, node)
}
}
if SystemOverlay == true {
wwlog.Printf(wwlog.INFO, "Updating System Overlays...\n")
return overlay.SystemBuild(updateNodes, true)
} else {
wwlog.Printf(wwlog.INFO, "Updating Runtime Overlays...\n")
return overlay.RuntimeBuild(updateNodes, true)
}
}
return nil
}

View File

@@ -0,0 +1,27 @@
package chmod
import "github.com/spf13/cobra"
var (
baseCmd = &cobra.Command{
Use: "chmod [overlay name] [numeric mode] [file path]",
Short: "Change permissions within an overlay",
Long: "Change permissions within an overlay",
RunE: CobraRunE,
Args: cobra.ExactArgs(3),
}
SystemOverlay bool
NoOverlayUpdate bool
)
func init() {
baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show System Overlays as well")
baseCmd.PersistentFlags().BoolVarP(&NoOverlayUpdate, "noupdate", "n", false, "Don't update overlays")
}
// GetRootCommand returns the root cobra.Command for the application.
func GetCommand() *cobra.Command {
return baseCmd
}

View File

@@ -54,7 +54,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
for i := 1; i < len(args); i++ {
removePath := path.Join(overlayPath, args[i])
if util.IsDir(removePath) == true || util.IsFile(removePath) == true {
if util.IsDir(removePath) == false && util.IsFile(removePath) == false {
wwlog.Printf(wwlog.ERROR, "Path to remove doesn't exist in overlay: %s\n", removePath)
os.Exit(1)
}

View File

@@ -18,7 +18,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
editor := config.Editor
var overlaySourceDir string
if SystemOverlay == true {
overlaySourceDir = config.SystemOverlaySource(args[0])
} else {
@@ -70,14 +69,29 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
fmt.Fprintf(w, "\n")
}
err := util.ExecInteractive(editor, overlayFile)
shasum1, err := util.ShaSumFile(overlayFile)
if err != nil {
wwlog.Printf(wwlog.WARN, "Could not open overlay file for checksum: %s\n", err)
}
err = util.ExecInteractive(editor, overlayFile)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Editor process existed with non-zero\n")
os.Exit(1)
}
wwlog.Printf(wwlog.INFO, "Updated: %s %s\n", args[0], args[1] )
shasum2, err := util.ShaSumFile(overlayFile)
if err != nil {
wwlog.Printf(wwlog.WARN, "Could not open overlay file for checksum: %s\n", err)
}
if shasum1 == shasum2 {
wwlog.Printf(wwlog.VERBOSE, "Not updating overlays, no file change\n")
NoOverlayUpdate = true
}
if NoOverlayUpdate == false {
nodes, err := assets.FindAllNodes()
if err != nil {

View File

@@ -2,6 +2,7 @@ package overlay
import (
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/build"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/chmod"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/create"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/delete"
"github.com/hpcng/warewulf/internal/app/wwctl/overlay/edit"
@@ -33,6 +34,7 @@ func init() {
baseCmd.AddCommand(mkdir.GetCommand())
baseCmd.AddCommand(build.GetCommand())
baseCmd.AddCommand(imprt.GetCommand())
baseCmd.AddCommand(chmod.GetCommand())
}

View File

@@ -1,6 +1,8 @@
package util
import (
"crypto/sha256"
"fmt"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"io"
"math/rand"
@@ -160,4 +162,23 @@ func ExecInteractive(command string, a...string) error {
c.Stderr = os.Stderr
err := c.Run()
return err
}
func ShaSumFile(file string) (string, error) {
var ret string
f, err := os.Open(file)
if err != nil {
return ret, nil
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return ret, err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}