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

@@ -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...)