Skip to content

Commit e9d74d7

Browse files
committed
Merge pull request #9 from brint-hacking/move_mysql_add_apache_option
Move mysql to recipe; Add apache option
2 parents b3fada2 + 60fb683 commit e9d74d7

File tree

15 files changed

+219
-116
lines changed

15 files changed

+219
-116
lines changed

.kitchen.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,15 @@ suites:
2727
magento:
2828
db:
2929
password: magento
30+
- name: apache
31+
run_list:
32+
- recipe[magento::default]
33+
attributes:
34+
mysql:
35+
server_debian_password: test
36+
server_root_password: test
37+
server_repl_password: test
38+
magento:
39+
webserver: apache2
40+
db:
41+
password: magento

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Documentation:
2+
Enabled: false

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Collection of recipes to build app stack for the [Magento][] deployments with
77

88
### With Berkshelf
99

10-
echo "cookbook 'magento', '~> 0.6'" >> Berksfile
10+
echo "cookbook 'magento', '~> 0.7'" >> Berksfile
1111
berks install
1212
berks upload # if using with Chef Server
1313

attributes/default.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
default[:magento][:url] = 'http://www.magentocommerce.com/downloads/assets/1.'\
44
'8.1.0/magento-1.8.1.0.tar.gz'
55
default[:magento][:dir] = '/var/www/magento'
6+
default[:magento][:domain] = node['fqdn']
67
# Magento CE's sample data can be found here:
78
# 'http://www.magentocommerce.com/downloads/assets/1.6.1.0/magento-sample-dat'\
89
# 'a-1.6.1.0.tar.gz'
@@ -43,9 +44,16 @@
4344
}
4445
]
4546

47+
# Web Server SSL Settings
48+
default[:magento][:cert_name] = "#{node[:magento][:domain]}.pem"
49+
4650
# Credentials
4751
::Chef::Node.send(:include, Opscode::OpenSSL::Password)
4852

53+
default[:magento][:database] = 'mysql'
54+
55+
default[:magento][:db][:host] = 'localhost'
4956
default[:magento][:db][:database] = 'magento'
5057
default[:magento][:db][:username] = 'magentouser'
5158
set_unless[:magento][:db][:password] = secure_password
59+
default[:magento][:db][:acl] = 'localhost'

definitions/magento_site.rb

Lines changed: 0 additions & 67 deletions
This file was deleted.

libraries/magento.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'fileutils'
2+
3+
class Chef
4+
class Recipe
5+
class Magento
6+
def self.create_ssl_cert(cert_path, domain, file_name)
7+
pem_file = File.join(cert_path, file_name)
8+
9+
unless File.exist?(pem_file)
10+
f = FileUtils
11+
f.mkdir_p cert_path
12+
13+
# One-liner to generate a SSL cert
14+
system "openssl req -x509 -nodes -days 365 -subj '/CN=#{domain}/O=O"\
15+
"ps/C=ZZ/ST=State/L=City' -newkey rsa:4096 -keyout "\
16+
"#{pem_file} -out #{pem_file} 2>/dev/null"
17+
end
18+
end
19+
end
20+
end
21+
end

metadata.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
end
1919

2020
depends 'php-fpm', '>= 0.6.4'
21+
depends 'nginx', '~> 2.6'

definitions/magento_database.rb renamed to recipes/_db_mysql.rb

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# coding: utf-8
2-
define :magento_database do
2+
3+
installed_file = '/root/.magento.db.installed'
4+
5+
unless File.exist?(installed_file)
36

47
include_recipe 'mysql::server'
58
include_recipe 'mysql::client'
69
include_recipe 'mysql-chef_gem'
710

11+
root_password = node[:mysql][:server_root_password]
12+
db_config = node[:magento][:db]
13+
814
execute 'mysql-install-mage-privileges' do
915
command <<-EOH
10-
/usr/bin/mysql -u root -p#{node[:mysql][:server_root_password]} < \
16+
/usr/bin/mysql -u root -p#{root_password} < \
1117
/etc/mysql/mage-grants.sql
1218
EOH
1319
action :nothing
@@ -26,14 +32,14 @@
2632

