fix configuring ip address if only one interface is set
This commit is contained in:
parent
b4b7458cd0
commit
7506bc6d0e
4 changed files with 48 additions and 9 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
config.json
|
||||
config.json
|
||||
*.exe
|
||||
|
|
@ -2,6 +2,7 @@ package cloudinit
|
|||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
|
@ -41,11 +42,38 @@ func (c *NetworkConfig) Load(cloudInitDrive string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *NetworkConfig) GetNameservers() []string {
|
||||
func (c *NetworkConfig) GetNameserversIpv4() []string {
|
||||
ipv4Pattern := `^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$`
|
||||
ipv4Regex := regexp.MustCompile(ipv4Pattern)
|
||||
|
||||
nameserver := []string{}
|
||||
|
||||
for _, config := range c.Config {
|
||||
if config.Type == "nameserver" {
|
||||
return config.Address
|
||||
for _, addr := range config.Address {
|
||||
if ipv4Regex.MatchString(addr) {
|
||||
nameserver = append(nameserver, addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return []string{}
|
||||
return nameserver
|
||||
}
|
||||
|
||||
func (c *NetworkConfig) GetNameserversIpv6() []string {
|
||||
ipv6Pattern := `^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$`
|
||||
ipv6Regex := regexp.MustCompile(ipv6Pattern)
|
||||
|
||||
nameserver := []string{}
|
||||
|
||||
for _, config := range c.Config {
|
||||
if config.Type == "nameserver" {
|
||||
for _, addr := range config.Address {
|
||||
if ipv6Regex.MatchString(addr) {
|
||||
nameserver = append(nameserver, addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nameserver
|
||||
}
|
||||
|
|
|
|||
7
main.go
7
main.go
|
|
@ -5,6 +5,7 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"jonasled.dev/jonasled/cloud-init-windows/cloudinit"
|
||||
"jonasled.dev/jonasled/cloud-init-windows/config"
|
||||
|
|
@ -51,7 +52,7 @@ func application() {
|
|||
|
||||
if config.Configuration.UpdateNetworkConfig {
|
||||
log.Log.Info("Configuring Network Interfaces")
|
||||
dnsServers := cloudInitConfig.NetworkConfig.GetNameservers()
|
||||
dnsServers4 := cloudInitConfig.NetworkConfig.GetNameserversIpv4()
|
||||
for _, networkconfig := range cloudInitConfig.NetworkConfig.Config {
|
||||
if networkconfig.Type != "physical" {
|
||||
continue
|
||||
|
|
@ -62,7 +63,7 @@ func application() {
|
|||
switch subnet.Type {
|
||||
case "static":
|
||||
log.Log.Debug("Setting IPv4 address static to " + subnet.Address)
|
||||
windows.SetNetworkAdapterPropertiesStatic(networkInterface.Name, subnet.Address, subnet.Netmask, subnet.Gateway, dnsServers)
|
||||
windows.SetNetworkAdapterPropertiesStatic(networkInterface.Name, subnet.Address, subnet.Netmask, subnet.Gateway, dnsServers4)
|
||||
case "dhcp4":
|
||||
log.Log.Debug("Setting IPv4 address to dhcp")
|
||||
windows.SetNetworkAdapterPropertiesDhcp(networkInterface.Name)
|
||||
|
|
@ -79,6 +80,8 @@ func application() {
|
|||
}
|
||||
}
|
||||
}
|
||||
log.Log.Info("Waiting 5s for network config to apply")
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
if config.Configuration.UpdatePassword {
|
||||
log.Log.Info("Changing Password for Administrator user")
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"jonasled.dev/jonasled/cloud-init-windows/log"
|
||||
)
|
||||
|
|
@ -46,9 +47,13 @@ func GetAllNetworkInterfaces() ([]NetworkInterface, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stdoutStr := stdout.String()
|
||||
// if we have only one network interface powershell outputs them as signle object
|
||||
if strings.HasPrefix(stdoutStr, "{") {
|
||||
stdoutStr = "[" + stdoutStr + "]"
|
||||
}
|
||||
var adapters []NetworkInterface
|
||||
err = json.Unmarshal([]byte(stdout.String()), &adapters)
|
||||
err = json.Unmarshal([]byte(stdoutStr), &adapters)
|
||||
if err != nil {
|
||||
log.Log.Error("Failed unmarshaling JSON: ", err.Error())
|
||||
return nil, err
|
||||
|
|
@ -76,10 +81,12 @@ func GetNetworkInterfaceByMac(macAddress string) NetworkInterface {
|
|||
func SetNetworkAdapterPropertiesStatic(interfaceName string, ipAddress string, subnetmask string, gateway string, dnsServer []string) {
|
||||
dnsServers := strings.Join(dnsServer[:], ",")
|
||||
|
||||
psCommandIP := fmt.Sprintf("netsh interface ipv4 set address name=\"%s\" address=%s mask=%s gateway=%s", interfaceName, ipAddress, subnetmask, gateway)
|
||||
psCommandIP := fmt.Sprintf("netsh interface ipv4 set address name=\"%s\" static %s %s %s", interfaceName, ipAddress, subnetmask, gateway)
|
||||
psCommandDNS := fmt.Sprintf("netsh interface ipv4 set dns name=\"%s\" static %s", interfaceName, dnsServers)
|
||||
|
||||
execPsCommand(psCommandIP)
|
||||
log.Log.Info("Waiting 5s for network config to apply")
|
||||
time.Sleep(5 * time.Second)
|
||||
execPsCommand(psCommandDNS)
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue