Revert "Move configuration code out of wwctl"

This commit is contained in:
Gregory M. Kurtzer
2022-02-11 16:20:05 -08:00
committed by GitHub
parent 5a7f99af18
commit 67ee3bf5f8
33 changed files with 685 additions and 683 deletions

View File

@@ -6,7 +6,6 @@ var (
bindir string = "UNDEF"
sysconfdir string = "UNDEF"
localstatedir string = "UNDEF"
sharedstatedir string = "UNDEF"
srvdir string = "UNDEF"
tftpdir string = "UNDEF"
firewallddir string = "UNDEF"
@@ -14,9 +13,9 @@ var (
wwoverlaydir string = "UNDEF"
wwchrootdir string = "UNDEF"
wwprovisiondir string = "UNDEF"
wwclientdir string = "UNDEF"
version string = "UNDEF"
release string = "UNDEF"
wwclientloc string = "UNDEF"
)
func BINDIR() string {
@@ -79,12 +78,7 @@ func RELEASE() string {
return release
}
func WWCLIENTDIR() string {
wwlog.Printf(wwlog.DEBUG, "WWPROVISIONDIR = '%s'\n", wwclientdir)
return wwclientdir
}
func SHAREDSTATEDIR() string {
wwlog.Printf(wwlog.DEBUG, "SHAREDSTATEDIR = '%s'\n", sharedstatedir)
return sharedstatedir
func WWCLIENTLOC() string {
wwlog.Printf(wwlog.DEBUG, "WWCLIENTLOC = '%s'\n", wwclientloc)
return wwclientloc
}

View File

@@ -4,7 +4,6 @@ func init() {
bindir = "@BINDIR@"
sysconfdir = "@SYSCONFDIR@"
localstatedir = "@LOCALSTATEDIR@"
sharedstatedir = "@SHAREDSTATEDIR@"
srvdir = "@SRVDIR@"
tftpdir = "@TFTPDIR@"
firewallddir = "@FIREWALLDDIR@"
@@ -12,7 +11,7 @@ func init() {
wwoverlaydir = "@WWOVERLAYDIR@"
wwchrootdir = "@WWCHROOTDIR@"
wwprovisiondir = "@WWPROVISIONDIR@"
wwclientdir = "@WWCLIENTDIR@"
version = "@VERSION@"
release = "@RELEASE@"
wwclientloc = "@WWCLIENTLOC"
}

View File

@@ -1,32 +0,0 @@
package configure
import (
"fmt"
"github.com/pkg/errors"
)
func Configure(s string, v bool) error {
if !v {
fmt.Printf("################################################################################\n")
fmt.Printf("Configuring: %s\n", s)
}
var err error
switch s {
case "DHCP":
err = configureDHCP(v)
case "hosts":
err = configureHosts(v)
case "NFS":
err = configureNFS(v)
case "SSH":
err = configureSSH(v)
case "TFTP":
err = configureTFTP(v)
}
if err != nil {
return errors.Wrap(err, "Failed to configure "+s)
}
return nil
}

View File

@@ -1,139 +0,0 @@
package configure
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"text/template"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors"
)
type dhcpTemplate struct {
Ipaddr string
Port string
RangeStart string
RangeEnd string
Network string
Netmask string
Nodes []node.NodeInfo
}
func configureDHCP(show bool) error {
var d dhcpTemplate
var templateFile string
nodeDB, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
os.Exit(1)
}
controller, err := warewulfconf.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
if controller.Ipaddr == "" {
wwlog.Printf(wwlog.ERROR, "The Warewulf IP Address is not properly configured\n")
os.Exit(1)
}
if controller.Netmask == "" {
wwlog.Printf(wwlog.ERROR, "The Warewulf Netmask is not properly configured\n")
os.Exit(1)
}
if !controller.Dhcp.Enabled {
wwlog.Printf(wwlog.INFO, "This system is not configured as a Warewulf DHCP controller\n")
os.Exit(1)
}
if controller.Dhcp.RangeStart == "" {
wwlog.Printf(wwlog.ERROR, "Configuration is not defined: `dhcpd range start`\n")
os.Exit(1)
}
if controller.Dhcp.RangeEnd == "" {
wwlog.Printf(wwlog.ERROR, "Configuration is not defined: `dhcpd range end`\n")
os.Exit(1)
}
if controller.Dhcp.ConfigFile == "" {
controller.Dhcp.ConfigFile = "/etc/dhcp/dhcpd.conf"
}
nodes, err := nodeDB.FindAllNodes()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not find all controllers: %s\n", err)
os.Exit(1)
}
d.Nodes = append(d.Nodes, nodes...)
templateFile = dhcpTemplateFile(controller.Dhcp.Template)
tmpl, err := template.New(path.Base(templateFile)).ParseFiles(templateFile)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
d.Ipaddr = controller.Ipaddr
d.Port = strconv.Itoa(controller.Warewulf.Port)
d.Network = controller.Network
d.Netmask = controller.Netmask
d.RangeStart = controller.Dhcp.RangeStart
d.RangeEnd = controller.Dhcp.RangeEnd
if !show {
fmt.Printf("Writing the DHCP configuration file\n")
configWriter, err := os.OpenFile(controller.Dhcp.ConfigFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0640)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
defer configWriter.Close()
err = tmpl.Execute(configWriter, d)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
fmt.Printf("Enabling and restarting the DHCP services\n")
err = util.SystemdStart(controller.Dhcp.SystemdName)
if err != nil {
return errors.Wrap(err, "failed to start")
}
} else {
err = tmpl.Execute(os.Stdout, d)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
}
return nil
}
// dhcpTemplateFile returns the path of the warewulf dhcp template given controller.Dhcp.Template.
func dhcpTemplateFile(controllerDhcpTemplate string) (templateFile string) {
if controllerDhcpTemplate == "" {
templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf")
} else {
if strings.HasPrefix(controllerDhcpTemplate, "/") {
templateFile = controllerDhcpTemplate
} else {
templateFile = path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/"+controllerDhcpTemplate+"-dhcpd.conf")
}
}
return
}

View File

@@ -1,27 +0,0 @@
package configure
import (
"path"
"testing"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
)
func TestDhcpTemplateFile(t *testing.T) {
tests := []struct {
parameter string
expected string
}{
{"", path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf")},
{"default", path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/default-dhcpd.conf")},
{"static", path.Join(buildconfig.SYSCONFDIR(), "warewulf/dhcp/static-dhcpd.conf")},
{"/test/absolute/path.conf", "/test/absolute/path.conf"},
}
for _, tt := range tests {
actual := dhcpTemplateFile(tt.parameter)
if actual != tt.expected {
t.Errorf("dhcpTemplateFile(%v) expected: %v, actual: %v",
tt.parameter, tt.expected, actual)
}
}
}

View File

@@ -1,110 +0,0 @@
package configure
import (
"bytes"
"os"
"path"
"text/template"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
)
type hostsTemplate struct {
PrevHostFile string
Ipaddr string
Fqdn string
AllNodes []node.NodeInfo
}
func configureHosts(show bool) error {
var replace hostsTemplate
if !util.IsFile(path.Join(buildconfig.SYSCONFDIR(), "warewulf/hosts.tmpl")) {
wwlog.Printf(wwlog.WARN, "Template not found, not updating host file\n")
return nil
}
controller, err := warewulfconf.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
n, err := node.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not open node configuration: %s\n", err)
os.Exit(1)
}
tmpl, err := template.ParseFiles(path.Join(buildconfig.SYSCONFDIR(), "warewulf/hosts.tmpl"))
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not parse hosts template: %s\n", err)
os.Exit(1)
}
replace.PrevHostFile = ""
w, err := os.Open("/etc/hosts")
if err != nil {
wwlog.Printf(wwlog.WARN, "%s\n", err)
} else {
// if /etc/hosts.ww does not exist, backup /etc/hosts to /etc/hosts.wwbackup
if !util.IsFile("/etc/hosts.wwbackup") {
err = util.CopyFile("/etc/hosts", "/etc/hosts.wwbackup")
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
}
}
// read all lines before the # warewulf comment and put into PrevHostFile template variable
lines, _ := util.ReadFile("/etc/hosts")
if lines != nil {
var buffer bytes.Buffer
for _, line := range lines {
//wwlog.Printf(wwlog.INFO, "Reading line: %s\n", line)
if util.ValidString(line, "^#.*maintained by warewulf") {
break
}
buffer.WriteString(line)
buffer.WriteString("\n")
}
replace.PrevHostFile = buffer.String()
}
}
w.Close()
nodes, _ := n.FindAllNodes()
replace.AllNodes = nodes
replace.Ipaddr = controller.Ipaddr
replace.Fqdn = controller.Fqdn
if !show {
// only open "/etc/hosts" when intended to write, as 'os.O_TRUNC' will empty the file otherwise.
w, err = os.OpenFile("/etc/hosts", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
defer w.Close()
err = tmpl.Execute(w, replace)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
} else {
err = tmpl.Execute(os.Stdout, replace)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
}
return nil
}

View File

@@ -1,69 +0,0 @@
package configure
import (
"fmt"
"os"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors"
)
func configureNFS(show bool) error {
controller, err := warewulfconf.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
if controller.Network == "" {
wwlog.Printf(wwlog.ERROR, "Network must be defined in warewulf.conf to configure NFS\n")
os.Exit(1)
}
if controller.Netmask == "" {
wwlog.Printf(wwlog.ERROR, "Netmask must be defined in warewulf.conf to configure NFS\n")
os.Exit(1)
}
if !show {
if controller.Nfs.Enabled {
exports, err := os.OpenFile("/etc/exports", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
os.Exit(1)
}
defer exports.Close()
fmt.Fprintf(exports, "# This file was written by Warewulf (wwctl configure nfs)\n")
for _, export := range controller.Nfs.ExportsExtended {
fmt.Fprintf(exports, "%s %s/%s(%s)\n", export.Path, controller.Network, controller.Netmask, export.ExportOptions)
}
fmt.Printf("Enabling and restarting the NFS services\n")
if controller.Nfs.SystemdName == "" {
err := util.SystemdStart("nfs-server")
if err != nil {
return errors.Wrap(err, "failed to start nfs-server")
}
} else {
err := util.SystemdStart(controller.Nfs.SystemdName)
if err != nil {
return errors.Wrap(err, "failed to start")
}
}
}
} else {
fmt.Printf("/etc/exports:\n")
for _, export := range controller.Nfs.ExportsExtended {
fmt.Printf("%s %s/%s\n", export.Path, controller.Network, controller.Netmask)
}
fmt.Printf("\n")
}
return nil
}

View File

@@ -1,72 +0,0 @@
package configure
import (
"fmt"
"os"
"path"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors"
)
func configureSSH(show bool) error {
if !show {
if os.Getuid() == 0 {
fmt.Printf("Updating system keys\n")
wwkeydir := path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys") + "/"
err := os.MkdirAll(path.Join(buildconfig.SYSCONFDIR(), "warewulf/keys"), 0755)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not create base directory: %s\n", err)
os.Exit(1)
}
for _, k := range [4]string{"rsa", "dsa", "ecdsa", "ed25519"} {
keytype := "ssh_host_" + k + "_key"
if !util.IsFile(path.Join(wwkeydir, keytype)) {
fmt.Printf("Setting up key: %s\n", keytype)
wwlog.Printf(wwlog.DEBUG, "Creating new %s key\n", keytype)
err = util.ExecInteractive("ssh-keygen", "-q", "-t", k, "-f", path.Join(wwkeydir, keytype), "-C", "", "-N", "")
if err != nil {
wwlog.Printf(wwlog.ERROR, "Failed to exec ssh-keygen: %s\n", err)
return errors.Wrap(err, "failed to exec ssh-keygen command")
}
} else {
fmt.Printf("Skipping, key already exists: %s\n", keytype)
}
}
} else {
fmt.Printf("Updating user's keys\n")
}
homeDir, err := os.UserHomeDir()
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not obtain the user's home directory: %s\n", err)
os.Exit(1)
}
authorizedKeys := path.Join(homeDir, "/.ssh/authorized_keys")
rsaPriv := path.Join(homeDir, "/.ssh/id_rsa")
rsaPub := path.Join(homeDir, "/.ssh/id_rsa.pub")
if !util.IsFile(authorizedKeys) {
fmt.Printf("Setting up: %s\n", authorizedKeys)
err = util.ExecInteractive("ssh-keygen", "-q", "-t", "rsa", "-f", rsaPriv, "-C", "", "-N", "")
if err != nil {
return errors.Wrap(err, "failed to exec ssh-keygen command")
}
err := util.CopyFile(rsaPub, authorizedKeys)
if err != nil {
return errors.Wrap(err, "failed to copy keys")
}
} else {
fmt.Printf("Skipping, authorized_keys already exists: %s\n", authorizedKeys)
}
} else {
fmt.Printf("'ssh -s' is not yet implemented.\n")
}
return nil
}

View File

@@ -1,51 +0,0 @@
package configure
import (
"fmt"
"os"
"path"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/staticfiles"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
)
var tftpdir string = path.Join(buildconfig.TFTPDIR(), "warewulf")
func configureTFTP(show bool) error {
if !show {
controller, err := warewulfconf.New()
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
return err
}
err = os.MkdirAll(tftpdir, 0755)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
return err
}
fmt.Printf("Writing PXE files to: %s\n", tftpdir)
for _, f := range [4]string{"x86.efi", "i386.efi", "i386.kpxe", "arm64.efi"} {
err = staticfiles.WriteData(path.Join("files/tftp", f), path.Join(tftpdir, f))
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
return err
}
}
fmt.Printf("Enabling and restarting the TFTP services\n")
err = util.SystemdStart(controller.Tftp.SystemdName)
if err != nil {
wwlog.Printf(wwlog.ERROR, "%s\n", err)
return err
}
} else {
fmt.Printf("'tftp -s' is not yet implemented.\n")
}
return nil
}