|
| 1 | +Param( |
| 2 | + #equivalent of using localhost in docker container |
| 3 | + [string]$AEM_HOST = "localhost", |
| 4 | + # TCP port SOURCE_CQ listens on |
| 5 | + [string]$AEM_PORT = "4502", |
| 6 | + # AEM Admin user for AEM_HOST |
| 7 | + [string]$AEM_USER = "admin", |
| 8 | + # AEM Admin password for AEM_HOST |
| 9 | + [string]$AEM_PASSWORD = "admin", |
| 10 | + # Root folder name for placing content |
| 11 | + [string]$SOURCE_CONTENT_FOLDER = "localhost-author-export", |
| 12 | + # Server WebDav Path |
| 13 | + #$AEM_WEBDAV_PATH = "/crx/server/crx.default/jcr:root/" |
| 14 | + [string]$AEM_WEBDAV_PATH = "/crx", |
| 15 | + [string]$AEM_SCHEMA = "http", |
| 16 | + #to set additional flags if required |
| 17 | + [string]$VLT_FLAGS = "--insecure -Xmx2g", |
| 18 | + [string]$VLT_CMD = "../tools/vault-cli/bin/vlt", |
| 19 | + # Root folder name for placing content |
| 20 | + [string]$CONTENT_DESTINATION = ".\src\main\content", |
| 21 | + [string]$FILTER_FILE = "${CONTENT_DESTINATION}\META-INF\vault\filter.xml", |
| 22 | + [string]$FILTER_FILE_LOCATION = "${CONTENT_DESTINATION}\META-INF", |
| 23 | + #which filter paths to import |
| 24 | + [string[]]$ROOT_PATHS, |
| 25 | + [string]$ROOT_PATH = "/", |
| 26 | + [string]$CONTENT_SOURCE = (Resolve-Path -Path "src\main\content\jcr_root" -Relative), |
| 27 | + # connection timeout |
| 28 | + [string]$TIMEOUT = "5", |
| 29 | + # host address |
| 30 | + [string]$ADDRESS = "${AEM_SCHEMA}://${AEM_HOST}:${AEM_PORT}", |
| 31 | + # Disable services before import |
| 32 | + [string[]]$SERVICES_TO_DISABLE = @( |
| 33 | + "/system/console/bundles/com.day.cq.cq-mailer", |
| 34 | + "/system/console/bundles/com.adobe.granite.workflow.core" |
| 35 | + ), |
| 36 | + |
| 37 | + [HashTable]$BODY_SERVICE_TO_DISABLE = @{ |
| 38 | + "action"="stop" |
| 39 | + }, |
| 40 | + [HashTable]$BODY_SERVICE_TO_DISABLE_ENABLE = @{ |
| 41 | + "action"="start" |
| 42 | + }, |
| 43 | + [switch]$Silent = $false, |
| 44 | + [string]$LOG_PATH = "..\logs" |
| 45 | +) |
| 46 | + |
| 47 | + |
| 48 | +Function Format-XMLIndent |
| 49 | +{ |
| 50 | + [Cmdletbinding()] |
| 51 | + [Alias("IndentXML")] |
| 52 | + param |
| 53 | + ( |
| 54 | + [Parameter(ValueFromPipeline)] |
| 55 | + [xml]$Content, |
| 56 | + [int]$Indent |
| 57 | + ) |
| 58 | + |
| 59 | + # String Writer and XML Writer objects to write XML to string |
| 60 | + $StringWriter = New-Object System.IO.StringWriter |
| 61 | + $XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter |
| 62 | + |
| 63 | + # Default = None, change Formatting to Indented |
| 64 | + $xmlWriter.Formatting = "indented" |
| 65 | + |
| 66 | + # Gets or sets how many IndentChars to write for each level in |
| 67 | + # the hierarchy when Formatting is set to Formatting.Indented |
| 68 | + $xmlWriter.Indentation = $Indent |
| 69 | + |
| 70 | + $Content.WriteContentTo($XmlWriter) |
| 71 | + $XmlWriter.Flush();$StringWriter.Flush() |
| 72 | + $StringWriter.ToString() |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +function doSlingPost { |
| 77 | + [CmdletBinding()] |
| 78 | + Param ( |
| 79 | + |
| 80 | + [Parameter(Mandatory=$true)] |
| 81 | + [string]$Url="http://localhost:4502", |
| 82 | + |
| 83 | + [Parameter(Mandatory)] |
| 84 | + [ValidateNotNullOrEmpty()] |
| 85 | + [ValidateSet('Post','Delete')] |
| 86 | + [string]$Method, |
| 87 | + |
| 88 | + [Parameter(Mandatory=$false)] |
| 89 | + [HashTable]$Body, |
| 90 | + |
| 91 | + [Parameter(Mandatory=$false, |
| 92 | + HelpMessage="Provide Basic Auth Credentials in following format: <user>:<pass>")] |
| 93 | + [string]$BasicAuthCreds="", |
| 94 | + |
| 95 | + [Parameter(Mandatory=$false)] |
| 96 | + [string]$UserAgent="", |
| 97 | + |
| 98 | + [Parameter(Mandatory=$false)] |
| 99 | + [string]$Referer="", |
| 100 | + |
| 101 | + [Parameter(Mandatory=$false)] |
| 102 | + [string]$Timeout="5" |
| 103 | + |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + $HEADERS = @{ |
| 109 | + } |
| 110 | + |
| 111 | + if (-not([string]::IsNullOrEmpty($UserAgent))) { |
| 112 | + $HEADERS.add("User-Agent",$UserAgent) |
| 113 | + } |
| 114 | + |
| 115 | + if (-not([string]::IsNullOrEmpty($Referer))) { |
| 116 | + $HEADERS.add("Referer",$Referer) |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + if (-not([string]::IsNullOrEmpty($BasicAuthCreds))) { |
| 121 | + $BASICAUTH = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($BasicAuthCreds)) |
| 122 | + $HEADERS.add("Authorization","Basic $BASICAUTH") |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + Write-Output "Performing action $Method on $Url." |
| 127 | + |
| 128 | + $Response = Invoke-WebRequest -Method Post -Headers $HEADERS -TimeoutSec $Timeout -Uri "$Url" -Form $Body -ContentType "application/x-www-form-urlencoded" |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +Write-Output "------- CONFIG ----------" |
| 133 | +Write-Output "AEM_SCHEMA: $AEM_SCHEMA" |
| 134 | +Write-Output "AEM_HOST: $AEM_HOST" |
| 135 | +Write-Output "AEM_PORT: $AEM_PORT" |
| 136 | +Write-Output "AEM_USER: $AEM_USER" |
| 137 | +Write-Output "CONTENT_SOURCE: $CONTENT_SOURCE" |
| 138 | +Write-Output "ROOT_PATH: $ROOT_PATH" |
| 139 | +Write-Output "FILTER_FILE: $FILTER_FILE" |
| 140 | +Write-Output "Silent: $Silent" |
| 141 | +Write-Output "VLT_FLAGS: $VLT_FLAGS" |
| 142 | +Write-Output "VLT_CMD: $VLT_CMD" |
| 143 | +Write-Output "ROOT_PATHS:" |
| 144 | +$ROOT_PATHS | ForEach-Object { |
| 145 | + Write-Output " - $_" |
| 146 | +} |
| 147 | +Write-Output "SERVICES_TO_DISABLE:" |
| 148 | +$SERVICES_TO_DISABLE | ForEach-Object { |
| 149 | + Write-Output " - $_" |
| 150 | +} |
| 151 | + |
| 152 | +if (-not($Silent)) |
| 153 | +{ |
| 154 | + $START = Read-Host -Prompt 'Do you want to start import with these settings? (y/n)' |
| 155 | + |
| 156 | + if ($START -ne "y") |
| 157 | + { |
| 158 | + Write-Output "Quiting..." |
| 159 | + Exit |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +Write-Output "------- Revert META-INF ----------" |
| 164 | +git checkout HEAD src/main/content/META-INF/ |
| 165 | +git clean -fx src/main/content/META-INF |
| 166 | +Write-Output "------- Revert META-INF ----------" |
| 167 | + |
| 168 | +if ($ROOT_PATHS) |
| 169 | +{ |
| 170 | + Write-Output "------- START Update filter ----------" |
| 171 | + $ROOT_PATHS_LAST = $ROOT_PATHS | Select-Object -Last 1 |
| 172 | + $ROOT_PATHS | ForEach-Object { |
| 173 | + $LOG_FILENAME = "$_".Replace("/","-") |
| 174 | + |
| 175 | + Write-Output "Remove exiting Filer..." |
| 176 | + Copy-Item ".\src\main\content\META-INF\vault\filter-blank.xml" -Destination "$FILTER_FILE" |
| 177 | + |
| 178 | + Write-Output "Create filter for: $_" |
| 179 | + $FILTER_XML = [xml](Get-Content $FILTER_FILE) |
| 180 | + $FILTER_XML_CONTENT = $FILTER_XML.SelectNodes("//workspaceFilter") |
| 181 | + $FILTER_XML_DELETE = $FILTER_XML_CONTENT.SelectNodes('//filter') |
| 182 | + $FILTER_XML_DELETE | ForEach-Object{ |
| 183 | + $DELETE_STATUS = $FILTER_XML_CONTENT.RemoveChild($_) |
| 184 | + } |
| 185 | + $FILTER_XML_CONTENT_NEW = $FILTER_XML.CreateNode("element","filter","") |
| 186 | + $FILTER_XML_CONTENT_NEW.SetAttribute("root",$_) |
| 187 | + $FILTER_XML_CONTENT_NEW_ADD = $FILTER_XML_CONTENT.AppendChild($FILTER_XML_CONTENT_NEW) |
| 188 | + Write-Output "Saving..." |
| 189 | + $FILTER_XML.OuterXml | IndentXML -Indent 4 | Out-File $FILTER_FILE -encoding "UTF8" |
| 190 | + Write-Output "Done..." |
| 191 | + } |
| 192 | + |
| 193 | + Write-Output "------- END Update filter ----------" |
| 194 | +} |
| 195 | + |
| 196 | +if ($SERVICES_TO_DISABLE) |
| 197 | +{ |
| 198 | + Write-Output "------- Disable Services ----------" |
| 199 | + $SERVICES_TO_DISABLE | ForEach-Object { |
| 200 | + Write-Output "Stopping service: $_" |
| 201 | + doSlingPost -Method Post -Referer $ADDRESS -UserAgent "curl" -Body $BODY_SERVICE_TO_DISABLE -Url "${ADDRESS}$_" -BasicAuthCreds ${AEM_USER}:${AEM_PASSWORD} -Timeout $TIMEOUT |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +Write-Output "------- START Importing content ----------" |
| 206 | +Write-Output "${VLT_CMD} ${VLT_FLAGS} --credentials ${AEM_USER}:****** import -v ${ADDRESS}${AEM_WEBDAV_PATH} ${CONTENT_SOURCE} ${ROOT_PATH}" |
| 207 | + |
| 208 | +Invoke-Expression -Command "${VLT_CMD} ${VLT_FLAGS} --credentials ${AEM_USER}:${AEM_PASSWORD} import -v ${ADDRESS}${AEM_WEBDAV_PATH} ${CONTENT_SOURCE} ${ROOT_PATH} " | Tee-Object -FilePath "${LOG_PATH}\filevailt-import.log" |
| 209 | + |
| 210 | +Write-Output "------- END Importing content ----------" |
| 211 | + |
| 212 | + |
| 213 | +if ($SERVICES_TO_DISABLE) |
| 214 | +{ |
| 215 | + Write-Output "------- Enable Services ----------" |
| 216 | + $SERVICES_TO_DISABLE | ForEach-Object { |
| 217 | + Write-Output "Stopping service: $_" |
| 218 | + doSlingPost -Method Post -Referer $ADDRESS -UserAgent "curl" -Body $BODY_SERVICE_TO_DISABLE_ENABLE -Url "${ADDRESS}$_" -BasicAuthCreds ${AEM_USER}:${AEM_PASSWORD} -Timeout $TIMEOUT |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +Write-Output "------- Revert META-INF ----------" |
| 223 | +git checkout HEAD src/main/content/META-INF/ |
| 224 | +git clean -fx src/main/content/META-INF |
| 225 | +Write-Output "------- Revert META-INF ----------" |
0 commit comments