server_maint 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (126) hide show
  1. data/.gitmodules +21 -0
  2. data/lib/cookbooks/aws/CHANGELOG.md +11 -0
  3. data/lib/cookbooks/aws/CONTRIBUTING +29 -0
  4. data/lib/cookbooks/aws/LICENSE +201 -0
  5. data/lib/cookbooks/aws/README.md +271 -0
  6. data/lib/cookbooks/aws/attributes/default.rb +20 -0
  7. data/lib/cookbooks/aws/libraries/ec2.rb +58 -0
  8. data/lib/cookbooks/aws/metadata.rb +7 -0
  9. data/lib/cookbooks/aws/providers/ebs_volume.rb +236 -0
  10. data/lib/cookbooks/aws/providers/elastic_ip.rb +90 -0
  11. data/lib/cookbooks/aws/providers/elastic_lb.rb +24 -0
  12. data/lib/cookbooks/aws/providers/resource_tag.rb +93 -0
  13. data/lib/cookbooks/aws/recipes/default.rb +25 -0
  14. data/lib/cookbooks/aws/resources/ebs_volume.rb +17 -0
  15. data/lib/cookbooks/aws/resources/elastic_ip.rb +11 -0
  16. data/lib/cookbooks/aws/resources/elastic_lb.rb +10 -0
  17. data/lib/cookbooks/aws/resources/resource_tag.rb +11 -0
  18. data/lib/cookbooks/database/.gitignore +1 -0
  19. data/lib/cookbooks/database/CHANGELOG.md +51 -0
  20. data/lib/cookbooks/database/CONTRIBUTING +29 -0
  21. data/lib/cookbooks/database/LICENSE +201 -0
  22. data/lib/cookbooks/database/README.md +468 -0
  23. data/lib/cookbooks/database/libraries/provider_database_mysql.rb +103 -0
  24. data/lib/cookbooks/database/libraries/provider_database_mysql_user.rb +76 -0
  25. data/lib/cookbooks/database/libraries/provider_database_postgresql.rb +131 -0
  26. data/lib/cookbooks/database/libraries/provider_database_postgresql_user.rb +83 -0
  27. data/lib/cookbooks/database/libraries/provider_database_sql_server.rb +109 -0
  28. data/lib/cookbooks/database/libraries/provider_database_sql_server_user.rb +106 -0
  29. data/lib/cookbooks/database/libraries/resource_database.rb +119 -0
  30. data/lib/cookbooks/database/libraries/resource_database_user.rb +90 -0
  31. data/lib/cookbooks/database/libraries/resource_mysql_database.rb +34 -0
  32. data/lib/cookbooks/database/libraries/resource_mysql_database_user.rb +34 -0
  33. data/lib/cookbooks/database/libraries/resource_postgresql_database.rb +35 -0
  34. data/lib/cookbooks/database/libraries/resource_postgresql_database_user.rb +35 -0
  35. data/lib/cookbooks/database/libraries/resource_sql_server_database.rb +34 -0
  36. data/lib/cookbooks/database/libraries/resource_sql_server_database_user.rb +34 -0
  37. data/lib/cookbooks/database/metadata.rb +22 -0
  38. data/lib/cookbooks/database/recipes/default.rb +20 -0
  39. data/lib/cookbooks/database/recipes/ebs_backup.rb +89 -0
  40. data/lib/cookbooks/database/recipes/ebs_volume.rb +204 -0
  41. data/lib/cookbooks/database/recipes/master.rb +78 -0
  42. data/lib/cookbooks/database/recipes/mysql.rb +20 -0
  43. data/lib/cookbooks/database/recipes/postgresql.rb +20 -0
  44. data/lib/cookbooks/database/recipes/snapshot.rb +62 -0
  45. data/lib/cookbooks/database/templates/default/app_grants.sql.erb +8 -0
  46. data/lib/cookbooks/database/templates/default/aws_config.erb +3 -0
  47. data/lib/cookbooks/database/templates/default/chef-solo-database-snapshot.cron.erb +6 -0
  48. data/lib/cookbooks/database/templates/default/chef-solo-database-snapshot.json.erb +1 -0
  49. data/lib/cookbooks/database/templates/default/chef-solo-database-snapshot.rb.erb +6 -0
  50. data/lib/cookbooks/database/templates/default/ebs-backup-cron.erb +2 -0
  51. data/lib/cookbooks/database/templates/default/ebs-db-backup.sh.erb +8 -0
  52. data/lib/cookbooks/database/templates/default/ebs-db-restore.sh.erb +10 -0
  53. data/lib/cookbooks/database/templates/default/s3cfg.erb +27 -0
  54. data/lib/cookbooks/mysql/.gitignore +5 -0
  55. data/lib/cookbooks/mysql/CHANGELOG.md +86 -0
  56. data/lib/cookbooks/mysql/CONTRIBUTING +29 -0
  57. data/lib/cookbooks/mysql/Gemfile +8 -0
  58. data/lib/cookbooks/mysql/LICENSE +201 -0
  59. data/lib/cookbooks/mysql/README.md +227 -0
  60. data/lib/cookbooks/mysql/attributes/client.rb +50 -0
  61. data/lib/cookbooks/mysql/attributes/server.rb +153 -0
  62. data/lib/cookbooks/mysql/files/default/tests/minitest/server_test.rb +36 -0
  63. data/lib/cookbooks/mysql/files/default/tests/minitest/support/helpers.rb +11 -0
  64. data/lib/cookbooks/mysql/libraries/helpers.rb +33 -0
  65. data/lib/cookbooks/mysql/metadata.rb +140 -0
  66. data/lib/cookbooks/mysql/recipes/client.rb +59 -0
  67. data/lib/cookbooks/mysql/recipes/default.rb +20 -0
  68. data/lib/cookbooks/mysql/recipes/ruby.rb +36 -0
  69. data/lib/cookbooks/mysql/recipes/server.rb +216 -0
  70. data/lib/cookbooks/mysql/recipes/server_ec2.rb +51 -0
  71. data/lib/cookbooks/mysql/templates/default/debian.cnf.erb +12 -0
  72. data/lib/cookbooks/mysql/templates/default/grants.sql.erb +15 -0
  73. data/lib/cookbooks/mysql/templates/default/my.cnf.erb +211 -0
  74. data/lib/cookbooks/mysql/templates/default/mysql-server.seed.erb +10 -0
  75. data/lib/cookbooks/mysql/templates/default/port_mysql.erb +3 -0
  76. data/lib/cookbooks/mysql/templates/windows/my.cnf.erb +61 -0
  77. data/lib/cookbooks/mysql/test/features/query_database.feature +26 -0
  78. data/lib/cookbooks/mysql/test/features/step_definitions/mysql_steps.rb +47 -0
  79. data/lib/cookbooks/mysql/test/features/support/env.rb +3 -0
  80. data/lib/cookbooks/mysql/test/features/support/mysql_helpers.rb +51 -0
  81. data/lib/cookbooks/mysql/test/kitchen/Kitchenfile +5 -0
  82. data/lib/cookbooks/mysql/test/kitchen/cookbooks/mysql_test/.gitignore +1 -0
  83. data/lib/cookbooks/mysql/test/kitchen/cookbooks/mysql_test/README.md +63 -0
  84. data/lib/cookbooks/mysql/test/kitchen/cookbooks/mysql_test/attributes/default.rb +27 -0
  85. data/lib/cookbooks/mysql/test/kitchen/cookbooks/mysql_test/metadata.rb +10 -0
  86. data/lib/cookbooks/mysql/test/kitchen/cookbooks/mysql_test/recipes/client.rb +20 -0
  87. data/lib/cookbooks/mysql/test/kitchen/cookbooks/mysql_test/recipes/server.rb +72 -0
  88. data/lib/cookbooks/openssl/CHANGELOG.md +0 -0
  89. data/lib/cookbooks/openssl/CONTRIBUTING +29 -0
  90. data/lib/cookbooks/openssl/LICENSE +201 -0
  91. data/lib/cookbooks/openssl/README.md +37 -0
  92. data/lib/cookbooks/openssl/libraries/secure_password.rb +37 -0
  93. data/lib/cookbooks/openssl/metadata.rb +8 -0
  94. data/lib/cookbooks/openssl/recipes/default.rb +19 -0
  95. data/lib/cookbooks/postgresql/CHANGELOG.md +35 -0
  96. data/lib/cookbooks/postgresql/CONTRIBUTING +29 -0
  97. data/lib/cookbooks/postgresql/LICENSE +201 -0
  98. data/lib/cookbooks/postgresql/README.md +148 -0
  99. data/lib/cookbooks/postgresql/attributes/default.rb +103 -0
  100. data/lib/cookbooks/postgresql/metadata.rb +21 -0
  101. data/lib/cookbooks/postgresql/recipes/client.rb +26 -0
  102. data/lib/cookbooks/postgresql/recipes/default.rb +20 -0
  103. data/lib/cookbooks/postgresql/recipes/ruby.rb +40 -0
  104. data/lib/cookbooks/postgresql/recipes/server.rb +64 -0
  105. data/lib/cookbooks/postgresql/recipes/server_debian.rb +64 -0
  106. data/lib/cookbooks/postgresql/recipes/server_redhat.rb +77 -0
  107. data/lib/cookbooks/postgresql/templates/default/debian.postgresql.conf.erb +499 -0
  108. data/lib/cookbooks/postgresql/templates/default/pg_hba.conf.erb +83 -0
  109. data/lib/cookbooks/postgresql/templates/default/redhat.postgresql.conf.erb +501 -0
  110. data/lib/cookbooks/sqlite/.gitignore +4 -0
  111. data/lib/cookbooks/sqlite/CHANGELOG.md +3 -0
  112. data/lib/cookbooks/sqlite/CONTRIBUTING +29 -0
  113. data/lib/cookbooks/sqlite/Gemfile +2 -0
  114. data/lib/cookbooks/sqlite/LICENSE +201 -0
  115. data/lib/cookbooks/sqlite/README.md +36 -0
  116. data/lib/cookbooks/sqlite/metadata.rb +12 -0
  117. data/lib/cookbooks/sqlite/recipes/default.rb +30 -0
  118. data/lib/cookbooks/sqlite/test/kitchen/Kitchenfile +1 -0
  119. data/lib/cookbooks/xfs/CHANGELOG.md +0 -0
  120. data/lib/cookbooks/xfs/CONTRIBUTING +29 -0
  121. data/lib/cookbooks/xfs/LICENSE +201 -0
  122. data/lib/cookbooks/xfs/README.md +30 -0
  123. data/lib/cookbooks/xfs/metadata.rb +12 -0
  124. data/lib/cookbooks/xfs/recipes/default.rb +26 -0
  125. data/lib/server_maint/version.rb +1 -1
  126. metadata +127 -4
