func addFrameworkMetadata()

in integration_test/gce-testing-internal/gce/gce_testing.go [1066:1130]


func addFrameworkMetadata(imageSpec string, inputMetadata map[string]string) (map[string]string, error) {
	metadataCopy := make(map[string]string)

	// Set serial-port-logging-enable to true by default to help diagnose startup
	// issues. inputMetadata can override this setting.
	metadataCopy["serial-port-logging-enable"] = "true"

	for k, v := range inputMetadata {
		metadataCopy[k] = v
	}

	if _, ok := metadataCopy["enable-oslogin"]; ok {
		return nil, errors.New("the 'enable-oslogin' metadata key is reserved for framework use")
	}
	// We manage our own ssh keys, so we don't need OS Login. For a while, it
	// worked to leave it enabled anyway, but one day that broke (b/181867249).
	// Disabling OS Login fixed the issue.
	metadataCopy["enable-oslogin"] = "false"

	if _, ok := metadataCopy["ssh-keys"]; ok {
		return nil, errors.New("the 'ssh-keys' metadata key is reserved for framework use")
	}
	publicKey, err := os.ReadFile(publicKeyFile)
	if err != nil {
		return nil, fmt.Errorf("could not read local public key file %v: %v", publicKeyFile, err)
	}
	metadataCopy["ssh-keys"] = fmt.Sprintf("%s:%s", sshUserName, string(publicKey))

	if IsWindows(imageSpec) {
		// From https://cloud.google.com/compute/docs/connect/windows-ssh#create_vm
		if _, ok := metadataCopy["sysprep-specialize-script-cmd"]; ok {
			return nil, errors.New("you cannot pass a sysprep script for Windows instances because they are needed to enable ssh-ing. Instead, wait for the instance to be ready and then run things with RunRemotely() or RunScriptRemotely()")
		}
		metadataCopy["sysprep-specialize-script-cmd"] = "googet -noconfirm=true install google-compute-engine-ssh"

		if _, ok := metadataCopy["enable-windows-ssh"]; ok {
			return nil, errors.New("the 'enable-windows-ssh' metadata key is reserved for framework use")
		}
		metadataCopy["enable-windows-ssh"] = "TRUE"
	} else {
		if _, ok := metadataCopy["startup-script"]; ok {
			return nil, errors.New("the 'startup-script' metadata key is reserved for future use. Instead, wait for the instance to be ready and then run things with RunRemotely() or RunScriptRemotely()")
		}
		// TODO(b/380470389): we actually *can't* do RunRemotely() on DLVM images due to a bug.
		// The workaround for the bug is to deploy a fix in-VM via startup scripts.
		if strings.Contains(imageSpec, "common-gpu-debian-11-py310") {
			metadataCopy["startup-script"] = fmt.Sprintf(`
#!/bin/bash
# Give time for the guest agent and jupyter stuff to finish modifying
# /etc/passwd and test_user home directory
sleep 120
HOMEDIR=/home/%[1]s
SSHFILE=$HOMEDIR/.ssh/authorized_keys
if [ ! -f "$SSHFILE" ]; then
  sudo mkdir -p "$HOMEDIR/.ssh"
  sudo touch "$SSHFILE"
fi
sudo chown -R %[1]s:%[1]s "$HOMEDIR"
sudo chmod 600 "$SSHFILE"`,
				sshUserName,
			)
		}
	}
	return metadataCopy, nil
}