|
|
@@ -0,0 +1,117 @@
|
|
|
+<#
|
|
|
+.SYNOPSIS
|
|
|
+ .
|
|
|
+.DESCRIPTION
|
|
|
+ consul自动安装程序
|
|
|
+.PARAMETER p
|
|
|
+ 安装位置 window默认当前目录, linux默认/etc/consul
|
|
|
+.PARAMETER server
|
|
|
+ 以server模式运行, 集群数量
|
|
|
+.EXAMPLE
|
|
|
+ C:\PS>
|
|
|
+ .\install.ps1 -p /etc/consul
|
|
|
+.NOTES
|
|
|
+ Author: zr
|
|
|
+ Date: Nov 04, 2020
|
|
|
+#>
|
|
|
+#Requires -RunAsAdministrator
|
|
|
+param(
|
|
|
+ [string]$p,
|
|
|
+ [int]$server
|
|
|
+)
|
|
|
+$installPath = $p
|
|
|
+
|
|
|
+if ($IsWindows){
|
|
|
+ if ($p -eq ""){
|
|
|
+ $installPath = $PSScriptRoot
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if ($IsLinux) {
|
|
|
+ if ($p -eq ""){
|
|
|
+ $installPath = "/etc/consul"
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+$serverStr = ""
|
|
|
+if ($server -gt 0){
|
|
|
+ $serverStr = "-server -bootstrap-expect=$server"
|
|
|
+}
|
|
|
+
|
|
|
+function WriteConfigFile {
|
|
|
+ param (
|
|
|
+ # Specifies a path to one or more locations.
|
|
|
+ [Parameter(Mandatory=$true,
|
|
|
+ Position=0,
|
|
|
+ ParameterSetName="ParameterSetName",
|
|
|
+ ValueFromPipeline=$true,
|
|
|
+ ValueFromPipelineByPropertyName=$true,
|
|
|
+ HelpMessage="Path to one or more locations.")]
|
|
|
+ [Alias("PSPath")]
|
|
|
+ [ValidateNotNullOrEmpty()]
|
|
|
+ [string[]]
|
|
|
+ $path
|
|
|
+ )
|
|
|
+ $config = '
|
|
|
+{
|
|
|
+ "data_dir": "data/",
|
|
|
+ "ui": true,
|
|
|
+ "bind_addr": "{{ GetPrivateInterfaces | include \"network\" \"192.168.0.0/16\" | attr \"address\" }}",
|
|
|
+ "start_join": [
|
|
|
+ // "192.168.0.1",
|
|
|
+ // "192.168.0.2",
|
|
|
+ // "192.168.0.3"
|
|
|
+ ],
|
|
|
+ "client_addr": "0.0.0.0"
|
|
|
+}
|
|
|
+'
|
|
|
+ $configFilePath = "$path/config.json"
|
|
|
+ if (-not (Test-Path $configFilePath)) {
|
|
|
+ $config | Out-File -FilePath $configFilePath
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function WinInstaill {
|
|
|
+ $configDir = "$installPath\conf"
|
|
|
+ if (-not (Test-Path $configDir)){
|
|
|
+ mkdir $configDir
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($PSScriptRoot -ne $installPath) {
|
|
|
+ Copy-Item "$PSScriptRoot\consul.exe" "$installPath\consul.exe" -Force
|
|
|
+ }
|
|
|
+ WriteConfigFile -path $configDir
|
|
|
+ Write-Output "安装目录: $installPath"
|
|
|
+ Write-Output "配置目录: $configDir"
|
|
|
+ $cmd = "$installPath\consul.exe agent $serverStr --config-dir $configDir"
|
|
|
+ Write-Output "启动指令:$cmd"
|
|
|
+ New-Service Consul $cmd -StartupType Automatic
|
|
|
+}
|
|
|
+
|
|
|
+function DockerInstaill {
|
|
|
+ try {
|
|
|
+ docker -v
|
|
|
+ }
|
|
|
+ catch {
|
|
|
+ Write-Output "未安装docker"
|
|
|
+ exit
|
|
|
+ }
|
|
|
+
|
|
|
+ $configDir = "$installPath"
|
|
|
+ if (-not (Test-Path $configDir)){
|
|
|
+ mkdir $configDir
|
|
|
+ }
|
|
|
+
|
|
|
+ WriteConfigFile -path $configDir
|
|
|
+ Write-Output "配置目录: $configDir"
|
|
|
+ $cmd = "docker run -d --net=host -v ${configDir}:/consul/config consul agent $serverStr --config-dir /consul/config"
|
|
|
+ Write-Output "执行指令:$cmd"
|
|
|
+ Invoke-Expression $cmd
|
|
|
+}
|
|
|
+if ($IsWindows){
|
|
|
+ WinInstaill
|
|
|
+}
|
|
|
+
|
|
|
+if ($IsLinux){
|
|
|
+ DockerInstaill
|
|
|
+}
|