2733
execute "create #{node[:magento][:db][:database]} database" do
2834
command <<-EOH
29-
/usr/bin/mysqladmin -u root -p#{node[:mysql][:server_root_password]} \
35+
/usr/bin/mysqladmin -u root -p#{root_password} \
3036
create #{node[:magento][:db][:database]}
3137
EOH
3238
not_if do
3339
require 'rubygems'
3440
Gem.clear_paths
3541
require 'mysql'
36-
m = Mysql.new('localhost', 'root', node[:mysql][:server_root_password])
42+
m = Mysql.new('localhost', 'root', root_password)
3743
m.list_dbs.include?(node[:magento][:db][:database])
3844
end
3945
end
@@ -50,4 +56,31 @@
5056
end
5157
end
5258

59+
# Import Sample Data
60+
unless node[:magento][:sample_data_url].empty?
61+
include_recipe 'mysql::client'
62+
63+
remote_file File.join(Chef::Config[:file_cache_path],
64+
'magento-sample-data.tar.gz') do
65+
source node[:magento][:sample_data_url]
66+
mode 0644
67+
end
68+
69+
bash 'magento-sample-data' do
70+
cwd "#{Chef::Config[:file_cache_path]}"
71+
code <<-EOH
72+
mkdir #{name}
73+
cd #{name}
74+
tar --strip-components 1 -xzf \
75+
#{Chef::Config[:file_cache_path]}/magento-sample-data.tar.gz
76+
mv media/* #{node[:magento][:dir]}/media/
77+
78+
mv magento_sample_data*.sql data.sql 2>/dev/null
79+
/usr/bin/mysql -h #{db_config[:host]} -u #{db_config[:username]} \
80+
-p#{db_config[:password]} #{db_config[:database]} < data.sql
81+
cd ..
82+
rm -rf #{name}
83+
EOH
84+
end
85+
end
5386
end

recipes/_web_apache2.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# coding: utf-8
2+
3+
node.set['apache']['default_modules'] = %w(status actions alias auth_basic
4+
authn_file authz_default
5+
authz_groupfile authz_host
6+
authz_user autoindex dir env mime
7+
negotiation setenvif ssl headers
8+
expires log_config logio fastcgi)
9+
include_recipe 'apache2'
10+
11+
Magento.create_ssl_cert(File.join(node[:apache][:dir], 'ssl'),
12+
node[:magento][:domain], node[:magento][:cert_name])
13+
14+
%w(default ssl).each do |site|
15+
web_app "#{site}" do
16+
template 'apache2-site.conf.erb'
17+
docroot node[:magento][:dir]
18+
server_name node[:magento][:domain]
19+
server_aliases node.fqdn
20+
ssl true if site == 'ssl'
21+
ssl_cert File.join(node[:apache][:dir], 'ssl', node[:magento][:cert_name])
22+
ssl_key File.join(node[:apache][:dir], 'ssl', node[:magento][:cert_name])
23+
end
24+
end
25+
26+
%w(default 000-default).each do |site|
27+
apache_site "#{site}" do
28+
enable false
29+
end
30+
end

recipes/_web_nginx.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# coding: utf-8
2+
3+
include_recipe 'nginx'
4+
5+
Magento.create_ssl_cert(File.join(node[:nginx][:dir], 'ssl'),
6+
node[:magento][:domain], node[:magento][:cert_name])
7+
8+
%w(backend).each do |file|
9+
cookbook_file File.join(node[:nginx][:dir], 'conf.d', "#{file}.conf") do
10+
source "nginx/#{file}.conf"
11+
mode 0644
12+
owner 'root'
13+
group 'root'
14+
end
15+
end
16+
17+
bash 'Drop default site' do
18+
cwd node[:nginx][:dir]
19+
code <<-EOH
20+
rm -rf conf.d/default.conf
21+
EOH
22+
notifies :reload, resources(service: 'nginx')
23+
end
24+
25+
%w(default ssl).each do |site|
26+
template File.join(node[:nginx][:dir], 'sites-available', site) do
27+
source 'nginx-site.erb'
28+
owner 'root'
29+
group 'root'
30+
mode 0644
31+
variables(
32+
path: node[:magento][:dir],
33+
ssl: (site == 'ssl') ? true : false,
34+
ssl_cert: File.join(node[:nginx][:dir], 'ssl',
35+
node[:magento][:cert_name]),
36+
ssl_key: File.join(node[:nginx][:dir], 'ssl', node[:magento][:cert_name])
37+
)
38+
end
39+
nginx_site site do
40+
template nil
41+
notifies :reload, resources(service: 'nginx')
42+
end
43+
end

0 commit comments

Comments
 (0)