lib/beaker/hypervisor/google_compute.rb in beaker-google-0.4.0 vs lib/beaker/hypervisor/google_compute.rb in beaker-google-0.5.0

- old
+ new

@@ -160,51 +160,71 @@ raise "Unable to create Google Compute Instance #{host.name}: [#{operation.error.errors[0].code}] #{operation.error.errors[0].message}" end @logger.debug("Created Google Compute instance for #{host.name}: #{host['vmhostname']}") instance = @gce_helper.get_instance(host['vmhostname']) + # Make sure we have a non root/Adminsitor user to log in as + if host['user'] == "root" || host['user'] == "Administrator" || host['user'].empty? + initial_user = 'google_compute' + else + initial_user = host['user'] + end + # add metadata to instance, if there is any to set # mdata = format_metadata # TODO: Set a configuration option for this to allow disabeling oslogin mdata = [ { key: 'ssh-keys', - value: "google_compute:#{File.read(find_google_ssh_public_key).strip}" + value: "#{initial_user}:#{File.read(find_google_ssh_public_key).strip}" }, # For now oslogin needs to be disabled as there's no way to log in as root and it would # take too much work on beaker to add sudo support to everything { key: 'enable-oslogin', value: 'FALSE' }, ] - next if mdata.empty? - # Add the metadata to the host - @gce_helper.set_metadata_on_instance(host['vmhostname'], mdata) - @logger.debug("Added tags to Google Compute instance #{host.name}: #{host['vmhostname']}") + + # Check for google's default windows images and turn on ssh if found + if image_project == "windows-cloud" || image_project == "windows-sql-cloud" + # Turn on SSH on GCP's default windows images + mdata << { + key: 'enable-windows-ssh', + value: 'TRUE', + } + mdata << { + key: 'sysprep-specialize-script-cmd', + value: 'googet -noconfirm=true update && googet -noconfirm=true install google-compute-engine-ssh', + } + # Some versions of windows don't seem to add the OpenSSH directory to the path which prevents scp from working + mdata << { + key: 'sysprep-specialize-script-ps1', + value: '[Environment]::SetEnvironmentVariable( "PATH", "$ENV:PATH;C:\Program Files\OpenSSH", [EnvironmentVariableTarget]::Machine )', + } + end + unless mdata.empty? + # Add the metadata to the host + @gce_helper.set_metadata_on_instance(host['vmhostname'], mdata) + @logger.debug("Added tags to Google Compute instance #{host.name}: #{host['vmhostname']}") + end host['ip'] = instance.network_interfaces[0].access_configs[0].nat_ip # Add the new host to the firewall @gce_helper.add_firewall_tag(@firewall, host['vmhostname']) if host['disable_root_ssh'] == true @logger.info('Not enabling root ssh as disable_root_ssh is true') else - - # # configure ssh - default_user = host['user'] - - # TODO: Pull this out into a configuration option or something - host['user'] = 'google_compute' - + real_user = host['user'] + host['user'] = initial_user # Set the ssh private key we need to use host.options['ssh']['keys'] = [find_google_ssh_private_key] copy_ssh_to_root(host, @options) enable_root_login(host, @options) - host['user'] = default_user - + host['user'] = real_user # shut down connection, will reconnect on next exec host.close end @logger.debug("Instance ready: #{host['vmhostname']} for #{host.name}}")