]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/rabbitmq/lib/puppet/provider/rabbitmq_binding/rabbitmqadmin.rb
move to puppetlabs rabbitmq module
[dsa-puppet.git] / 3rdparty / modules / rabbitmq / lib / puppet / provider / rabbitmq_binding / rabbitmqadmin.rb
1 require 'json'
2 require 'puppet'
3 Puppet::Type.type(:rabbitmq_binding).provide(:rabbitmqadmin) do
4
5   if Puppet::PUPPETVERSION.to_f < 3
6     commands :rabbitmqctl   => 'rabbitmqctl'
7     commands :rabbitmqadmin => '/usr/local/bin/rabbitmqadmin'
8   else
9     has_command(:rabbitmqctl, 'rabbitmqctl') do
10       environment :HOME => "/tmp"
11     end
12     has_command(:rabbitmqadmin, '/usr/local/bin/rabbitmqadmin') do
13       environment :HOME => "/tmp"
14     end
15   end
16   defaultfor :feature => :posix
17
18   def should_vhost
19     if @should_vhost
20       @should_vhost
21     else
22       @should_vhost = resource[:name].split('@').last
23     end
24   end
25
26   def self.all_vhosts
27     vhosts = []
28     rabbitmqctl('list_vhosts', '-q').split(/\n/).collect do |vhost|
29       vhosts.push(vhost)
30     end
31     vhosts
32   end
33
34   def self.all_bindings(vhost)
35     rabbitmqctl('list_bindings', '-q', '-p', vhost, 'source_name', 'destination_name', 'destination_kind', 'routing_key', 'arguments').split(/\n/)
36   end
37
38   def self.instances
39     resources = []
40     all_vhosts.each do |vhost|
41       all_bindings(vhost).collect do |line|
42         source_name, destination_name, destination_type, routing_key, arguments = line.split(/\t/)
43         # Convert output of arguments from the rabbitmqctl command to a json string.
44         if !arguments.nil?
45           arguments = arguments.gsub(/^\[(.*)\]$/, "").gsub(/\{("(?:.|\\")*?"),/, '{\1:').gsub(/\},\{/, ",")
46           if arguments == ""
47             arguments = '{}'
48           end
49         else
50           arguments = '{}'
51         end
52         if (source_name != '')
53           binding = {
54             :destination_type => destination_type,
55             :routing_key      => routing_key,
56             :arguments        => JSON.parse(arguments),
57             :ensure           => :present,
58             :name             => "%s@%s@%s" % [source_name, destination_name, vhost],
59           }
60           resources << new(binding) if binding[:name]
61         end
62       end
63     end
64     resources
65   end
66
67   def self.prefetch(resources)
68     packages = instances
69     resources.keys.each do |name|
70       if provider = packages.find{ |pkg| pkg.name == name }
71         resources[name].provider = provider
72       end
73     end
74   end
75
76   def exists?
77     @property_hash[:ensure] == :present
78   end
79
80   def create
81     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
82     name = resource[:name].split('@').first
83     destination = resource[:name].split('@')[1]
84     arguments = resource[:arguments]
85     if arguments.nil?
86       arguments = {}
87     end
88     rabbitmqadmin('declare',
89       'binding',
90       vhost_opt,
91       "--user=#{resource[:user]}",
92       "--password=#{resource[:password]}",
93       '-c',
94       '/etc/rabbitmq/rabbitmqadmin.conf',
95       "source=#{name}",
96       "destination=#{destination}",
97       "arguments=#{arguments.to_json}",
98       "routing_key=#{resource[:routing_key]}",
99       "destination_type=#{resource[:destination_type]}"
100     )
101     @property_hash[:ensure] = :present
102   end
103
104   def destroy
105     vhost_opt = should_vhost ? "--vhost=#{should_vhost}" : ''
106     name = resource[:name].split('@').first
107     destination = resource[:name].split('@')[1]
108     rabbitmqadmin('delete', 'binding', vhost_opt, "--user=#{resource[:user]}", "--password=#{resource[:password]}", '-c', '/etc/rabbitmq/rabbitmqadmin.conf', "source=#{name}", "destination_type=#{resource[:destination_type]}", "destination=#{destination}", "properties_key=#{resource[:routing_key]}")
109     @property_hash[:ensure] = :absent
110   end
111
112 end