Last active
August 29, 2015 14:14
-
-
Save darylteo/d51937dbfaf70eb5f98e to your computer and use it in GitHub Desktop.
Crazy? Lets write a powerscript to upgrade umbraco for us. (WIP duh, but I ain't creating a whole repo for this)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[string]$version, | |
[string]$target = ".", | |
[string]$config, | |
[string]$sitehost, | |
[string]$dbhost, | |
[string]$dbname, | |
[string]$dbuser, | |
[string]$dbpass, | |
[switch]$backup, | |
[switch]$download, | |
[switch]$install, | |
[switch]$upgrade, | |
[switch]$clean, | |
[switch]$enableVega, | |
[switch]$disableVega | |
) | |
# Functions | |
function removeFile($path) { | |
if(Test-Path $path) { | |
Remove-Item $path -verbose | |
} | |
} | |
function copyFile($src, $dest) { | |
if(Test-Path $dest) { | |
return; | |
} | |
Copy-Item $src $dest | |
} | |
function postUrl($url, $data) { | |
Write-Host $url | |
Write-Host $data | |
$WebRequest = [System.Net.HttpWebRequest]::Create($url); | |
$WebRequest.Method = 'POST'; | |
$WebRequest.ContentType = 'application/x-www-form-urlencoded'; | |
$BodyBytes = [System.Text.Encoding]::UTF8.GetBytes($data); | |
$stream = $WebRequest.GetRequestStream(); | |
$stream.Write($BodyBytes, 0, $BodyBytes.Length); | |
$stream.Close(); | |
$WebRequest.GetResponse(); | |
return | |
} | |
function getUrl($url) { | |
$webclient = New-Object System.Net.WebClient; | |
$response = $webclient.DownloadString($url); | |
$webclient.Dispose(); | |
$response | |
} | |
function downloadFile($uri, $dest) { | |
Write-Host "Downloading $($uri) to $($dest)`n" | |
if(Test-Path($dest)) { | |
Write-Host "$dest exists - skipping`n"; | |
return; | |
} | |
$webclient = New-Object System.Net.WebClient | |
$webclient.DownloadFile($uri,$dest) | |
$webclient.Dispose(); | |
return; | |
} | |
function unzip($zipFile, $dest) { | |
Write-Host "Extracting $($zipFile) to $($dest)`n" | |
if(Test-Path($dest)) { | |
Write-Host "$dest exists - skipping`n"; | |
return; | |
} | |
md -Force $dest; | |
$shell = new-object -com shell.application; | |
$zip = $shell.namespace($zipFile); | |
$destDir = $shell.Namespace($dest); | |
$itemCount = $zip.items().Count; | |
$destDir.copyhere($zip.items()); | |
Start-Sleep -Seconds 1 | |
} | |
function downloadUmbracoVersion($version, $dir) { | |
$id = $version -replace "\.", ""; | |
$hostname = "http://our.umbraco.org"; | |
$infoFile = "$dir\$($id).html"; | |
$zipFile = "$dir\umbraco_$($version).zip"; | |
$binariesDir = "$downloadsDir\umbraco_$($version)"; | |
downloadFile "$hostname/contribute/releases/$id/" $infoFile | |
[regex]$pattern = '/ReleaseDownload\?id=\d+'; | |
$info = get-content $infoFile; | |
$_matches = $pattern.Matches($info); | |
if($_matches.Count -gt 0) { | |
$downloadUri = $_matches[0]; | |
downloadFile "$hostname$downloadUri" $zipFile; | |
} | |
unzip $zipFile $binariesDir; | |
$binariesDir; | |
return; | |
} | |
function downloadUmbracoFixup($dir) { | |
$uri = "http://our.umbraco.org/FileDownload?id=4999"; | |
$zipFile = "$dir\PathFixup.zip"; | |
$binariesDir = "$dir\PathFixup"; | |
downloadFile $uri $zipFile | |
unzip $zipFile $binariesDir; | |
} | |
function upgradeDatabase($sitehost) { | |
$url = "http://$sitehost/install/?installStep=license"; | |
Write-Host "Retrieving View State $url"; | |
$response = getUrl($url); | |
if([string]::IsNullOrEmpty($response)) { | |
Write-Host 'No Response'; | |
return; | |
} | |
$__VIEWSTATE = (echo $response | Select-String -Pattern '<input.*name="__VIEWSTATE".*value="(.+)".*>').Matches[0].Groups[1]; | |
$__VIEWSTATEGENERATOR = (echo $response | Select-String -Pattern '<input.*name="__VIEWSTATEGENERATOR".*value="(.+)".*>').Matches[0].Groups[1]; | |
$__VIEWSTATE = [System.Web.HttpUtility]::UrlEncode($__VIEWSTATE); | |
$__VIEWSTATEGENERATOR = [System.Web.HttpUtility]::UrlEncode($__VIEWSTATEGENERATOR); | |
Write-Host "Attempt to upgrade database"; | |
$Body = "__EVENTTARGET=ctl09%24ctl00&__EVENTARGUMENT=&__VIEWSTATE=$($__VIEWSTATE)&__VIEWSTATEGENERATOR=$($__VIEWSTATEGENERATOR)&database=blank&ctl09%24DatabaseType=SqlServer&ctl09%24DatabaseServer=$dbhost&ctl09%24DatabaseName=$dbname&ctl09%24DatabaseUsername=$dbuser&ctl09%24DatabasePassword=$dbpass&ctl09%24ConnectionString="; | |
postUrl "http://$sitehost/install/?installStep=database" $Body; | |
getUrl "http://$sitehost/install/?installStep=theend"; | |
} | |
function backup($cwd) { | |
Write-Host "`nBackup Umbraco installed in the following directory: $($cwd)`n" | |
$backupsDir = md -force "$cwd\upgrade_files\backups" | |
Copy-Item -path "$cwd\bin" -destination "$backupsDir\bin" -recurse | |
Copy-Item -path "$cwd\umbraco" -destination "$backupsDir\umbraco" -recurse | |
Copy-Item -path "$cwd\umbraco_client" -destination "$backupsDir\umbraco_client" -recurse | |
Copy-Item -path "$cwd\config" -destination "$backupsDir\config" -recurse | |
Copy-Item -path "$cwd\*.aspx" -destination "$backupsDir" -recurse | |
Copy-Item -path "$cwd\*.asax" -destination "$backupsDir" -recurse | |
Copy-Item -path "$cwd\*.config" -destination "$backupsDir" -recurse | |
} | |
function toggleVega($cwd, $enabled) { | |
[xml]$config = get-content "$cwd\web.config" | |
$modules = $config.SelectSingleNode("//configuration/system.web/httpModules"); | |
$add = $modules.SelectSingleNode('add[@name="USiteBuilderHttpModule"]'); | |
if(!$add) { | |
$add = $config.CreateElement("add"); | |
$attr = $config.CreateAttribute("name"); | |
$attr.Value = "USiteBuilderHttpModule"; | |
$add.Attributes.Append($attr); | |
$attr = $config.CreateAttribute("type"); | |
$attr.Value = "Vega.USiteBuilder.USiteBuilderHttpModule, Vega.USiteBuilder"; | |
$add.Attributes.Append($attr); | |
$modules.AppendChild($add); | |
} | |
$remove = $modules.SelectSingleNode('remove[@name="USiteBuilderHttpModule"]'); | |
if($enabled -and $remove) { | |
$modules.RemoveChild($remove); | |
} elseif (!$enabled -and !$remove) { | |
$remove = $config.CreateElement("remove"); | |
$attr = $config.CreateAttribute("name"); | |
$attr.Value = "USiteBuilderHttpModule"; | |
$remove.Attributes.Append($attr); | |
$modules.InsertAfter($remove, $add); | |
} | |
$modules = $config.SelectSingleNode("//configuration/system.webServer/modules"); | |
$add = $modules.SelectSingleNode('add[@name="USiteBuilderHttpModule"]'); | |
if(!$add) { | |
$add = $config.CreateElement("add"); | |
$attr = $config.CreateAttribute("name"); | |
$attr.Value = "USiteBuilderHttpModule"; | |
$add.Attributes.Append($attr); | |
$attr = $config.CreateAttribute("type"); | |
$attr.Value = "Vega.USiteBuilder.USiteBuilderHttpModule, Vega.USiteBuilder"; | |
$add.Attributes.Append($attr); | |
$modules.AppendChild($add); | |
} | |
$remove = $modules.SelectSingleNode('remove[@name="USiteBuilderHttpModule"]'); | |
if($enabled -and $remove) { | |
$modules.RemoveChild($remove); | |
} elseif (!$enabled -and !$remove) { | |
$remove = $config.CreateElement("remove"); | |
$attr = $config.CreateAttribute("name"); | |
$attr.Value = "USiteBuilderHttpModule"; | |
$remove.Attributes.Append($attr); | |
$modules.InsertAfter($remove, $add); | |
} | |
$config.Save("$cwd\web.config"); | |
} | |
function upgrade($cwd, $versionNumber) { | |
Write-Host "`nUpdate Umbraco installed in the following directory: $($cwd) to $version`n" | |
$downloadsDir = md -force "$cwd\upgrade_files\downloads" | |
$binaryDir = downloadUmbracoVersion $version $downloadsDir | |
if($install) { | |
before_upgrade $cwd; | |
do_upgrade $cwd $versionNumber $binaryDir $hostname; | |
after_upgrade $cwd; | |
} | |
} | |
function before_upgrade($cwd) { | |
# custom | |
Write-Host "Begin Upgrade"; | |
} | |
function do_upgrade($cwd, $versionNumber, $binaryDir, $hostname) { | |
# copy required umbraco files (make sure web.config is backed up! | |
Copy-Item -path "$binaryDir\bin" -destination "$cwd" -recurse -force | |
Copy-Item -path "$binaryDir\umbraco" -destination "$cwd" -recurse -force | |
Copy-Item -path "$binaryDir\umbraco_client" -destination "$cwd" -recurse -force | |
Copy-Item -path "$binaryDir\install" -destination "$cwd" -recurse -force | |
Copy-Item -path "$binaryDir\web.config" -destination "$cwd" -force | |
Copy-Item -path "$binaryDir\default.aspx" -destination "$cwd" -force | |
if($versionNumber -ge 40702) { | |
removeFile "$cwd\bin\umbraco.MacroEngines.Legacy.dll" | |
} | |
if($versionNumber -ge 40800) { | |
# delete legacy files that cause conflict with global.asax | |
removeFile "$cwd\bin\App_Browsers.dll" | |
removeFile "$cwd\bin\App_global.asax.dll" | |
removeFile "$cwd\bin\Fizzler.Systems.HtmlAgilityPack.dll" | |
} | |
if($versionNumber -ge 41000) { | |
Copy-Item -path "$binaryDir\App_Plugins" -destination "$cwd" -recurse -force | |
Copy-Item -path "$binaryDir\Views" -destination "$cwd" -recurse -force | |
Copy-Item -path "$binaryDir\Global.asax" -destination "$cwd" -force | |
copyFile "$binaryDir\config\BaseRestExtensions.config" "$cwd\config\BaseRestExtensions.config" | |
copyFile "$binaryDir\config\EmbeddedMedia.config" "$cwd\config\EmbeddedMedia.config" | |
copyFile "$binaryDir\config\feedProxy.config" "$cwd\config\feedProxy.config" | |
copyFile "$binaryDir\config\FileSystemProviders.config" "$cwd\config\FileSystemProviders.config" | |
removeFile "$cwd\bin\umbraco.linq.core.dll" | |
removeFile "$cwd\config\formHandlers.config" | |
} | |
} | |
function after_upgrade($cwd) { | |
# custom | |
Write-Host "Upgrade Complete"; | |
} | |
function realVersion($semver) { | |
$parts = $semver.Split("."); | |
$result = 10000 * $parts[0]; # 40000 | |
$result += 100 * $parts[1]; # 41100 | |
$result += $parts[2]; # 41110 | |
$result; | |
return; | |
} | |
function main() { | |
$cwd = Convert-Path $target | |
if($clean) { | |
Remove-Item "$cwd\upgrade_files" -recurse -force | |
} elseif ($backup) { | |
backup $cwd | |
} elseif ($enableVega) { | |
toggleVega $cwd $true; | |
} elseif ($disableVega) { | |
toggleVega $cwd $false; | |
} elseif ($install -or $download) { | |
if([string]::IsNullOrEmpty($version)) { | |
Write-Host "Please specify version."; | |
return; | |
} | |
$versionNumber = realVersion($version); | |
upgrade $cwd $versionNumber | |
} elseif($upgrade) { | |
if([string]::IsNullOrEmpty($sitehost)) { | |
Write-Host "Please provide sitehost and db details"; | |
return; | |
} | |
# trigger stupid vega shit | |
$response = getUrl("http://$sitehost"); | |
toggleVega $cwd $false; | |
upgradeDatabase $sitehost; | |
toggleVega $cwd $true; | |
} else { | |
Write-Host "Please specify action -download or -install or -upgrade or -clean" | |
return; | |
} | |
} | |
Add-Type -AssemblyName System.Web | |
# run main | |
main |
.\upgrade.ps1 4.11.10 -download
.\upgrade.ps1 4.11.10 -install
.\upgrade.ps1 -upgrade -sitehost <hostname> -dbhost <dbhost> -dbname <dbname> -dbuser <dbuser> -dbpass <dbpass>
.\upgrade.ps1 -enableVega
.\upgrade.ps1 -disableVega
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS (haha) : PS sucks
PSS: I suck at PS too