Skip to content

Commit

Permalink
Merge pull request #14 from Akaizoku/develop
Browse files Browse the repository at this point in the history
Fix WhatIf gaps
  • Loading branch information
Akaizoku authored Dec 13, 2021
2 parents d1119dd + 4f581b6 commit 4ef8d93
Showing 1 changed file with 94 additions and 86 deletions.
180 changes: 94 additions & 86 deletions powershell/Invoke-RestoreAlteryx.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -138,90 +138,94 @@ function Invoke-RestoreAlteryx {
# ----------------------------------------------------------------------------
# Update configuration
Write-Log -Type "INFO" -Message "Updating configuration"
$RunTimeSettingsXML = New-Object -TypeName "System.XML.XMLDocument"
$RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings)
# Gallery URL
$BaseAddress = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/BaseAddress"
if ($BaseAddress.InnerText -match "https?://.+?/gallery") {
# Assume this is a hostname
$BaseAddressMatch = Select-String -InputObject $BaseAddress.InnerText -Pattern "https?://(.+?)/gallery"
$SourceHostname = $BaseAddressMatch.Matches.Groups[1].Value
$NewHostname = $env:ComputerName
if ($SourceHostname -ne $NewHostname) {
Write-Log -Type "INFO" -Message "Updating hostname"
# Update base Gallery URL
Write-Log -Type "DEBUG" -Message "Source=$SourceHostname"
Write-Log -Type "DEBUG" -Message "Destination=$NewHostname"
$NewBaseAddress = $BaseAddress.InnerText.Replace($SourceHostname, $NewHostname)
Write-Log -Type "DEBUG" -Message $NewBaseAddress
$BaseAddress.InnerText = $NewBaseAddress
# Update authentication URL
$ServiceProviderEntityID = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Authentication/ServiceProviderEntityID"
$ServiceProviderEntityID.InnerText = $ServiceProviderEntityID.InnerText.Replace($SourceHostname, $NewHostname)
}
}
# SSL
if ($Properties.EnableSSL -eq $true) {
$Protocol = "https"
$Action = "Enabling"
} else {
$Protocol = "http"
$Action = "Disabling"
}
# ! Node name is case-sensitive: SslEnabled
$SSLEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/SslEnabled"
if ($null -ne $SSLEnabled) {
if ($SSLEnabled.InnerText -ne $Properties.EnableSSL.ToString()) {
Write-Log -Type "INFO" -Message ($Action + " SSL")
$SSLEnabled.InnerText = $Properties.EnableSSL.ToString()
if ($PSCmdlet.ShouldProcess("RunTimeSetting.xml", "Update")) {
$RunTimeSettingsXML = New-Object -TypeName "System.XML.XMLDocument"
$RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings)
# Gallery URL
$BaseAddress = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/BaseAddress"
if ($BaseAddress.InnerText -match "https?://.+?/gallery") {
# Assume this is a hostname
$BaseAddressMatch = Select-String -InputObject $BaseAddress.InnerText -Pattern "https?://(.+?)/gallery"
$SourceHostname = $BaseAddressMatch.Matches.Groups[1].Value
$NewHostname = $env:ComputerName
if ($SourceHostname -ne $NewHostname) {
Write-Log -Type "INFO" -Message "Updating hostname"
# Update base Gallery URL
Write-Log -Type "DEBUG" -Message "Source=$SourceHostname"
Write-Log -Type "DEBUG" -Message "Destination=$NewHostname"
$NewBaseAddress = $BaseAddress.InnerText.Replace($SourceHostname, $NewHostname)
Write-Log -Type "DEBUG" -Message $NewBaseAddress
$BaseAddress.InnerText = $NewBaseAddress
# Update authentication URL
$ServiceProviderEntityID = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Authentication/ServiceProviderEntityID"
$ServiceProviderEntityID.InnerText = $ServiceProviderEntityID.InnerText.Replace($SourceHostname, $NewHostname)
}
}
} else {
# If SSL configuration is not explicitly defined
# SSL
if ($Properties.EnableSSL -eq $true) {
# Create SSL node
$Gallery = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery"
$SSLEnabled = $RunTimeSettingsXML.CreateElement("SslEnabled")
$SSLEnabled.InnerText = $Properties.EnableSSL.ToString()
[Void]$Gallery.AppendChild($SSLEnabled)
$Protocol = "https"
$Action = "Enabling"
} else {
$Protocol = "http"
$Action = "Disabling"
}
}
if ($BaseAddress.InnerText -notmatch "^${Protocol}://") {
Write-Log -Type "INFO" -Message ($Action + " HTTPS")
$SSLAddress = $BaseAddress.InnerText -replace "^https?", "$Protocol"
Write-Log -Type "DEBUG" -Message $SSLAddress
$BaseAddress.InnerText = $SSLAddress
}
# Update configuration file
$RunTimeSettingsXML.Save($ConfigurationFiles.RunTimeSettings)
# Installation path
$WorkingPath = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Environment/WorkingPath"
if ($WorkingPath.InnerText -ne $Properties.InstallationPath) {
Write-Log -Type "INFO" -Message "Updating installation paths"
Write-Log -Type "DEBUG" -Message "Source=$($WorkingPath.InnerText)"
Write-Log -Type "DEBUG" -Message "Destination=$($Properties.InstallationPath)"
$RunTimeSettings = Get-Content -Path $ConfigurationFiles.RunTimeSettings
$UpdatedSettings = New-Object -TypeName "System.Collections.Arraylist"
foreach ($Property in $RunTimeSettings) {
[Void]$UpdatedSettings.Add($Property.Replace($WorkingPath.InnerText, $Properties.InstallationPath))
# ! Node name is case-sensitive: SslEnabled
$SSLEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery/SslEnabled"
if ($null -ne $SSLEnabled) {
if ($SSLEnabled.InnerText -ne $Properties.EnableSSL.ToString()) {
Write-Log -Type "INFO" -Message ($Action + " SSL")
$SSLEnabled.InnerText = $Properties.EnableSSL.ToString()
}
} else {
# If SSL configuration is not explicitly defined
if ($Properties.EnableSSL -eq $true) {
# Create SSL node
$Gallery = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Gallery"
$SSLEnabled = $RunTimeSettingsXML.CreateElement("SslEnabled")
$SSLEnabled.InnerText = $Properties.EnableSSL.ToString()
[Void]$Gallery.AppendChild($SSLEnabled)
}
}
if ($BaseAddress.InnerText -notmatch "^${Protocol}://") {
Write-Log -Type "INFO" -Message ($Action + " HTTPS")
$SSLAddress = $BaseAddress.InnerText -replace "^https?", "$Protocol"
Write-Log -Type "DEBUG" -Message $SSLAddress
$BaseAddress.InnerText = $SSLAddress
}
# Update configuration file
$RunTimeSettingsXML.Save($ConfigurationFiles.RunTimeSettings)
# Installation path
$WorkingPath = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Environment/WorkingPath"
if ($WorkingPath.InnerText -ne $Properties.InstallationPath) {
Write-Log -Type "INFO" -Message "Updating installation paths"
Write-Log -Type "DEBUG" -Message "Source=$($WorkingPath.InnerText)"
Write-Log -Type "DEBUG" -Message "Destination=$($Properties.InstallationPath)"
$RunTimeSettings = Get-Content -Path $ConfigurationFiles.RunTimeSettings
$UpdatedSettings = New-Object -TypeName "System.Collections.Arraylist"
foreach ($Property in $RunTimeSettings) {
[Void]$UpdatedSettings.Add($Property.Replace($WorkingPath.InnerText, $Properties.InstallationPath))
}
# Overwrite source configuration
Set-Content -Path $ConfigurationFiles.RunTimeSettings -Value $UpdatedSettings
# Reload XML settings
$RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings)
}
# Overwrite source configuration
Set-Content -Path $ConfigurationFiles.RunTimeSettings -Value $UpdatedSettings
# Reload XML settings
$RunTimeSettingsXML.Load($ConfigurationFiles.RunTimeSettings)
}
# ----------------------------------------------------------------------------
# (Re)Set controller token
if ($Restore.Token -eq $true) {
Write-Log -Type "INFO" -Message "Remove encrypted controller token"
$ControllerSettingsXML = New-Object -TypeName "System.XML.XMLDocument"
$ControllerSettingsXML.Load($ConfigurationFiles.RunTimeSettings)
$ServerSecretEncrypted = Select-XMLNode -XML $ControllerSettingsXML -XPath "SystemSettings/Controller/ServerSecretEncrypted"
if ($null -ne $ServerSecretEncrypted) {
Write-Log -Type "DEBUG" -Message $ServerSecretEncrypted.InnerText
[Void]$ServerSecretEncrypted.ParentNode.RemoveChild($ServerSecretEncrypted)
$ControllerSettingsXML.Save($ConfigurationFiles.RunTimeSettings)
} else {
Write-Log -Type "WARN" -Message "Encrypted controller token could not be found"
if ($PSCmdlet.ShouldProcess("Encrypted controller token", "Remove")) {
$ControllerSettingsXML = New-Object -TypeName "System.XML.XMLDocument"
$ControllerSettingsXML.Load($ConfigurationFiles.RunTimeSettings)
$ServerSecretEncrypted = Select-XMLNode -XML $ControllerSettingsXML -XPath "SystemSettings/Controller/ServerSecretEncrypted"
if ($null -ne $ServerSecretEncrypted) {
Write-Log -Type "DEBUG" -Message $ServerSecretEncrypted.InnerText
[Void]$ServerSecretEncrypted.ParentNode.RemoveChild($ServerSecretEncrypted)
$ControllerSettingsXML.Save($ConfigurationFiles.RunTimeSettings)
} else {
Write-Log -Type "WARN" -Message "Encrypted controller token could not be found"
}
}
Write-Log -Type "INFO" -Message "Restore controller token"
if ($PSCmdlet.ShouldProcess("Controller token", "Restore")) {
Expand All @@ -242,16 +246,20 @@ function Invoke-RestoreAlteryx {
}
}
# ----------------------------------------------------------------------------
# Set Run-As user
# Set Run-as user
if ($Restore.RunAsUser -eq $true) {
# TODO
Write-Log -Type "WARN" -Message "Run As User restore is not currently supported"
if ($PSCmdlet.ShouldProcess("Run-as user credentials", "Restore")) {
# TODO
Write-Log -Type "WARN" -Message "Run-as user credentials restore is not currently supported"
}
}
# ----------------------------------------------------------------------------
# Set SMTP password
if ($Restore.SMTPPassword -eq $true) {
# TODO
Write-Log -Type "WARN" -Message "SMTP password restore is not currently supported"
if ($PSCmdlet.ShouldProcess("SMTP password", "Restore")) {
# TODO
Write-Log -Type "WARN" -Message "SMTP password restore is not currently supported"
}
}
# ----------------------------------------------------------------------------
# Reset storage key
Expand Down Expand Up @@ -281,9 +289,9 @@ function Invoke-RestoreAlteryx {
# Restore database
if ($Restore.Database -eq $true) {
Write-Log -Type "INFO" -Message "Restore MongoDB database from backup"
$EmbeddedMongoDBEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Controller/EmbeddedMongoDBEnabled"
if ($EmbeddedMongoDBEnabled.InnerText -eq $true) {
if ($PSCmdlet.ShouldProcess("MongoDB", "Restore")) {
if ($PSCmdlet.ShouldProcess("MongoDB", "Restore")) {
$EmbeddedMongoDBEnabled = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Controller/EmbeddedMongoDBEnabled"
if ($EmbeddedMongoDBEnabled.InnerText -eq $true) {
$EmbeddedMongoDBRootPath = Select-XMLNode -XML $RunTimeSettingsXML -XPath "SystemSettings/Controller/EmbeddedMongoDBRootPath"
if ($null -ne $EmbeddedMongoDBRootPath) {
$TargetPath = $EmbeddedMongoDBRootPath.InnerText
Expand All @@ -299,10 +307,10 @@ function Invoke-RestoreAlteryx {
} else {
Write-Log -Type "DEBUG" -Message $DatabaseRestore
}
} else {
Write-Log -Type "ERROR" -Message "User-managed MongoDB is not supported"
Write-Log -Type "WARN" -Message "Skipping database restore"
}
} else {
Write-Log -Type "ERROR" -Message "User-managed MongoDB is not supported"
Write-Log -Type "WARN" -Message "Skipping database restore"
}
}
# ----------------------------------------------------------------------------
Expand Down

0 comments on commit 4ef8d93

Please sign in to comment.