Major refactor for Cobra and new pkg/app structure for wwctl

This commit is contained in:
Gregory Kurtzer
2020-11-10 21:28:39 -08:00
parent 0c369270ef
commit 189618a111
24 changed files with 658 additions and 130 deletions

View File

@@ -59,7 +59,7 @@ func runtimeOverlaySend(w http.ResponseWriter, req *http.Request) {
}
} else {
w.WriteHeader(503)
log.Printf("ERROR: No 'runtime overlay' set for node %s\n", node.Fqdn)
log.Printf("ERROR: No 'runtime system-overlay' set for node %s\n", node.Fqdn)
}
return

View File

@@ -27,7 +27,7 @@ func systemOverlaySend(w http.ResponseWriter, req *http.Request) {
}
} else {
w.WriteHeader(503)
log.Printf("ERROR: No 'system overlay' set for node %s\n", node.Fqdn)
log.Printf("ERROR: No 'system system-overlay' set for node %s\n", node.Fqdn)
}
return

View File

@@ -66,8 +66,8 @@ func main() {
http.HandleFunc("/kernel/", kernelSend)
http.HandleFunc("/kmods/", kmodsSend)
http.HandleFunc("/vnfs/", vnfsSend)
http.HandleFunc("/overlay-system/", systemOverlaySend)
http.HandleFunc("/overlay-runtime", runtimeOverlaySend)
http.HandleFunc("/system-overlay-system/", systemOverlaySend)
http.HandleFunc("/system-overlay-runtime", runtimeOverlaySend)
http.ListenAndServe(":9873", nil)
}

View File

