* Combine provision response handling, add hwaddr and interface to dhcp * Simplify stage cases, add backward compatible uri parse * Move control of image compression to client * Fix kernel search path for ubuntu, buffer template generation * Implement cpio and gz as functions, add log named functions * Generalize fs image creation routine * Combine daemonLogf to generalized wwlog * Fix template newline insertion, update dhcp and hosts tmpl * Update IPXE template to handle non-compressed files * Update DHCP template to set network interfaces and server IP assignment * Update DHCP/hosts templates to choose a host-name self-consistently
103 lines
3.0 KiB
Go
103 lines
3.0 KiB
Go
package overlay
|
|
|
|
import (
|
|
"bufio"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
|
|
"github.com/hpcng/warewulf/internal/pkg/container"
|
|
"github.com/hpcng/warewulf/internal/pkg/util"
|
|
"github.com/hpcng/warewulf/internal/pkg/wwlog"
|
|
)
|
|
|
|
/*
|
|
Reads a file file from the host fs. If the file has nor '/' prefix
|
|
the path is relative to SYSCONFDIR.
|
|
Templates in the file are no evaluated.
|
|
*/
|
|
func templateFileInclude(inc string) string {
|
|
if !strings.HasPrefix(inc, "/") {
|
|
inc = path.Join(buildconfig.SYSCONFDIR(), "warewulf", inc)
|
|
}
|
|
wwlog.Printf(wwlog.DEBUG, "Including file into template: %s\n", inc)
|
|
content, err := ioutil.ReadFile(inc)
|
|
if err != nil {
|
|
wwlog.Printf(wwlog.VERBOSE, "Could not include file into template: %s\n", err)
|
|
}
|
|
return strings.TrimSuffix(string(content), "\n")
|
|
}
|
|
|
|
/*
|
|
Reads a file into template the abort string is found in a line. First argument
|
|
is the file to read, the second the abort string
|
|
Templates in the file are no evaluated.
|
|
*/
|
|
func templateFileBlock(inc string, abortStr string) (string, error) {
|
|
if !strings.HasPrefix(inc, "/") {
|
|
inc = path.Join(buildconfig.SYSCONFDIR(), "warewulf", inc)
|
|
}
|
|
wwlog.Printf(wwlog.DEBUG, "Including file block into template: %s\n", inc)
|
|
readFile, err := os.Open(inc)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer readFile.Close()
|
|
var cont string
|
|
fileScanner := bufio.NewScanner(readFile)
|
|
fileScanner.Split(bufio.ScanLines)
|
|
for fileScanner.Scan() {
|
|
line := fileScanner.Text()
|
|
if strings.Contains(line, abortStr) {
|
|
break
|
|
}
|
|
cont += line + "\n"
|
|
}
|
|
|
|
// NOTE: the text originally contains N-1 newlines for N lines, but the above
|
|
// loop will always add one at the end
|
|
// Avoids adding a blank line that was not present in the original file
|
|
// by adding 'abort' string to the end of the included block (without a newline)
|
|
// instead of manually in the template
|
|
cont += abortStr
|
|
|
|
return cont, nil
|
|
|
|
}
|
|
|
|
/*
|
|
Reads a file relative to given container.
|
|
Templates in the file are no evaluated.
|
|
*/
|
|
func templateContainerFileInclude(containername string, filepath string) string {
|
|
wwlog.Printf(wwlog.VERBOSE, "Including file from Container into template: %s:%s\n", containername, filepath)
|
|
|
|
if containername == "" {
|
|
wwlog.Printf(wwlog.WARN, "Container is not defined for node: %s\n", filepath)
|
|
return ""
|
|
}
|
|
|
|
if !container.ValidSource(containername) {
|
|
wwlog.Printf(wwlog.WARN, "Template requires file(s) from non-existant container: %s:%s\n", containername, filepath)
|
|
return ""
|
|
}
|
|
|
|
containerDir := container.RootFsDir(containername)
|
|
|
|
wwlog.Printf(wwlog.DEBUG, "Including file from container: %s:%s\n", containerDir, filepath)
|
|
|
|
if !util.IsFile(path.Join(containerDir, filepath)) {
|
|
wwlog.Printf(wwlog.WARN, "Requested file from container does not exist: %s:%s\n", containername, filepath)
|
|
return ""
|
|
}
|
|
|
|
content, err := ioutil.ReadFile(path.Join(containerDir, filepath))
|
|
|
|
if err != nil {
|
|
wwlog.Printf(wwlog.ERROR, "Template include failed: %s\n", err)
|
|
}
|
|
return strings.TrimSuffix(string(content), "\n")
|
|
}
|