zrufo před 5 roky
rodič
revize
c6ffef1f1a
4 změnil soubory, kde provedl 120 přidání a 3 odebrání
  1. 2 0
      .gitignore
  2. 1 3
      README.md
  3. binární
      consul.exe
  4. 117 0
      install.ps1

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+# Folders
+conf

+ 1 - 3
README.md

@@ -1,3 +1 @@
-# consul
-
-consul安装
+#Consul安装程序

binární
consul.exe


+ 117 - 0
install.ps1

@@ -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
+}