Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 
  2. ##################################################
  3. # MerlotDeploy.ps1
  4. # Created by: Tzvi Friedman
  5. # This Script extracts, modifies, and placed Merlot deploy forlders to the appropriate folsders in a given Merlot environment
  6. #
  7. #####################################################
  8.  
  9. #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.
  10.  
  11. # Gets current date/Time to timestamp log
  12. $date = get-date -uformat "%m%d%y%H%M"
  13. # Create Log file
  14.  
  15. Function LogWrite
  16. {
  17.    Param ([string]$logstring)
  18.    [string]$date = get-date -uformat "%m%d%y%H%M"
  19.    $value = $date + " " + $logstring
  20.    
  21.    Add-content $Logfile -value $value
  22. }
  23.  
  24. ##This created the cmdlet prepare-merlot. It starts with denoting which parameters it accepts as arguments.
  25.  
  26. function prepare-merlot{
  27. param(
  28.     # Chooses set of servers for the deploy
  29.     [Parameter(Mandatory=$true)]
  30.     [ValidateSet('stg','prd','ndd','poc')]
  31.     $environment,
  32.    
  33.     # This sets the timestamp for folder renaming to the current day. Can be changed if neccessary
  34.     [String]$timestamp = (get-date -uformat "%m%d%y"),
  35.    
  36.     # Sets the location of the zipped downloads
  37.     [Parameter(Mandatory=$true)]
  38.     [String]$FileLocation
  39.  
  40.     )
  41.  
  42. ​​##The script creates a log file for the current run and appends a header with parameter values
  43. # Remove log file is one for this timestamp already exists
  44. $Logfile = "C:\$env:homepath\Documents\MerlotLog_$date.log"
  45. if(gci $logfile){remove-item $logfile -Force -ErrorAction SilentlyContinue}
  46.  
  47.     LogWrite *************************************************************
  48.     LogWrite "Beginning Merlot Deploy $date"
  49.     Logwrite "Current Values: Env: $environment "
  50.     Logwrite "Filelocation: $FileLocation"
  51.     LogWrite *************************************************************
  52.  
  53. ​​##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.
  54. #Unzip the files to the current location
  55.     #create new Directories
  56.    
  57.    
  58.    
  59.     write-host Creating Merlot Deploy Directories
  60.     Logwrite "Creating directories"
  61.     New-Item -ItemType Directory -Path $filelocation -Name MerlotApplication -ErrorAction SilentlyContinue
  62.     New-Item -ItemType Directory -Path $filelocation -Name MerlotJobEngine -ErrorAction SilentlyContinue
  63.     New-Item -ItemType Directory -Path $filelocation -Name MerlotPortal -ErrorAction SilentlyContinue
  64.    
  65.    
  66.     Logwrite ""
  67.     Logwrite "Extracting from source archives"
  68.     write-host Extracting from source archives
  69.     #look for the Merlot Application Files and unzip to new directory
  70.     $AppFile = gci $fileLocation "*Application.zip"
  71.     $shell = new-object -com shell.application
  72.     $zip = $shell.NameSpace($appfile.FullName)
  73.     foreach($item in $zip.items())
  74.     {
  75.     $shell.Namespace($filelocation\MerlotApplication”).copyhere($item)
  76.     $filename = $item.path
  77.     logwrite "Extracted $filename"
  78.     }
  79.    
  80.    
  81.     ##look for the Merlot JobEngine Files and unzip to new directory
  82.     $AppFile = gci $fileLocation "*JobEngine.zip"
  83.     $shell = new-object -com shell.application
  84.     $zip = $shell.NameSpace($appfile.FullName)
  85.     foreach($item in $zip.items())
  86.     {
  87.     $shell.Namespace($filelocation\MerlotJobEngine”).copyhere($item)
  88.     $filename = $item.path
  89.     logwrite "Extracted $filename"
  90.     }
  91.    
  92.    
  93.     ##look for the Merlot JobEngine Files and unzip to new directory
  94.     $AppFile = gci $fileLocation "*portals.zip"
  95.     $shell = new-object -com shell.application
  96.     $zip = $shell.NameSpace($appfile.FullName)
  97.     foreach($item in $zip.items())
  98.     {
  99.     $shell.Namespace($filelocation\MerlotPortal”).copyhere($item)
  100.     $filename = $item.path
  101.     logwrite "Extracted $filename"
  102.     }
  103.  
  104. #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.
  105. #determine which set of servers we're stopping depending on $environment variable
  106.  
  107.  
  108.  
  109. $env = $environment
  110. Switch ($env)
  111.     {
  112.         "stg"
  113.             {
  114.             $eaServers = "vswstgmerea01", "vswstgmerea02"
  115.             $paServers = "vswstgmerpa01", "vswstgmerpa02"
  116.             $jeServers = "vswstgmerje01", "vswstgmerje02"
  117.             $shServers = "vswstgmersh01", "vswstgmersh02"
  118.             $connectStr = "Data Source=vswstgmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
  119.             $driveletter = "D"
  120.             }
  121.         "prd"{
  122.             $eaServers = "vswprdmerea01", "vswprdmerea02"
  123.             $paServers = "vswprdmerpa01", "vswprdmerpa02"
  124.             $jeServers = "vswprdmerje01", "vswprdmerje02"
  125.             $shServers = "vswprdmersh01", "vswprdmersh02"
  126.             $connectStr= "Data Source=vswprdmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
  127.             $driveletter = "E"
  128.             }
  129.         "ndd"{
  130.             $eaServers = "nddprdmerea01", "nddprdmerea02"
  131.             $paServers = "nddprdmerpa01", "nddprdmerpa02"
  132.             $jeServers = "nddprdmerje01", "nddprdmerje02"
  133.             $shServers = "nddprdmersh01", "nddprdmersh02"
  134.             $connectStr= "Data Source=nddprdmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
  135.             $driveletter = "D"
  136.             }
  137.         "poc"
  138.             {
  139.             $eaServers = "lhqpocmerwb01", "lhqpocmerwb02"
  140.             $paServers = "lhqpocmerpa01", "lhqpocmerpa02"
  141.             $jeServers = "lhqpocmerje01", "lhqpocmerje02"
  142.             $shServers = "lhqpocmersh01", "lhqpocmersh02"
  143.             $connectStr= "Data Source=lhqpocmerdb01\Merlot;Initial Catalog=MerlotG4;User=MerlotDBUser;Password=dvMHhjuAZR;MultipleActiveResultSets=true;"
  144.             $driveletter = "E"
  145.             }
  146.      }
  147.  
  148. Logwrite "*******************************"
  149. Logwrite "current Servers"
  150. Logwrite "EA: $easervers"
  151. Logwrite "JE: $jeservers"
  152. Logwrite "PA: $paservers"
  153. Logwrite "SH: $shservers"
  154. Logwrite "Current Connection String: $connectStr"
  155. Logwrite "Data Drive is on Volume $driveletter"
  156. Logwrite "*******************************"
  157.  
  158. #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.​
  159. # After servers and connection string are confirmed, ask for permission to shut down IIS
  160. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
  161. Logwrite "Asked for permission to Shut down IIS and Merlot services on Selected Merlot servers"
  162.  
  163. $confirm = [System.Windows.Forms.Messagebox]::show("OK to shutdown IIS on Merlot IIS servers on $environment","Confirm IIS Shutdown", 4)
  164. #Logwrite "Asked for permission to Shut down IIS and Merlot services on Selected Merlot servers"
  165.  
  166. if($confirm -eq "yes"){
  167. logwrite "User selected Yes. Shutting down services"
  168. $easervers | %{get-service w3svc -ComputerName $_ | Stop-Service   ; logwrite "Stopped IIS on $_"}
  169. $paservers | %{get-service w3svc -ComputerName $_ | Stop-Service   ; logwrite "Stopped IIS on $_"}
  170. $shservers | %{get-service w3svc -ComputerName $_ | Stop-Service   ; logwrite "Stopped IIS on $_"}
  171. $jeservers | %{get-service "Merlot Jobengine" -ComputerName $_ | Stop-Service; logwrite "Stopped Merlot Job Engine on $_"}
  172. }
  173. else
  174.     {
  175.     logwrite "User selected NO. Exiting..."
  176.     break}
  177.  
  178. #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 cont​inuing.
  179. #Pause Script to let DacPac Run
  180.  
  181. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
  182. $confirm = [System.Windows.Forms.Messagebox]::show("If DacPac needs to be run, do it now. When complete, press OK.","Confirm DacPac Run", 0 )
  183.  
  184.  
  185.  
  186. ##rename the web.config files in each Merlot Application and Portal deploy folders
  187.  
  188. rename-item $filelocation\MerlotApplication\Web.Svn.Config -NewName Web.Config
  189.     LogWrite "renamed $filelocation\MerlotApplication\Web.Svn.Config to Web.config"
  190. rename-item $filelocation\MerlotApplication\Services\Web.Svn.Config -NewName Web.Config
  191.     logwrite "renamed $filelocation\MerlotApplication\Services\Web.Svn.Config to web.config"
  192. rename-item $filelocation\MerlotPortal\Common\Web.Svn.Config -NewName Web.Config
  193.     logwrite "renamed $filelocation\MerlotApplication\Common\Web.Svn.Config to web.config"
  194. rename-item $filelocation\MerlotPortal\Crew\Web.Svn.Config -NewName Web.Config
  195.     logwrite "renamed $filelocation\MerlotPortal\Crew\Web.Svn.Config to Web.config"
  196. rename-item $filelocation\MerlotPortal\Aircraft\Web.Svn.Config -NewName Web.Config
  197.     logwrite "renamed $filelocation\MerlotPortal\Aircraft\Web.Svn.Config to Web.config"
  198. rename-item $filelocation\MerlotPortal\Admin\Web.Svn.Config -NewName Web.Config
  199.     logwrite "renamed $filelocation\MerlotPortal\Admin\Web.Svn.Config to Web.config"
  200.    
  201.  
  202. ##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
  203.  
  204. [xml]$appxml = get-content $filelocation\MerlotApplication\services\web.config
  205. $appxml.childnodes.location.connectionstrings.add.connectionString = $connectStr
  206. $appxml.Save("$filelocation\MerlotApplication\services\web.config")
  207.     logwrite "Changed Connection String in $filelocation\MerlotApplication\services\web.config to $connectstr"
  208.  
  209. #Modify config files in Merlot Job Engine Deploy Folders
  210. ##Modify the connection string
  211. [xml]$jexml = get-content "$FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config"
  212. $jexml.configuration.connectionStrings.add.connectionString = $connectStr
  213.     logwrite "Changed Connection String in $FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config to $connectstr"
  214.  
  215.  
  216. ##Modify the MaxBackoff from 60sec to 1min
  217. $jexml.configuration.RetryPolicyConfiguration.exponentialBackoff.maxBackoff = "00:01:00"
  218.     logwrite "Modified MaxBackoff Element in $FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config to 00:01:00"
  219.  
  220. ##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.
  221. ##Add the Instance modifier element. This will be modified to the appropriate values on the remote server when the folder gets moved
  222. [xml]$element = @"
  223. <add key="InstanceNumber" value="0" />
  224. "@
  225. $jexml.configuration.appSettings.AppendChild($jexml.ImportNode($element.add, $true))
  226. $jexml.save("$FileLocation\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config")
  227.     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"
  228.  
  229. #Modify the web.config files in the MerlotPortal Deploy Folders
  230.  
  231. $PortalFolders = "Common","Crew","Aircraft","Admin"
  232. $PortalFolders | %{
  233.     [xml]$PorXML = get-content $FileLocation\MerlotPortal\$_\web.config
  234.     $PorXML.configuration.connectionStrings.add[0].connectionString = $connectStr
  235.     $PorXML.save("$FileLocation\MerlotPortal\$_\web.config")
  236.         logwrite "Changed Connection String in $FileLocation\MerlotPortal\$_\web.config to $connectstr"
  237.     }
  238.  
  239. ##On the destination server, this will rename the current Merlot folders to thje timestamped archive version before copying to new files over.
  240. #Rename the current Folders in the Data drive of each server with a timestamped version
  241. $shortdate = get-date -UFormat %m%d%Y
  242. $easervers | %{rename-item "\\$_\$driveletter$\MerlotApplication" -NewName MerlotApplication_$shortdate ; logwrite "renamed \\$_\$Driveletter$\MerlotApplication to MerlotApplication_$shortdate" }
  243. $jeservers | %{rename-item "\\$_\$driveletter$\MerlotJobEngine" -NewName MerlotJobEngine_$shortdate ; logwrite "renamed \\$_\$driveletter$\MerlotJobngine to MerlotJobEngine_$shortdate"}
  244. $paServers | %{rename-item "\\$_\$driveletter$\MerlotPortal" -NewName MerlotPortal_$shortdate ; logwrite "renamed \\$_\$driveletter$\MerlotPortal to MerlotPortal_$shortdate"
  245.                rename-item "\\$_\$driveletter$\MerlotApplication" -NewName MerlotApplication_$shortdate; logwrite "renamed \\$_\$driveletter$\MerlotApplication to MerlotApplication_$shortdate"}
  246.  
  247. #​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.
  248. #move Folders from local server to Merlot Destinations
  249.  
  250. #Since bandwidth can be problematic to NDD, if env is NDD, ask if user weants to copy manually
  251. $confirm = "Cancel"
  252. if ($environment -eq "ndd")
  253.     {$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 `
  254.    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)
  255.     }
  256. If ($confirm -eq "Cancel")
  257. {
  258.     $easervers | %{copy-item $FileLocation\MerlotApplication -Recurse -Destination "\\$_\$driveletter$"; logwrite "Moved $FileLocation\MerlotApplication to $_\$driveletter$\" }
  259.     $paservers | %{copy-Item $FileLocation\MerlotApplication -Recurse -Destination "\\$_\$driveletter$"; logwrite "Moved $FileLocation\MerlotApplication to $_\$driveletter$\"
  260.                    copy-item $FileLocation\MerlotPortal -Recurse -Destination "\\$_\$driveletter$"; logwrite "Moved $FileLocation\MerlotPortal to $_\$driveletter$\"}
  261.     $jeservers | %{copy-item $FileLocation\MerlotJobEngine -Recurse -Destination "\\$_\$driveletter$" ; logwrite "Moved $FileLocation\MerlotJobEngine to $_\$driveletter$\"}
  262.     }
  263.  
  264. #As discussed above, this is where we change the value of instancenumber to 1 on  the destination server.
  265. #modify XML in secondary jobengine server to change InstanceNUmber to 1
  266.  
  267. $jeserver = $jeservers[1]
  268. [xml]$newJeXML = Get-Content \\$jeserver\$driveletter$\MerlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config
  269. [xml]$element = @"
  270. <add key="InstanceNumber" value="1" />
  271. "@
  272. $newjexml.configuration.appSettings.AppendChild($newjexml.ImportNode($element.add, $true))
  273. $newjexml.save("\\$jeserver\$driveletter$\merlotJobEngine\Merlot.Aero.Utilities.JobEngine.exe.config")
  274.     logwrite "Changed InstanceNumber on $jeserver to 1"
  275.  
  276. #​Once all necessary items are completed, prompt for permission to restart merlot services.
  277. #restart Merlot Services
  278.  
  279.     logwrite "Asking Permission to restart Merlot services"
  280. $confirm = [System.Windows.Forms.Messagebox]::show("OK to Turn on IIS on Merlot IIS servers on $environment","Confirm IIS Startup", 4)
  281.  
  282. if($confirm -eq "yes"){
  283. $easervers | %{get-service w3svc -ComputerName $_ | Start-Service ;  logwrite "started IIS on $_"}
  284. $paservers | %{get-service w3svc -ComputerName $_ | Start-Service;  logwrite "Started IIS on $_"}
  285. $shservers | %{get-service w3svc -ComputerName $_ | Start-Service ;  logwrite "Started IIS on $_"}
  286. $jeservers | %{get-service "Merlot Jobengine" -ComputerName $_ | Start-Service ; logwrite "Started Merlot Job Engine on $_"}
  287. }
  288. else
  289.     {break}
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement