elzar 0.0.1

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.
Files changed (156) hide show
  1. data/.chef/knife.rb +3 -0
  2. data/.gitignore +1 -0
  3. data/.rvmrc +1 -0
  4. data/Gemfile +11 -0
  5. data/Gemfile.lock +96 -0
  6. data/README.md +42 -0
  7. data/Rakefile +1 -0
  8. data/Vagrantfile +16 -0
  9. data/cookbooks/.gitkeep +0 -0
  10. data/cookbooks/apt/README.md +122 -0
  11. data/cookbooks/apt/files/default/apt-cacher +9 -0
  12. data/cookbooks/apt/files/default/apt-cacher.conf +144 -0
  13. data/cookbooks/apt/files/default/apt-proxy-v2.conf +50 -0
  14. data/cookbooks/apt/metadata.rb +13 -0
  15. data/cookbooks/apt/providers/repository.rb +73 -0
  16. data/cookbooks/apt/recipes/cacher-client.rb +44 -0
  17. data/cookbooks/apt/recipes/cacher.rb +45 -0
  18. data/cookbooks/apt/recipes/default.rb +50 -0
  19. data/cookbooks/apt/resources/repository.rb +30 -0
  20. data/cookbooks/build-essential/README.md +24 -0
  21. data/cookbooks/build-essential/metadata.rb +10 -0
  22. data/cookbooks/build-essential/recipes/default.rb +45 -0
  23. data/cookbooks/database/README.md +403 -0
  24. data/cookbooks/database/libraries/provider_database_mysql.rb +97 -0
  25. data/cookbooks/database/libraries/provider_database_mysql_user.rb +76 -0
  26. data/cookbooks/database/libraries/provider_database_postgresql.rb +126 -0
  27. data/cookbooks/database/libraries/provider_database_postgresql_user.rb +78 -0
  28. data/cookbooks/database/libraries/provider_database_sql_server.rb +109 -0
  29. data/cookbooks/database/libraries/provider_database_sql_server_user.rb +106 -0
  30. data/cookbooks/database/libraries/resource_database.rb +111 -0
  31. data/cookbooks/database/libraries/resource_database_user.rb +90 -0
  32. data/cookbooks/database/libraries/resource_mysql_database.rb +34 -0
  33. data/cookbooks/database/libraries/resource_mysql_database_user.rb +34 -0
  34. data/cookbooks/database/libraries/resource_postgresql_database.rb +35 -0
  35. data/cookbooks/database/libraries/resource_postgresql_database_user.rb +35 -0
  36. data/cookbooks/database/libraries/resource_sql_server_database.rb +34 -0
  37. data/cookbooks/database/libraries/resource_sql_server_database_user.rb +34 -0
  38. data/cookbooks/database/metadata.rb +21 -0
  39. data/cookbooks/database/recipes/default.rb +20 -0
  40. data/cookbooks/database/recipes/ebs_backup.rb +89 -0
  41. data/cookbooks/database/recipes/ebs_volume.rb +196 -0
  42. data/cookbooks/database/recipes/master.rb +78 -0
  43. data/cookbooks/database/recipes/snapshot.rb +62 -0
  44. data/cookbooks/database/templates/default/app_grants.sql.erb +8 -0
  45. data/cookbooks/database/templates/default/aws_config.erb +3 -0
  46. data/cookbooks/database/templates/default/chef-solo-database-snapshot.cron.erb +10 -0
  47. data/cookbooks/database/templates/default/chef-solo-database-snapshot.json.erb +1 -0
  48. data/cookbooks/database/templates/default/chef-solo-database-snapshot.rb.erb +6 -0
  49. data/cookbooks/database/templates/default/ebs-backup-cron.erb +2 -0
  50. data/cookbooks/database/templates/default/ebs-db-backup.sh.erb +8 -0
  51. data/cookbooks/database/templates/default/ebs-db-restore.sh.erb +10 -0
  52. data/cookbooks/database/templates/default/s3cfg.erb +27 -0
  53. data/cookbooks/mysql/README.md +148 -0
  54. data/cookbooks/mysql/attributes/server.rb +68 -0
  55. data/cookbooks/mysql/libraries/database.rb +19 -0
  56. data/cookbooks/mysql/libraries/helpers.rb +33 -0
  57. data/cookbooks/mysql/metadata.rb +90 -0
  58. data/cookbooks/mysql/recipes/client.rb +51 -0
  59. data/cookbooks/mysql/recipes/default.rb +20 -0
  60. data/cookbooks/mysql/recipes/server.rb +133 -0
  61. data/cookbooks/mysql/recipes/server_ec2.rb +49 -0
  62. data/cookbooks/mysql/templates/default/debian.cnf.erb +12 -0
  63. data/cookbooks/mysql/templates/default/grants.sql.erb +15 -0
  64. data/cookbooks/mysql/templates/default/my.cnf.erb +176 -0
  65. data/cookbooks/mysql/templates/default/mysql-server.seed.erb +10 -0
  66. data/cookbooks/mysql/templates/default/port_mysql.erb +3 -0
  67. data/cookbooks/openssl/README.md +37 -0
  68. data/cookbooks/openssl/libraries/secure_password.rb +37 -0
  69. data/cookbooks/openssl/metadata.rb +8 -0
  70. data/cookbooks/openssl/recipes/default.rb +19 -0
  71. data/cookbooks/passenger_enterprise/README.rdoc +8 -0
  72. data/cookbooks/passenger_enterprise/attributes/default.rb +28 -0
  73. data/cookbooks/passenger_enterprise/cc.gemspec +36 -0
  74. data/cookbooks/passenger_enterprise/metadata.json +44 -0
  75. data/cookbooks/passenger_enterprise/metadata.rb +16 -0
  76. data/cookbooks/passenger_enterprise/recipes/apache2.rb +54 -0
  77. data/cookbooks/passenger_enterprise/recipes/default.rb +30 -0
  78. data/cookbooks/passenger_enterprise/recipes/nginx.rb +50 -0
  79. data/cookbooks/passenger_enterprise/templates/default/passenger.conf.erb +2 -0
  80. data/cookbooks/passenger_enterprise/templates/default/passenger.load.erb +1 -0
  81. data/cookbooks/passenger_enterprise/templates/default/passenger_nginx.conf.erb +2 -0
  82. data/cookbooks/ruby_enterprise/README.rdoc +55 -0
  83. data/cookbooks/ruby_enterprise/attributes/default.rb +33 -0
  84. data/cookbooks/ruby_enterprise/cc.gemspec +36 -0
  85. data/cookbooks/ruby_enterprise/definitions/ree_gem.rb +28 -0
  86. data/cookbooks/ruby_enterprise/metadata.json +45 -0
  87. data/cookbooks/ruby_enterprise/metadata.rb +13 -0
  88. data/cookbooks/ruby_enterprise/recipes/default.rb +67 -0
  89. data/data_bags/deploy/authorized_keys.json +9 -0
  90. data/dna.json +25 -0
  91. data/elzar.gemspec +22 -0
  92. data/lib/elzar.rb +21 -0
  93. data/lib/elzar/assistant.rb +77 -0
  94. data/lib/elzar/chef_dna.rb +48 -0
  95. data/lib/elzar/template.rb +24 -0
  96. data/lib/elzar/templates/Gemfile +11 -0
  97. data/lib/elzar/templates/Vagrantfile.erb +16 -0
  98. data/lib/elzar/templates/dna.json +25 -0
  99. data/lib/elzar/templates/solo.rb.erb +6 -0
  100. data/lib/elzar/version.rb +3 -0
  101. data/roles/enterprise_appstack.rb +4 -0
  102. data/roles/plumbing.rb +11 -0
  103. data/roles/postgres_database.rb +4 -0
  104. data/roles/ruby_appstack.rb +4 -0
  105. data/script/install_cookbook +6 -0
  106. data/script/new_cookbook +5 -0
  107. data/site-cookbooks/.gitkeep +0 -0
  108. data/site-cookbooks/curl/README.md +17 -0
  109. data/site-cookbooks/curl/metadata.rb +6 -0
  110. data/site-cookbooks/curl/recipes/default.rb +11 -0
  111. data/site-cookbooks/nginx/CHANGELOG.md +11 -0
  112. data/site-cookbooks/nginx/CONTRIBUTING +29 -0
  113. data/site-cookbooks/nginx/LICENSE +201 -0
  114. data/site-cookbooks/nginx/README.md +76 -0
  115. data/site-cookbooks/nginx/attributes/default.rb +65 -0
  116. data/site-cookbooks/nginx/definitions/nginx_site.rb +35 -0
  117. data/site-cookbooks/nginx/files/default/mime.types +73 -0
  118. data/site-cookbooks/nginx/metadata.rb +89 -0
  119. data/site-cookbooks/nginx/recipes/default.rb +57 -0
  120. data/site-cookbooks/nginx/recipes/source.rb +126 -0
  121. data/site-cookbooks/nginx/templates/default/default-site.erb +11 -0
  122. data/site-cookbooks/nginx/templates/default/nginx.conf.erb +42 -0
  123. data/site-cookbooks/nginx/templates/default/nginx.init.erb +91 -0
  124. data/site-cookbooks/nginx/templates/default/nxdissite.erb +29 -0
  125. data/site-cookbooks/nginx/templates/default/nxensite.erb +38 -0
  126. data/site-cookbooks/passenger/README.md +12 -0
  127. data/site-cookbooks/passenger/attributes/default.rb +3 -0
  128. data/site-cookbooks/passenger/metadata.rb +6 -0
  129. data/site-cookbooks/passenger/recipes/default.rb +39 -0
  130. data/site-cookbooks/passenger/templates/default/passenger_nginx.conf.erb +2 -0
  131. data/site-cookbooks/postgresql/README.md +150 -0
  132. data/site-cookbooks/postgresql/attributes/default.rb +72 -0
  133. data/site-cookbooks/postgresql/metadata.rb +21 -0
  134. data/site-cookbooks/postgresql/recipes/client.rb +40 -0
  135. data/site-cookbooks/postgresql/recipes/default.rb +20 -0
  136. data/site-cookbooks/postgresql/recipes/server.rb +74 -0
  137. data/site-cookbooks/postgresql/recipes/server_debian.rb +62 -0
  138. data/site-cookbooks/postgresql/recipes/server_redhat.rb +82 -0
  139. data/site-cookbooks/postgresql/templates/default/debian.postgresql.conf.erb +499 -0
  140. data/site-cookbooks/postgresql/templates/default/pg_hba.conf.erb +83 -0
  141. data/site-cookbooks/postgresql/templates/default/redhat.postgresql.conf.erb +501 -0
  142. data/site-cookbooks/rails_app/README.md +15 -0
  143. data/site-cookbooks/rails_app/attributes/default.rb +2 -0
  144. data/site-cookbooks/rails_app/files/default/sudoers +26 -0
  145. data/site-cookbooks/rails_app/metadata.rb +6 -0
  146. data/site-cookbooks/rails_app/recipes/default.rb +143 -0
  147. data/site-cookbooks/rails_app/recipes/system_libraries.rb +12 -0
  148. data/site-cookbooks/rails_app/templates/default/pg_hba.conf.erb +84 -0
  149. data/site-cookbooks/rails_app/templates/default/rails_app_nginx.erb +10 -0
  150. data/site-cookbooks/ruby/README.md +12 -0
  151. data/site-cookbooks/ruby/attributes/default.rb +6 -0
  152. data/site-cookbooks/ruby/metadata.rb +6 -0
  153. data/site-cookbooks/ruby/recipes/default.rb +54 -0
  154. data/solo.rb +7 -0
  155. data/upgrade-chef.sh +8 -0
  156. metadata +272 -0
