func validateAndDeriveInstance()

in cli_tools/gce_windows_upgrade/upgrader/validators.go [141:185]


func validateAndDeriveInstance(derivedVars *derivedVars, sourceOS, targetOS string) error {
	inst, err := computeClient.GetInstance(derivedVars.instanceProject, derivedVars.instanceZone, derivedVars.instanceName)
	if err != nil {
		return daisy.Errf("Failed to get instance: %v", err)
	}

	if len(inst.Disks) == 0 {
		return daisy.Errf("No disks attached to the instance.")
	}
	// Boot disk is always with index=0: https://cloud.google.com/compute/docs/reference/rest/v1/instances/attachDisk
	// "0 is reserved for the boot disk"
	bootDisk := inst.Disks[0]
	if err := validateAndDeriveOSDisk(bootDisk, derivedVars); err != nil {
		return err
	}
	if err := validateLicense(bootDisk, sourceOS, targetOS); err != nil {
		return err
	}

	// We need to launch upgrade by a startup script, whose URL is set by a metadata
	// 'windows-startup-script-url'.
	// If that metadata key has been used by the customer before the upgrade, we need
	// to backup it and restore after the upgrade finished. We backup it to metadata
	// 'windows-startup-script-url-backup'.
	// There are 3 possible scenarios:
	// 1. 'windows-startup-script-url' doesn't exist originally. Which means, the customer
	//    doesn't set it. In that case, we don't need to backup anything.
	// 2. 'windows-startup-script-url' exists. Which means, the customer set it for
	//    their purposes. We should backup it in order to restore from it when cleanup
	//    or rollback.
	// 3. 'windows-startup-script-url' exists, but 'windows-startup-script-url-backup'
	//    also exists. That means the customer tried to run upgrade before but got
	//    interrupted for some reason. In that case, 'windows-startup-script-url'
	//    must have been modified, so we should backup 'windows-startup-script-url-backup'
	//    instead.
	if inst.Metadata != nil && inst.Metadata.Items != nil {
		originalURL := getMetadataValue(inst.Metadata.Items, metadataWindowsStartupScriptURLBackup)
		if originalURL == nil {
			originalURL = getMetadataValue(inst.Metadata.Items, metadataWindowsStartupScriptURL)
		}
		derivedVars.originalWindowsStartupScriptURL = originalURL
	}

	return nil
}