Last active
May 12, 2017 14:53
-
-
Save lonniev/350a444c626119c31208 to your computer and use it in GitHub Desktop.
(Tries to) Add Vagrant interfaces to the Softlayer Windows instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$RunningAsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") | |
if ($RunningAsAdmin) | |
{ | |
$code = { | |
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) | |
Write-Output "Chocolatey apparently installed." | |
Write-Output "Creating Vagrant User" | |
net user vagrant "vagrant" /add | |
net localgroup administrators /add vagrant | |
# setup openssh | |
Write-Host "Installing OpenSSH for a 64 bit OS" | |
$ssh_download_url = "http://www.mls-software.com/files/setupssh-6.6p1-1(x64).exe" | |
if (!(Test-Path "C:\Program Files\OpenSSH\bin\ssh.exe")) { | |
Write-Host "Downloading $ssh_download_url" | |
(New-Object System.Net.WebClient).DownloadFile($ssh_download_url, "C:\Windows\Temp\openssh.exe") | |
Start-Process "C:\Windows\Temp\openssh.exe" "/S /port=22 /privsep=1 /password=D@rj33l1ng" -NoNewWindow -Wait | |
} | |
Stop-Service "OpenSSHd" -Force | |
# ensure vagrant can log in | |
Write-Host "Setting vagrant user file permissions" | |
New-Item -ItemType Directory -Force -Path "C:\Users\vagrant\.ssh" | |
C:\Windows\System32\icacls.exe "C:\Users\vagrant" /grant "vagrant:(OI)(CI)F" | |
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\bin" /grant "vagrant:(OI)RX" | |
C:\Windows\System32\icacls.exe "C:\Program Files\OpenSSH\usr\sbin" /grant "vagrant:(OI)RX" | |
Write-Host "Setting SSH home directories" | |
(Get-Content "C:\Program Files\OpenSSH\etc\passwd") | | |
Foreach-Object { $_ -replace '/home/(\w+)', '/cygdrive/c/Users/$1' } | | |
Set-Content 'C:\Program Files\OpenSSH\etc\passwd' | |
# Set shell to /bin/sh to return exit status | |
$passwd_file = Get-Content 'C:\Program Files\OpenSSH\etc\passwd' | |
$passwd_file = $passwd_file -replace '/bin/bash', '/bin/sh' | |
Set-Content 'C:\Program Files\OpenSSH\etc\passwd' $passwd_file | |
# fix opensshd to not be strict | |
Write-Host "Setting OpenSSH to be non-strict" | |
$sshd_config = Get-Content "C:\Program Files\OpenSSH\etc\sshd_config" | |
$sshd_config = $sshd_config -replace 'StrictModes yes', 'StrictModes no' | |
$sshd_config = $sshd_config -replace '#PubkeyAuthentication yes', 'PubkeyAuthentication yes' | |
$sshd_config = $sshd_config -replace '#PermitUserEnvironment no', 'PermitUserEnvironment yes' | |
# disable the use of DNS to speed up the time it takes to establish a connection | |
$sshd_config = $sshd_config -replace '#UseDNS yes', 'UseDNS no' | |
# disable the login banner | |
$sshd_config = $sshd_config -replace 'Banner /etc/banner.txt', '#Banner /etc/banner.txt' | |
Set-Content "C:\Program Files\OpenSSH\etc\sshd_config" $sshd_config | |
# use c:\Windows\Temp as /tmp location | |
Write-Host "Setting temp directory location" | |
New-Item -ItemType Directory -Force -Path "C:\Windows\Temp" | |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue "C:\Program Files\OpenSSH\tmp" | |
C:\Program` Files\OpenSSH\bin\junction.exe /accepteula "C:\Program Files\OpenSSH\tmp" "C:\Windows\Temp" | |
C:\Windows\System32\icacls.exe "C:\Windows\Temp" /grant "vagrant:(OI)(CI)F" | |
# add 64 bit environment variables missing from SSH | |
Write-Host "Setting SSH environment" | |
$sshenv = "TEMP=C:\Windows\Temp" | |
$env_vars = "ProgramFiles(x86)=C:\Program Files (x86)", ` | |
"ProgramW6432=C:\Program Files", ` | |
"CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files", ` | |
"CommonProgramW6432=C:\Program Files\Common Files" | |
$sshenv = $sshenv + "`r`n" + ($env_vars -join "`r`n") | |
Set-Content C:\Users\vagrant\.ssh\environment $sshenv | |
# record the path for provisioners (without the newline) | |
Write-Host "Recording PATH for provisioners" | |
Set-Content C:\Windows\Temp\PATH ([byte[]][char[]] $env:PATH) -Encoding Byte | |
# configure firewall | |
Write-Host "Configuring firewall" | |
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow service=OpenSSHd enable=yes | |
netsh advfirewall firewall add rule name="SSHD" dir=in action=allow program="C:\Program Files\OpenSSH\usr\sbin\sshd.exe" enable=yes | |
netsh advfirewall firewall add rule name="ssh" dir=in action=allow protocol=TCP localport=22 | |
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub', 'C:\Users\vagrant\.ssh\authorized_keys')" | |
Write-Host "Starting the OpenSSHd Service" | |
Start-Service "OpenSSHd" | |
pushd C:\Windows\Temp | |
& 'c:\ProgramData\chocolatey\bin\choco.exe' install -fy 7zip | |
& 'c:\ProgramData\chocolatey\bin\choco.exe' install -fy notepadplusplus | |
Write-Host "Downloading and Installing rsync" | |
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('http://mirrors.kernel.org/sourceware/cygwin/x86_64/release/rsync/rsync-3.1.0-1.tar.xz', 'C:\Windows\Temp\rsync-3.1.0-1.tar.xz')" | |
& 'C:\Program Files\7-Zip\7z.exe' x -y rsync-3.1.0-1.tar.xz | |
& 'C:\Program Files\7-Zip\7z.exe' x -y rsync-3.1.0-1.tar | |
copy-item -Force usr\bin\rsync.exe 'C:\Program Files\OpenSSH\bin\rsync.exe' | |
Remove-Item -Force -Recurse usr | |
Remove-Item rsync-3.1.0-1.tar | |
popd | |
Write-Host "Creating the symlink for the rsynced share of vagrant files from the host into the guest" | |
# make symlink for c:/vagrant share | |
Invoke-Expression -Command "cmd /c mklink /D 'C:\Program Files\OpenSSH\vagrant' 'C:\vagrant'" | |
# make symlink for c:/tmp share | |
Invoke-Expression -Command "cmd /c mklink /D 'C:\tmp' 'C:\Windows\Temp'" | |
# turn on the NFS Service for Windows because of pathname problems with rsync | |
Write-Host "Installing and Enabling NFS Server for Windows" | |
Import-Module ServerManager | |
Add-WindowsFeature FS-NFS-Services | |
Import-Module NFS | |
Write-Host "Enabling WinRM for Vagrant" | |
C:\ProgramData\chocolatey\bin\choco install -f -y vagrant-winrm-config | |
Write-Host "Done with Post-Install Scripting." | |
} | |
# (C) 2012 Dr. Tobias Weltner | |
# you may freely use this code for commercial or non-commercial purposes at your own risk | |
# as long as you credit its original author and keep this comment block. | |
# For PowerShell training or PowerShell support, feel free to contact [email protected] | |
#Addition modification by Boe Prox to show the use of PSJobs and its performance | |
Write-Host "Starting Provisioning in Separate RunScope..." | |
$start = Get-Date | |
$newPowerShell = [PowerShell]::Create().AddScript($code) | |
$job = $newPowerShell.BeginInvoke() | |
While (-Not $job.IsCompleted) {} | |
$completed = Get-Date | |
$result = $newPowerShell.EndInvoke($job) | |
$newPowerShell.Dispose() | |
$timeToRunCommand = ($completed - $start).TotalSeconds | |
Write-Host "Captured Output:" | |
$result | |
Write-Host "---" | |
Write-Host "Time when Started: " $start | |
Write-Host "Time to run code: " $timeToRunCommand "seconds" | |
Write-Host "Time when Finished: " $completed | |
} | |
else | |
{ | |
Write-Warning "Must be executed in Administrator level shell." | |
Write-Warning "Script Cancelled!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment