Add an Overlay type with helper methods
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
committed by
Christian Goll
parent
7d4b7ab432
commit
c03dc9436b
@@ -3,7 +3,6 @@ package chmod
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -12,8 +11,6 @@ import (
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
var overlaySourceDir string
|
||||
|
||||
overlayName := args[0]
|
||||
fileName := args[1]
|
||||
|
||||
@@ -21,19 +18,19 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not convert requested mode: %s", err)
|
||||
}
|
||||
err = overlay.CloneSiteOverlay(overlayName)
|
||||
if err != nil {
|
||||
return err
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
if !overlay_.IsSiteOverlay() {
|
||||
overlay_, err = overlay_.CloneSiteOverlay()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
overlaySourceDir, _ = overlay.GetOverlay(overlayName)
|
||||
|
||||
if !util.IsDir(overlaySourceDir) {
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
overlayFile := path.Join(overlaySourceDir, fileName)
|
||||
|
||||
if !util.IsFile(overlayFile) && !util.IsDir(overlayFile) {
|
||||
overlayFile := overlay_.File(fileName)
|
||||
if !(util.IsFile(overlayFile) || util.IsDir(overlayFile)) {
|
||||
return fmt.Errorf("file does not exist within overlay: %s:%s", overlayName, fileName)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package chown
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/warewulf/warewulf/internal/pkg/overlay"
|
||||
@@ -13,7 +12,6 @@ import (
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
var overlaySourceDir string
|
||||
var uid int
|
||||
var gid int
|
||||
var err error
|
||||
@@ -34,19 +32,21 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
} else {
|
||||
gid = -1
|
||||
}
|
||||
err = overlay.CloneSiteOverlay(overlayName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
overlaySourceDir, _ = overlay.GetOverlay(overlayName)
|
||||
|
||||
if !util.IsDir(overlaySourceDir) {
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
overlayFile := path.Join(overlaySourceDir, fileName)
|
||||
if !overlay_.IsSiteOverlay() {
|
||||
overlay_, err = overlay_.CloneSiteOverlay()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !util.IsFile(overlayFile) && !util.IsDir(overlayFile) {
|
||||
overlayFile := overlay_.File(fileName)
|
||||
if !(util.IsFile(overlayFile) || util.IsDir(overlayFile)) {
|
||||
return fmt.Errorf("file does not exist within overlay: %s:%s", overlayName, fileName)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,5 @@ import (
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
_, err = overlay.CreateSiteOverlay(args[0])
|
||||
return err
|
||||
return overlay.GetSiteOverlay(args[0]).Create()
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warewulf/warewulf/internal/pkg/overlay"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
@@ -13,7 +12,6 @@ import (
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
var overlayPath string
|
||||
var fileName string
|
||||
|
||||
overlayName := args[0]
|
||||
@@ -22,30 +20,22 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
fileName = args[1]
|
||||
}
|
||||
|
||||
overlayPath, isSite := overlay.GetOverlay(overlayName)
|
||||
if !isSite {
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
if overlay_.IsDistributionOverlay() {
|
||||
return fmt.Errorf("distribution overlay can't deleted")
|
||||
|
||||
}
|
||||
if overlayPath == "" {
|
||||
return fmt.Errorf("overlay name did not resolve: '%s'", overlayName)
|
||||
}
|
||||
|
||||
if !util.IsDir(overlayPath) {
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
if fileName == "" {
|
||||
if overlayName == "wwinit" || overlayName == "host" {
|
||||
return errors.New("refusing to delete the Warewulf overlay")
|
||||
}
|
||||
if Force {
|
||||
err := os.RemoveAll(overlayPath)
|
||||
err := os.RemoveAll(overlay_.Path())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed deleting overlay: %w", err)
|
||||
}
|
||||
} else {
|
||||
err := os.Remove(overlayPath)
|
||||
err := os.Remove(overlay_.Path())
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed deleting overlay: %w", err)
|
||||
}
|
||||
@@ -53,28 +43,28 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
wwlog.Info("Deleted overlay: %s\n", args[0])
|
||||
|
||||
} else {
|
||||
removePath := path.Join(overlayPath, fileName)
|
||||
removePath := overlay_.File(fileName)
|
||||
|
||||
if !util.IsDir(removePath) && !util.IsFile(removePath) {
|
||||
if !(util.IsDir(removePath) || util.IsFile(removePath)) {
|
||||
return fmt.Errorf("path to remove doesn't exist in overlay: %s", removePath)
|
||||
}
|
||||
|
||||
if Force {
|
||||
err := os.RemoveAll(removePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed deleting file from overlay: %s:%s", overlayName, overlayPath)
|
||||
return fmt.Errorf("failed deleting file from overlay: %s:%s", overlayName, removePath)
|
||||
}
|
||||
} else {
|
||||
err := os.Remove(removePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed deleting overlay: %s:%s", overlayName, overlayPath)
|
||||
return fmt.Errorf("failed deleting overlay: %s:%s", overlayName, removePath)
|
||||
}
|
||||
}
|
||||
|
||||
if Parents {
|
||||
// Cleanup any empty directories left behind...
|
||||
i := path.Dir(removePath)
|
||||
for i != overlayPath {
|
||||
for i != overlay_.Rootfs() {
|
||||
wwlog.Debug("Evaluating directory to remove: %s", i)
|
||||
err := os.Remove(i)
|
||||
if err != nil {
|
||||
|
||||
@@ -36,19 +36,19 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
overlayName := args[0]
|
||||
fileName := args[1]
|
||||
createdSite := false
|
||||
overlaySourceDir, isSite := overlay.GetOverlay(overlayName)
|
||||
if !isSite {
|
||||
err = overlay.CloneSiteOverlay(overlayName)
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
if !overlay_.IsSiteOverlay() {
|
||||
overlay_, err = overlay_.CloneSiteOverlay()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
createdSite = true
|
||||
}
|
||||
if !util.IsDir(overlaySourceDir) {
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
overlayFile := path.Join(overlaySourceDir, fileName)
|
||||
overlayFile := overlay_.File(fileName)
|
||||
wwlog.Debug("Will edit overlay file: %s", overlayFile)
|
||||
|
||||
overlayFileDir := path.Dir(overlayFile)
|
||||
@@ -107,7 +107,7 @@ func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
if startTime == fileInfo.ModTime() {
|
||||
wwlog.Debug("No change detected. Not updating overlay.")
|
||||
if createdSite {
|
||||
os.Remove(overlaySourceDir)
|
||||
os.Remove(overlay_.Path())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
var dest string
|
||||
var overlaySource string
|
||||
|
||||
overlayName := args[0]
|
||||
source := args[1]
|
||||
@@ -27,22 +26,22 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
wwlog.Verbose("Copying '%s' into overlay '%s:%s'", source, overlayName, dest)
|
||||
overlaySource, _ = overlay.GetOverlay(overlayName)
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
|
||||
if !util.IsDir(overlaySource) {
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
if util.IsDir(path.Join(overlaySource, dest)) {
|
||||
if util.IsDir(overlay_.File(dest)) {
|
||||
dest = path.Join(dest, path.Base(source))
|
||||
}
|
||||
|
||||
if util.IsFile(path.Join(overlaySource, dest)) {
|
||||
if util.IsFile(overlay_.File(dest)) {
|
||||
return fmt.Errorf("a file with that name already exists in the overlay: %s", overlayName)
|
||||
}
|
||||
|
||||
if CreateDirs {
|
||||
parent := filepath.Dir(path.Join(overlaySource, dest))
|
||||
parent := filepath.Dir(overlay_.File(dest))
|
||||
if _, err := os.Stat(parent); os.IsNotExist(err) {
|
||||
wwlog.Debug("Create dir: %s", parent)
|
||||
srcInfo, err := os.Stat(source)
|
||||
@@ -56,7 +55,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
err := util.CopyFile(source, path.Join(overlaySource, dest))
|
||||
err := util.CopyFile(source, overlay_.File(dest))
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not copy file into overlay: %w", err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"syscall"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warewulf/warewulf/internal/app/wwctl/table"
|
||||
"github.com/warewulf/warewulf/internal/pkg/overlay"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
@@ -24,51 +25,52 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
}
|
||||
|
||||
t := table.New(cmd.OutOrStdout())
|
||||
if ListLong {
|
||||
wwlog.Info("%-10s %5s %-5s %-18s %s\n", "PERM MODE", "UID", "GID", "SYSTEM-OVERLAY", "FILE PATH", "SITE")
|
||||
t.AddHeader("PERM MODE", "UID", "GID", "SYSTEM-OVERLAY", "FILE PATH", "SITE")
|
||||
} else {
|
||||
wwlog.Info("%-30s %-12s-%12s\n", "OVERLAY NAME", "FILES/DIRS", "SITE")
|
||||
t.AddHeader("OVERLAY NAME", "FILES/DIRS", "SITE")
|
||||
}
|
||||
|
||||
for o := range overlays {
|
||||
name := overlays[o]
|
||||
path, isSite := overlay.GetOverlay(name)
|
||||
for _, name := range overlays {
|
||||
overlay_ := overlay.GetOverlay(name)
|
||||
|
||||
if util.IsDir(path) {
|
||||
files := util.FindFiles(path)
|
||||
if !overlay_.Exists() {
|
||||
wwlog.Error("system/%s (path not found:%s)", name, overlay_.Rootfs())
|
||||
continue
|
||||
}
|
||||
|
||||
wwlog.Debug("Iterating overlay path: %s", path)
|
||||
if ListLong {
|
||||
for file := range files {
|
||||
s, err := os.Stat(files[file])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
files := util.FindFiles(overlay_.Rootfs())
|
||||
|
||||
fileMode := s.Mode()
|
||||
perms := fileMode & os.ModePerm
|
||||
|
||||
sys := s.Sys()
|
||||
|
||||
wwlog.Info("%v %5d %-5d %-18s /%s\n", perms, sys.(*syscall.Stat_t).Uid, sys.(*syscall.Stat_t).Gid, overlays[o], files[file], isSite)
|
||||
wwlog.Debug("Iterating overlay rootfs: %s", overlay_.Rootfs())
|
||||
if ListLong {
|
||||
for file := range files {
|
||||
s, err := os.Stat(files[file])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
} else if ListContents {
|
||||
var fileCount int
|
||||
for file := range files {
|
||||
wwlog.Info("%-30s /%-12s\n", name, files[file])
|
||||
fileCount++
|
||||
}
|
||||
if fileCount == 0 {
|
||||
wwlog.Info("%-30s %-12d\n", name, 0)
|
||||
}
|
||||
} else {
|
||||
wwlog.Info("%-30s %-12d\n", name, len(files), isSite)
|
||||
|
||||
fileMode := s.Mode()
|
||||
perms := fileMode & os.ModePerm
|
||||
|
||||
sys := s.Sys()
|
||||
|
||||
t.AddLine(perms, sys.(*syscall.Stat_t).Uid, sys.(*syscall.Stat_t).Gid, name, files[file], overlay_.IsSiteOverlay())
|
||||
}
|
||||
} else if ListContents {
|
||||
var fileCount int
|
||||
for file := range files {
|
||||
t.AddLine(name, files[file], overlay_.IsSiteOverlay())
|
||||
fileCount++
|
||||
}
|
||||
if fileCount == 0 {
|
||||
t.AddLine(name, 0, overlay_.IsSiteOverlay())
|
||||
}
|
||||
|
||||
} else {
|
||||
wwlog.Error("system/%s (path not found:%s)", overlays[o], path)
|
||||
t.AddLine(name, len(files), overlay_.IsSiteOverlay())
|
||||
}
|
||||
}
|
||||
t.Print()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,36 +3,33 @@ package mkdir
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/warewulf/warewulf/internal/pkg/overlay"
|
||||
"github.com/warewulf/warewulf/internal/pkg/util"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
|
||||
var overlaySourceDir string
|
||||
|
||||
overlayName := args[0]
|
||||
dirName := args[1]
|
||||
err = overlay.CloneSiteOverlay(overlayName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
overlaySourceDir, _ = overlay.GetOverlay(overlayName)
|
||||
|
||||
if !util.IsDir(overlaySourceDir) {
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
if !overlay_.IsSiteOverlay() {
|
||||
overlay_, err = overlay_.CloneSiteOverlay()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
overlayDir := path.Join(overlaySourceDir, dirName)
|
||||
|
||||
overlayDir := overlay_.File(dirName)
|
||||
wwlog.Debug("Will create directory in overlay: %s:%s", overlayName, dirName)
|
||||
|
||||
err = os.MkdirAll(overlayDir, os.FileMode(PermMode))
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not create directory: %s", path.Dir(overlayDir))
|
||||
return fmt.Errorf("could not create directory: %s", overlayDir)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -18,18 +17,15 @@ import (
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
var overlaySourceDir string
|
||||
|
||||
overlayName := args[0]
|
||||
fileName := args[1]
|
||||
overlaySourceDir, _ = overlay.GetOverlay(overlayName)
|
||||
|
||||
if !util.IsDir(overlaySourceDir) {
|
||||
return fmt.Errorf("overlay dir: %s does not exist", overlaySourceDir)
|
||||
overlay_ := overlay.GetOverlay(overlayName)
|
||||
if !overlay_.Exists() {
|
||||
return fmt.Errorf("overlay does not exist: %s", overlayName)
|
||||
}
|
||||
|
||||
overlayFile := path.Join(overlaySourceDir, fileName)
|
||||
|
||||
overlayFile := overlay_.File(fileName)
|
||||
if !util.IsFile(overlayFile) {
|
||||
return fmt.Errorf("file: %s does not exist within overlay", overlayFile)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user