@@ -0,0 +1,103 @@
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
+ create_sql = "CREATE DATABASE #{new_resource.database_name}"
40
+ create_sql += " CHARACTER SET = #{new_resource.encoding}" if new_resource.encoding
41
+ create_sql += " COLLATE = #{new_resource.collation}" if new_resource.collation
42
+ Chef::Log.debug("#{@new_resource}: Performing query [#{create_sql}]")
43
+ db.query(create_sql)
44
+ @new_resource.updated_by_last_action(true)
45
+ ensure
46
+ close
47
+ end
48
+ end
49
+ end
50
+
51
+ def action_drop
52
+ if exists?
53
+ begin
54
+ Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
55
+ db.query("drop database #{new_resource.database_name}")
56
+ @new_resource.updated_by_last_action(true)
57
+ ensure
58
+ close
59
+ end
60
+ end
61
+ end
62
+
63
+ def action_query
64
+ if exists?
65
+ begin
66
+ db.select_db(@new_resource.database_name) if @new_resource.database_name
67
+ Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql_query}]")
68
+ db.query(@new_resource.sql_query)
69
+ @new_resource.updated_by_last_action(true)
70
+ ensure
71
+ close
72
+ end
73
+ end
74
+ end
75
+
76
+ private
77
+ def exists?
78
+ db.list_dbs.include?(@new_resource.database_name)
79
+ end
80
+
81
+ def db
82
+ @db ||= begin
83
+ connection = ::Mysql.new(
84
+ @new_resource.connection[:host],
85
+ @new_resource.connection[:username],
86
+ @new_resource.connection[:password],
87
+ nil,
88
+ @new_resource.connection[:port] || 3306
89
+ )
90
+ connection.set_server_option ::Mysql::OPTION_MULTI_STATEMENTS_ON
91
+ connection
92
+ end
93
+ end
94
+
95
+ def close
96
+ @db.close rescue nil
97
+ @db = nil
98
+ end
99
+
100
+ end
101
+ end
102
+ end
103
+ 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,131 @@
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 += " LC_CTYPE = '#{new_resource.collation}' LC_COLLATE = '#{new_resource.collation}'" if new_resource.collation
49
+ create_sql += " CONNECTION LIMIT = #{new_resource.connection_limit}" if new_resource.connection_limit
50
+ create_sql += " OWNER = #{new_resource.owner}" if new_resource.owner
51
+ Chef::Log.debug("#{@new_resource}: Performing query [#{create_sql}]")
52
+ db("template1").query(create_sql)
53
+ @new_resource.updated_by_last_action(true)
54
+ ensure
55
+ close
56
+ end
57
+ end
58
+ end
59
+
60
+ def action_drop
61
+ if exists?
62
+ begin
63
+ Chef::Log.debug("#{@new_resource}: Dropping database #{new_resource.database_name}")
64
+ db("template1").query("drop database #{new_resource.database_name}")
65
+ @new_resource.updated_by_last_action(true)
66
+ ensure
67
+ close
68
+ end
69
+ end
70
+ end
71
+
72
+ def action_query
73
+ if exists?
74
+ begin
75
+ Chef::Log.debug("#{@new_resource}: Performing query [#{new_resource.sql_query}]")
76
+ db(@new_resource.database_name).query(@new_resource.sql_query)
77
+ Chef::Log.debug("#{@new_resource}: query [#{new_resource.sql_query}] succeeded")
78
+ @new_resource.updated_by_last_action(true)
79
+ ensure
80
+ close
81
+ end
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ def exists?
88
+ begin
89
+ Chef::Log.debug("#{@new_resource}: checking if database #{@new_resource.database_name} exists")
90
+ ret = db("template1").query("select * from pg_database where datname = '#{@new_resource.database_name}'").num_tuples != 0
91
+ ret ? Chef::Log.debug("#{@new_resource}: database #{@new_resource.database_name} exists") :
92
+ Chef::Log.debug("#{@new_resource}: database #{@new_resource.database_name} does not exist")
93
+ ensure
94
+ close
95
+ end
96
+ ret
97
+ end
98
+
99
+ #
100
+ # Specifying the database in the connection parameter for the postgres resource is not recommended.
101
+ #
102
+ # - action_create/drop/exists will use the "template1" database to do work by default.
103
+ # - action_query will use the resource database_name.
104
+ # - specifying a database in the connection will override this behavior
105
+ #
106
+ def db(dbname = nil)
107
+ close if @db
108
+ dbname = @new_resource.connection[:database] if @new_resource.connection[:database]
109
+ host = @new_resource.connection[:host]
110
+ port = @new_resource.connection[:port] || 5432
111
+ user = @new_resource.connection[:username] || "postgres"
112
+ Chef::Log.debug("#{@new_resource}: connecting to database #{dbname} on #{host}:#{port} as #{user}")
113
+ password = @new_resource.connection[:password] || node[:postgresql][:password][:postgres]
114
+ @db = ::PGconn.new(
115
+ :host => host,
116
+ :port => port,
117
+ :dbname => dbname,
118
+ :user => user,
119
+ :password => password
120
+ )
121
+ end
122
+
123
+ def close
124
+ @db.close rescue nil
125
+ @db = nil
126
+ end
127
+
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,83 @@
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
+ begin
73
+ exists = db("template1").query("select * from pg_user where usename='#{@new_resource.username}'").num_tuples != 0
74
+ ensure
75
+ close
76
+ end
77
+ exists
78
+ end
79
+
80
+ end
81
+ end
82
+ end
83
+ 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_query}]")
64
+ db.execute(@new_resource.sql_query).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
@@ -0,0 +1,106 @@
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_sql_server')
20
+
21
+ class Chef
22
+ class Provider
23
+ class Database
24
+ class SqlServerUser < Chef::Provider::Database::SqlServer
25
+ include Chef::Mixin::ShellOut
26
+
27
+ def load_current_resource
28
+ Gem.clear_paths
29
+ require 'tiny_tds'
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
+ begin
37
+ unless exists?(:logins)
38
+ db.execute("CREATE LOGIN #{@new_resource.username} WITH PASSWORD = '#{@new_resource.password}', CHECK_POLICY = OFF").do
39
+ @new_resource.updated_by_last_action(true)
40
+ end
41
+ unless exists?(:users)
42
+ if @new_resource.database_name
43
+ Chef::Log.info("#{@new_resource} creating user in '#{@new_resource.database_name}' database context.")
44
+ db.execute("USE #{@new_resource.database_name}").do
45
+ else
46
+ Chef::Log.info("#{@new_resource} database_name not provided, creating user in global context.")
47
+ end
48
+ db.execute("CREATE USER #{@new_resource.username} FOR LOGIN #{@new_resource.username}").do
49
+ @new_resource.updated_by_last_action(true)
50
+ end
51
+ ensure
52
+ close
53
+ end
54
+ end
55
+
56
+ def action_drop
57
+ begin
58
+ if exists?(:users)
59
+ db.execute("DROP USER #{@new_resource.username}").do
60
+ @new_resource.updated_by_last_action(true)
61
+ end
62
+ if exists?(:logins)
63
+ db.execute("DROP LOGIN #{@new_resource.username}").do
64
+ @new_resource.updated_by_last_action(true)
65
+ end
66
+ ensure
67
+ close
68
+ end
69
+ end
70
+
71
+ def action_grant
72
+ begin
73
+ if @new_resource.password
74
+ action_create
75
+ end
76
+ Chef::Application.fatal!('Please provide a database_name, SQL Server does not support global GRANT statements.') unless @new_resource.database_name
77
+ grant_statement = "GRANT #{@new_resource.privileges.join(', ')} ON DATABASE::#{@new_resource.database_name} TO #{@new_resource.username}"
78
+ Chef::Log.info("#{@new_resource} granting access with statement [#{grant_statement}]")
79
+ db.execute("USE #{@new_resource.database_name}").do
80
+ db.execute(grant_statement).do
81
+ @new_resource.updated_by_last_action(true)
82
+ ensure
83
+ close
84
+ end
85
+ end
86
+
87
+ private
88
+ def exists?(type=:users)
89
+ case type
90
+ when :users
91
+ table = "database_principals"
92
+ if @new_resource.database_name
93
+ Chef::Log.debug("#{@new_resource} searching for existing user in '#{@new_resource.database_name}' database context.")
94
+ db.execute("USE #{@new_resource.database_name}").do
95
+ end
96
+ when :logins
97
+ table = "server_principals"
98
+ end
99
+
100
+ result = db.execute("SELECT name FROM sys.#{table} WHERE name='#{@new_resource.username}'")
101
+ result.each.any?
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end