Moving warewulfd code around and implementing new API for config.*()

This commit is contained in:
Gregory Kurtzer
2020-11-11 22:16:24 -08:00
parent 9a9672a060
commit fa2a7e26d3
18 changed files with 140 additions and 447 deletions

View File

@@ -0,0 +1,27 @@
package warewulfd
import (
warewulfd_responses "github.com/hpcng/warewulf/internal/app/warewulfd/warewulfd-reponses"
"github.com/spf13/cobra"
"net/http"
)
// TODO: https://github.com/danderson/netboot/blob/master/pixiecore/dhcp.go
// TODO: https://github.com/pin/tftp
//const LocalStateDir = "/var/warewulf"
func CobraRunE(cmd *cobra.Command, args []string) error {
http.HandleFunc("/ipxe/", warewulfd_responses.IpxeSend)
http.HandleFunc("/kernel/", warewulfd_responses.KernelSend)
http.HandleFunc("/kmods/", warewulfd_responses.KmodsSend)
http.HandleFunc("/vnfs/", warewulfd_responses.VnfsSend)
http.HandleFunc("/overlay-system/", warewulfd_responses.SystemOverlaySend)
http.HandleFunc("/overlay-runtime", warewulfd_responses.RuntimeOverlaySend)
http.ListenAndServe(":9873", nil)
return nil
}

View File

