-
Notifications
You must be signed in to change notification settings - Fork 6
/
Create-SitecoreModule-DockerAssetImage.ps1
245 lines (190 loc) · 9.75 KB
/
Create-SitecoreModule-DockerAssetImage.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
<#
.SYNOPSIS
Script to handle the auto creation of a Docker Asset Image for a given Sitecore module
.DESCRIPTION
The purpose of this script is to create a Docker asset image for your Sitecore module
The script is intended to automate some steps that need to be taken in order to create a custom
Docker Asset Image for your Sitecore module
I got the inspiration for this project after reading the following blogposts:
How to create a Docker asset image for your Sitecore Module - Árvai Mihály
https://medium.com/@mitya_1988/how-to-create-a-docker-asset-image-for-your-sitecore-module-58e1f3a47672
Creating a Docker Asset Image for Your Sitecore Module and Adding It To Your Site - Erica Stockwell-Alpert
https://ericastockwellalpert.wordpress.com/2021/02/23/creating-a-docker-asset-image-for-your-sitecore-module-and-adding-it-to-your-site/
.NOTES
Version: 1.0
Author: Robbert Hock - Kayee - Sitecore MVP 2010-2022
Creation Date: June/July 2021
Purpose/Change: Initial script development
#>
using module ".\logo.psm1"
#---------------------------------[Parameters]--------------------------------------------------------
param(
[string] $ModulePackageName = "",
[string] $Tag = "",
[switch] $GenerateCdContentDirectory
)
$ErrorActionPreference = "Stop"
#---------------------------------[Read configuration]------------------------------------------------
$configurationPath = "configuration.json"
$jsonConfiguration = Get-Content -Path $configurationPath | ConvertFrom-Json
$satUrl = $jsonConfiguration.Parameters.SitecoreAzureToolkitUrl.DefaultValue
$satPackageName = $jsonConfiguration.Parameters.SitecoreAzureToolkitPackageName.DefaultValue
Show-Start
if (!$ModulePackageName) {
Write-Host "================================================================================================================================="
Write-Host "`n"
Write-Host "ERROR - Make sure you pass in the -ModulePackageName parameter. e.g. .\\Create-SitecoreModule-DockerAssetImage.ps1 -ModulePackageName 'YOUR PACKAGE NAME'" -ForegroundColor Red
Write-Host "`n"
Write-Host "================================================================================================================================="
Break
}
Write-Host "================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Sitecore Azure Toolkit download]"
Write-Host "`n"
$satDirecory = $PSScriptRoot + "\SAT"
If (!(Test-Path($satDirecory))) {
New-Item -ItemType Directory -Force -Path $satDirecory
}
if (Test-Path -Path "$satDirecory\$satPackageName") {
Write-Host "SKIPPING - $satDirecory folder already contains the $satPackageName file" -ForegroundColor Cyan
}
else {
Write-Host "START - downloading the $satPackageName file from dev.sitecore.net"
Invoke-WebRequest -Uri $satUrl -OutFile "$satDirecory\$satPackageName"
Write-Host "`n"
Write-Host "SUCCESS - Downloaded the $satPackageName file from dev.sitecore.net" -ForegroundColor Green
}
Write-Host "`n"
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Sitecore Azure Toolkit extract]"
Write-Host "`n"
if (-not(Test-Path "$satDirecory\tools\Sitecore.Cloud.Cmdlets.dll")) {
Expand-Archive -Path "$satDirecory\$satPackageName" -DestinationPath "$satDirecory" -Force
Write-Host "SUCCESS - Extracted $satPackageName to the $satDirecory directory" -ForegroundColor Green
Write-Host "`n"
}
else {
Write-Host "SKIPPING - $satPackageName is already extracted to the $satDirecory directory" -ForegroundColor Cyan
Write-Host "`n"
}
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Convert Sitecore Module to .scwdp]"
Write-Host "`n"
$dateTime = (Get-Date).tostring("yyyyMMdd_HHmmss")
$packagePath = $PSScriptRoot + "\Package\$ModulePackageName"
$modulePackageItem = Get-Item $packagePath
$modulePackageNameSinExtension = $modulePackageItem.BaseName
$scwdpDirectory = $PSScriptRoot + "\scwdp"
$scwdpModuleFolder = $modulePackageNameSinExtension + "_$dateTime"
$scwdpDestination = "$scwdpDirectory\$scwdpModuleFolder"
Import-Module .\SAT\tools\Sitecore.Cloud.Cmdlets.psm1
Import-Module .\SAT\tools\Sitecore.Cloud.Cmdlets.dll
If (!(Test-Path($packagePath))) {
Write-Host "ERROR - Make sure the $packagePath exists!" -ForegroundColor Red
Break
}
If (!(Test-Path($scwdpDirectory))) {
New-Item -ItemType Directory -Force -Path $scwdpDirectory
}
$scwdpPath = ConvertTo-SCModuleWebDeployPackage -Path $packagePath -Destination $scwdpDestination -Force
Write-Host "SUCCESS - Your Sitecore Module was converted to a Sitecore WebDeploy package and is located at:" -ForegroundColor Green
Write-Host "`n"
Write-Host "$scwdpDestination" -ForegroundColor Yellow
Write-Host "`n"
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Extracting Sitecore WebDeploy package]"
Write-Host "`n"
$extractSCwdpDirectory = $scwdpDestination + "\extract_scwdp"
if (!(Test-Path($extractSCwdpDirectory))) {
New-Item -ItemType Directory -Force -Path $extractSCwdpDirectory
}
$extractSCwdpDirectory
Expand-Archive -Path "$scwdpPath" -DestinationPath "$extractSCwdpDirectory" -Force
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Creating Sitecore module asset image structure]"
Write-Host "`n"
$moduleDirectory = "$scwdpDestination\Module"
$cmContentDirectory = $moduleDirectory + "\cm\content"
$cdContentDirectory = $moduleDirectory + "\cd\content"
$dbDirectory = $moduleDirectory + "\db"
$solrDirectory = $moduleDirectory + "\solr"
$toolsDirectory = $moduleDirectory + "\tools"
If (!(Test-Path($moduleDirectory))) {
New-Item -ItemType Directory -Force -Path $moduleDirectory
}
If (!(Test-Path($cmContentDirectory))) {
New-Item -ItemType Directory -Force -Path $cmContentDirectory
}
If ($GenerateCdContentDirectory -and !(Test-Path($cdContentDirectory))) {
New-Item -ItemType Directory -Force -Path $cdContentDirectory
}
If (!(Test-Path($dbDirectory))) {
New-Item -ItemType Directory -Force -Path $dbDirectory
}
If (!(Test-Path($solrDirectory))) {
New-Item -ItemType Directory -Force -Path $solrDirectory
}
If (!(Test-Path($toolsDirectory))) {
New-Item -ItemType Directory -Force -Path $toolsDirectory
}
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Copying over .scwdp contents to Sitecore module asset image structure]"
Write-Host "`n"
# Copy content
If (Test-Path "$extractSCwdpDirectory\Content\Website") {
Copy-Item -Path "$extractSCwdpDirectory\Content\Website\*" -Destination $cmContentDirectory -PassThru -Recurse
if ($GenerateCdContentDirectory) {
# Copy content to create CD layer folders
Copy-Item -Path $cmContentDirectory\* -Destination $cdContentDirectory -PassThru -Recurse
}
}
# Copy dacpacs + rename
if (Test-Path("$extractSCwdpDirectory\core.dacpac")) {
Copy-Item -Path "$extractSCwdpDirectory\core.dacpac" -Destination $dbDirectory -PassThru
Rename-Item -Path "$dbDirectory\core.dacpac" -NewName "Sitecore.Core.dacpac"
}
if (Test-Path("$extractSCwdpDirectory\master.dacpac")) {
Copy-Item -Path "$extractSCwdpDirectory\master.dacpac" -Destination $dbDirectory -PassThru
Rename-Item -Path "$dbDirectory\master.dacpac" -NewName "Sitecore.Master.dacpac"
}
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "START - [Copying over dockerfile]"
Write-Host "`n"
Copy-Item -Path "$PSScriptRoot\dockerfile" -Destination $moduleDirectory -PassThru
Copy-Item -Path "$PSScriptRoot\.dockerignore" -Destination $moduleDirectory -PassThru
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "SUCCESS - Succesfully created the Docker Asset Image structure in directory $moduleDirectory" -ForegroundColor Green
Write-Host "`n"
tree $moduleDirectory /f /a
if ($Tag) {
if (-Not (docker ps)) {
Write-Host "FAILED - Could not create the Docker image. Are you sure the Docker daemon is running?" -ForegroundColor Red
Break
}
Write-Host "========================================================================================================================"
Write-Host "`n"
Write-Host "START - [Building Docker Image] -" $Tag.ToLower()
Write-Host "`n"
Set-Location -Path $moduleDirectory
docker build --tag $Tag.ToLower() .
Write-Host "=================================================================================================================================="
Write-Host "`n"
Write-Host "SUCCESS - Created local image" $Tag.ToLower() -ForegroundColor Green
Write-Host "Don't forget to push the image to the Container Registry or Docker Hub." -ForegroundColor Yellow
Write-Host "`n"
}
Write-Host "`n"
Write-Host "=================================================================================================================================="
Write-Host "`n"
Show-Stop
Set-Location -Path $PSScriptRoot
# Cleaning up the modules
Get-Module | Remove-Module