@@ -19,7 +19,9 @@ func overlayRuntime(node assets.NodeInfo, replace map[string]string, wg *sync.Wa
/*
destModTime := time.Time{}
destMod, err := os.Stat(OverlayFile)
if err == nil {
if err == n
il {
destModTime = destMod.ModTime()
}
configMod, err := os.Stat("/etc/warewulf/nodes.conf")
@@ -43,7 +45,7 @@ func overlayRuntime(node assets.NodeInfo, replace map[string]string, wg *sync.Wa
// if sourceModTime.After(destModTime) || configModTime.After(destModTime) {
fmt.Printf("RUNTIME: %s\n", node.Fqdn)
overlayDest := "/tmp/.overlay-" + util.RandomString(16)
overlayDest := "/tmp/.system-overlay-" + util.RandomString(16)
BuildOverlayDir(OverlayDir, overlayDest, replace)
cmd := fmt.Sprintf("cd %s && find . | cpio --quiet -o -H newc -F \"%s\"", overlayDest, OverlayFile)

View File

@@ -45,7 +45,7 @@ func overlaySystem(node assets.NodeInfo, replace map[string]string, wg *sync.Wai
if sourceModTime.After(destModTime) || configModTime.After(destModTime) {
fmt.Printf("SYSTEM: %s\n", node.Fqdn)
overlayDest := "/tmp/.overlay-" + util.RandomString(16)
overlayDest := "/tmp/.system-overlay-" + util.RandomString(16)
BuildOverlayDir(OverlayDir, overlayDest, replace)
cmd := fmt.Sprintf("cd %s && find . | cpio --quiet -o -H newc -F \"%s\"", overlayDest, OverlayFile)

View File

@@ -1,44 +0,0 @@
package main
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/vnfs"
"os"
"path"
"sync"
)
func vnfsLocalBuild(vnfsPath string, wg *sync.WaitGroup) {
defer wg.Done()
v := vnfs.New(vnfsPath)
if _, err := os.Stat(vnfsPath); err == nil {
// TODO: Build VNFS to temporary file and move to real location when complete atomically
// TODO: Check time stamps of sourcedir and build file to see if we need to rebuild or skip
//vnfsDestination := fmt.Sprintf("%s/provision/vnfs/%s.img.gz", LocalStateDir, path.Base(vnfsPath))
fmt.Printf("Building local Container: %s\n", vnfsPath)
err := os.MkdirAll(path.Dir(v.Image()), 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
err = buildVnfs(vnfsPath, v.Image())
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
err = buildLinks(v, vnfsPath)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
} else {
fmt.Printf("SKIPPING VNFS: (bad path) %s\n", vnfsPath)
}
}

View File

@@ -1,93 +0,0 @@
package main
import (
"context"
"fmt"
"github.com/hpcng/warewulf/internal/pkg/config"
"github.com/hpcng/warewulf/internal/pkg/oci"
"github.com/hpcng/warewulf/internal/pkg/vnfs"
"log"
"os"
"path"
"sync"
)
const (
OciCacheDir = config.LocalStateDir + "/oci"
VnfsHashDir = config.LocalStateDir + "/oci/vnfs/hash/"
)
func vnfsOciBuild(OciPath string, wg *sync.WaitGroup) {
v := vnfs.New(OciPath)
// vnfsDestination := fmt.Sprintf("%s/provision/vnfs/%s.img.gz", LocalStateDir, path.Base(OciPath))
defer wg.Done()
log.Printf("Building OCI Container: %s\n", OciPath)
c, err := oci.NewCache(oci.OptSetCachePath(OciCacheDir))
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
log.Printf("Downloading OCI container layers\n")
sourcePath, err := c.Pull(context.Background(), OciPath, nil)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
hashDestination := VnfsHashDir + path.Base(sourcePath)
name, err := os.Readlink(v.Image())
if err == nil {
if name == hashDestination {
log.Printf("Container already built, no update available\n")
return
}
}
err = os.MkdirAll(VnfsHashDir, 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
err = os.MkdirAll(path.Dir(v.Image()), 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
err = os.MkdirAll(path.Dir(v.Root()), 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
log.Printf("Building bootable VNFS image\n")
err = buildVnfs(sourcePath, hashDestination)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
log.Printf("Finalizing Build\n")
_ = os.Remove(v.Image() + "-link")
err = os.Symlink(hashDestination, v.Image()+"-link")
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
err = os.Rename(v.Image()+"-link", v.Image())
err = buildLinks(v, sourcePath)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
log.Printf("Completed building VNFS: %s\n", path.Base(OciPath))
return
}

View File

@@ -1,36 +0,0 @@
package main
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/vnfs"
"os"
"os/exec"
)
func buildVnfs(source string, dest string) error {
cmd := fmt.Sprintf("cd %s; find . | cpio --quiet -o -H newc | gzip -c > \"%s\"", source, dest)
err := exec.Command("/bin/sh", "-c", cmd).Run()
return err
}
func buildLinks(v vnfs.VnfsObject, source string) error {
// Just incase the temporary link location is present, remove it if we can
_ = os.Remove(v.Root() + "-link")
// Link to a temporary location so we can atomically move the link into place
err := os.Symlink(source, v.Root()+"-link")
if err != nil {
return err
}
// Atomically move the link into place
err = os.Rename(v.Root()+"-link", v.Root())
if err != nil {
return err
}
return nil
}

View File

@@ -1,5 +1,10 @@
package main
import "os"
import "os"
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/assets"
@@ -16,7 +21,7 @@ import (
func main() {
if len(os.Args) < 2 {
fmt.Printf("USAGE: %s [vnfs/kernel/overlay] (node regex)\n", os.Args[0])
fmt.Printf("USAGE: %s [vnfs/kernel/system-overlay] (node regex)\n", os.Args[0])
return
}
@@ -107,7 +112,7 @@ func main() {
}
}
}
} else if os.Args[1] == "overlay" {
} else if os.Args[1] == "system-overlay" {
var nodeList []assets.NodeInfo
var wg sync.WaitGroup

View File

@@ -19,7 +19,7 @@ func main() {
time.Sleep(5000 * time.Millisecond)
} else {
fmt.Printf("Called via: %s\n", os.Args[0])
fmt.Printf("Runtime overlay is being put in '/warewulf/wwclient-test' rather than '/'\n")
fmt.Printf("Runtime system-overlay is being put in '/warewulf/wwclient-test' rather than '/'\n")
os.MkdirAll("/warewulf/wwclient-test", 0755)
os.Chdir("/warewulf/wwclient-test")
}
@@ -61,7 +61,7 @@ func main() {
for true {
var err error
getString := fmt.Sprintf("http://%s:%d/overlay-runtime", config.Ipaddr, config.Port)
getString := fmt.Sprintf("http://%s:%d/system-overlay-runtime", config.Ipaddr, config.Port)
resp, err = webclient.Get(getString)
if err == nil {
break
@@ -84,7 +84,7 @@ func main() {
if resp.StatusCode != 200 {
log.Printf("Not updating runtime overlay, got status code: %d\n", resp.StatusCode)
log.Printf("Not updating runtime system-overlay, got status code: %d\n", resp.StatusCode)
time.Sleep(60000 * time.Millisecond)
continue
}

View File

@@ -6,7 +6,6 @@ import (
func main() {
root := wwctl.GetRootCommand()
root.Execute()