forked from dsccommunity/DscWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03 Full Lab with DSC and TFS.ps1
271 lines (220 loc) · 13.9 KB
/
03 Full Lab with DSC and TFS.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
$labName = 'psconf18'
#region Lab setup
#--------------------------------------------------------------------------------------------------------------------
#----------------------- CHANGING ANYTHING BEYOND THIS LINE SHOULD NOT BE REQUIRED ----------------------------------
#----------------------- + EXCEPT FOR THE LINES STARTING WITH: REMOVE THE COMMENT TO --------------------------------
#----------------------- + EXCEPT FOR THE LINES CONTAINING A PATH TO AN ISO OR APP --------------------------------
#--------------------------------------------------------------------------------------------------------------------
#create an empty lab template and define where the lab XML files and the VMs will be stored
New-LabDefinition -Name $labName -DefaultVirtualizationEngine HyperV
#make the network definition
Add-LabVirtualNetworkDefinition -Name $labName -AddressSpace 192.168.111.0/24
Add-LabVirtualNetworkDefinition -Name External -HyperVProperties @{ SwitchType = 'External'; AdapterName = 'Wi-Fi' }
#and the domain definition with the domain admin account
Add-LabDomainDefinition -Name contoso.com -AdminUser Install -AdminPassword Somepass1
#these credentials are used for connecting to the machines. As this is a lab we use clear-text passwords
Set-LabInstallationCredential -Username Install -Password Somepass1
# Add the reference to our necessary ISO files
Add-LabIsoImageDefinition -Name Tfs2018 -Path $labsources\ISOs\tfsserver2018.2_rc1.iso
Add-LabIsoImageDefinition -Name SQLServer2017 -Path $labsources\ISOs\SQLServer2017-x64-ENU.iso
#defining default parameter values, as these ones are the same for all the machines
$PSDefaultParameterValues = @{
'Add-LabMachineDefinition:Network' = $labName
'Add-LabMachineDefinition:ToolsPath' = "$labSources\Tools"
'Add-LabMachineDefinition:DomainName' = 'contoso.com'
'Add-LabMachineDefinition:DnsServer1' = '192.168.111.10'
'Add-LabMachineDefinition:OperatingSystem' = 'Windows Server 2016 Datacenter Evaluation (Desktop Experience)'
'Add-LabMachineDefinition:Gateway' = '192.168.111.50'
}
#The PostInstallationActivity is just creating some users
$postInstallActivity = @()
$postInstallActivity += Get-LabPostInstallationActivity -ScriptFileName 'New-ADLabAccounts 2.0.ps1' -DependencyFolder $labSources\PostInstallationActivities\PrepareFirstChildDomain
$postInstallActivity += Get-LabPostInstallationActivity -ScriptFileName PrepareRootDomain.ps1 -DependencyFolder $labSources\PostInstallationActivities\PrepareRootDomain
Add-LabMachineDefinition -Name DSCDC01 -Memory 512MB -Roles RootDC -IpAddress 192.168.111.10 -PostInstallationActivity $postInstallActivity
#file server and router
$netAdapter = @()
$netAdapter += New-LabNetworkAdapterDefinition -VirtualSwitch $labName -Ipv4Address 192.168.111.50
$netAdapter += New-LabNetworkAdapterDefinition -VirtualSwitch External -UseDhcp
# The good, the bad and the ugly
Add-LabMachineDefinition -Name DSCCASQL01 -Memory 3GB -Roles CaRoot, SQLServer2017, Routing -NetworkAdapter $netAdapter
# DSC Pull Server with SQL server backing, TFS Build Worker
$roles = @(
Get-LabMachineRoleDefinition -Role DSCPullServer -Properties @{ DoNotPushLocalModules = 'true'; DatabaseEngine = 'mdb' }
Get-LabMachineRoleDefinition -Role TfsBuildWorker
Get-LabMachineRoleDefinition -Role WebServer
)
$proGetRole = Get-LabPostInstallationActivity -CustomRole ProGet5 -Properties @{
ProGetDownloadLink = 'https://s3.amazonaws.com/cdn.inedo.com/downloads/proget/ProGetSetup5.0.10.exe'
SqlServer = 'DSCCASQL01'
}
Add-LabMachineDefinition -Name DSCPULL01 -Memory 2GB -Roles $roles -PostInstallationActivity $proGetRole
# Build Server
Add-LabMachineDefinition -Name DSCTFS01 -Memory 2GB -Roles Tfs2018
# DSC target nodes - our legacy VMs with an existing configuration
# Your run-of-the-mill file server in Dev
Add-LabMachineDefinition -Name "DSCFile01" -Memory 1GB -OperatingSystem 'Windows Server 2016 Datacenter Evaluation' -Roles FileServer
# and Prod
Add-LabMachineDefinition -Name "DSCFile02" -Memory 1GB -OperatingSystem 'Windows Server 2016 Datacenter Evaluation' -Roles FileServer
# The ubiquitous web server in Dev
Add-LabMachineDefinition -Name "DSCWeb01" -Memory 1GB -OperatingSystem 'Windows Server 2016 Datacenter Evaluation' -Roles WebServer
# and Prod
Add-LabMachineDefinition -Name "DSCWeb02" -Memory 1GB -OperatingSystem 'Windows Server 2016 Datacenter Evaluation' -Roles WebServer
Install-Lab
Enable-LabCertificateAutoenrollment -Computer -User
Install-LabWindowsFeature -ComputerName (Get-LabVM -Role DSCPullServer, FileServer, WebServer, Tfs2018) -FeatureName RSAT-AD-Tools
Install-LabSoftwarePackage -Path $labsources\SoftwarePackages\Notepad++.exe -CommandLine /S -ComputerName (Get-LabVM)
# in case you screw something up
Checkpoint-LabVM -All -SnapshotName AfterInstall
#endregion
#region Lab customizations
# Web server
$deployUserName = (Get-LabVm -Role WebServer).GetCredential((Get-Lab)).UserName
$deployUserPassword = (Get-LabVm -Role WebServer).GetCredential((Get-Lab)).GetNetworkCredential().Password
Copy-LabFileItem -Path "$PSScriptRoot\LabData\LabSite.zip" -ComputerName (Get-LabVm -Role WebServer)
Invoke-LabCommand -Activity 'Setup Web Site' -ComputerName (Get-LabVm -Role WebServer) -ScriptBlock {
New-Item -ItemType Directory -Path C:\PSConfSite
Expand-Archive -Path C:\LabSite.zip -DestinationPath C:\PSConfSite -Force
$pool = New-WebAppPool -Name PSConfSite
$pool.processModel.identityType = 3
$pool.processModel.userName = $deployUserName
$pool.processModel.password = $deployUserPassword
$pool | Set-Item
New-Website -name "PSConfSite" -PhysicalPath C:\PsConfSite -ApplicationPool "PSConfSite"
} -Variable (Get-Variable deployUserName, deployUserPassword)
# File server
Invoke-LabCommand -Activity 'Creating folders and shares' -ComputerName (Get-LabVM -Role FileServer) -ScriptBlock {
New-Item -ItemType Directory -Path C:\UserHome
foreach ($User in (Get-ADUser -Filter * | Select-Object -First 1000)) {
New-Item -ItemType Directory -Path C:\UserHome -Name $User.samAccountName
}
New-Item -ItemType Directory -Path C:\GroupData
'Accounting', 'Legal', 'HR', 'Janitorial' | ForEach-Object {New-Item -ItemType Directory -Path C:\GroupData -Name $_}
New-SmbShare -Name Home -Path C:\UserHome
New-SmbShare -Name Department -Path C:\GroupData
}
# TFS Server
$tfsServer = Get-LabVM -Role Tfs2018
$tfsWorker = Get-LabVM -Role TfsBuildWorker
Get-LabInternetFile -Uri https://go.microsoft.com/fwlink/?Linkid=852157 -Path $labSources\SoftwarePackages\VSCodeSetup.exe
Get-LabInternetFile -Uri https://github.com/git-for-windows/git/releases/download/v2.16.2.windows.1/Git-2.16.2-64-bit.exe -Path $labSources\SoftwarePackages\Git.exe
New-Item -ItemType Directory -Path $labSources\SoftwarePackages\VSCodeExtensions -ErrorAction SilentlyContinue | Out-Null
Get-LabInternetFile -Uri https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-vscode/vsextensions/PowerShell/1.6.0/vspackage -Path $labSources\SoftwarePackages\VSCodeExtensions\ps.vsix
Install-LabSoftwarePackage -Path $labSources\SoftwarePackages\VSCodeSetup.exe -CommandLine /SILENT -ComputerName $tfsServer
Install-LabSoftwarePackage -Path $labSources\SoftwarePackages\Git.exe -CommandLine /SILENT -ComputerName $tfsServer
Restart-LabVM -ComputerName $tfsServer #somehow required to finish all parts of the VSCode installation
Copy-LabFileItem -Path $labSources\SoftwarePackages\VSCodeExtensions -ComputerName $tfsServer
Invoke-LabCommand -ActivityName 'Install VSCode Extensions' -ComputerName $tfsServer -ScriptBlock {
dir -Path C:\VSCodeExtensions | ForEach-Object {
code --install-extension $_.FullName
}
} -NoDisplay
Invoke-LabCommand -ActivityName 'Create link to TFS' -ComputerName $tfsServer -ScriptBlock {
$shell = New-Object -ComObject WScript.Shell
$desktopPath = [System.Environment]::GetFolderPath('Desktop')
$shortcut = $shell.CreateShortcut("$desktopPath\TFS.url")
$shortcut.TargetPath = 'https://DSCTFS01:8080/AutomatedLab/PSConfEU2018'
$shortcut.Save()
$shortcut = $shell.CreateShortcut("$desktopPath\ProGet.url")
$shortcut.TargetPath = 'http://dscpull01:8624/'
$shortcut.Save()
}
Invoke-LabCommand -ActivityName 'Getting required modules and publishing them to ProGet' -ComputerName $tfsServer -ScriptBlock {
$requiredModules = 'BuildHelpers', 'datum', 'DscBuildHelpers', 'InvokeBuild', 'PackageManagement', 'Pester', 'powershell-yaml', 'PowerShellGet', 'ProtectedData', 'PSDepend', 'PSDeploy', 'PSScriptAnalyzer', 'xDSCResourceDesigner', 'xPSDesiredStateConfiguration'
Install-PackageProvider -Name NuGet -Force
mkdir -Path C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet -Force
Invoke-WebRequest -Uri 'https://nuget.org/nuget.exe' -OutFile C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\nuget.exe
Install-Module -Name $requiredModules -Repository PSGallery -Force -AllowClobber -SkipPublisherCheck -WarningAction SilentlyContinue
$path = "http://DSCPull01.contoso.com:8624/nuget/Internal"
if (-not (Get-PSRepository -Name Internal -ErrorAction SilentlyContinue)) {
Register-PSRepository -Name Internal -SourceLocation $path -PublishLocation $path -InstallationPolicy Trusted
}
foreach ($requiredModule in $requiredModules) {
$module = Get-Module $requiredModule -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
if (-not (Find-Module -Name $requiredModule -Repository Internal -ErrorAction SilentlyContinue)) {
Publish-Module -Name $requiredModule -RequiredVersion $module.Version -Repository Internal -NuGetApiKey '[email protected]:Somepass1' -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
foreach ($requiredModule in $requiredModules) {
Write-Host "Publishing module '$requiredModule'"
$module = Get-Module $requiredModule -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1
if (-not (Find-Module -Name $requiredModule -Repository Internal -ErrorAction SilentlyContinue)) {
Publish-Module -Name $requiredModule -RequiredVersion $module.Version -Repository Internal -NuGetApiKey '[email protected]:Somepass1' -Force #-ErrorAction SilentlyContinue -WarningAction SilentlyContinue
}
}
foreach ($requiredModule in $requiredModules) {
Uninstall-Module -Name $requiredModule -ErrorAction SilentlyContinue
}
foreach ($requiredModule in $requiredModules) {
Uninstall-Module -Name $requiredModule -ErrorAction SilentlyContinue
}
if (Get-PSRepository -Name Internal) {
Unregister-PSRepository -Name Internal
}
}
<# The Default PSGallery is not removed as the build process does not support an internal repository yet.
Invoke-LabCommand -ActivityName 'Register ProGet Gallery' -ComputerName (Get-LabVM) -ScriptBlock {
Unregister-PSRepository -Name PSGallery
$path = "http://DSCPull01.contoso.com:8624/nuget/Internal"
Register-PSRepository -Name Internal -SourceLocation $path -PublishLocation $path -InstallationPolicy Trusted
}
#>
Invoke-LabCommand -ActivityName 'Disable Git SSL Certificate Check' -ComputerName $tfsServer, $tfsWorker -ScriptBlock {
[System.Environment]::SetEnvironmentVariable('GIT_SSL_NO_VERIFY', '1', 'Machine')
}
Restart-LabVM -ComputerName $tfsServer, $tfsWorker -Wait
Invoke-LabCommand -ActivityName 'Setting the worker service account to local system to be able to write to deployment path' -ComputerName $tfsWorker -ScriptBlock {
$services = Get-CimInstance -ClassName Win32_Service -Filter 'Name like "vsts%"'
foreach ($service in $services)
{
$service | Invoke-CimMethod -MethodName Change -Arguments @{ StartName = 'LocalSystem' } | Out-Null
$service | Restart-Service
}
}
Checkpoint-LabVM -All -SnapshotName AfterCustomizations
# Create a new release pipeline
# Get those build steps from Get-LabBuildStep
$buildSteps = @(
@{
"enabled" = $true
"continueOnError" = $false
"alwaysRun" = $false
"displayName" = "Execute Build.ps1"
"task" = @{
"id" = "e213ff0f-5d5c-4791-802d-52ea3e7be1f1" # We need to refer to a valid ID - refer to Get-LabBuildStep for all available steps
"versionSpec" = "*"
}
"inputs" = @{
scriptType = "filePath"
scriptName = ".Build.ps1"
arguments = "-resolveDependency"
failOnStandardError = $false
}
}
@{
enabled = $True
continueOnError = $False
alwaysRun = $False
displayName = 'Publish test results' # e.g. Publish Test Results $(testResultsFiles) or Publish Test Results
task = @{
id = '0b0f01ed-7dde-43ff-9cbb-e48954daf9b1'
versionSpec = '*'
}
inputs = @{
testRunner = 'NUnit' # Type: pickList, Default: JUnit, Mandatory: True
testResultsFiles = '**/testresults.xml' # Type: filePath, Default: **/TEST-*.xml, Mandatory: True
}
}
)
# Which will make use of TFS, clone the stuff, add the necessary build step, publish the test results and so on
# You will see two remotes, Origin (Our code on GitHub) and TFS (Our code pushed to your lab)
Write-ScreenInfo 'Creating TFS project and cloning from GitHub...' -NoNewLine
New-LabReleasePipeline -ProjectName PSConfEU2018 -SourceRepository https://github.com/AutomatedLab/DscWorkshop -BuildSteps $buildSteps
cd "$labSources\GitRepositories\DscWorkshop"
git checkout master 2>&1 | Out-Null
git pull origin master 2>&1 | Out-Null
git -c http.sslverify=false push tfs 2>&1 | Out-Null
Write-ScreenInfo done
# in case you screw something up
Checkpoint-LabVM -All -SnapshotName AfterPipeline
#endregion
Show-LabDeploymentSummary -Detailed