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 - Don't use DHCP for interfaces attached to a bond. #1743
- Wait until ignition has completed before trying to mount. - Wait until ignition has completed before trying to mount.
- Fix timeout problem for wwclient. #1741 - Fix timeout problem for wwclient. #1741
- Fixed default "true" state of NetDev.OnBoot. #1754
### Removed ### Removed

View File

@@ -7,7 +7,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/warewulf/warewulf/internal/pkg/util"
"github.com/warewulf/warewulf/internal/pkg/wwtype" "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) { 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 return "", nil
} }
switch valType { switch valType {

View File

@@ -1,5 +1,15 @@
package wwtype package wwtype
import (
"strings"
"github.com/warewulf/warewulf/internal/pkg/util"
)
func GetUnsetVerbs() []string { 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" "fmt"
"strconv" "strconv"
"strings" "strings"
"github.com/warewulf/warewulf/internal/pkg/util"
) )
// Simple string which can be converted to bool. Backend storage // Simple string which can be converted to bool. Backend storage
@@ -16,17 +14,38 @@ type WWbool string
Transform the underlying string value to bool Transform the underlying string value to bool
*/ */
func (val WWbool) Bool() bool { func (val WWbool) Bool() bool {
str := string(val) str := strings.ToLower(string(val))
if util.InSlice(GetUnsetVerbs(), str) { if IsUnsetVerb(str) {
return false 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 return true
} }
if strings.ToLower(str) == "no" {
return false
}
bval, _ := strconv.ParseBool(str)
return bval return bval
} }
@@ -34,7 +53,7 @@ func (val WWbool) Bool() bool {
Set the string, only accept bool values like true, false, but also UNDEF Set the string, only accept bool values like true, false, but also UNDEF
*/ */
func (val *WWbool) Set(str string) error { 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 // run the unset verb trough, will be filtered out later
*val = WWbool(str) *val = WWbool(str)
return nil return nil

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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