| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2017, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
|
| 8 |
function Get-EnabledPlugin($rabbitmq_plugins_cmd) { |
| 9 |
try { |
| 10 |
$enabled_plugins = @(& $rabbitmq_plugins_cmd list -E -m | Where-Object { $_ }) |
| 11 |
return , $enabled_plugins |
| 12 |
} |
| 13 |
catch { |
| 14 |
Fail-Json -obj $result -message "Can't execute '$rabbitmq_plugins_cmd list -E -m': $($_.Exception.Message)" |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
function Enable-Plugin($rabbitmq_plugins_cmd, $plugin_name) { |
| 19 |
try { |
| 20 |
& $rabbitmq_plugins_cmd enable $plugin_name |
| 21 |
} |
| 22 |
catch { |
| 23 |
Fail-Json -obj $result -message "Can't execute '$rabbitmq_plugins_cmd enable $plugin_name': $($_.Exception.Message)" |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
function Disable-Plugin($rabbitmq_plugins_cmd, $plugin_name) { |
| 28 |
try { |
| 29 |
& $rabbitmq_plugins_cmd disable $plugin_name |
| 30 |
} |
| 31 |
catch { |
| 32 |
Fail-Json -obj $result -message "Can't execute '$rabbitmq_plugins_cmd disable $plugin_name': $($_.Exception.Message)" |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
function Get-RabbitmqPathFromRegistry { |
| 37 |
$reg64Path = "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\RabbitMQ" |
| 38 |
$reg32Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\RabbitMQ" |
| 39 |
|
| 40 |
if (Test-Path -LiteralPath $reg64Path) { |
| 41 |
$regPath = $reg64Path |
| 42 |
} |
| 43 |
elseif (Test-Path -LiteralPath $reg32Path) { |
| 44 |
$regPath = $reg32Path |
| 45 |
} |
| 46 |
|
| 47 |
if ($regPath) { |
| 48 |
$path = Split-Path -Parent (Get-ItemProperty -LiteralPath $regPath "UninstallString").UninstallString |
| 49 |
$version = (Get-ItemProperty -LiteralPath $regPath "DisplayVersion").DisplayVersion |
| 50 |
return "$path\rabbitmq_server-$version" |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
function Get-RabbitmqBinPath($installation_path) { |
| 55 |
$result = Join-Path -Path $installation_path -ChildPath 'bin' |
| 56 |
if (Test-Path -LiteralPath $result) { |
| 57 |
return $result |
| 58 |
} |
| 59 |
|
| 60 |
$result = Join-Path -Path $installation_path -ChildPath 'sbin' |
| 61 |
if (Test-Path -LiteralPath $result) { |
| 62 |
return $result |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$ErrorActionPreference = "Stop" |
| 67 |
|
| 68 |
$result = @{ |
| 69 |
changed = $false |
| 70 |
enabled = @() |
| 71 |
disabled = @() |
| 72 |
} |
| 73 |
|
| 74 |
$params = Parse-Args $args -supports_check_mode $true |
| 75 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 76 |
$diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false |
| 77 |
|
| 78 |
$names = Get-AnsibleParam -obj $params -name "names" -type "str" -failifempty $true -aliases "name" |
| 79 |
$new_only = Get-AnsibleParam -obj $params -name "new_only" -type "bool" -default $false |
| 80 |
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "enabled" -validateset "enabled", "disabled" |
| 81 |
$prefix = Get-AnsibleParam -obj $params -name "prefix" -type "str" |
| 82 |
|
| 83 |
if ($diff_support) { |
| 84 |
$result.diff = @{} |
| 85 |
$result.diff.prepared = "" |
| 86 |
} |
| 87 |
|
| 88 |
$plugins = $names.Split(",") |
| 89 |
|
| 90 |
if ($prefix) { |
| 91 |
$rabbitmq_bin_path = Get-RabbitmqBinPath -installation_path $prefix |
| 92 |
if (-not $rabbitmq_bin_path) { |
| 93 |
Fail-Json -obj $result -message "No binary folder in prefix `"$($prefix)`"" |
| 94 |
} |
| 95 |
} |
| 96 |
else { |
| 97 |
$rabbitmq_reg_path = Get-RabbitmqPathFromRegistry |
| 98 |
if ($rabbitmq_reg_path) { |
| 99 |
$rabbitmq_bin_path = Get-RabbitmqBinPath -installation_path $rabbitmq_reg_path |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if ($rabbitmq_bin_path) { |
| 104 |
$rabbitmq_plugins_cmd = "$(Join-Path -Path $rabbitmq_bin_path -ChildPath "rabbitmq-plugins")" |
| 105 |
} |
| 106 |
else { |
| 107 |
$rabbitmq_plugins_cmd = "rabbitmq-plugins" |
| 108 |
} |
| 109 |
|
| 110 |
$enabled_plugins = Get-EnabledPlugin -rabbitmq_plugins_cmd $rabbitmq_plugins_cmd |
| 111 |
|
| 112 |
if ($state -eq "enabled") { |
| 113 |
$plugins_to_enable = $plugins | Where-Object { -not ($enabled_plugins -contains $_) } |
| 114 |
foreach ($plugin in $plugins_to_enable) { |
| 115 |
if (-not $check_mode) { |
| 116 |
Enable-Plugin -rabbitmq_plugins_cmd $rabbitmq_plugins_cmd -plugin_name $plugin |
| 117 |
} |
| 118 |
if ($diff_support) { |
| 119 |
$result.diff.prepared += "+[$plugin]`n" |
| 120 |
} |
| 121 |
$result.enabled += $plugin |
| 122 |
$result.changed = $true |
| 123 |
} |
| 124 |
|
| 125 |
if (-not $new_only) { |
| 126 |
$plugins_to_disable = $enabled_plugins | Where-Object { -not ($plugins -contains $_) } |
| 127 |
foreach ($plugin in $plugins_to_disable) { |
| 128 |
if (-not $check_mode) { |
| 129 |
Disable-Plugin -rabbitmq_plugins_cmd $rabbitmq_plugins_cmd -plugin_name $plugin |
| 130 |
} |
| 131 |
if ($diff_support) { |
| 132 |
$result.diff.prepared += "-[$plugin]`n" |
| 133 |
} |
| 134 |
$result.disabled += $plugin |
| 135 |
$result.changed = $true |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
else { |
| 140 |
$plugins_to_disable = $enabled_plugins | Where-Object { $plugins -contains $_ } |
| 141 |
foreach ($plugin in $plugins_to_disable) { |
| 142 |
if (-not $check_mode) { |
| 143 |
Disable-Plugin -rabbitmq_plugins_cmd $rabbitmq_plugins_cmd -plugin_name $plugin |
| 144 |
} |
| 145 |
if ($diff_support) { |
| 146 |
$result.diff.prepared += "-[$plugin]`n" |
| 147 |
} |
| 148 |
$result.disabled += $plugin |
| 149 |
$result.changed = $true |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
Exit-Json $result |