From d3339a49a6682be52011fe1ccf613bae5c327b14 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Sun, 30 Jun 2019 17:28:00 -0400 Subject: [PATCH] added MySQL --- attributes/mysql.rb | 6 ++++++ metadata.rb | 3 ++- recipes/mysql.rb | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 attributes/mysql.rb create mode 100644 recipes/mysql.rb diff --git a/attributes/mysql.rb b/attributes/mysql.rb new file mode 100644 index 0000000..28422de --- /dev/null +++ b/attributes/mysql.rb @@ -0,0 +1,6 @@ +require 'securerandom' + +node.default['db']['name'] = "#{node['app']['name']}" +node.default['db']['user'] = "#{node['app']['name']}" +node.default['db']['root_password'] = SecureRandom.hex(13) +node.default['db']['password'] = SecureRandom.hex(13) diff --git a/metadata.rb b/metadata.rb index 51e84b5..3172552 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,11 +4,12 @@ maintainer_email 'you@example.com' license 'All Rights Reserved' description 'Installs/Configures t42-common' long_description 'Installs/Configures t42-common' -version '0.1.4' +version '0.1.5' chef_version '>= 13.0' depends 'nodejs' depends 'postgresql' +depends 'mysql' # The `issues_url` points to the location where issues for this cookbook are # tracked. A `View Issues` link will be displayed on this cookbook's page when diff --git a/recipes/mysql.rb b/recipes/mysql.rb new file mode 100644 index 0000000..47c33fd --- /dev/null +++ b/recipes/mysql.rb @@ -0,0 +1,21 @@ +mysql_service node['db']['name'] do + # version '5.7' + bind_address '127.0.0.1' + port '3306' + # data_dir '/data' + initial_root_password node['db']['root_password'] + + action [:create, :start] +end + + +bash 'Make mysql Database and User' do + code <<~EOH + mysql -h 127.0.0.1 -uroot -p"#{node['db']['root_password']}" -e "CREATE DATABASE #{node['db']['user']} /*\!40100 DEFAULT CHARACTER SET utf8 */;" + mysql -h 127.0.0.1 -uroot -p"#{node['db']['root_password']}" -e "CREATE USER #{node['db']['user']}@localhost IDENTIFIED BY '#{node['db']['password']}';" + mysql -h 127.0.0.1 -uroot -p"#{node['db']['root_password']}" -e "GRANT ALL PRIVILEGES ON #{node['db']['user']}.* TO '#{node['db']['user']}'@'localhost';" + mysql -h 127.0.0.1 -uroot -p"#{node['db']['root_password']}" -e "FLUSH PRIVILEGES;" + + EOH + not_if "mysql -h 127.0.0.1 -uroot -p\"#{node['db']['root_password']}\" -e 'use #{node['db']['name']}'" +end