Skip to content

Commit

Permalink
Merge pull request #9 from brint-hacking/move_mysql_add_apache_option
Browse files Browse the repository at this point in the history
Move mysql to recipe; Add apache option
  • Loading branch information
brint committed Apr 29, 2014
2 parents b3fada2 + 60fb683 commit e9d74d7
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 116 deletions.
12 changes: 12 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ suites:
magento:
db:
password: magento
- name: apache
run_list:
- recipe[magento::default]
attributes:
mysql:
server_debian_password: test
server_root_password: test
server_repl_password: test
magento:
webserver: apache2
db:
password: magento
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Documentation:
Enabled: false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Collection of recipes to build app stack for the [Magento][] deployments with

### With Berkshelf

echo "cookbook 'magento', '~> 0.6'" >> Berksfile
echo "cookbook 'magento', '~> 0.7'" >> Berksfile
berks install
berks upload # if using with Chef Server

Expand Down
8 changes: 8 additions & 0 deletions attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
default[:magento][:url] = 'http://www.magentocommerce.com/downloads/assets/1.'\
'8.1.0/magento-1.8.1.0.tar.gz'
default[:magento][:dir] = '/var/www/magento'
default[:magento][:domain] = node['fqdn']
# Magento CE's sample data can be found here:
# 'http://www.magentocommerce.com/downloads/assets/1.6.1.0/magento-sample-dat'\
# 'a-1.6.1.0.tar.gz'
Expand Down Expand Up @@ -43,9 +44,16 @@
}
]

# Web Server SSL Settings
default[:magento][:cert_name] = "#{node[:magento][:domain]}.pem"

# Credentials
::Chef::Node.send(:include, Opscode::OpenSSL::Password)

default[:magento][:database] = 'mysql'

default[:magento][:db][:host] = 'localhost'
default[:magento][:db][:database] = 'magento'
default[:magento][:db][:username] = 'magentouser'
set_unless[:magento][:db][:password] = secure_password
default[:magento][:db][:acl] = 'localhost'
67 changes: 0 additions & 67 deletions definitions/magento_site.rb

This file was deleted.

21 changes: 21 additions & 0 deletions libraries/magento.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'fileutils'

class Chef
class Recipe
class Magento
def self.create_ssl_cert(cert_path, domain, file_name)
pem_file = File.join(cert_path, file_name)

unless File.exist?(pem_file)
f = FileUtils
f.mkdir_p cert_path

# One-liner to generate a SSL cert
system "openssl req -x509 -nodes -days 365 -subj '/CN=#{domain}/O=O"\
"ps/C=ZZ/ST=State/L=City' -newkey rsa:4096 -keyout "\
"#{pem_file} -out #{pem_file} 2>/dev/null"
end
end
end
end
end
1 change: 1 addition & 0 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
end

depends 'php-fpm', '>= 0.6.4'
depends 'nginx', '~> 2.6'
41 changes: 37 additions & 4 deletions definitions/magento_database.rb → recipes/_db_mysql.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# coding: utf-8
define :magento_database do

installed_file = '/root/.magento.db.installed'

unless File.exist?(installed_file)

include_recipe 'mysql::server'
include_recipe 'mysql::client'
include_recipe 'mysql-chef_gem'

root_password = node[:mysql][:server_root_password]
db_config = node[:magento][:db]

execute 'mysql-install-mage-privileges' do
command <<-EOH
/usr/bin/mysql -u root -p#{node[:mysql][:server_root_password]} < \
/usr/bin/mysql -u root -p#{root_password} < \
/etc/mysql/mage-grants.sql
EOH
action :nothing
Expand All @@ -26,14 +32,14 @@

execute "create #{node[:magento][:db][:database]} database" do
command <<-EOH
/usr/bin/mysqladmin -u root -p#{node[:mysql][:server_root_password]} \
/usr/bin/mysqladmin -u root -p#{root_password} \
create #{node[:magento][:db][:database]}
EOH
not_if do
require 'rubygems'
Gem.clear_paths
require 'mysql'
m = Mysql.new('localhost', 'root', node[:mysql][:server_root_password])
m = Mysql.new('localhost', 'root', root_password)
m.list_dbs.include?(node[:magento][:db][:database])
end
end
Expand All @@ -50,4 +56,31 @@
end
end

# Import Sample Data
unless node[:magento][:sample_data_url].empty?
include_recipe 'mysql::client'

remote_file File.join(Chef::Config[:file_cache_path],
'magento-sample-data.tar.gz') do
source node[:magento][:sample_data_url]
mode 0644
end

