install.ps1 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <#
  2. .SYNOPSIS
  3. .
  4. .DESCRIPTION
  5. consul自动安装程序
  6. .PARAMETER p
  7. 安装位置 window默认当前目录, linux默认/etc/consul
  8. .PARAMETER server
  9. 以server模式运行, 集群数量
  10. .EXAMPLE
  11. C:\PS>
  12. .\install.ps1 -p /etc/consul
  13. .NOTES
  14. Author: zr
  15. Date: Nov 04, 2020
  16. #>
  17. #Requires -RunAsAdministrator
  18. param(
  19. [string]$p,
  20. [int]$server
  21. )
  22. $installPath = $p
  23. if ($IsWindows){
  24. if ($p -eq ""){
  25. $installPath = $PSScriptRoot
  26. }
  27. }
  28. if ($IsLinux) {
  29. if ($p -eq ""){
  30. $installPath = "/etc/consul"
  31. }
  32. }
  33. $serverStr = ""
  34. if ($server -gt 0){
  35. $serverStr = "-server -bootstrap-expect=$server"
  36. }
  37. function WriteConfigFile {
  38. param (
  39. # Specifies a path to one or more locations.
  40. [Parameter(Mandatory=$true,
  41. Position=0,
  42. ParameterSetName="ParameterSetName",
  43. ValueFromPipeline=$true,
  44. ValueFromPipelineByPropertyName=$true,
  45. HelpMessage="Path to one or more locations.")]
  46. [Alias("PSPath")]
  47. [ValidateNotNullOrEmpty()]
  48. [string[]]
  49. $path
  50. )
  51. $config = '
  52. {
  53. "data_dir": "data/",
  54. "ui": true,
  55. "bind_addr": "{{ GetPrivateInterfaces | include \"network\" \"192.168.0.0/16\" | attr \"address\" }}",
  56. "start_join": [
  57. // "192.168.0.1",
  58. // "192.168.0.2",
  59. // "192.168.0.3"
  60. ],
  61. "client_addr": "0.0.0.0"
  62. }
  63. '
  64. $configFilePath = "$path/config.json"
  65. if (-not (Test-Path $configFilePath)) {
  66. $config | Out-File -FilePath $configFilePath
  67. }
  68. }
  69. function WinInstaill {
  70. $configDir = "$installPath\conf"
  71. if (-not (Test-Path $configDir)){
  72. mkdir $configDir
  73. }
  74. if ($PSScriptRoot -ne $installPath) {
  75. Copy-Item "$PSScriptRoot\consul.exe" "$installPath\consul.exe" -Force
  76. }
  77. WriteConfigFile -path $configDir
  78. Write-Output "安装目录: $installPath"
  79. Write-Output "配置目录: $configDir"
  80. $cmd = "$installPath\consul.exe agent $serverStr --config-dir $configDir"
  81. Write-Output "启动指令:$cmd"
  82. New-Service Consul $cmd -StartupType Automatic
  83. }
  84. function DockerInstaill {
  85. try {
  86. docker -v
  87. }
  88. catch {
  89. Write-Output "未安装docker"
  90. exit
  91. }
  92. $configDir = "$installPath"
  93. if (-not (Test-Path $configDir)){
  94. mkdir $configDir
  95. }
  96. WriteConfigFile -path $configDir
  97. Write-Output "配置目录: $configDir"
  98. $cmd = "docker run -d --net=host -v ${configDir}:/consul/config consul agent $serverStr --config-dir /consul/config"
  99. Write-Output "执行指令:$cmd"
  100. Invoke-Expression $cmd
  101. }
  102. if ($IsWindows){
  103. WinInstaill
  104. }
  105. if ($IsLinux){
  106. DockerInstaill
  107. }