Last active
December 18, 2015 00:29
-
-
Save shimizukawa/5697337 to your computer and use it in GitHub Desktop.
chef + mysql + grant users to databases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cookbook Name:: myrecipe | |
# Recipe:: mysql_databases | |
# | |
# Copyright 2013, Takayuki SHIMIZUKAWA | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
node.myrecipe.mysql_databases.each do |database_name| | |
mysqlcmd = "mysql -u root --password=#{node.mysql.server_root_password} -e " | |
bash "mysql create #{database_name}" do | |
code <<-EOH | |
#{mysqlcmd} "CREATE DATABASE #{database_name};" | |
EOH | |
not_if <<-EOH | |
#{mysqlcmd} "SHOW DATABASES LIKE \\"#{database_name}\\"\\G" | grep "#{database_name}" | |
EOH | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Cookbook Name:: myrecipe | |
# Recipe:: mysql_grants | |
# | |
# Copyright 2013, Takayuki SHIMIZUKAWA | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
node.myrecipe.mysql_grants.each do |database_name, grant| | |
grant.grants_ipaddresses.each do |ipaddr| | |
mysqlcmd = "mysql -u root --password=#{node.mysql.server_root_password} -e " | |
bash "mysql grant #{database_name} to #{grant.database_account}@#{ipaddr}" do | |
code <<-EOH | |
#{mysqlcmd} \ | |
"GRANT ALL PRIVILEGES ON #{database_name}.* \ | |
TO '#{grant.database_account}'@'#{ipaddr}' \ | |
IDENTIFIED BY '#{grant.database_password}';" | |
EOH | |
not_if "#{mysqlcmd} \"show grants for '#{grant.database_account}'@'#{ipaddr}';\" | grep \"ON \`#{database_name}\`.*\"" | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"default_attributes": { | |
"myrecipe": { | |
"mysql_grants": { | |
"spam": { | |
"grants_ipaddresses": ["10.0.0.1", "10.0.0.2", "localhost"], | |
"database_account": "spam", | |
"database_password": "spamspamspam" | |
}, | |
"egg": { | |
"grants_ipaddresses": ["10.0.0.10", "localhost"], | |
"database_account": "egg", | |
"database_password": "eggeggegg" | |
} | |
}, | |
"mysql_databases": ["spam", "egg"] | |
} | |
}, | |
"run_list": [ | |
"recipe[myrecipe::mysql_databases]", | |
"recipe[myrecipe::mysql_grants]" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment