Skip to content
Snippets Groups Projects
default.rb 10.22 KiB
#
# Cookbook Name:: typo3_site
# Recipe:: default
#
# Copyright sgalinski Internet Services
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

##########################
### Add hostname entry ###
##########################

hostsfile_entry node['typo3_site']['ip_address'] do
	hostname node['typo3_site']['hostname']
	action :append
end

hostsfile_entry node['typo3_site']['ip_address'] do
	hostname node['typo3_site']['server_aliases']
	action :append
end

###################################
### Install additional packages ###
###################################

include_recipe 'graphicsmagick'
include_recipe 'apache2'
include_recipe 'apache2::mod_php5'
include_recipe 'apache2::mod_ssl'

%w(php5-curl php5-intl php5-gd php5-mcrypt php5-mysql php5-imagick).each do |name|
	package name do
		action :install
	end
end

################################
### Modify Php Configuration ###
################################

replace_or_add 'Increase time limit' do
	path '/etc/php5/apache2/php.ini'
	pattern 'max_execution_time =.*'
	line 'max_execution_time = 240'
end

replace_or_add 'Increase time limit - CLI' do
	path '/etc/php5/cli/php.ini'
	pattern 'max_execution_time =.*'
	line 'max_execution_time = 240'
end

replace_or_add 'Increase memory limit' do
	path '/etc/php5/apache2/php.ini'
	pattern 'memory_limit =.*'
	line 'memory_limit = 256M'
end

replace_or_add 'Increase upload size limit' do
	path '/etc/php5/apache2/php.ini'
	pattern 'upload_max_filesize =.*'
	line 'upload_max_filesize = 10M'
end

replace_or_add 'Increase upload size limit - CLI' do
	path '/etc/php5/cli/php.ini'
	pattern 'upload_max_filesize =.*'
	line 'upload_max_filesize = 10M'
end

replace_or_add 'Increase post_max_size limit' do
	path '/etc/php5/apache2/php.ini'
	pattern 'post_max_size =.*'
	line 'post_max_size = 10M'
end

replace_or_add 'Increase post_max_size limit - CLI' do
	path '/etc/php5/cli/php.ini'
	pattern 'post_max_size =.*'
	line 'post_max_size = 10M'
end

replace_or_add 'Set date.timezone' do
	path '/etc/php5/apache2/php.ini'
	pattern 'date.timezone =.*'
	line 'date.timezone = Europe/Berlin'
end

replace_or_add 'date.timezone - CLI' do
	path '/etc/php5/cli/php.ini'
	pattern 'date.timezone =.*'
	line 'date.timezone = Europe/Berlin'
end

################################
### Setup Apache Environment ###
################################

# copy ssl key data
template '/etc/apache2/ssl/sslKey.key' do
	source 'sslKey.key'
	owner 'vagrant'
	group 'vagrant'
end

template '/etc/apache2/ssl/sslKey.crt' do
	source 'sslKey.crt'
	owner 'vagrant'
	group 'vagrant'
end

# create web app with an own virtual host
web_app node['typo3_site']['hostname'] do
	template 'vHost.conf.erb'
	docroot "#{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}/"
	server_name node['typo3_site']['hostname']
	server_aliases node['typo3_site']['server_aliases']
end

#######################
### Provide Sources ###
#######################
if node['typo3_site']['repository'] != ''
	ssh_known_hosts_entry node['typo3_site']['repository_hostname'] do
		port node['typo3_site']['repository_port']
	end

	git "#{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}" do
		repository node['typo3_site']['repository']
		remote 'origin'
		revision 'HEAD'
		reference 'master'
		timeout 100000
		user 'vagrant'
		group 'vagrant'
		action :checkout
		ssh_wrapper '/home/vagrant/ssh_wrapper.sh'
	end
end

###################
### Setup TYPO3 ###
###################

