From 7d84d59d4e1dcbbcd6b56c482e473ee4fc574b18 Mon Sep 17 00:00:00 2001 From: Phil Fenstermacher Date: Tue, 25 Feb 2014 17:14:28 -0500 Subject: [PATCH] Add support for mod_pagespeed --- README.md | 53 ++++++++++++++ manifests/mod/pagespeed.pp | 48 ++++++++++++ manifests/params.pp | 2 + spec/acceptance/mod_pagespeed_spec.rb | 85 ++++++++++++++++++++++ spec/classes/mod/pagespeed_spec.rb | 32 ++++++++ spec/spec_helper_acceptance.rb | 4 + templates/mod/pagespeed.conf.erb | 101 ++++++++++++++++++++++++++ 7 files changed, 325 insertions(+) create mode 100644 manifests/mod/pagespeed.pp create mode 100644 spec/acceptance/mod_pagespeed_spec.rb create mode 100644 spec/classes/mod/pagespeed_spec.rb create mode 100644 templates/mod/pagespeed.conf.erb diff --git a/README.md b/README.md index 3b072f8d7..f71ee33e5 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ * [Class: apache::default_mods](#class-apachedefault_mods) * [Defined Type: apache::mod](#defined-type-apachemod) * [Classes: apache::mod::*](#classes-apachemodname) + * [Class: apache::mod::pagespeed](#class-apachemodpagespeed) * [Class: apache::mod::ssl](#class-apachemodssl) * [Class: apache::mod::wsgi](#class-apachemodwsgi) * [Defined Type: apache::vhost](#defined-type-apachevhost) @@ -442,6 +443,7 @@ There are many `apache::mod::[name]` classes within this module that can be decl * `mime_magic`* * `negotiation` * `nss`* +* `pagespeed` (see [`apache::mod::pagespeed`](#class-apachemodpagespeed) below) * `passenger`* * `perl` * `peruser` @@ -470,6 +472,57 @@ Modules noted with a * indicate that the module has settings and, thus, a templa The modules mentioned above, and other Apache modules that have templates, will cause template files to be dropped along with the mod install and the module will not work without the template. Any module without a template will install the package but drop no files. +####Class: `apache::mod::pagespeed` + +Installs and manages mod_pagespeed, which is a Google module that rewrites web pages to reduce latency and bandwidth. + +This module does *not* manage the software repositories needed to automatically install the +mod-pagespeed-stable package. The module does however require that the package be installed, +or be installable using the system's default package provider. You should ensure that this +pre-requisite is met or declaring `apache::mod::pagespeed` will cause the puppet run to fail. + +These are the defaults: + +```puppet + class { 'apache::mod::pagespeed': + inherit_vhost_config => 'on', + filter_xhtml => false, + cache_path => '/var/cache/mod_pagespeed/', + log_dir => '/var/log/pagespeed', + memache_servers => [], + rewrite_level => 'CoreFilters', + disable_filters => [], + enable_filters => [], + forbid_filters => [], + rewrite_deadline_per_flush_ms => 10, + additional_domains => undef, + file_cache_size_kb => 102400, + file_cache_clean_interval_ms => 3600000, + lru_cache_per_process => 1024, + lru_cache_byte_limit => 16384, + css_flatten_max_bytes => 2048, + css_inline_max_bytes => 2048, + css_image_inline_max_bytes => 2048, + image_inline_max_bytes => 2048, + js_inline_max_bytes => 2048, + css_outline_min_bytes => 3000, + js_outline_min_bytes => 3000, + inode_limit => 500000, + image_max_rewrites_at_once => 8, + num_rewrite_threads => 4, + num_expensive_rewrite_threads => 4, + collect_statistics => 'on', + statistics_logging => 'on', + allow_view_stats => [], + allow_pagespeed_console => [], + allow_pagespeed_message => [], + message_buffer_size => 100000, + additional_configuration => { } + } +``` + +Full documentation for mod_pagespeed is available from [Google](http://modpagespeed.com). + ####Class: `apache::mod::ssl` Installs Apache SSL capabilities and uses the ssl.conf.erb template. These are the defaults: diff --git a/manifests/mod/pagespeed.pp b/manifests/mod/pagespeed.pp new file mode 100644 index 000000000..d58f99fe2 --- /dev/null +++ b/manifests/mod/pagespeed.pp @@ -0,0 +1,48 @@ +class apache::mod::pagespeed ( + $inherit_vhost_config = 'on', + $filter_xhtml = false, + $cache_path = '/var/cache/mod_pagespeed/', + $log_dir = '/var/log/pagespeed', + $memache_servers = [], + $rewrite_level = 'CoreFilters', + $disable_filters = [], + $enable_filters = [], + $forbid_filters = [], + $rewrite_deadline_per_flush_ms = 10, + $additional_domains = undef, + $file_cache_size_kb = 102400, + $file_cache_clean_interval_ms = 3600000, + $lru_cache_per_process = 1024, + $lru_cache_byte_limit = 16384, + $css_flatten_max_bytes = 2048, + $css_inline_max_bytes = 2048, + $css_image_inline_max_bytes = 2048, + $image_inline_max_bytes = 2048, + $js_inline_max_bytes = 2048, + $css_outline_min_bytes = 3000, + $js_outline_min_bytes = 3000, + $inode_limit = 500000, + $image_max_rewrites_at_once = 8, + $num_rewrite_threads = 4, + $num_expensive_rewrite_threads = 4, + $collect_statistics = 'on', + $statistics_logging = 'on', + $allow_view_stats = [], + $allow_pagespeed_console = [], + $allow_pagespeed_message = [], + $message_buffer_size = 100000, + $additional_configuration = {}, +){ + + apache::mod { 'pagespeed': } + + file { 'pagespeed.conf': + ensure => file, + path => "${::apache::mod_dir}/pagespeed.conf", + content => template('apache/mod/pagespeed.conf.erb'), + require => Exec["mkdir ${::apache::mod_dir}"], + before => File[$::apache::mod_dir], + notify => Service['httpd'], + } + +} diff --git a/manifests/params.pp b/manifests/params.pp index 04c3b65fe..33c492794 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -61,6 +61,7 @@ 'authnz_ldap' => 'mod_authz_ldap', 'fastcgi' => 'mod_fastcgi', 'fcgid' => 'mod_fcgid', + 'pagespeed' => 'mod-pagespeed-stable', 'passenger' => 'mod_passenger', 'perl' => 'mod_perl', 'php5' => $distrelease ? { @@ -123,6 +124,7 @@ 'fastcgi' => 'libapache2-mod-fastcgi', 'fcgid' => 'libapache2-mod-fcgid', 'nss' => 'libapache2-mod-nss', + 'pagespeed' => 'mod-pagespeed-stable', 'passenger' => 'libapache2-mod-passenger', 'perl' => 'libapache2-mod-perl2', 'php5' => 'libapache2-mod-php5', diff --git a/spec/acceptance/mod_pagespeed_spec.rb b/spec/acceptance/mod_pagespeed_spec.rb new file mode 100644 index 000000000..de59a347c --- /dev/null +++ b/spec/acceptance/mod_pagespeed_spec.rb @@ -0,0 +1,85 @@ +require 'spec_helper_acceptance' + +describe 'apache::mod::pagespeed class', :unless => UNSUPPORTED_PLATFORMS.include?(fact('osfamily')) do + case fact('osfamily') + when 'Debian' + vhost_dir = '/etc/apache2/sites-enabled' + mod_dir = '/etc/apache2/mods-available' + service_name = 'apache2' + when 'RedHat' + vhost_dir = '/etc/httpd/conf.d' + mod_dir = '/etc/httpd/conf.d' + service_name = 'httpd' + when 'FreeBSD' + vhost_dir = '/usr/local/etc/apache22/Vhosts' + mod_dir = '/usr/local/etc/apache22/Modules' + service_name = 'apache22' + end + + context "default pagespeed config" do + it 'succeeds in puppeting pagespeed' do + pp= <<-EOS + if $::osfamily == 'Debian' { + class { 'apt': } + + apt::source { 'mod-pagespeed': + key => '7FAC5991', + key_server => 'pgp.mit.edu', + location => 'http://dl.google.com/linux/mod-pagespeed/deb/', + release => 'stable', + repos => 'main', + include_src => false, + before => Class['apache'], + } + } elsif $::osfamily == 'RedHat' { + yumrepo { 'mod-pagespeed': + baseurl => 'http://dl.google.com/linux/mod-pagespeed/rpm/stable/x86_64', + enabled => 1, + gpgcheck => 1, + gpgkey => 'https://dl-ssl.google.com/linux/linux_signing_key.pub', + before => Class['apache'], + } + } + + class { 'apache': + mpm_module => 'prefork', + } + class { 'apache::mod::pagespeed': + enable_filters => ['remove_comments'], + disable_filters => ['extend_cache'], + forbid_filters => ['rewrite_javascript'], + } + apache::vhost { 'pagespeed.example.com': + port => '80', + docroot => '/var/www/pagespeed', + } + host { 'pagespeed.example.com': ip => '127.0.0.1', } + file { '/var/www/pagespeed/index.html': + ensure => file, + content => "\n\n\n

Hello World!

\n\n", + } + EOS + apply_manifest(pp, :catch_failures => true) + end + + describe service(service_name) do + it { should be_enabled } + it { should be_running } + end + + describe file("#{mod_dir}/pagespeed.conf") do + it { should contain "AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html" } + it { should contain "ModPagespeedEnableFilters remove_comments" } + it { should contain "ModPagespeedDisableFilters extend_cache" } + it { should contain "ModPagespeedForbidFilters rewrite_javascript" } + end + + it 'should answer to pagespeed.example.com and include and be stripped of comments by mod_pagespeed' do + shell("/usr/bin/curl pagespeed.example.com:80") do |r| + r.stdout.should =~ // + r.stdout.should_not =~ // + r.exit_code.should == 0 + end + end + end +end diff --git a/spec/classes/mod/pagespeed_spec.rb b/spec/classes/mod/pagespeed_spec.rb new file mode 100644 index 000000000..7480a2b52 --- /dev/null +++ b/spec/classes/mod/pagespeed_spec.rb @@ -0,0 +1,32 @@ +describe 'apache::mod::pagespeed', :type => :class do + let :pre_condition do + 'include apache' + end + context "on a Debian OS" do + let :facts do + { + :osfamily => 'Debian', + :operatingsystemrelease => '6', + :concat_basedir => '/dne', + } + end + it { should contain_class("apache::params") } + it { should contain_apache__mod('pagespeed') } + it { should contain_package("mod-pagespeed-stable") } + it { should contain_file('pagespeed.conf') } + end + + context "on a RedHat OS" do + let :facts do + { + :osfamily => 'RedHat', + :operatingsystemrelease => '6', + :concat_basedir => '/dne', + } + end + it { should contain_class("apache::params") } + it { should contain_apache__mod('pagespeed') } + it { should contain_package("mod-pagespeed-stable") } + it { should contain_file('pagespeed.conf') } + end +end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 8e4115128..370de46c0 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -34,6 +34,10 @@ if fact('osfamily') == 'RedHat' on host, puppet('module','install','stahnma/epel'), { :acceptable_exit_codes => [0,1] } end + # Required for manifest to make mod_pagespeed repository available + if fact('osfamily') == 'Debian' + on host, puppet('module','install','puppetlabs-apt'), { :acceptable_exit_codes => [0,1] } + end on host, puppet('module','install','puppetlabs-stdlib'), { :acceptable_exit_codes => [0,1] } on host, puppet('module','install','puppetlabs-concat'), { :acceptable_exit_codes => [0,1] } end diff --git a/templates/mod/pagespeed.conf.erb b/templates/mod/pagespeed.conf.erb new file mode 100644 index 000000000..3bbf7f29b --- /dev/null +++ b/templates/mod/pagespeed.conf.erb @@ -0,0 +1,101 @@ +ModPagespeed on + +ModPagespeedInheritVHostConfig <%= @inherit_vhost_config %> +AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html +<% if @filter_xhtml -%> +AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER application/xhtml+xml +<% end -%> +ModPagespeedFileCachePath "<%= @cache_path %>" +ModPagespeedLogDir "<%= @log_dir %>" + +<% @memache_servers.each do |server| -%> +ModPagespeedMemcachedServers <%= server -%> +<% end -%> + +ModPagespeedRewriteLevel <%= @rewrite_level -%> + +<% @disable_filters.each do |filter| -%> +ModPagespeedDisableFilters <%= filter -%> +<% end -%> + +<% @enable_filters.each do |filter| -%> +ModPagespeedEnableFilters <%= filter -%> +<% end -%> + +<% @forbid_filters.each do |filter| -%> +ModPagespeedForbidFilters <%= filter -%> +<% end -%> + +ModPagespeedRewriteDeadlinePerFlushMs <%= @rewrite_deadline_per_flush_ms %> + +<% if @additional_domains -%> +ModPagespeedDomain <%= @additional_domains -%> +<% end -%> + +ModPagespeedFileCacheSizeKb <%= @file_cache_size_kb %> +ModPagespeedFileCacheCleanIntervalMs <%= @file_cache_clean_interval_ms %> +ModPagespeedLRUCacheKbPerProcess <%= @lru_cache_per_process %> +ModPagespeedLRUCacheByteLimit <%= @lru_cache_byte_limit %> +ModPagespeedCssFlattenMaxBytes <%= @css_flatten_max_bytes %> +ModPagespeedCssInlineMaxBytes <%= @css_inline_max_bytes %> +ModPagespeedCssImageInlineMaxBytes <%= @css_image_inline_max_bytes %> +ModPagespeedImageInlineMaxBytes <%= @image_inline_max_bytes %> +ModPagespeedJsInlineMaxBytes <%= @js_inline_max_bytes %> +ModPagespeedCssOutlineMinBytes <%= @css_outline_min_bytes %> +ModPagespeedJsOutlineMinBytes <%= @js_outline_min_bytes %> + + +ModPagespeedFileCacheInodeLimit <%= @inode_limit %> +ModPagespeedImageMaxRewritesAtOnce <%= @image_max_rewrites_at_once %> + +ModPagespeedNumRewriteThreads <%= @num_rewrite_threads %> +ModPagespeedNumExpensiveRewriteThreads <%= @num_expensive_rewrite_threads %> + +ModPagespeedStatistics <%= @collect_statistics %> + + + Order allow,deny + # You may insert other "Allow from" lines to add hosts you want to + # allow to look at generated statistics. Another possibility is + # to comment out the "Order" and "Allow" options from the config + # file, to allow any client that can reach your server to examine + # statistics. This might be appropriate in an experimental setup or + # if the Apache server is protected by a reverse proxy that will + # filter URLs in some fashion. + Allow from localhost + Allow from 127.0.0.1 + Allow from ::1 + <% @allow_view_stats.each do |host| -%> + Allow from <%= host %> + <% end -%> + SetHandler mod_pagespeed_statistics + + +ModPagespeedStatisticsLogging <%= @statistics_logging %> + + Order allow,deny + Allow from localhost + Allow from 127.0.0.1 + Allow from ::1 + <% @allow_pagespeed_console.each do |host| -%> + Allow from <%= host %> + <% end -%> + SetHandler pagespeed_console + + +ModPagespeedMessageBufferSize <%= @message_buffer_size %> + + + Order allow,deny + Allow from localhost + Allow from 127.0.0.1 + Allow from ::1 + <% @allow_pagespeed_message.each do |host| -%> + Allow from <%= host %> + <% end -%> + SetHandler mod_pagespeed_message + + +<% @additional_configuration.each_pair do |key, value| -%> +<%= key %> <%= value %> +<% end -%>