cloudinit-windows/main.go
2024-08-04 00:13:19 +02:00

115 lines
3.6 KiB
Go

package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"jonasled.dev/jonasled/cloud-init-windows/cloudinit"
"jonasled.dev/jonasled/cloud-init-windows/config"
"jonasled.dev/jonasled/cloud-init-windows/log"
"jonasled.dev/jonasled/cloud-init-windows/windows"
)
func main() {
if len(os.Args) > 1 && os.Args[1] == "firstboot" {
fmt.Println("Firstboot windows configuration, please wait one moment")
}
log.Init()
log.Log.Info("Running Cloud-Init for Windows")
windows.SetwindowTitle("Cloud-Init")
config.ParseConfig()
// if the applications runs as service, it should never exit
if len(os.Args) > 1 && os.Args[1] == "service" {
go application()
for {
}
} else {
application()
}
}
func application() {
rebootRequired := false
var cloudInitConfig cloudinit.CloudInit
err := cloudInitConfig.Load()
if err != nil {
log.Log.Fatal("Failed reading Cloud-Init configuration ", err.Error())
}
log.Log.Info("Updating Hostname")
if windows.GetHostname() != cloudInitConfig.UserData.Hostname && config.Configuration.UpdateHostname {
log.Log.Debug("New Hostname: ", cloudInitConfig.UserData.Hostname)
windows.SetHostname(cloudInitConfig.UserData.Hostname)
rebootRequired = true
} else if config.Configuration.UpdateHostname {
log.Log.Debug("Hostname hsn't been changed")
}
if config.Configuration.UpdateNetworkConfig {
log.Log.Info("Configuring Network Interfaces")
dnsServers4 := cloudInitConfig.NetworkConfig.GetNameserversIpv4()
for _, networkconfig := range cloudInitConfig.NetworkConfig.Config {
if networkconfig.Type != "physical" {
continue
}
networkInterface := windows.GetNetworkInterfaceByMac(networkconfig.MacAddress)
log.Log.Info("Setting IP address for Interface " + networkInterface.Interfacealias)
for _, subnet := range networkconfig.Subnets {
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, dnsServers4)
case "dhcp4":
log.Log.Debug("Setting IPv4 address to dhcp")
windows.SetNetworkAdapterPropertiesDhcp(networkInterface.Name)
case "dhcp6":
log.Log.Debug("Setting IPv6 address to dhcp")
windows.SetNetworkAdapterPropertiesDhcp6(networkInterface.IfIndex)
case "static6":
log.Log.Debug("Setting IPv6 address static to " + subnet.Address)
ipParts := strings.Split(subnet.Address, "/")
prefixLen, _ := strconv.Atoi(ipParts[1])
windows.SetNetworkAdapterPropertiesStatic6(networkInterface.IfIndex, ipParts[0], prefixLen, subnet.Gateway)
}
}
}
}
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")
password, err := cloudInitConfig.UserData.GetAdminPassword()
if err != nil {
log.Log.Error("Failed fetching admin password from API ", err.Error())
} else {
windows.SetUserPassword("Administrator", password)
}
}
if config.Configuration.ResizeDisk {
err := windows.ResizeDisk("C")
if err != nil {
log.Log.Error("Failed resizing disk", err.Error())
}
}
if rebootRequired {
log.Log.Warn("Reboot is required to apply the changes")
if len(os.Args) > 1 && os.Args[1] == "firstboot" {
os.Exit(1) // Exit 1 means, reboot the system and then continue https://learn.microsoft.com/en-us/windows-hardware/customize/desktop/unattend/microsoft-windows-deployment-runsynchronous-runsynchronouscommand-willreboot
} else if config.Configuration.RebootIfRequired {
windows.Reboot()
}
}
log.Log.Info("Done executing Cloud-Init")
os.Exit(0)
}