@@ -536,16 +536,31 @@ func updateUserdataFile(driverOpts *rpcdriver.RPCFlags, machineName, userdataFla
536536 return nil
537537}
538538
539- // writeCloudConfig sets custom install script path to the runcmd directive
539+ // writeCloudConfig sets the custom install script path for the runcmd directive
540540// and passes the script path to commonCloudConfig
541+ // RK - sets the hostname based on OS as cloud-init (linux) and cloudbase-init (windows) diverge
542+ // on how hostnames are set in cloud-config (userdata)
541543func writeCloudConfig (machineName , encodedData , machineOS string , cf map [interface {}]interface {}, newUserDataFile * os.File ) error {
542544 command := "sh"
543545 path := "/usr/local/custom_script/install.sh"
544546 if strings .Contains (machineOS , "windows" ) {
545547 // the writeFile path should ideally be C:\usr\local\custom_script\install.ps1
546- // but we can't guarantee that directory exists or can be created on the target machine
548+ // however, we can't guarantee that directory exists or can be created on the target machine
547549 command = "powershell"
548550 path = "C:\\ install.ps1"
551+ // set_hostname is a cloudbase-init specific cloud-config parameter
552+ // that is only pertinent for windows VMs
553+ // https://cloudbase-init.readthedocs.io/en/latest/userdata.html
554+ if err := addToCloudConfig (cf , "set_hostname" , machineName ); err != nil {
555+ log .Debugf ("[writeCloudConfig] wrote set_hostname field for %s machine %s" , machineName , machineOS )
556+ return err
557+ }
558+ } else {
559+ // Add the hostname
560+ if _ , ok := cf ["hostname" ]; ! ok {
561+ cf ["hostname" ] = machineName
562+ log .Debugf ("[writeCloudConfig] wrote hostname field for %s machine %s" , machineName , machineOS )
563+ }
549564 }
550565 return commonCloudConfig (machineName , machineOS , encodedData , command , path , cf , newUserDataFile )
551566}
@@ -570,15 +585,6 @@ func commonCloudConfig(machineName, machineOS, encodedData, command, path string
570585 return err
571586 }
572587
573- // Add the hostname
574- if _ , ok := cf ["hostname" ]; ! ok {
575- cf ["hostname" ] = machineName
576- }
577- if err := addToCloudConfig (cf , "set_hostname" , machineName ); err != nil {
578- log .Debugf ("writeCloudConfig: wrote set_hostname field for %s machine %s" , machineName , machineOS )
579- return err
580- }
581-
582588 userdataContent , err := yaml .Marshal (cf )
583589 if err != nil {
584590 return err
0 commit comments