rabbitmqadmin-cli 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README CHANGED
@@ -4,4 +4,6 @@ http://www.rabbitmq.com/management-cli.html
4
4
 
5
5
  Once rabbitmqadmin is on the path you can use this gem in rake, capistrano or regular ruby.
6
6
 
7
- Added parameters to connect to any host/port
7
+ v1.0.3 Added parameters to connect to any host/port
8
+ v1.0.4 Added class based test_connection(host, port) method and use it in initialize to raise if no connection
9
+ v1.0.5 Added set_permissions method, non destructive, will create vhost and/or user if necessary and set permissions
@@ -1,12 +1,29 @@
1
1
  require 'json'
2
2
 
3
3
  class RabbitMqAdminCli
4
+
5
+ def self.test_connect(host, port, scheme = "http")
6
+ `wget --spider -T 1 -t 1 #{scheme}://#{host}:#{port}/img/rabbitmqlogo.png >/dev/null 2>&1`
7
+ case $?.exitstatus
8
+ when 0, 6
9
+ [0, "All OK"]
10
+ when 4
11
+ [1, "Attempt to connect to #{host}:#{port} timed out"]
12
+ when 5
13
+ [2, "Using scheme #{scheme}, SSL verification failed"]
14
+ else
15
+ [3, "General problem, cannot determine if RabbitMq http server is running on #{host}:#{port}"]
16
+ end
17
+ end
18
+
4
19
  def initialize admin_user = 'guest', admin_password = 'guest', host = 'localhost', port = 55672
5
20
  @admin_user = admin_user
6
21
  @admin_password = admin_password
7
22
  @host = host
8
23
  @port = port
9
24
  @verbose = false
25
+ code, msg = self.class.test_connect(@host, @port)
26
+ raise msg unless code == 0
10
27
  end
11
28
 
12
29
  attr_accessor :verbose, :host, :port
@@ -59,6 +76,10 @@ class RabbitMqAdminCli
59
76
  parse execute("#{stub} list vhosts")
60
77
  end
61
78
 
79
+ def list_permissions
80
+ parse execute("#{stub} list permissions")
81
+ end
82
+
62
83
  def purge_queues vhost
63
84
  list_queues(vhost).each { |queue| execute "#{stub vhost} purge queue name=#{queue[:name]}" }
64
85
  end
@@ -71,10 +92,36 @@ class RabbitMqAdminCli
71
92
  list_vhosts.any? { |h| h[:name] == vhost }
72
93
  end
73
94
 
95
+ def permissions_exist? vhost, user
96
+ permission = list_permissions.select {|perm| perm[:user] == user && perm[:vhost] == vhost}
97
+ return false if permission.empty?
98
+ permission.first.values_at(:configure,:read,:write).all? {|val| val == ".*"}
99
+ end
100
+
101
+ def set_permissions vhost, user, password, tags = ''
102
+ return if permissions_exist?(vhost, user)
103
+
104
+ create_vhost = !vhost_exists?(vhost)
105
+ create_user = !user_exists?(user)
106
+
107
+ if create_vhost && create_user
108
+ initialize_vhost(vhost)
109
+ initialize_user(vhost, user, password, tags)
110
+ elsif !create_vhost && create_user
111
+ initialize_user(vhost, user, password, tags)
112
+ elsif create_vhost && !create_user
113
+ initialize_vhost(vhost)
114
+ add_user_to_vhost(vhost, user)
115
+ else
116
+ add_user_to_vhost(vhost, user)
117
+ end
118
+
119
+ end
120
+
74
121
  private
75
122
 
76
123
  def add_user_to_vhost vhost, user
77
- execute "#{stub} -V #{vhost} declare permission vhost=#{vhost} user=#{user} configure=\"\.\*\" write=\"\.\*\" read=\"\.\*\""
124
+ execute %Q(#{stub} -V #{vhost} declare permission vhost=#{vhost} user=#{user} configure=".*" write=".*" read=".*")
78
125
  end
79
126
 
80
127
  def execute command
@@ -83,6 +130,8 @@ class RabbitMqAdminCli
83
130
  end
84
131
 
85
132
  def parse json
133
+ puts json if @verbose
134
+ return unless json.is_a?(String)
86
135
  JSON.parse json, :symbolize_names => true
87
136
  end
88
137
  end
@@ -1,3 +1,3 @@
1
1
  module RabbitMqAdminCli
2
- VERSION = "1.0.4"
2
+ VERSION = "1.0.5"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rabbitmqadmin-cli'
3
- s.version = '1.0.4'
3
+ s.version = '1.0.5'
4
4
  s.platform = RUBY_PLATFORM.to_s == 'java' ? 'java' : Gem::Platform::RUBY
5
5
  s.authors = ['Lee Henson', 'Guy Boertje']
6
6
  s.email = ['lee.m.henson@gmail.com', 'guyboertje@gmail.com']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabbitmqadmin-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2011-10-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: json
17
- requirement: &77963690 !ruby/object:Gem::Requirement
17
+ requirement: &14786860 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *77963690
25
+ version_requirements: *14786860
26
26
  description: Requires rabbitmqadmin to be in your path
27
27
  email:
28
28
  - lee.m.henson@gmail.com
@@ -57,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  version: '0'
58
58
  requirements: []
59
59
  rubyforge_project:
60
- rubygems_version: 1.8.10
60
+ rubygems_version: 1.8.6
61
61
  signing_key:
62
62
  specification_version: 3
63
63
  summary: Ruby wrapper for calling out the rabbitmqadmin cli script