@@ -0,0 +1,97 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/provider'
20
+
21
+ class Chef
22
+ class Provider
23
+ class Database
24
+ class Mysql < Chef::Provider
25
+ include Chef::Mixin::ShellOut
26
+
27
+ def load_current_resource
28
+ Gem.clear_paths
29
+ require 'mysql'
30
+ @current_resource = Chef::Resource::Database.new(@new_resource.name)
31
+ @current_resource.database_name(@new_resource.database_name)
32
+ @current_resource
33
+ end
34
+
35
+ def action_create
36
+ unless exists?
37
+ begin
38
+ Chef::Log.debug("#{@new_resource}: Creating database #{new_resource.database_name}")
39
+ db.query("create database #{new_resource.database_name}")
40
+ @new_resource.updated_by_last_action(true)
41
+ ensure
42
+ close
43
+ end
44
+ end
45
+ end
46
+
47
+ def action_drop
48
+ if exists?
49
+ begin
50
+ Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
51
+ db.query("drop database #{new_resource.database_name}")
52
+ @new_resource.updated_by_last_action(true)
53
+ ensure
54
+ close
55
+ end
56
+ end
57
+ end
58
+
59
+ def action_query
60
+ if exists?
61
+ begin
62
+ db.select_db(@new_resource.database_name) if @new_resource.database_name
63
+ Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql}]")
64
+ db.query(@new_resource.sql)
65
+ @new_resource.updated_by_last_action(true)
66
+ ensure
67
+ close
68
+ end
69
+ end
70
+ end
71
+
72
+ private
73
+ def exists?
74
+ db.list_dbs.include?(@new_resource.database_name)
75
+ end
76
+
77
+ def db
78
+ @db ||= begin
79
+ ::Mysql.new(
80
+ @new_resource.connection[:host],
81
+ @new_resource.connection[:username],
82
+ @new_resource.connection[:password],
83
+ nil,
84
+ @new_resource.connection[:port] || 3306
85
+ )
86
+ end
87
+ end
88
+
89
+ def close
90
+ @db.close rescue nil
91
+ @db = nil
92
+ end
93
+
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,76 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require File.join(File.dirname(__FILE__), 'provider_database_mysql')
20
+
21
+ class Chef
22
+ class Provider
23
+ class Database
24
+ class MysqlUser < Chef::Provider::Database::Mysql
25
+ include Chef::Mixin::ShellOut
26
+
27
+ def load_current_resource
28
+ Gem.clear_paths
29
+ require 'mysql'
30
+ @current_resource = Chef::Resource::DatabaseUser.new(@new_resource.name)
31
+ @current_resource.username(@new_resource.name)
32
+ @current_resource
33
+ end
34
+
35
+ def action_create
36
+ unless exists?
37
+ begin
38
+ db.query("CREATE USER '#{@new_resource.username}'@'#{@new_resource.host}' IDENTIFIED BY '#{@new_resource.password}'")
39
+ @new_resource.updated_by_last_action(true)
40
+ ensure
41
+ close
42
+ end
43
+ end
44
+ end
45
+
46
+ def action_drop
47
+ if exists?
48
+ begin
49
+ db.query("DROP USER '#{@new_resource.username}'@'#{@new_resource.host}'")
50
+ @new_resource.updated_by_last_action(true)
51
+ ensure
52
+ close
53
+ end
54
+ end
55
+ end
56
+
57
+ def action_grant
58
+ begin
59
+ grant_statement = "GRANT #{@new_resource.privileges.join(', ')} ON #{@new_resource.database_name || "*"}.#{@new_resource.table || "*"} TO '#{@new_resource.username}'@'#{@new_resource.host}' IDENTIFIED BY '#{@new_resource.password}'"
60
+ Chef::Log.info("#{@new_resource}: granting access with statement [#{grant_statement}]")
61
+ db.query(grant_statement)
62
+ @new_resource.updated_by_last_action(true)
63
+ ensure
64
+ close
65
+ end
66
+ end
67
+
68
+ private
69
+ def exists?
70
+ db.query("select User,host from mysql.user where User='#{@new_resource.username}' AND host = '#{@new_resource.host}'").num_rows != 0
71
+ end
72
+
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,126 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Author:: Lamont Granquist (<lamont@opscode.com>)
4
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require 'chef/provider'
21
+
22
+ class Chef
23
+ class Provider
24
+ class Database
25
+ class Postgresql < Chef::Provider
26
+ include Chef::Mixin::ShellOut
27
+
28
+ def load_current_resource
29
+ Gem.clear_paths
30
+ require 'pg'
31
+ @current_resource = Chef::Resource::Database.new(@new_resource.name)
32
+ @current_resource.database_name(@new_resource.database_name)
33
+ @current_resource
34
+ end
35
+
36
+ def action_create
37
+ unless exists?
38
+ begin
39
+ encoding = @new_resource.encoding
40
+ if encoding != "DEFAULT"
41
+ encoding = "'#{@new_resource.encoding}'"
42
+ end
43
+ Chef::Log.debug("#{@new_resource}: Creating database #{new_resource.database_name}")
44
+ create_sql = "CREATE DATABASE #{new_resource.database_name}"
45
+ create_sql += " TEMPLATE = #{new_resource.template}" if new_resource.template
46
+ create_sql += " ENCODING = #{encoding}" if new_resource.encoding
47
+ create_sql += " TABLESPACE = #{new_resource.tablespace}" if new_resource.tablespace
48
+ create_sql += " CONNECTION LIMIT = #{new_resource.connection_limit}" if new_resource.connection_limit
49
+ create_sql += " OWNER = #{new_resource.owner}" if new_resource.owner
50
+ Chef::Log.debug("#{@new_resource}: Performing query [#{create_sql}]")
51
+ db("template1").query(create_sql)
52
+ @new_resource.updated_by_last_action(true)
53
+ ensure
54
+ close
55
+ end
56
+ end
57
+ end
58
+
59
+ def action_drop
60
+ if exists?
61
+ begin
62
+ Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
63
+ db("template1").query("drop database #{new_resource.database_name}")
64
+ @new_resource.updated_by_last_action(true)
65
+ ensure
66
+ close
67
+ end
68
+ end
69
+ end
70
+
71
+ def action_query
72
+ if exists?
73
+ begin
74
+ Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql}]")
75
+ db(@new_resource.database_name).query(@new_resource.sql)
76
+ Chef::Log.debug("#{@new_resource}: query [#{new_resource.sql}] succeeded")
77
+ @new_resource.updated_by_last_action(true)
78
+ ensure
79
+ close
80
+ end
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def exists?
87
+ Chef::Log.debug("#{@new_resource}: checking if database #{@new_resource.database_name} exists")
88
+ ret = db("template1").query("select * from pg_database where datname = '#{@new_resource.database_name}'").num_tuples != 0
89
+ ret ? Chef::Log.debug("#{@new_resource}: database #{@new_resource.database_name} exists") :
90
+ Chef::Log.debug("#{@new_resource}: database #{@new_resource.database_name} does not exist")
91
+ ret
92
+ end
93
+
94
+ #
95
+ # Specifying the database in the connection parameter for the postgres resource is not recommended.
96
+ #
97
+ # - action_create/drop/exists will use the "template1" database to do work by default.
98
+ # - action_query will use the resource database_name.
99
+ # - specifying a database in the connection will override this behavior
100
+ #
101
+ def db(dbname = nil)
102
+ close if @db
103
+ dbname = @new_resource.connection[:database] if @new_resource.connection[:database]
104
+ host = @new_resource.connection[:host]
105
+ port = @new_resource.connection[:port] || 5432
106
+ user = @new_resource.connection[:username] || "postgres"
107
+ Chef::Log.debug("#{@new_resource}: connecting to database #{dbname} on #{host}:#{port} as #{user}")
108
+ password = @new_resource.connection[:password] || node[:postgresql][:password][:postgres]
109
+ @db = ::PGconn.new(
110
+ :host => host,
111
+ :port => port,
112
+ :dbname => dbname,
113
+ :user => user,
114
+ :password => password
115
+ )
116
+ end
117
+
118
+ def close
119
+ @db.close rescue nil
120
+ @db = nil
121
+ end
122
+
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,78 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Author:: Lamont Granquist (<lamont@opscode.com>)
4
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
5
+ # License:: Apache License, Version 2.0
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ require File.join(File.dirname(__FILE__), 'provider_database_postgresql')
21
+
22
+ class Chef
23
+ class Provider
24
+ class Database
25
+ class PostgresqlUser < Chef::Provider::Database::Postgresql
26
+ include Chef::Mixin::ShellOut
27
+
28
+ def load_current_resource
29
+ Gem.clear_paths
30
+ require 'pg'
31
+ @current_resource = Chef::Resource::DatabaseUser.new(@new_resource.name)
32
+ @current_resource.username(@new_resource.name)
33
+ @current_resource
34
+ end
35
+
36
+ def action_create
37
+ unless exists?
38
+ begin
39
+ db("template1").query("CREATE USER #{@new_resource.username} WITH PASSWORD '#{@new_resource.password}'")
40
+ @new_resource.updated_by_last_action(true)
41
+ ensure
42
+ close
43
+ end
44
+ end
45
+ end
46
+
47
+ def action_drop
48
+ if exists?
49
+ begin
50
+ db("template1").query("DROP USER #{@new_resource.username}")
51
+ @new_resource.updated_by_last_action(true)
52
+ ensure
53
+ close
54
+ end
55
+ end
56
+ end
57
+
58
+ def action_grant
59
+ begin
60
+ # FIXME: grants on individual tables
61
+ grant_statement = "GRANT #{@new_resource.privileges.join(', ')} ON DATABASE #{@new_resource.database_name} TO #{@new_resource.username}"
62
+ Chef::Log.info("#{@new_resource}: granting access with statement [#{grant_statement}]")
63
+ db(@new_resource.database_name).query(grant_statement)
64
+ @new_resource.updated_by_last_action(true)
65
+ ensure
66
+ close
67
+ end
68
+ end
69
+
70
+ private
71
+ def exists?
72
+ db("template1").query("select * from pg_user where usename='#{@new_resource.username}'").num_tuples != 0
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,109 @@
1
+ #
2
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
3
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/provider'
20
+
21
+ class Chef
22
+ class Provider
23
+ class Database
24
+ class SqlServer < Chef::Provider
25
+ include Chef::Mixin::ShellOut
26
+
27
+ def load_current_resource
28
+ Gem.clear_paths
29
+ require 'tiny_tds'
30
+ @current_resource = Chef::Resource::Database.new(@new_resource.name)
31
+ @current_resource.database_name(@new_resource.database_name)
32
+ @current_resource
33
+ end
34
+
35
+ def action_create
36
+ unless exists?
37
+ begin
38
+ Chef::Log.debug("#{@new_resource}: Creating database #{new_resource.database_name}")
39
+ db.execute("CREATE DATABASE [#{new_resource.database_name}]").do
40
+ @new_resource.updated_by_last_action(true)
41
+ ensure
42
+ close
43
+ end
44
+ end
45
+ end
46
+
47
+ def action_drop
48
+ if exists?
49
+ begin
50
+ Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
51
+ db.execute("DROP DATABASE #{new_resource.database_name}").do
52
+ @new_resource.updated_by_last_action(true)
53
+ ensure
54
+ close
55
+ end
56
+ end
57
+ end
58
+
59
+ def action_query
60
+ if exists?
61
+ begin
62
+ #db.select_db(@new_resource.database_name) if @new_resource.database_name
63
+ Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql}]")
64
+ db.execute(@new_resource.sql).do
65
+ @new_resource.updated_by_last_action(true)
66
+ ensure
67
+ close
68
+ end
69
+ end
70
+ end
71
+
72
+ private
73
+ def exists?
74
+ exists = false
75
+ begin
76
+ result = db.execute("SELECT name FROM sys.databases")
77
+ result.each do |row|
78
+ if row['name'] =~ /#{@new_resource.database_name}/i
79
+ exists = true
80
+ break
81
+ end
82
+ end
83
+ result.cancel
84
+ ensure
85
+ close
86
+ end
87
+ exists
88
+ end
89
+
90
+ def db
91
+ @db ||= begin
92
+ ::TinyTds::Client.new(
93
+ :host => @new_resource.connection[:host],
94
+ :username => @new_resource.connection[:username],
95
+ :password => @new_resource.connection[:password],
96
+ :port => @new_resource.connection[:port] || 1433
97
+ )
98
+ end
99
+ end
100
+
101
+ def close
102
+ @db.close rescue nil
103
+ @db = nil
104
+ end
105
+
106
+ end
107
+ end
108
+ end
109
+ end