Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
-
- ##################################################
- # MerlotDeploy.ps1
- # Created by: Tzvi Friedman
- # This Script extracts, modifies, and placed Merlot deploy forlders to the appropriate folsders in a given Merlot environment
- #
- #####################################################
- #This creates a function called LogWrite. This is what will be creating a log file for each run of the script. It takes whatever input you give it, and adds it to the file it creates at the begining of the script.
- # Gets current date/Time to timestamp log
- $date = get-date -uformat "%m%d%y%H%M"
- # Create Log file
- Function LogWrite
- {
- Param ([string]$logstring)
- [string]$date = get-date -uformat "%m%d%y%H%M"
- $value = $date + " " + $logstring
- Add-content $Logfile -value $value
- }
- ##This created the cmdlet prepare-merlot. It starts with denoting which parameters it accepts as arguments.
- function prepare-merlot{
- param(
- # Chooses set of servers for the deploy
- [Parameter(Mandatory=$true)]
- [ValidateSet('stg','prd','ndd','poc')]
- $environment,
- # This sets the timestamp for folder renaming to the current day. Can be changed if neccessary
- [String]$timestamp = (get-date -uformat "%m%d%y"),
- # Sets the location of the zipped downloads
- [Parameter(Mandatory=$true)]
- [String]$FileLocation
- )
- ##The script creates a log file for the current run and appends a header with parameter values
- # Remove log file is one for this timestamp already exists
- $Logfile = "C:\$env:homepath\Documents\MerlotLog_$date.log"
- if(gci $logfile){remove-item $logfile -Force -ErrorAction SilentlyContinue}
- LogWrite *************************************************************
- LogWrite "Beginning Merlot Deploy $date"
- Logwrite "Current Values: Env: $environment "
- Logwrite "Filelocation: $FileLocation"
- LogWrite *************************************************************
- ##The script creates the Merlot Deploy folders, and extracts all files in the archives into their respective folders. Since the extract is recursive on each folder in the archive, you will see a lot of file copy popups during this process.
- #Unzip the files to the current location
- #create new Directories
- write-host Creating Merlot Deploy Directories
- Logwrite "Creating directories"
- New-Item -ItemType Directory -Path $filelocation -Name MerlotApplication -ErrorAction SilentlyContinue
- New-Item -ItemType Directory -Path $filelocation -Name MerlotJobEngine -ErrorAction SilentlyContinue
- New-Item -ItemType Directory -Path $filelocation -Name MerlotPortal -ErrorAction SilentlyContinue
- Logwrite ""
- Logwrite "Extracting from source archives"
- write-host Extracting from source archives
- #look for the Merlot Application Files and unzip to new directory
- $AppFile = gci $fileLocation "*Application.zip"
- $shell = new-object -com shell.application
- $zip = $shell.NameSpace($appfile.FullName)
- foreach($item in $zip.items())
- {
- $shell.Namespace(“$filelocation\MerlotApplication”).copyhere($item)
- $filename = $item.path
- logwrite "Extracted $filename"
- }
- ##look for the Merlot JobEngine Files and unzip to new directory
- $AppFile = gci $fileLocation "*JobEngine.zip"
- $shell = new-object -com shell.application
- $zip = $shell.NameSpace($appfile.FullName)
- foreach($item in $zip.items())
- {
- $shell.Namespace(“$filelocation\MerlotJobEngine”).copyhere($item)
- $filename = $item.path
- logwrite "Extracted $filename"
- }
- ##look for the Merlot JobEngine Files and unzip to new directory
- $AppFile = gci $fileLocation "*portals.zip"
- $shell = new-object -com shell.application
- $zip = $shell.NameSpace($appfile.FullName)
- foreach($item in $zip.items())
- {
- $shell.Namespace(“$filelocation\MerlotPortal”).copyhere($item)
- $filename = $item.path
- logwrite "Extracted $filename"
- }
- #This section takes your environement parameter, and determines some important information with it: which servers we're deploying to, which connection string we're insterting into config files, and which drive letter on the destination server we'll be copying the files to.
- #determine which set of servers we're stopping depending on $environment variable
- $env = $environment
- Switch ($env)
- {
- "stg"
- {
- $eaServers = "vswstgmerea01", "vswstgmerea02"
- $paServers = "vswstgmerpa01", "vswstgmerpa02"
- $jeServers = "vswstgmerje01", "vswstgmerje02"
- $shServers = "vswstgmersh01", "vswstgmersh02"
- $connectStr = "Data Source=vswstgmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
- $driveletter = "D"
- }
- "prd"{
- $eaServers = "vswprdmerea01", "vswprdmerea02"
- $paServers = "vswprdmerpa01", "vswprdmerpa02"
- $jeServers = "vswprdmerje01", "vswprdmerje02"
- $shServers = "vswprdmersh01", "vswprdmersh02"
- $connectStr= "Data Source=vswprdmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
- $driveletter = "E"
- }
- "ndd"{
- $eaServers = "nddprdmerea01", "nddprdmerea02"
- $paServers = "nddprdmerpa01", "nddprdmerpa02"
- $jeServers = "nddprdmerje01", "nddprdmerje02"
- $shServers = "nddprdmersh01", "nddprdmersh02"
- $connectStr= "Data Source=nddprdmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
- $driveletter = "D"
- }
- "poc"
- {
- $eaServers = "lhqpocmerwb01", "lhqpocmerwb02"
- $paServers = "lhqpocmerpa01", "lhqpocmerpa02"
- $jeServers = "lhqpocmerje01", "lhqpocmerje02"
- $shServers = "lhqpocmersh01", "lhqpocmersh02"
- $connectStr= "Data Source=lhqpocmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
- $driveletter = "E"
- }
- }
- Logwrite "*******************************"
- Logwrite "current Servers"
- Logwrite "EA: $easervers"
- Logwrite "JE: $jeservers"
- Logwrite "PA: $paservers"
- Logwrite "SH: $shservers"
- Logwrite "Current Connection String: $connectStr"
- Logwrite "Data Drive is on Volume $driveletter"
- Logwrite "*******************************"
- #As a precaution, the script will not shut down services without asking for explicit permission from the user to continue. If the user refuses, the script will abort.
- # After servers and connection string are confirmed, ask for permission to shut down IIS
- [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
- Logwrite "Asked for permission to Shut down IIS and Merlot services on Selected Merlot servers"
- $confirm = [System.Windows.Forms.Messagebox]::show("OK to shutdown IIS on Merlot IIS servers on $environment","Confirm IIS Shutdown", 4)
- #Logwrite "Asked for permission to Shut down IIS and Merlot services on Selected Merlot servers"
- if($confirm -eq "yes"){
- logwrite "User selected Yes. Shutting down services"
- $easervers | %{get-service w3svc -ComputerName $_ | Stop-Service ; logwrite "Stopped IIS on $_"}
- $paservers | %{get-service w3svc -ComputerName $_ | Stop-Service ; logwrite "Stopped IIS on $_"}
- $shservers | %{get-service w3svc -ComputerName $_ | Stop-Service ; logwrite "Stopped IIS on $_"}
- $jeservers | %{get-service "Merlot Jobengine" -ComputerName $_ | Stop-Service; logwrite "Stopped Merlot Job Engine on $_"}
- }
- else
- {
- logwrite "User selected NO. Exiting..."
- break}
- #In most cases, a DacPac will need to be run on the database before anything esle can contonue. The script pauses for this to happen, and waits for the user to manually press OK before continuing.
- #Pause Script to let DacPac Run
- [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
- $confirm = [System.Windows.Forms.Messagebox]::show("If DacPac needs to be run, do it now. When complete, press OK.","Confirm DacPac Run", 0 )
- ##rename the web.config files in each Merlot Application and Portal deploy folders
- rename-item $filelocation\MerlotApplication\Web.Svn.Config -NewName Web.Config
- LogWrite "renamed $filelocation\MerlotApplication\Web.Svn.Config to Web.config"
- rename-item $filelocation\MerlotApplication\Services\Web.Svn.Config -NewName Web.Config
- logwrite "renamed $filelocation\MerlotApplication\Services\Web.Svn.Config to web.config"
- rename-item $filelocation\MerlotPortal\Common\Web.Svn.Config -NewName Web.Config
- logwrite "renamed $filelocation\MerlotApplication\Common\Web.Svn.Config to web.config"
- rename-item $filelocation\MerlotPortal\Crew\Web.Svn.Config -NewName Web.Config
- logwrite "renamed $filelocation\MerlotPortal\Crew\Web.Svn.Config to Web.config"
- rename-item $filelocation\MerlotPortal\Aircraft\Web.Svn.Config -NewName Web.Config
- logwrite "renamed $filelocation\MerlotPortal\Aircraft\Web.Svn.Config to Web.config"
- rename-item $filelocation\MerlotPortal\Admin\Web.Svn.Config -NewName Web.Config
- logwrite "renamed $filelocation\MerlotPortal\Admin\Web.Svn.Config to Web.config"
- ##modify the web.config files in the Merlot Application Deploy Folder. There are XML changes that need to happen in each deploy folder. The script is taking the XML content as a .NET XML object, drilling down into the appropriate element, modifying the element and then saving the modified XML to the original location
- [xml]$appxml = get-content $filelocation\MerlotApplication\services\web.config
- $appxml.childnodes.location.connectionstrings.add.connectionString = $connectStr
- $appxml.Save("$filelocation\MerlotApplication\services\web.config")
- logwrite "Changed Connection String in $filelocation\MerlotApplication\services\web.config to $connectstr"
- #Modify config files in Merlot Job Engine Deploy Folders
- ##Modify the connection string
- [xml]$jexml = get-content "$FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config"
- $jexml.configuration.connectionStrings.add.connectionString = $connectStr
- logwrite "Changed Connection String in $FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config to $connectstr"
- ##Modify the MaxBackoff from 60sec to 1min
- $jexml.configuration.RetryPolicyConfiguration.exponentialBackoff.maxBackoff = "00:01:00"
- logwrite "Modified MaxBackoff Element in $FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config to 00:01:00"
- ##In this case (The InstanceNumber element), since we're not just modifying the value of an element. but add a new key/value pair, the process is a little different. It creates an XML element object that contains the key and value, and uses ,NET in the XML object to insert that into the proper node. The Merlot deploy instructions are to have the 01 server have a vlue of 0, and the 02 server have a value of 1. Since we only have one copy of the config file at this point, we will set to the value to 0. Once the files get copied to botyh server, we will run this again on the 02 server and change the value to 1.
- ##Add the Instance modifier element. This will be modified to the appropriate values on the remote server when the folder gets moved
- [xml]$element = @"
- <add key="InstanceNumber" value="0" />
- "@
- $jexml.configuration.appSettings.AppendChild($jexml.ImportNode($element.add, $true))
- $jexml.save("$FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config")
- logwrite "Added InstanceNumber element to $FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config. Set value to 0. The 1 value will be set after files are copied to destination servers"
- #Modify the web.config files in the MerlotPortal Deploy Folders
- $PortalFolders = "Common","Crew","Aircraft","Admin"
- $PortalFolders | %{
- [xml]$PorXML = get-content $FileLocation\MerlotPortal\$_\web.config
- $PorXML.configuration.connectionStrings.add[0].connectionString = $connectStr
- $PorXML.save("$FileLocation\MerlotPortal\$_\web.config")
- logwrite "Changed Connection String in $FileLocation\MerlotPortal\$_\web.config to $connectstr"
- }
- ##On the destination server, this will rename the current Merlot folders to thje timestamped archive version before copying to new files over.
- #Rename the current Folders in the Data drive of each server with a timestamped version
- $shortdate = get-date -UFormat %m%d%Y
- $easervers | %{rename-item "\\$_\$driveletter$\MerlotApplication" -NewName MerlotApplication_$shortdate ; logwrite "renamed \\$_\$Driveletter$\MerlotApplication to MerlotApplication_$shortdate" }
- $jeservers | %{rename-item "\\$_\$driveletter$\MerlotJobEngine" -NewName MerlotJobEngine_$shortdate ; logwrite "renamed \\$_\$driveletter$\MerlotJobngine to MerlotJobEngine_$shortdate"}
- $paServers | %{rename-item "\\$_\$driveletter$\MerlotPortal" -NewName MerlotPortal_$shortdate ; logwrite "renamed \\$_\$driveletter$\MerlotPortal to MerlotPortal_$shortdate"
- rename-item "\\$_\$driveletter$\MerlotApplication" -NewName MerlotApplication_$shortdate; logwrite "renamed \\$_\$driveletter$\MerlotApplication to MerlotApplication_$shortdate"}
- #This section is where the files are moved to the servers, Since we ran into problems moving large files to NDD during periods of congestion, there is an option here to copy the files manually (using SFT or something else) to get around this problem. The script will pause and wait for confirmation that this is completed.
- #move Folders from local server to Merlot Destinations
- #Since bandwidth can be problematic to NDD, if env is NDD, ask if user weants to copy manually
- $confirm = "Cancel"
- if ($environment -eq "ndd")
- {$confirm = [System.Windows.Forms.Messagebox]::show("You are about to copy large files to NDD. Due to possible network congestion, you may want to copy these files manually. If you `
- want to copy the files manually, do so now and press OK when done. If you want to let the script copy the files, press Cancel","Confirm NDD File Copy", 1)
- }
- If ($confirm -eq "Cancel")
- {
- $easervers | %{copy-item $FileLocation\MerlotApplication -Recurse -Destination "\\$_\$driveletter$"; logwrite "Moved $FileLocation\MerlotApplication to $_\$driveletter$\" }
- $paservers | %{copy-Item $FileLocation\MerlotApplication -Recurse -Destination "\\$_\$driveletter$"; logwrite "Moved $FileLocation\MerlotApplication to $_\$driveletter$\"
- copy-item $FileLocation\MerlotPortal -Recurse -Destination "\\$_\$driveletter$"; logwrite "Moved $FileLocation\MerlotPortal to $_\$driveletter$\"}
- $jeservers | %{copy-item $FileLocation\MerlotJobEngine -Recurse -Destination "\\$_\$driveletter$" ; logwrite "Moved $FileLocation\MerlotJobEngine to $_\$driveletter$\"}
- }
- #As discussed above, this is where we change the value of instancenumber to 1 on the destination server.
- #modify XML in secondary jobengine server to change InstanceNUmber to 1
- $jeserver = $jeservers[1]
- [xml]$newJeXML = Get-Content \\$jeserver\$driveletter$\MerlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config
- [xml]$element = @"
- <add key="InstanceNumber" value="1" />
- "@
- $newjexml.configuration.appSettings.AppendChild($newjexml.ImportNode($element.add, $true))
- $newjexml.save("\\$jeserver\$driveletter$\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config")
- logwrite "Changed InstanceNumber on $jeserver to 1"
- #Once all necessary items are completed, prompt for permission to restart merlot services.
- #restart Merlot Services
- logwrite "Asking Permission to restart Merlot services"
- $confirm = [System.Windows.Forms.Messagebox]::show("OK to Turn on IIS on Merlot IIS servers on $environment","Confirm IIS Startup", 4)
- if($confirm -eq "yes"){
- $easervers | %{get-service w3svc -ComputerName $_ | Start-Service ; logwrite "started IIS on $_"}
- $paservers | %{get-service w3svc -ComputerName $_ | Start-Service; logwrite "Started IIS on $_"}
- $shservers | %{get-service w3svc -ComputerName $_ | Start-Service ; logwrite "Started IIS on $_"}
- $jeservers | %{get-service "Merlot Jobengine" -ComputerName $_ | Start-Service ; logwrite "Started Merlot Job Engine on $_"}
- }
- else
- {break}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement