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