bash 'Setup TYPO3' do
	cwd '/home/vagrant/'
	user 'vagrant'
	group 'vagrant'
	timeout 3000000
	only_if { node['typo3_site']['download_typo3'] }

	code <<-EOF
		if [ ! -d #{node['typo3_site']['webroot']}/typo3_src-#{node['typo3_site']['typo3_version']} ]
		then
			wget http://get.typo3.org/#{node['typo3_site']['typo3_version']} \
				-O typo3_src-#{node['typo3_site']['typo3_version']}.tgz

			mkdir -p #{node['typo3_site']['webroot']}/typo3_src-#{node['typo3_site']['typo3_version']}
			tar -xzf typo3_src-#{node['typo3_site']['typo3_version']}.tgz --strip-components=1 \
				-C #{node['typo3_site']['webroot']}/typo3_src-#{node['typo3_site']['typo3_version']};
		fi

		mkdir -p #{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']};
		cd #{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}
		rm -f typo3_src
		ln -s ../typo3_src-#{node['typo3_site']['typo3_version']} typo3_src
		touch typo3conf/ENABLE_INSTALL_TOOL

		if [ ! -f typo3 ]
		then
			ln -s typo3_src/typo3 typo3
		fi

		if [ ! -f index.php ]
		then
			ln -s typo3_src/index.php index.php
		fi
	EOF
	action :run
end

###########################################
### Provide site specific configuration ###
###########################################

directory "#{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}/typo3conf/" do
	owner 'vagrant'
	mode '0755'
	action :create
end

template "#{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}/typo3conf/#{node['typo3_site']['site_configuration']}" do
	source 'SiteConfiguration.erb'
	owner 'vagrant'
	mode '0660'
end

#########################
### Synchronize Files ###
#########################

node['typo3_site']['sync_directories'].each do |sync_data|
	ssh_known_hosts_entry sync_data['hostname'] do
		port sync_data['port']
	end

	bash 'Synchronize ' + sync_data['local'] do
		user 'vagrant'
		group 'vagrant'
		timeout 3000000
		code <<-EOF
			rsync --delete -auvz --progress -e "ssh -i /home/vagrant/.ssh/id_rsa -p #{sync_data['port']} -o StrictHostKeyChecking=no" \
				--no-o --no-g #{sync_data['user']}@#{sync_data['hostname']}:#{sync_data['remote']} \
				"#{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}/#{sync_data['local']}";
		EOF
		action :run
	end
end

####################
### Create Links ###
####################

node['typo3_site']['create_links'].each do |link_data|
	link link_data['source'] do
		to link_data['target']
	end
end

###################
### Setup MySQL ###
###################

mysql_service 'default' do
	initial_root_password 'root'
	action [:create, :start]
end

mysql_config 'default' do
	source 'my.cnf.erb'
	action :create
	notifies :restart, 'mysql_service[default]'
end

mysql_client 'default' do
	action :create
end

########################
### Create Databases ###
########################

# install the necessary mysql2 gem
mysql2_chef_gem 'default' do
	action :install
end

# create the databases
connection_info = {:host => '127.0.0.1', :username => 'root', :password => 'root'}

node['typo3_site']['sync_databases'].each do |database_data|
	# create database
	mysql_database database_data['database_name'] do
		connection connection_info
		action :create
	end

	# create user
	mysql_database_user database_data['database_user'] do
		connection connection_info
		database_name database_data['database_name']
		password database_data['database_password']
		host '127.0.0.1'
		privileges [:select, :update, :insert, :create, :alter, :drop, :delete]
		action :grant
	end

	if database_data['dump_hostname']
		# add host to ssh known hosts
		ssh_known_hosts_entry database_data['dump_hostname'] do
			port database_data['dump_port']
		end

		# download the sql data
		bash 'Download Dump for ' + database_data['database_name'] do
			user 'vagrant'
			group 'vagrant'
			timeout 3000000
			code <<-EOF
			rsync --delete -auvz --progress -e "ssh -i /home/vagrant/.ssh/id_rsa -p #{database_data['dump_port']} -o StrictHostKeyChecking=no" \
				--no-o --no-g #{database_data['dump_user']}@#{database_data['dump_hostname']}:#{database_data['dump_remote']} \
				#{database_data['dump_local']};
			EOF
			action :run
		end

		# load the dump
		bash 'Import Dump for ' + database_data['database_name'] do
			code <<-EOF
				mysql -h 127.0.0.1 -u root -proot #{database_data['database_name']} < #{database_data['dump_local']}
			EOF
			action :run
		end

		# # this would be better, but currently buggy in the database cookbook
		# # https://github.com/opscode-cookbooks/database/issues/128
		# mysql_database 'Import Dump for ' + database_data['database_name'] do
		# 	connection connection_info
		# 	database_name #{database_data['database_name']}
		# 	sql { ::File.open(#{database_data['dump_local']}).read }
		# 	action :query
		# end
	end

	if database_data['post_install_queries']
		database_data['post_install_queries'].each do |query|
			mysql_database 'Execute Post Install Query' do
				connection connection_info
				database_name database_data['database_name']
				sql query
				action :query
			end
		end
	end
end

##################
### Setup Solr ###
##################

if node['typo3_site']['solr']['init']
	include_recipe 'java'
	include_recipe 'typo3_solr'

	typo3_solr_app 'solr' do
		solr node['typo3_site']['solr']['version_solr']
		extension node['typo3_site']['solr']['version_extension']
		plugin_access node['typo3_site']['solr']['version_plugin_access']
		plugin_utils node['typo3_site']['solr']['version_plugin_utils']
		plugin_lang node['typo3_site']['solr']['version_plugin_lang']
		languages node['typo3_site']['solr']['languages']
	end

	node['typo3_site']['solr']['cores'].each do |core_data|
		typo3_solr_core core_data['name'] do
			language core_data['language']
			app 'solr'
			action :add
		end
	end

	execute 'solr-updateConnections' do
		command "php #{node['typo3_site']['webroot']}/#{node['typo3_site']['hostname']}/typo3/cli_dispatch.phpsh solr updateConnections || true"
		user 'vagrant'
		group 'vagrant'
		action :run
	end
end