Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force $verbosePreference = "Continue" // Relies on Powershell.MWA for Internet Information Servives routines // Download to .\Powershell.MWA-master // @see https://github.com/jgigler/Powershell.MWA Import-Module WebAdministration Import-Module "$(Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)\Powershell.MWA-master\Powershell.MWA.psm1" Push-Location $env:temp <# .SYNOPSIS IIS deployment utilities .DESCRIPTION Run deploy powesrshell as administrator. .EXAMPLE powershell> Import-Module C:\inetpub\sphsc_website_admin\deploy\sphsc-deploy.ps1 powershell> My-Function powershell> sRunAdministatorScripts -doDatabseUpdate $true .EXAMPLE CreateSite -ComputerName "" -SiteName "spchs-testy" .NOTES #> $NL = "`r`n=============================== " function sphsc_update_admin_scripts { [CmdletBinding()] Param ( [Parameter(Mandatory=$false)] [string]$repoAddress = "https://meyerjn@bitbucke.org/meyerjn/shsc_admin_www.git", [Parameter(Mandatory=$false)] [string]$destinationPath = "C:\inetpub\sphsc_website_admin" ) Begin {} Process { "$NL Extracting admin repository into.... $destinationPath" _remove_folder -path $destinationPath _download_repo -destinationPath $destinationPath -repoAddress $repoAddress } } function sphsc_import_database { [CmdletBinding()] Param ( [Parameter(Mandatory=$false)] [bool]$doDatabseUpdate = $false, [Parameter(Mandatory=$false)] [string]$databaseUpdateScript = "C:\inetpub\sphsc_website_admin\db\database-update.mysql" ) Begin {} Process { if($doDatabseUpdate -eq $true) { "$NL Importing script into MySQL...... $databaseUpdateScript" _configure_db -databaseImportFile $databaseUpdateScript } } } function sphsc_deploy_website { [CmdletBinding()] Param () Begin {} Process { _do_deploy } } function _remove_folder { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$path = "" ) Begin {} Process { "$NL Removing $path....." if((Test-Path -Path $path)) { Remove-Item -Path $path -Recurse -Force } } } function _create_folder { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$path = "", [Parameter(Mandatory=$false)] [string]$prune = $false ) Begin {} Process { "$NL Creating $path....." if($prune -eq $true) { _remove_folder -path $path } if(!(Test-Path -Path $path)) { New-Item -ItemType Directory -Path $path -Verbose:$true } Dir $path } } function _prune_folder { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$path = "", [Parameter(Mandatory=$false)] [int]$numberToKeep = 10 ) Begin {} Process { $dirs = dir -Path $path -Directory "$NL Found $($dirs.Count) archive folders" if ($dirs.count -ge $numberToKeep) { "$NL Deleting folders older than first $numberToKeep...." " In $archiveDir" $oldest = $dirs | Sort-Object CreationTime -Descending | Select-Object -First ($dirs.Count - $numberToKeep) $oldest | Format-List $oldest | Remove-Item -Recurse -Force } } } function _archive_folder { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)] [string]$sourcePath = "", [Parameter(Mandatory=$true)] [string]$destinationPath = "" ) Begin {} Process { "$NL Creating folder $destinationPath..... " _create_folder -path $destinationPath -prune $true "$NL Arhive copying $sourcePath....." " to $destinationPath" Copy-Item $sourcePath -Destination $destinationPath -Recurse -Force Dir $destinationPath } } function _archive_db { [CmdletBinding()] Param ( [Parameter(Mandatory=$false)] [string]$databaseName = "sphsc_drupal", [Parameter(Mandatory=$true)] [string]$destinationPath = "" ) Begin {} Process { "$NL Creating folder $destinationPath..... " _create_folder -path $destinationPath -prune $true $fileName = "$destinationPath\$databaseName.sql" "$NL Outputting MySQL dump of '$databaseName' to ..... $fileName" mysqldump -u root --databases $databaseName > $fileName Dir $destinationPath "$NL Creating MySQL hotcopy of database....." $databaseSource = $env:ProgramFiles +"\MySQL\MySQL Server 5.5\data\" + $databaseName if(!(Test-Path -Path $databaseSource)) { $databaseSource = mysql -uroot -h'127.0.0.1' -sNe"SELECT @@datadir" } "$NL Copying $databaseSource .... to $destinationPath" Copy-Item "$databaseSource" -Destination "$destinationPath" -Recurse -Force Dir $destinationPath } } function _download_repo { [CmdletBinding()] Param ( [Parameter(Mandatory=$false)] [string]$repoAddress = "", [Parameter(Mandatory=$true)] [string]$destinationPath = "" ) Begin {} Process { _create_folder -path $destinationPath #Import-Module posh-git #$sshKey = "C:\inetpub\_git_ssh_keys\id_rsa" #$env:path += ";${env:ProgramFiles(x86)}\Git\bin" #ssh-agent bash -c "ssh-add $sshKey; git clone meyerjn@bitbucket.org:meyerjn/shscience_dr7v1.git "$NL Downloading clone of repository...." git clone "$repoAddress" "$destinationPath" "$NL Trying a git pull...." git pull "$repoAddress" } } function _configure_db { [CmdletBinding()] Param ( [Parameter(Mandatory=$false)] [string]$databaseName = "sphsc_drupal", [Parameter(Mandatory=$false)] [string]$databaseUser = "sphsc_drupal", [Parameter(Mandatory=$false)] [string]$databaseImportFile = "", # e.g. "./db/import.sql" [Parameter(Mandatory=$false)] [string]$destinationPath = "" ) Begin {} Process { function _addDatabaseUser () { "$NL Creating no-password database account...." mysql -uroot -h'127.0.0.1' --verbose -e "GRANT ALL ON ``$databaseName``.* TO '$databaseUser'" mysql -uroot -h'127.0.0.1' --verbose -e "FLUSH PRIVILEGES" mysql -uroot -h'127.0.0.1' --verbose -e "SET PASSWORD FOR '$databaseUser'@'localhost' = PASSWORD('')" mysql -uroot -h'127.0.0.1' --verbose -e "FLUSH PRIVILEGES" #"$NL Testing MySQL access....." #mysql --user="$databaseUser" --database="$databaseName" -e "SHOW TABLES" } function _addConnectionString () { $settingsFile = "$destinationPath\sites\default\settings.php" $connectionString = "`$databases['default']['default'] = array ( 'host' => 'localhost', 'port' => '', 'driver' => 'mysql', 'prefix' => '', 'database' => '$databaseName', 'username' => '$databaseUser', 'password' => '', );" "$NL Adding database connection string to $settingsFile....." Add-Content -Path "$settingsFile" -Value "`r`n$connectionString" Get-Content -Path "$settingsFile" -Tail 22 } function _importDatabase () { "$NL Creating database $databaseName....." mysql -uroot -h'127.0.0.1' --verbose -e "DROP DATABASE IF EXISTS $databaseName" mysql -uroot -h'127.0.0.1' --verbose -e "CREATE DATABASE ``$databaseName`` CHARACTER SET utf8 COLLATE utf8_general_ci" "$NL Importing database into $databaseName from $databaseImportFile....." mysql -uroot -h'127.0.0.1' $databaseName -e "source $($databaseImportFile)" } if($databaseUser -ne "") { _addDatabaseUser } if($databaseImportFile -ne "") { _importDatabase } if($destinationPath -ne "") { _addConnectionString } } } function _do_deploy { [CmdletBinding()] Param ( [Parameter(Mandatory=$false)] [string]$hostName = "staging.sphsc.washington.edu", [Parameter(Mandatory=$false)] [string]$databaseName = "sphsc_drupal", [Parameter(Mandatory=$false)] [string]$repository = "https://meyerjn@bitbucket.org/meyerjn/shsciences_dr7v1.git", [Parameter(Mandatory=$false)] [string]$backupFolders = $true ) Begin {} Process { try { function _backupFolders() { if($backupFolders -eq $true) { return; } "$NL Creating web folders......." $dir = (Join-Path $env:SystemDrive "i-archive") $dateString = "" + $(Get-Date).Year + $(Get-Date).Month + $(Get-Date).Day _create_folder -path $dir _prune_folder -path $dir _create_folder -path $tempPath -prune $true _create_folder -path $sitePath "$NL Copying to archives......" _archive_folder -sourcePath $sitePath -destinationPath (Join-Path $dir $($hostName +"-"+ $dateString +"-"+ "website")) _archive_db -destinationPath (Join-Path $dir $($hostName +"-"+ $dateString +"-"+ "database")) } function _copyUserFiles() { "$NL Creating Drupal's private folder......" $privateFolder = "$(Split-Path $sitePath)\private_for_drupal" _create_folder -path $privateFolder icacls $privateFolder /grant Everyone:F /t "$NL Copying user uploads....." $old = Join-Path $sitePath "\sites\default\files" $new = Join-Path $tempPath "\sites\default\" Copy-Item -Path $old -Destination $new -Recurse -Force icacls $new /grant Everyone:F /t } cls "$NL $NL RUNNING DEPLOY DEFAULTS...... $NL" $sitePath = Join-Path $env:SystemDrive (Join-Path "inetpub" $hostName) $tempPath = $sitePath +"temp" $prevPath = $sitePath +"old" _backupFolders "$NL Downloading Drupal to temp...... $tempPath" _download_repo -destinationPath $tempPath -repoAddress $repository _copyUserFiles "$NL Setting up database....." _configure_db -destinationPath $tempPath $test = "$destinationPath\sites\default\settings.php" if(!(Test-Path $test)) { throw [System.IO.FileNotFoundException] "$test not found." } "$NL Removing current IIS site...." Remove-Website -Name "$hostName" "$NL Renaming temp folders....... $tempPath...becomes... $sitePath...." _remove_folder -path $prevPath Rename-Item -Path $sitePath -NewName $prevPath Rename-Item -Path $tempPath -NewName $sitePath "$NL Adding IIS site (using imported IMA modules)...." New-IisSite -ComputerName localhost -SiteName $hostName -CodePath $sitePath -Bindings "*:80:$($hostName)" Get-IisSite -ComputerName localhost -SiteName $hostName "$NL Starting the IIS website..... " Start-IisSite -ComputerName localhost -SiteName $hostName } catch { $exceptionMessage = "Error in Line: " + $_.Exception.Line + ". " + $_.Exception.GetType().FullName + ": " + $_.Exception.Message + " Stacktrace: " + $_.Exception.StackTrace #Restore-WebConfiguration $backupName "$NL $NL Erroring out...." $exceptionMessage "$NL Origional message...." throw } } } Get-Command -name "sphsc*"