Added configuration pkg, iPXE templating, clean up, etc.

This commit is contained in:
Gregory Kurtzer
2020-11-04 18:45:36 -08:00
parent 260315cccd
commit d2ed5766cd
13 changed files with 127 additions and 85 deletions

View File

@@ -1,10 +1,14 @@
package main
import (
"bufio"
"fmt"
"github.com/hpcng/warewulf/internal/pkg/assets"
"github.com/hpcng/warewulf/internal/pkg/config"
"log"
"net/http"
"os"
"strconv"
"strings"
)
@@ -25,17 +29,35 @@ func ipxe(w http.ResponseWriter, req *http.Request) {
}
if node.HostName != "" {
log.Printf("IPXE: %15s: hwaddr=%s\n", node.Fqdn, hwaddr)
replace := make(map[string]string)
fmt.Fprintf(w, "#!ipxe\n")
conf, err := config.New()
if err != nil {
log.Printf("Could not get config: %s\n", err)
return
}
ipxeTemplate := fmt.Sprintf("/etc/warewulf/ipxe/%s.ipxe", node.Ipxe)
sourceFD, err := os.Open(ipxeTemplate)
if err != nil {
log.Printf("ERROR: Could not open iPXE Template: %s\n", err)
w.WriteHeader(404)
return
}
scanner := bufio.NewScanner(sourceFD)
for scanner.Scan() {
newLine := scanner.Text()
newLine = strings.ReplaceAll(newLine, "@HWADDR@", url[2])
newLine = strings.ReplaceAll(newLine, "@IPADDR@", conf.Ipaddr)
newLine = strings.ReplaceAll(newLine, "@HOSTNAME@", node.HostName)
newLine = strings.ReplaceAll(newLine, "@PORT@", strconv.Itoa(conf.Port))
fmt.Fprintln(w, newLine)
}
fmt.Fprintf(w, "echo Now booting Warewulf - v4 Proof of Concept\n")
fmt.Fprintf(w, "set base http://192.168.1.1:9873/\n")
fmt.Fprintf(w, "kernel ${base}/kernel/%s crashkernel=no quiet\n", url[2])
fmt.Fprintf(w, "initrd ${base}/vnfs/%s\n", url[2])
fmt.Fprintf(w, "initrd ${base}/kmods/%s\n", url[2])
fmt.Fprintf(w, "initrd ${base}/overlay-system/%s\n", url[2])
fmt.Fprintf(w, "boot\n")
} else {
log.Printf("ERROR: iPXE request from unknown Node (hwaddr=%s)\n", url[2])
}

View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/assets"
"github.com/hpcng/warewulf/internal/pkg/config"
"log"
"net/http"
"strconv"
@@ -10,7 +11,6 @@ import (
)
func runtimeOverlay(w http.ResponseWriter, req *http.Request) {
remote := strings.Split(req.RemoteAddr, ":")
port, err := strconv.Atoi(remote[1])
if err != nil {
@@ -19,12 +19,20 @@ func runtimeOverlay(w http.ResponseWriter, req *http.Request) {
return
}
if port >= 1024 {
log.Panicf("DENIED: Connection coming from non-privledged port: %s\n", req.RemoteAddr)
w.WriteHeader(401)
config, err := config.New()
if err != nil {
fmt.Printf("ERROR: Could not load configuration file: %s\n", err)
return
}
if config.InsecureRuntime == false {
if port >= 1024 {
log.Panicf("DENIED: Connection coming from non-privledged port: %s\n", req.RemoteAddr)
w.WriteHeader(401)
return
}
}
node, err := assets.FindByIpaddr(remote[0])
if err != nil {
fmt.Printf("Could not find node by IP address: %s\n", remote[0])

View File

@@ -99,6 +99,9 @@ func BuildOverlayDir(sourceDir string, destDir string, replace map[string]string
return err
}
includeFD.Close()
} else if strings.HasPrefix(newLine, "#WW") {
// Anything else is a comment or to be ignored
continue
} else {
_, err := w.WriteString(newLine + "\n")
if err != nil {

View File

@@ -7,9 +7,9 @@ import (
"net/http"
"os"
"os/exec"
// "os/exec"
"time"
"github.com/hpcng/warewulf/internal/pkg/config"
)
func main() {
@@ -24,9 +24,18 @@ func main() {
os.Chdir("/warewulf/wwclient-test")
}
// Setup local port to 987
localTCPAddr := net.TCPAddr{
Port: 987,
config, err := config.New()
if err != nil {
fmt.Printf("ERROR: Could not load configuration file: %s\n", err)
return
}
localTCPAddr := net.TCPAddr{}
if config.InsecureRuntime == false {
// Setup local port to something privileged (<1024)
localTCPAddr.Port = 987
} else {
fmt.Printf("INFO: Running from an insecure port\n")
}
webclient := &http.Client{
@@ -52,7 +61,8 @@ func main() {
for true {
var err error
resp, err = webclient.Get("http://192.168.1.1:9873/overlay-runtime")
getString := fmt.Sprintf("http://%s:%d/overlay-runtime", config.Ipaddr, config.Port)
resp, err = webclient.Get(getString)
if err == nil {
break
} else {
@@ -79,25 +89,7 @@ func main() {
continue
}
/*
// TODO: Turn all of this into a pipe instead of having to use a tmpfile which
// I tried to get working, but when running on a node, it always gave a
// trying to write on closed file descriptor... This maybe ugly, but it
// works.
tmpfile := fmt.Sprintf("/tmp/.wwclient-%s", util.RandomString(14))
tmpFD, _ := os.Create(tmpfile)
defer tmpFD.Close()
io.Copy(tmpFD, resp.Body)
tmpFD.Close()
err := exec.Command("cpio", "-i", "-F", tmpfile).Run()
if err != nil {
fmt.Printf("%s", err)
}
os.Remove(tmpfile)
*/
log.Printf("Updating runtime system\n")
command := exec.Command("/bin/cpio", "-iu")
command.Stdin = resp.Body
@@ -105,27 +97,6 @@ func main() {
if err != nil {
log.Printf("ERROR: Failed running CPIO: %s\n", err)
}
/*
command.Wait()
stdin, err := command.StdinPipe()
if err != nil {
log.Println(err)
}
defer stdin.Close()
go func() {
bytes, err := io.Copy(stdin, resp.Body)
if err != nil {
log.Printf("ERROR: io.Copy() failed: %s\n", err)
} else {
log.Printf("Updated the runtime overlay (recv: %d)\n", bytes)
}
}()
command.Run()
*/
// defer webclient.CloseIdleConnections()
time.Sleep(30000 * time.Millisecond)
}