bash 'magento-sample-data' do
cwd "#{Chef::Config[:file_cache_path]}"
code <<-EOH
mkdir #{name}
cd #{name}
tar --strip-components 1 -xzf \
#{Chef::Config[:file_cache_path]}/magento-sample-data.tar.gz
mv media/* #{node[:magento][:dir]}/media/
mv magento_sample_data*.sql data.sql 2>/dev/null
/usr/bin/mysql -h #{db_config[:host]} -u #{db_config[:username]} \
-p#{db_config[:password]} #{db_config[:database]} < data.sql
cd ..
rm -rf #{name}
EOH
end
end
end
30 changes: 30 additions & 0 deletions recipes/_web_apache2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# coding: utf-8

node.set['apache']['default_modules'] = %w(status actions alias auth_basic
authn_file authz_default
authz_groupfile authz_host
authz_user autoindex dir env mime
negotiation setenvif ssl headers
expires log_config logio fastcgi)
include_recipe 'apache2'

Magento.create_ssl_cert(File.join(node[:apache][:dir], 'ssl'),
node[:magento][:domain], node[:magento][:cert_name])

%w(default ssl).each do |site|
web_app "#{site}" do
template 'apache2-site.conf.erb'
docroot node[:magento][:dir]
server_name node[:magento][:domain]
server_aliases node.fqdn
ssl true if site == 'ssl'
ssl_cert File.join(node[:apache][:dir], 'ssl', node[:magento][:cert_name])
ssl_key File.join(node[:apache][:dir], 'ssl', node[:magento][:cert_name])
end
end

%w(default 000-default).each do |site|
apache_site "#{site}" do
enable false
end
end
43 changes: 43 additions & 0 deletions recipes/_web_nginx.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# coding: utf-8

include_recipe 'nginx'

Magento.create_ssl_cert(File.join(node[:nginx][:dir], 'ssl'),
node[:magento][:domain], node[:magento][:cert_name])

%w(backend).each do |file|
cookbook_file File.join(node[:nginx][:dir], 'conf.d', "#{file}.conf") do
source "nginx/#{file}.conf"
mode 0644
owner 'root'
group 'root'
end
end

bash 'Drop default site' do
cwd node[:nginx][:dir]
code <<-EOH
rm -rf conf.d/default.conf
EOH
notifies :reload, resources(service: 'nginx')
end

%w(default ssl).each do |site|
template File.join(node[:nginx][:dir], 'sites-available', site) do
source 'nginx-site.erb'
owner 'root'
group 'root'
mode 0644
variables(
path: node[:magento][:dir],
ssl: (site == 'ssl') ? true : false,
ssl_cert: File.join(node[:nginx][:dir], 'ssl',
node[:magento][:cert_name]),
ssl_key: File.join(node[:nginx][:dir], 'ssl', node[:magento][:cert_name])
)
end
nginx_site site do
template nil
notifies :reload, resources(service: 'nginx')
end
end
43 changes: 8 additions & 35 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@
end

bash 'Tweak apc.ini file' do
cwd "#{php_conf[1]}" # module ini files
cwd php_conf[1] # module ini files
code <<-EOH
grep -q -e 'apc.stat=0' apc.ini || echo "apc.stat=0" >> apc.ini
EOH
end

bash 'Tweak FPM php.ini file' do
cwd "#{php_conf[0]}" # php.ini location
cwd php_conf[0] # php.ini location
code <<-EOH
sed -i 's/memory_limit = .*/memory_limit = 128M/' php.ini
sed -i 's/;realpath_cache_size = .*/realpath_cache_size = 32K/' php.ini
Expand All @@ -80,7 +80,7 @@
recursive true
end

magento_site
include_recipe "magento::_web_#{node[:magento][:webserver]}"

# Fetch magento release
unless node[:magento][:url].empty?
Expand All @@ -99,8 +99,8 @@

# Setup Database
# if Chef::Config[:solo]
db_config = { host: 'localhost' }
db_user = node[:magento][:db]
db_config = node[:magento][:db]

# else
# FIXME: data bags search throwing 404 error: Net::HTTPServerException
# db_config = search(:db_config, "id:master").first ||
Expand All @@ -109,45 +109,18 @@
# enc_key = search(:magento, "id:enckey").first
# end

magento_database if db_config[:host] == 'localhost'

# Import Sample Data
unless node[:magento][:sample_data_url].empty?
include_recipe 'mysql::client'

remote_file File.join(Chef::Config[:file_cache_path],
'magento-sample-data.tar.gz') do
source node[:magento][:sample_data_url]
mode 0644
end

bash 'magento-sample-data' do
cwd "#{Chef::Config[:file_cache_path]}"
code <<-EOH
mkdir #{name}
cd #{name}
tar --strip-components 1 -xzf \
#{Chef::Config[:file_cache_path]}/magento-sample-data.tar.gz
mv media/* #{node[:magento][:dir]}/media/
mv magento_sample_data*.sql data.sql 2>/dev/null
/usr/bin/mysql -h #{db_config[:host]} -u #{db_user[:username]} \
-p#{db_user[:password]} #{db_user[:database]} < data.sql
cd ..
rm -rf #{name}
EOH
end
if db_config[:host] == 'localhost'
include_recipe "magento::_db_#{node[:magento][:database]}"
end

# Generate local.xml file
if enc_key
template File.join(node[:magento][:dir], 'app/etc/local.xml') do
template File.join(node[:magento][:dir], 'app', 'etc', 'local.xml') do
source 'local.xml.erb'
mode 0600
owner node[:magento][:user]
variables(
db_config: db_config,
db_user: db_user,
enc_key: enc_key,
session: node[:magento][:session],
inst_date: inst_date
Expand Down
Loading

0 comments on commit e9d74d7

Please sign in to comment.