Skip to content

Instantly share code, notes, and snippets.

@avtar
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avtar/4d7e5a249f4b5cf7495b to your computer and use it in GitHub Desktop.
Save avtar/4d7e5a249f4b5cf7495b to your computer and use it in GitHub Desktop.
GPII test user account creation and Jenkins provisioning using cloudbase-init
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%create_gpiitestuser.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" -nodejsVersion """"0.10.33"""" ' -Verb RunAs}";
# At the time of writing, nodejsVersion = 0.10.33
Param (
[Parameter(Mandatory=$True)] [String]$nodejsVersion
)
$testUserName = "GPIITestUser"
$testUserPassword = "password"
# Create our test user
net user $testUserName $testUserPassword /add
net localgroup Administrators $testUserName /add
# Configure auto logon
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1 /f
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d $testUserName /f
reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d $testUserPassword /f
# Add the Chocolatey path system-wide
$oldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
$newPath=$oldPath+";C:\ProgramData\chocolatey\bin\;C:\ProgramData\chocolatey\lib\nodejs.commandline.${nodejsVersion}\tools\;C:\Program Files (x86)\Git\bin\"
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
# Install Cygwin and OpenSSH
setup-x86_64.exe -s http://mirrors.kernel.org/sourceware/cygwin/ -R C:\cygwin -a x86_64 -q -l C:\cygwin_packages -N -P openssh
In a Cygwin administrator terminal:
1. Run /usr/bin/ssh-host-config
* Should StrictModes be used?: no
* Should privilege separation be used?: no
* Do you want to install sshd as a service?: yes
* Enter the value of CYGWIN for the daemon: <blank>
* Do you want to use a different name?: yes
* Enter the new user name: GPIITestUser
* Please enter the password for user GPIITestUser:
2. Make sure the /etc/sshd_config file has the following options set:
* PubkeyAuthentication yes
* PasswordAuthentication no
3. Add the Jenkins SSH public key to ~GPIITestUser/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2ZjKoTzIDGmlsCvX+z/7E3mLXqjpd3xCdv9JcMM7WuPy07J5hXDUSwxHPhoOq8uhf9XXRnBdG25KK3zutWFXflemB9PuTqqUjypFWoyUxzPzZvca14UZHTkjKVLUkdLINn4aFyfPfvsu2viC29z8JwUOcmvnD+JPBev2BMdWPMZvq8oVzhNh8mm+gB0nQBOYAExVCWIbH5y1FJvh0qeB3eYLfGPwRNdpHgvaZeUpjoEKd8162sNt0vSmPiUoykiB9TnaPDG0+Y704y5MUkI7/TH9FCIod0BmgKPsJuMpgKj8Gtzc1w00igq0ZpO1kaQI+hnW2RuLw8Ke9vi9JqvUV
4. Allow SSH traffic through the Windows firewall
netsh advfirewall firewall add rule name="Cygwin SSHD" dir=in action=allow program="c:\cygwin\usr\sbin\sshd.exe" enable=yes
netsh advfirewall firewall add rule name="ssh" dir=in action=allow protocol=TCP localport=22
5. Disable all Cloudbase-init functionality except for what will allow us to overwrite the logon script. Make sure the 'C:\Program Files (x86)\Cloudbase Solutions\Cloudbase-Init\conf\cloudbase-init.conf' file contents resemble:
[DEFAULT]
username=Admin
groups=Administrators
inject_user_password=false
verbose=true
allow_reboot=false
plugins=cloudbaseinit.plugins.windows.userdata.UserDataPlugin
config_drive_raw_hhd=true
config_drive_cdrom=true
#ps1_sysnative
$JenkinsMasterUrl = "http://host:8080/"
$JenkinsSlaveName = "gpii-win-8.1"
$JenkinsJnlpCredentials = "jenkinsusername:password"
$testUserName = "GPIITestUser"
$logonScriptName = "OnLogon.bat"
# *** NOTE ***
# We are running the Jenkins Slave Agent with elevated privileges. This is a
# workaround to enable the tests to run with elevated privileges. Elevated
# privileges are required to kill the Magnifier process on Windows 8 when
# using taskkill.exe. See:
# http://issues.gpii.net/browse/GPII-899 and
# http://issues.gpii.net/browse/GPII-12
$logonScriptContents = @"
%HOMEDRIVE%
cd %HOMEPATH%
git clone -b chocolatey-npm-path https://github.com/avtar/gpii-automation
curl -O ${JenkinsMasterUrl}jnlpJars/slave.jar
powershell.exe -ExecutionPolicy RemoteSigned -File gpii-automation\gpii-win-8.1\StartElevated-JenkinsSlaveAgent.ps1 ${JenkinsMasterUrl}computer/${JenkinsSlaveName}/slave-agent.jnlp ${JenkinsJnlpCredentials}
pause
"@
# Overwrite the placeholder logon script with Jenkins info
Out-File -FilePath "C:\Users\${testUserName}\${logonScriptName}" -Encoding ASCII -InputObject $logonScriptContents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment