forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisionvm.ps1
105 lines (81 loc) · 4.12 KB
/
provisionvm.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
Param([string]$serverAddress, [string]$vmName, [string]$userName, [string]$apiKey)
# ---------------------------------------------------
# Script used to provision Windows Server 2012 VMs
# for .NET sample testing.
# Originally referenced here: https://gist.github.com/snallami/5aa9ea2c57836a3b3635
# Modified by Den Delimarsky (dendeli)
# ---------------------------------------------------
Set-ExecutionPolicy Unrestricted
$logFile = "C:\provisionlog.txt"
$ProvisionArtifacts = "C:\prstack"
Function LogWrite
{
Param ([string]$logString)
Write-Host $logString
Add-content $logFile -value $logString
}
LogWrite "Bootrstapping provisioning..."
LogWrite "Server URL: $serverAddress"
LogWrite "VM: $vmName"
# Install Chocolatey, and subsequently - GIT tools
# This will do a silent install and by far one that will cause the least headache.
LogWrite "Installing Chocolatey..."
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) | Out-Null
LogWrite "Installing Git tools..."
choco install git -params '"/GitAndUnixToolsOnPath"' -y --force | Out-Null
# Set the environment variable to containt the path to Git tools (somehow, this is missed within the current context)
$env:Path = [Environment]::GetEnvironmentVariable('Path',[System.EnvironmentVariableTarget]::Machine) + ";C:\Program Files\Git\cmd"
LogWrite "Git tools installed. Current PATH is:"
LogWrite $env:Path
# Install VCREDIST
LogWrite "Installing Visual C++ Redistributable for Visual Studio 2015 (x64)..."
Invoke-WebRequest "https://download.microsoft.com/download/9/3/F/93FCF1E7-E6A4-478B-96E7-D4B285925B00/vc_redist.x64.exe" -OutFile "$ProvisionArtifacts\vcredist_x64.exe"
Start-Process $ProvisionArtifacts\vcredist_x64.exe -ArgumentList '/q' -NoNewWindow -Wait
# Install .NET Core
LogWrite "Installing .NET Core..."
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview2/scripts/obtain/dotnet-install.ps1" -OutFile "./dotnet-install.ps1"
./dotnet-install.ps1 -Version 1.0.0-preview2-003121 -InstallDir "C:\dotnet"
$env:Path += ";c:\dotnet"
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\dotnet", [EnvironmentVariableTarget]::Machine)
LogWrite ".NET Core installed. Current PATH is:"
LogWrite $env:Path
# Download the Zulu ZIP file to a location within the VM
# and extract it. This is our Java runtime.
LogWrite "Downloading Zulu SDK..."
$source = "http://cdn.azul.com/zulu/bin/zulu8.15.0.1-jdk8.0.92-win_x64.zip?jenkins"
$destination = "$ProvisionArtifacts\zuluJDK.zip"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($source, $destination)
LogWrite "Unzipping Zulu SDK..."
$shell_app=new-object -com shell.application
$zip_file = $shell_app.namespace($destination)
mkdir c:\java
$destination = $shell_app.namespace("c:\java")
$destination.Copyhere($zip_file.items())
LogWrite "Successfully downloaded and extracted Zulu SDK."
# Downloading Jenkins slave JAR
LogWrite "Downloading Jenkins slave JAR..."
$slaveSource = $serverAddress + "jnlpJars/slave.jar"
$destSource = "c:\java\slave.jar"
LogWrite "Slave source: $slaveSource"
LogWrite "Destination: $destSource"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($slaveSource, $destSource)
# Run Jenkins slave on the Windows host.
LogWrite "Running Jenkins slave process..."
$java="c:\java\zulu8.15.0.1-jdk8.0.92-win_x64\bin\java.exe"
$jar="-jar"
$jnlpUrl="-jnlpUrl"
$serverURL=$serverAddress+"computer/" + $vmName + "/slave-agent.jnlp"
$jnlpCredentialsFlag="-jnlpCredentials"
$credentials="$userName`:$apiKey"
LogWrite "Will execute this:"
$executedCommand = "$java $jar $destSource $jnlpCredentialsFlag $credentials $jnlpUrl $serverURL }"
LogWrite $executedCommand
Invoke-WebRequest "https://raw.githubusercontent.com/dotnet/docs/master/ci-scripts/agentman.ps1" -OutFile "$ProvisionArtifacts\agentman.ps1"
$executedCommand | Out-File -Encoding "UTF8" $ProvisionArtifacts\agentman.ps1 -append
# SchTasks /Create /SC MINUTE /MO 1 /TN "Jenkins Agent Inspector" /TR "powershell.exe -File $ProvisionArtifacts\agentman.ps1 -WindowStyle Hidden"
while ($true)
{
& $java $jar $destSource $jnlpCredentialsFlag $credentials $jnlpUrl $serverURL | Out-Null
}