@@ -11,9 +11,8 @@ import (
"path"
)
const kernelProvisionPath = "/provision/kernel/"
func Build(nodeList []assets.NodeInfo, force bool) error {
config := config.New()
set := make(map[string]int)
wwlog.Printf(wwlog.INFO, "Importing Kernels:\n")
@@ -29,12 +28,13 @@ func Build(nodeList []assets.NodeInfo, force bool) error {
for kernelVersion := range set {
kernelImage := "/boot/vmlinuz-"+kernelVersion
kernelDrivers := "/lib/modules/"+kernelVersion
kernelDestination := path.Join(config.LocalStateDir, kernelProvisionPath, "vmlinuz-"+kernelVersion)
driversDestination := path.Join(config.LocalStateDir, kernelProvisionPath, "kmods-"+kernelVersion+".img")
kernelDestination := config.KernelImage(kernelVersion)
driversDestination := config.KmodsImage(kernelVersion)
// Create the destination paths just in case it doesn't exist
os.MkdirAll(path.Dir(kernelDestination), 0755)
os.MkdirAll(path.Dir(driversDestination), 0755)
// Create the kernel destination path just in case it doesn't exist
os.MkdirAll(path.Join(config.LocalStateDir, kernelProvisionPath), 0755)
if _, err := os.Stat(kernelImage); err == nil {
if util.PathIsNewer(kernelImage, kernelDestination) && force == false {

View File

@@ -27,12 +27,14 @@ func fileInclude(path string) string {
func Build(nodeList []assets.NodeInfo, force bool) error {
wwlog.Printf(wwlog.INFO, "Building Runtime Overlays:\n")
config := config.New()
wwlog.SetIndent(4)
for _, node := range nodeList {
if node.RuntimeOverlay != "" {
OverlayDir := path.Join(config.LocalStateDir, "/overlays/runtime/", node.RuntimeOverlay)
OverlayFile := path.Join(config.LocalStateDir, "/provision/overlays/runtime/", node.Fqdn+".img")
OverlayDir := config.RuntimeOverlaySource(node.RuntimeOverlay)
OverlayFile := config.RuntimeOverlayImage(node.Fqdn)
wwlog.Printf(wwlog.VERBOSE, "Building Runtime Overlay for: %s\n", node.Fqdn)

View File

@@ -27,12 +27,14 @@ func fileInclude(path string) string {
func Build(nodeList []assets.NodeInfo, force bool) error {
wwlog.Printf(wwlog.INFO, "Building System Overlays:\n")
config := config.New()
wwlog.SetIndent(4)
for _, node := range nodeList {
if node.SystemOverlay != "" {
OverlayDir := path.Join(config.LocalStateDir, "/overlays/system/", node.SystemOverlay)
OverlayFile := path.Join(config.LocalStateDir, "/provision/overlays/system/", node.Fqdn+".img")
OverlayDir := config.SystemOverlaySource(node.SystemOverlay)
OverlayFile := config.SystemOverlayImage(node.Fqdn)
wwlog.Printf(wwlog.VERBOSE, "Building System Overlay for: %s\n", node.Fqdn)

View File

@@ -2,6 +2,7 @@ package vnfs
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/config"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/vnfs"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
@@ -10,6 +11,7 @@ import (
)
func BuildContainerdir(v vnfs.VnfsObject) {
config := config.New()
if _, err := os.Stat(v.Source()); err != nil {
wwlog.Printf(wwlog.INFO, "%-35s: Skipping (bad path)\n", v.Name())
@@ -17,31 +19,35 @@ func BuildContainerdir(v vnfs.VnfsObject) {
}
wwlog.Printf(wwlog.DEBUG, "Checking if there have been any updates to the VNFS directory\n")
if util.PathIsNewer(v.Source(), v.Image()) {
if util.PathIsNewer(v.Source(), config.VnfsImage(v.NameClean())) {
if buildForce == false {
wwlog.Printf(wwlog.INFO, "%-35s: Skipping, VNFS is current\n", v.Name())
return
}
}
wwlog.Printf(wwlog.DEBUG, "Making the directory: %s\n", path.Dir(v.Image()))
err := os.MkdirAll(path.Dir(v.Image()), 0755)
wwlog.Printf(wwlog.DEBUG, "Making the directory: %s\n", path.Dir(config.VnfsImage(v.NameClean())))
err := os.MkdirAll(path.Dir(config.VnfsImage(v.NameClean())), 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
wwlog.Printf(wwlog.DEBUG, "Building VNFS image: '%s' -> '%s'\n", v.Source(), v.Image())
err = buildVnfs(v.Source(), v.Image())
wwlog.Printf(wwlog.DEBUG, "Building VNFS image: '%s' -> '%s'\n", v.Source(), config.VnfsImage(v.NameClean()))
err = buildVnfs(v.Source(), config.VnfsImage(v.NameClean()))
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
wwlog.Printf(wwlog.DEBUG, "Building links for Warewulf access to chroot\n")
err = buildLinks(v, v.Source())
// Setup links from OCI rootfs to chroot path
_ = os.Remove(config.VnfsChroot(v.NameClean()) + "-link")
err = os.Symlink(v.Source(), config.VnfsChroot(v.NameClean())+"-link")
if err != nil {
os.Exit(1)
}
err = os.Rename(config.VnfsChroot(v.NameClean())+"-link", config.VnfsChroot(v.NameClean()))
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}

View File

@@ -11,13 +11,13 @@ import (
"path"
)
const (
OciCacheDir = config.LocalStateDir + "/oci"
VnfsHashDir = config.LocalStateDir + "/oci/vnfs/hash/"
)
func BuildDocker(v vnfs.VnfsObject) {
wwlog.Printf(wwlog.VERBOSE, "Building OCI Container: %s\n", v.Source())
config := config.New()
OciCacheDir := config.LocalStateDir + "/oci"
VnfsHashDir := config.LocalStateDir + "/oci/vnfs"
c, err := oci.NewCache(oci.OptSetCachePath(OciCacheDir))
if err != nil {
@@ -34,7 +34,7 @@ func BuildDocker(v vnfs.VnfsObject) {
hashDestination := VnfsHashDir + path.Base(sourcePath)
name, err := os.Readlink(v.Image())
name, err := os.Readlink(config.VnfsImage(v.NameClean()))
if err == nil {
if name == hashDestination {
wwlog.Printf(wwlog.INFO, "%-35s: Skipping, VNFS is current\n", v.Name())
@@ -47,12 +47,12 @@ func BuildDocker(v vnfs.VnfsObject) {
fmt.Printf("ERROR: %s\n", err)
return
}
err = os.MkdirAll(path.Dir(v.Image()), 0755)
err = os.MkdirAll(path.Dir(config.VnfsImage(v.NameClean())), 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
}
err = os.MkdirAll(path.Dir(v.Root()), 0755)
err = os.MkdirAll(path.Dir(config.VnfsChroot(v.NameClean())), 0755)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
return
@@ -68,19 +68,29 @@ func BuildDocker(v vnfs.VnfsObject) {
wwlog.Printf(wwlog.VERBOSE, "Finalizing Build\n")
_ = os.Remove(v.Image() + "-link")
err = os.Symlink(hashDestination, v.Image()+"-link")
// Setup links from OCI image to provision path
_ = os.Remove(config.VnfsImage(v.NameClean()) + "-link")
err = os.Symlink(hashDestination, config.VnfsImage(v.NameClean())+"-link")
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
err = os.Rename(v.Image()+"-link", v.Image())
err = os.Rename(config.VnfsImage(v.NameClean())+"-link", config.VnfsImage(v.NameClean()))
if err != nil {
os.Exit(1)
}
err = buildLinks(v, sourcePath)
// Setup links from OCI rootfs to chroot path
_ = os.Remove(config.VnfsChroot(v.NameClean()) + "-link")
err = os.Symlink(sourcePath, config.VnfsChroot(v.NameClean())+"-link")
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
err = os.Rename(config.VnfsChroot(v.NameClean())+"-link", config.VnfsChroot(v.NameClean()))
if err != nil {
os.Exit(1)
}
wwlog.Printf(wwlog.INFO, "%-35s: Done\n", v.Name())

View File

@@ -1,12 +1,8 @@
package vnfs
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/vnfs"
"os"
"os/exec"
"path"
)
func buildVnfs(source string, dest string) error {
@@ -16,26 +12,3 @@ func buildVnfs(source string, dest string) error {
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")
// Just incase the directory doesn't exist, make it
_ = os.MkdirAll(path.Dir(v.Root()), 0755)
// 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

@@ -2,12 +2,8 @@ package assets
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/vnfs"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"gopkg.in/yaml.v2"
"io/ioutil"
"strings"
// "os"
// "os"
@@ -138,6 +134,7 @@ func FindAllNodes() ([]NodeInfo, error) {
n.Fqdn = node.Hostname
}
/*
if b, _ := regexp.MatchString(`^[a-z\-]+://`, n.Vnfs); b == true {
//if strings.HasPrefix(n.Vnfs, "docker://") {
//TODO: This is a kludge and shouldn't be done here. We need to go back
@@ -149,6 +146,7 @@ func FindAllNodes() ([]NodeInfo, error) {
} else {
wwlog.Printf(wwlog.ERROR, "Configuration 'Vnfs' is invalid for node: %s\n", n.Fqdn)
}
*/
ret = append(ret, n)
}

View File

@@ -2,15 +2,13 @@ package config
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
const (
SysConfDir = "/etc/warewulf/"
LocalStateDir = "/var/warewulf"
)
type Config struct {
Port int `yaml:"warewulfd port", envconfig:"WAREWULFD_PORT"`
@@ -21,34 +19,76 @@ type Config struct {
LocalStateDir string `yaml:"local state dir"`
}
func New() (Config, error) {
var c Config
var c Config
fd, err := ioutil.ReadFile(SysConfDir + "warewulf.conf")
func init() {
fd, err := ioutil.ReadFile("/etc/warewulf/warewulf.conf")
if err != nil {
return c, err
wwlog.Printf(wwlog.ERROR, "Could not read config file: %s\n", err)
os.Exit(255)
}
err = yaml.Unmarshal(fd, &c)
if err != nil {
return c, err
wwlog.Printf(wwlog.ERROR, "Could not unmarshal config file: %s\n", err)
os.Exit(255)
}
err = envconfig.Process("", &c)
if err != nil {
return c, err
wwlog.Printf(wwlog.ERROR, "Could not obtain environment configuration: %s\n", err)
os.Exit(255)
}
if c.Ipaddr == "" {
fmt.Printf("ERROR: 'warewulf ipaddr' has not been set in %s\n", SysConfDir+"warewulf.conf")
fmt.Printf("ERROR: 'warewulf ipaddr' has not been set in /etc/warewulf/warewulf.conf\n")
}
if c.SysConfDir == "" {
c.SysConfDir = SysConfDir
c.SysConfDir = "/etc/warewulf"
}
if c.LocalStateDir == "" {
c.LocalStateDir = LocalStateDir
c.LocalStateDir = "/var/warewulf"
}
return c, nil
}
func New() (Config) {
return c
}
func (self *Config) NodeConfig() (string) {
return fmt.Sprintf("%s/nodes.conf", self.LocalStateDir)
}
func (self *Config) SystemOverlaySource(overlayName string) (string) {
return fmt.Sprintf("%s/overlays/system/%s", self.LocalStateDir, overlayName)
}
func (self *Config) RuntimeOverlaySource(overlayName string) (string) {
return fmt.Sprintf("%s/overlays/runtime/%s", self.LocalStateDir, overlayName)
}
func (self *Config) KernelImage(kernelVersion string) (string) {
return fmt.Sprintf("%s/kernel/vmlinuz-%s", self.LocalStateDir, kernelVersion)
}
func (self *Config) KmodsImage(kernelVersion string) (string) {
return fmt.Sprintf("%s/kernel/kmods-%s.img", self.LocalStateDir, kernelVersion)
}
func (self *Config) VnfsImage(vnfsNameClean string) (string) {
return fmt.Sprintf("%s/vnfs/%s.img.gz", self.LocalStateDir, vnfsNameClean)
}
func (self *Config) SystemOverlayImage(nodeName string) (string) {
return fmt.Sprintf("%s/overlay/system/%s.img", self.LocalStateDir, nodeName)
}
func (self *Config) RuntimeOverlayImage(nodeName string) (string) {
return fmt.Sprintf("%s/overlay/runtime/%s.img", self.LocalStateDir, nodeName)
}
func (self *Config) VnfsChroot(vnfsNameClean string) (string) {
return fmt.Sprintf("%s/chroot/%s.img", self.LocalStateDir, vnfsNameClean)
}

View File

@@ -1,15 +1,12 @@
package vnfs
import (
"github.com/hpcng/warewulf/internal/pkg/config"
"path"
"strings"
)
type VnfsObject struct {
SourcePath string
RootPath string
ImagePath string
}
func New(s string) VnfsObject {
@@ -20,7 +17,6 @@ func New(s string) VnfsObject {
return ret
}
func (self *VnfsObject) Name() string {
if self.SourcePath == "" {
return ""
@@ -52,21 +48,4 @@ func (self *VnfsObject) Source() string {
}
return self.SourcePath
}
func (self *VnfsObject) Image() string {
if self.SourcePath == "" {
return ""
}
return config.LocalStateDir + "/provision/vnfs/" + self.NameClean() + ".img.gz"
}
func (self *VnfsObject) Root() string {
if self.SourcePath == "" {
return ""
}
return config.LocalStateDir + "/chroots/" + self.NameClean()
}
}

View File

@@ -15,11 +15,11 @@ const (
)
var (
logLevel uint
logLevel = INFO
Indent string
)
func SetLevel(level uint) {
func SetLevel(level int) {
logLevel = level
if level == DEBUG {
@@ -35,7 +35,7 @@ func SetIndent(i int) {
Indent = strings.Repeat(" ", i)
}
func prefixLevel(level uint) {
func prefixLevel(level int) {
if level == DEBUG {
log.SetPrefix("[DEBUG] "+Indent)
} else if level == VERBOSE {
@@ -51,7 +51,7 @@ func prefixLevel(level uint) {
}
}
func Println(level uint, message string) {
func Println(level int, message string) {
if level <= logLevel {
prefixLevel(level)
log.Println(message)
@@ -60,7 +60,7 @@ func Println(level uint, message string) {
log.SetPrefix("[LOG] "+Indent)
}
func Printf(level uint, message string, a...interface{}) {
func Printf(level int, message string, a...interface{}) {
if level <= logLevel {
prefixLevel(level)
log.Printf(message, a...)