Skip to content

Commit 490fd23

Browse files
authored
use local-hostname for meta-data and update windows logic (#192)
* use local-hostname for meta-data and update windows logic * fix grammar
1 parent 88a638a commit 490fd23

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

commands/create.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
541543
func 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

drivers/vmwarevsphere/cloudinit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func (d *Driver) createCloudInitIso() error {
155155
return err
156156
}
157157

158-
md := []byte(fmt.Sprintf("hostname: %s\n", d.MachineName))
158+
md := []byte(fmt.Sprintf("local-hostname: %s\n", d.MachineName))
159159
if err = ioutil.WriteFile(metadata, md, perm); err != nil {
160160
return err
161161
}
@@ -258,7 +258,7 @@ func (d *Driver) addSSHUserToYaml(sshkey string) (string, error) {
258258

259259
switch d.OS {
260260
default:
261-
log.Debug("Adding linux ssh user to cloud-init")
261+
log.Debug("[addSSHUserToYaml] Adding linux ssh user to cloud-init")
262262
// implements https://github.com/canonical/cloud-init/blob/master/cloudinit/config/cc_users_groups.py#L28-L71
263263
// technically not in the spec, see this code for context
264264
// https://github.com/canonical/cloud-init/blob/master/cloudinit/distros/__init__.py#L394-L397
@@ -267,11 +267,11 @@ func (d *Driver) addSSHUserToYaml(sshkey string) (string, error) {
267267
commonUser["no_user_group"] = true
268268

269269
// Administrator is the default ssh user on Windows Server 2019/2022
270-
// This implements cloudbase-init for Windows VMs as cloudinit doesn't support Windows
270+
// This implements cloudbase-init for Windows VMs as cloud-init doesn't support Windows
271271
// https://cloudbase-init.readthedocs.io/en/latest/
272272
// On Windows, primary_group and groups are concatenated.
273273
case WindowsMachineOS:
274-
log.Debug("Adding windows ssh user to cloud-init")
274+
log.Debug("[addSSHUserToYaml] Adding windows ssh user to cloud-init")
275275
commonUser["inactive"] = false
276276
}
277277

0 commit comments

Comments
 (0)