Add WWbool.BoolDefaultTrue()

New support in WWbool for booleans that default "true."

Used primarily for NetDev.OnBoot.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-19 18:01:17 -07:00
parent 9ae77cb5b9
commit fd8de8bfab
15 changed files with 62 additions and 27 deletions

View File

@@ -51,6 +51,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Don't use DHCP for interfaces attached to a bond. #1743
- Wait until ignition has completed before trying to mount.
- Fix timeout problem for wwclient. #1741
- Fixed default "true" state of NetDev.OnBoot. #1754
### Removed

View File

@@ -7,7 +7,6 @@ import (
"strconv"
"strings"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwtype"
)
@@ -74,7 +73,7 @@ func check(infoType reflect.Type, infoVal reflect.Value) (err error) {
}
func checker(value string, valType string) (niceValue string, err error) {
if valType == "" || value == "" || util.InSlice(wwtype.GetUnsetVerbs(), value) {
if valType == "" || value == "" || wwtype.IsUnsetVerb(value) {
return "", nil
}
switch valType {

View File

@@ -1,5 +1,15 @@
package wwtype
import (
"strings"
"github.com/warewulf/warewulf/internal/pkg/util"
)
func GetUnsetVerbs() []string {
return []string{"UNSET", "DELETE", "UNDEF", "undef", "--", "nil", "0.0.0.0"}
return []string{"unset", "delete", "undef", "--", "nil", "0.0.0.0"}
}
func IsUnsetVerb(value string) bool {
return util.InSlice(GetUnsetVerbs(), strings.ToLower(value))
}

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"strconv"
"strings"
"github.com/warewulf/warewulf/internal/pkg/util"
)
// Simple string which can be converted to bool. Backend storage
@@ -16,17 +14,38 @@ type WWbool string
Transform the underlying string value to bool
*/
func (val WWbool) Bool() bool {
str := string(val)
if util.InSlice(GetUnsetVerbs(), str) {
str := strings.ToLower(string(val))
if IsUnsetVerb(str) {
return false
}
if strings.ToLower(str) == "yes" {
switch str {
case "yes":
return true
case "no", "":
return false
}
bval, err := strconv.ParseBool(str)
if err != nil {
return false
}
return bval
}
func (val WWbool) BoolDefaultTrue() bool {
str := strings.ToLower(string(val))
if IsUnsetVerb(str) {
return false
}
switch str {
case "yes", "":
return true
case "no":
return false
}
bval, err := strconv.ParseBool(str)
if err != nil {
return true
}
if strings.ToLower(str) == "no" {
return false
}
bval, _ := strconv.ParseBool(str)
return bval
}
@@ -34,7 +53,7 @@ func (val WWbool) Bool() bool {
Set the string, only accept bool values like true, false, but also UNDEF
*/
func (val *WWbool) Set(str string) error {
if util.InSlice(GetUnsetVerbs(), str) {
if IsUnsetVerb(str) {
// run the unset verb trough, will be filtered out later
*val = WWbool(str)
return nil

View File

@@ -256,6 +256,7 @@ Filename: warewulf-bond0.conf
id=bond0
interface-name=bond0
type=bond
autoconnect=true
[bond]
downdelay=0
miimon=100
@@ -282,6 +283,7 @@ interface-name=en1
type=ethernet
master=bond0
slave-type=bond
autoconnect=true
[ethernet]
mac-address=e6:92:39:49:7b:03
[ipv4]
@@ -300,6 +302,7 @@ interface-name=en2
type=ethernet
master=bond0
slave-type=bond
autoconnect=true
[ethernet]
mac-address=9a:77:29:73:14:f1
[ipv4]

View File

@@ -10,9 +10,7 @@ type={{ default "ethernet" (lower $netdev.Type) }}
master={{ $netdev.Tags.master }}
slave-type=bond
{{- end }}
{{- if $netdev.OnBoot }}
autoconnect=true
{{- end }}
autoconnect={{ $netdev.OnBoot.BoolDefaultTrue }}
{{- if eq (lower $netdev.Type) "bond" }}
[bond]

View File

@@ -54,6 +54,7 @@ writeFile: true
Filename: default
# This file is autogenerated by warewulf
auto wwnet0
allow-hotplug wwnet0
iface wwnet0 inet static
address 192.168.3.21
@@ -65,6 +66,7 @@ backupFile: true
writeFile: true
Filename: secondary
# This file is autogenerated by warewulf
auto wwnet1
allow-hotplug wwnet1
iface wwnet1 inet static
address 192.168.3.22

View File

@@ -1,7 +1,7 @@
{{- range $devname, $netdev := .ThisNode.NetDevs }}
{{ file $devname }}
# This file is autogenerated by warewulf
{{- if $netdev.OnBoot }}
{{- if $netdev.OnBoot.BoolDefaultTrue }}
auto {{ $netdev.Device }}
{{- end }}
allow-hotplug {{ $netdev.Device }}

View File

@@ -169,7 +169,7 @@ node methods in addition to its fields.
- Tags:
- NetDevs[default]:
- Type:
- OnBoot: true
- OnBoot: true (true)
- Device: wwnet0
- Hwaddr: e6:92:39:49:7b:03
- Ipaddr: 192.168.3.21
@@ -182,7 +182,7 @@ node methods in addition to its fields.
- Tags:
- NetDevs[secondary]:
- Type:
- OnBoot: true
- OnBoot: true (true)
- Device: wwnet1
- Hwaddr: 9a:77:29:73:14:f1
- Ipaddr: 192.168.3.22

View File

@@ -114,7 +114,7 @@ node methods in addition to its fields.
{{- range $index, $netdev := .NetDevs }}
- NetDevs[{{ $index }}]:
- Type: {{ $netdev.Type }}
- OnBoot: {{ $netdev.OnBoot }}
- OnBoot: {{ $netdev.OnBoot }} ({{ $netdev.OnBoot.BoolDefaultTrue }})
- Device: {{ $netdev.Device }}
- Hwaddr: {{ $netdev.Hwaddr }}
- Ipaddr: {{ $netdev.Ipaddr }}

View File

@@ -57,6 +57,7 @@ IPADDR=192.168.3.21
NETMASK=255.255.255.0
GATEWAY=192.168.3.1
HWADDR=e6:92:39:49:7b:03
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
@@ -77,6 +78,7 @@ IPADDR=192.168.3.22
NETMASK=255.255.255.0
GATEWAY=192.168.3.1
HWADDR=9a:77:29:73:14:f1
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
@@ -202,6 +204,7 @@ BOOTPROTO=static
DEVTIMEOUT=10
IPADDR=192.168.3.110
NETMASK=255.255.255.0
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
@@ -217,6 +220,7 @@ NAME=en1
BOOTPROTO=static
DEVTIMEOUT=10
HWADDR=e6:92:39:49:7b:03
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
@@ -232,6 +236,7 @@ NAME=en2
BOOTPROTO=static
DEVTIMEOUT=10
HWADDR=9a:77:29:73:14:f1
ONBOOT=true
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes

View File

@@ -28,9 +28,7 @@ GATEWAY={{ $netdev.Gateway }}
{{- if $netdev.Hwaddr }}
HWADDR={{ $netdev.Hwaddr }}
{{- end }}
{{- if $netdev.OnBoot }}
ONBOOT=true
{{- end }}
ONBOOT={{ $netdev.OnBoot.BoolDefaultTrue }}
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes

View File

@@ -66,7 +66,7 @@ This file is autogenerated by warewulf
<name>wwnet0</name>
<link-type>ethernet</link-type>
<control>
<mode>manual</mode>
<mode>boot</mode>
</control>
<firewall/>
<link/>
@@ -101,7 +101,7 @@ This file is autogenerated by warewulf
<name>wwnet1</name>
<link-type>ethernet</link-type>
<control>
<mode>manual</mode>
<mode>boot</mode>
</control>
<firewall/>
<link/>

View File

@@ -18,7 +18,7 @@ This file is autogenerated by warewulf
<mtu>{{ $netdev.MTU }}</mtu>
{{- end }}
<control>
<mode>{{ if $netdev.OnBoot }}boot{{ else }}manual{{ end }}</mode>
<mode>{{ if $netdev.OnBoot.BoolDefaultTrue }}boot{{ else }}manual{{ end }}</mode>
</control>
<firewall/>
<link/>

View File

@@ -7,4 +7,4 @@ WWIPMI_NETMASK="{{$.Ipmi.Netmask}}"
WWIPMI_GATEWAY="{{$.Ipmi.Gateway}}"
WWIPMI_USER="{{$.Ipmi.UserName}}"
WWIPMI_PASSWORD="{{$.Ipmi.Password}}"
WWIPMI_WRITE="{{$.Ipmi.Write}}"
WWIPMI_WRITE="{{$.Ipmi.Write.Bool}}"