Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Installs Consul and creates a Windows service to run the agent.
  4.  
  5. .DESCRIPTION
  6. Installs Consul and creates a Windows service to run the agent.
  7.  
  8. Requirements:
  9. - You must execute script with administrator priveleges
  10. - A service named 'Consul' MUST NOT exist
  11. - C:\ProgramData\Consul MUST NOT exist
  12. - This script must reside in a directory resembling the following. You must provide
  13. all files other than this script.
  14.  
  15. config/
  16. config.json
  17. ssl/
  18. ca.crt
  19. agent.crt
  20. agent.key
  21. consul.exe
  22. Install-Consul.ps1
  23. nssm.exe
  24.  
  25. What it does:
  26. - Installs NSSM to C:\ProgramData\NSSM if it does not already exist there
  27. - Installs Consul to C:\ProgramData\Consul
  28. - Creates firewall rules required by Consul
  29. - Creates a Windows service which runs the agent
  30. - Uses NSSM to configure the Windows service
  31. #>
  32. $ErrorActionPreference = 'Stop'
  33.  
  34. $consulDir = "C:\ProgramData\Consul"
  35. $consulExe = (Join-Path $consulDir "consul.exe")
  36. $nssmDir = "C:\ProgramData\NSSM"
  37. $nssmExe = (Join-Path $nssmDir "nssm.exe")
  38.  
  39. function Invoke-NSSM {
  40. param (
  41. [Parameter(Mandatory=$true, Position=0)]
  42. [string]
  43. $Command,
  44.  
  45. [Parameter()]
  46. [switch]
  47. $Echo
  48. )
  49. if ($Echo) {
  50. Write-Host $Command
  51. }
  52. $sb = [scriptblock]::Create("$nssmExe $Command")
  53. Invoke-Command $sb
  54. if ($LASTEXITCODE) {
  55. throw "Exit code '$LASTEXITCODE' from '$Command'"
  56. }
  57. }
  58.  
  59. if ((Test-Path $consulDir) -or ((Get-Service Consul -ErrorAction SilentlyContinue) -ne $null)) {
  60. throw "Consul already installed. This script only supports fresh installs"
  61. }
  62.  
  63. # Copy NSSM if necessary
  64. if (!(Test-Path $nssmExe)) {
  65. New-Item -ItemType Container $nssmDir -ErrorAction SilentlyContinue
  66. Copy-Item "$PSScriptRoot/nssm.exe" $nssmDir
  67. }
  68.  
  69. # Copy Consul binaries, configs, etc.
  70. New-Item -ItemType Container $consulDir
  71. New-Item -ItemType Container "$consulDir/data"
  72. New-Item -ItemType Container "$consulDir/logs"
  73. Copy-Item "$PSScriptRoot/consul.exe" $consulDir
  74. Copy-Item "$PSScriptRoot/config" $consulDir -Recurse
  75. Copy-Item "$PSScriptRoot/ssl" $consulDir -Recurse
  76.  
  77. # Install Consul Firewall rules
  78. New-NetFirewallRule -DisplayName 'Consul Server RPC' `
  79. -Direction Inbound -LocalPort 8300 -Protocol TCP
  80. New-NetFirewallRule -DisplayName 'Consul Serf LAN TCP' `
  81. -Direction Inbound -LocalPort 8301 -Protocol TCP
  82. New-NetFirewallRule -DisplayName 'Consul Serf LAN UDP' `
  83. -Direction Inbound -LocalPort 8301 -Protocol UDP
  84. New-NetFirewallRule -DisplayName 'Consul Serf WAN TCP' `
  85. -Direction Inbound -LocalPort 8302 -Protocol TCP
  86. New-NetFirewallRule -DisplayName 'Consul Serf WAN UDP' `
  87. -Direction Inbound -LocalPort 8302 -Protocol UDP
  88.  
  89. # Install the Consul service
  90. Invoke-NSSM "install Consul $consulExe"
  91. Invoke-NSSM "set Consul AppDirectory $consulDir"
  92. Invoke-NSSM "set Consul AppParameters `"agent -config-dir=$consulDir/config`""
  93. Invoke-NSSM "set Consul AppStdout $consulDir/logs/consul.log"
  94. Invoke-NSSM "set Consul AppStderr $consulDir/logs/consul.log"
  95.  
  96. Start-Service Consul
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement