Skip to content

Commit 41ec613

Browse files
Azrael808wohali
authored andcommitted
Adding database LWRP to the cookbook - used to manage the creation and (eventually) deletion of CouchDB databases.
Adding curl install to recipes - needed for LWRP. Added user and password attributes to the database LWRP in case admin party is disabled. Updated load_current_resource method to include new user and password attributes. Removed call to database LWRP from default recipe - inadvertantly committed. Moved create database commands to distinct method, using this to populate a shellout request from which a JSON response is parsed. Removed erroneous "code" keyword prefacing the db creation command strings. Corrected invalid call to shell_out. Tried alternative means of calling shellout. Fixed incorrect call to include? method. Log fatal error if unable to create DB. Changed Chef::Log.fatal to Chef::Application.fatal! - ensuring the Chef run is aborted. Updated the README to document the database LWRP.
1 parent 151582a commit 41ec613

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ source
136136

137137
Downloads the CouchDB source from the Apache project site, plus development dependencies. Then builds the binaries for installation, creates a user and directories, then sets up the couchdb service. Uses the init script provided in the cookbook.
138138

139+
LWRPs
140+
=====
141+
142+
database
143+
--------
144+
Can be used to create a database on a CouchDB install.
145+
146+
* `database_name` - name of the database to create.
147+
* `database_host` - host name or IP address of the database server.
148+
* `database_port` - TCP port of the database server.
149+
* `couchdb_user` - username to bind to the CouchDB server with (optional).
150+
* `couchdb_password` - password to bind to the CouchDB server with (optional).
151+
152+
139153
License and Author
140154
==================
141155

providers/database.rb

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
require 'chef/mixin/shell_out'
3+
require 'chef/mixin/language'
4+
require 'json'
5+
6+
def whyrun_supported?
7+
true
8+
end
9+
10+
action :create do
11+
if @current_resource.exists
12+
Chef::Log.info "#{ @new_resource } already exists - nothing to do."
13+
else
14+
converge_by("Create #{ @new_resource }") do
15+
create_database
16+
end
17+
end
18+
end
19+
20+
action :delete do
21+
if @current_resource.exists
22+
converge_by("Delete #{ @new_resource }") do
23+
delete_database
24+
end
25+
else
26+
Chef::Log.info "#{ @current_resource } doesn't exist - can't delete."
27+
end
28+
end
29+
30+
def load_current_resource
31+
32+
@current_resource = Chef::Resource::CouchdbDatabase.new(@new_resource.name)
33+
@current_resource.name(@new_resource.name)
34+
@current_resource.couchdb_user(@new_resource.couchdb_user)
35+
@current_resource.couchdb_password(@new_resource.couchdb_password)
36+
37+
if database_exists?(@current_resource.database_name)
38+
@current_resource.exists = true
39+
end
40+
end
41+
42+
def create_database
43+
cmd = Mixlib::ShellOut.new(create_db_command)
44+
cmd.run_command
45+
response = JSON.parse(cmd.stdout)
46+
Chef::Application.fatal!("Unable to create DB!") unless response.include?("ok")
47+
end
48+
49+
def create_db_command
50+
if new_resource.couchdb_user == nil && new_resource.couchdb_password == nil
51+
"curl -X PUT http://#{new_resource.database_host}:#{new_resource.database_port}/#{new_resource.database_name}"
52+
else
53+
"curl -X PUT http://#{new_resource.couchdb_user}:#{new_resource.couchdb_password}@#{new_resource.database_host}:#{new_resource.database_port}/#{new_resource.database_name}"
54+
end
55+
end
56+
57+
def database_exists?(name)
58+
Chef::Log.debug "Checking to see if the #{new_resource.database_name} database exists."
59+
result = JSON.parse(`curl -X GET http://#{new_resource.database_host}:#{new_resource.database_port}/_all_dbs`)
60+
result.include?(new_resource.database_name)
61+
end
62+

recipes/default.rb

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
erl_recipe_name = 'erlang'
2626
include_recipe erl_recipe_name if node['couch_db']['install_erlang']
2727

28+
package "curl"
29+
2830
case node['platform_family']
2931
when 'rhel'
3032
group 'couchdb' do
@@ -73,3 +75,4 @@
7375
supports [:restart, :status]
7476
action [:enable, :start]
7577
end
78+

recipes/source.rb

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
1919

20+
package "curl"
21+
2022
if node['platform'] == 'ubuntu' && node['platform_version'].to_f == 8.04
2123
log "Ubuntu 8.04 does not supply sufficient development libraries via APT to install CouchDB #{node['couch_db']['src_version']} from source."
2224
return

resources/database.rb

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
actions :create, :delete
3+
default_action :create
4+
5+
attribute :database_name, :name_attribute => true, :kind_of => String, :required => true
6+
attribute :database_host, :kind_of => String, :default => node['couch_db']['config']['httpd']['bind_address']
7+
attribute :database_port, :kind_of => Fixnum, :default => node['couch_db']['config']['httpd']['port']
8+
attribute :couchdb_user, :kind_of => String, :default => nil
9+
attribute :couchdb_password, :kind_of => String, :default => nil
10+
11+
attr_accessor :exists
12+

0 commit comments

Comments
 (0)