From b6239479b6c3f7b54d6e27e9ab9cd846582b9620 Mon Sep 17 00:00:00 2001 From: Phil Hendren Date: Sat, 29 Oct 2016 17:05:14 -0400 Subject: [PATCH] rubocop and foodcritic - dir creation fix --- CHANGELOG.md | 12 ++-- README.md | 51 +++++++-------- attributes/default.rb | 34 +++++----- metadata.rb | 6 +- recipes/default.rb | 65 ++++++++++--------- templates/default/httpd.conf.erb | 108 +++++++++++++++---------------- 6 files changed, 142 insertions(+), 134 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 306b279..eb5987d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,19 +1,23 @@ amzapache CHANGELOG ==================== -This file is used to list changes made in each version of the amzapache cookbook. +This file lists changes made in each version of the amzapache cookbook. 0.1.0 ----- -- [phendren] - 12.04.2014 - Initial release of amzapache +12.04.2014 - Initial release of amzapache 0.1.1 ----- -- [phendren] - 12.08.2014 - Released with basic fixes +12.08.2014 - Released with basic fixes 0.1.2 ----- -- [phendren] - 02.13.2016 - small fixes to format and adding test kitchen +02.13.2016 - small fixes to format and adding test kitchen + +0.1.3 +----- +10.29.2016 - rubocop/foodcritic and small refactor - - - diff --git a/README.md b/README.md index f00c4d1..fe84d81 100644 --- a/README.md +++ b/README.md @@ -1,55 +1,54 @@ amzapache Cookbook =================== -This is a relatively straightforward Chef cookbook for installing Apache Web server w/ mod ssl on Amazon Linux -I have not tested this cookbook on CentOS or RHE but it should (in theory) work the same. +amzapache is a Chef cookbook for installing Apache Web server w/ mod ssl on Amazon Linux. +I have not tested this cookbook on CentOS or RHEL. Requirements ------------ +This cookbook was written for use with AWS OpsWorks (Chef 11) for building a custom +web server layer running Amazon Linux. -I wrote it for use with AWS OpsWorks for building a custom layer using Amazon Linux. -Tested with Chef 11.4 with AWS OpsWorks - http://aws.amazon.com/opsworks/ +http://aws.amazon.com/opsworks/ Attributes ---------- amzapache attributes: Are all related to the included httpd.conf configuration template and are configured as default attributes -`default[:amzapache][:document_root]` - Document root - default: /var/www/html
-`default[:amzapache][:dir]` - Apache config directory - default: /etc/httpd
-`default[:amzapache][:log_dir]` - Apache log directory - default: /var/log/httpd
-`default[:amzapache][:user]` - User Apache will run as - default: apache
-`default[:amzapache][:group]` - Group Apache will run as - default: apache
-`default[:amzapache][:binary]` - Where binary will live - default: /usr/sbin/httpd
-`default[:amzapache][:icondir]` - Apache icon directory - default: /var/www/icons/
-`default[:amzapache][:init_script]` - Apache init script - default: /etc/init.d/httpd
-`default[:amzapache][:http_port]` - Port apache will listen on - default: 80
-`default[:amzapache][:ssl_port]` - Port SSL will use - default: 443
-`default[:amzapache][:contact]` - you@yourdomain.com
-`default[:amzapache][:servhost]` - Domain you want for your default hostname
-`default[:amzapache][:timeout]` - Apache timeout - default: 120
-`default[:amzapache][:keepalive]` - Apache keepalive - default: off
-`default[:amzapache][:keepaliverequests]` - Apache keep alive requests - default: 100
-`default[:amzapache][:keepalivetimeout]` - Apache keepalive timeout - default: 3
+`default['amzapache']['document_root']` - Document root - default: /var/www/html
+`default['amzapache']['dir']` - Apache config directory - default: /etc/httpd
+`default['amzapache']['log_dir']` - Apache log directory - default: /var/log/httpd
+`default['amzapache']['user']` - User Apache will run as - default: apache
+`default['amzapache']['group']` - Group Apache will run as - default: apache
+`default['amzapache']['binary']` - Where binary will live - default: /usr/sbin/httpd
+`default['amzapache']['icon_dir']` - Apache icon directory - default: /var/www/icons/
+`default['amzapache']['init_script']` - Apache init script - default: /etc/init.d/httpd
+`default['amzapache']['http_port']` - Port apache will listen on - default: 80
+`default['amzapache']['ssl_port']` - Port SSL will use - default: 443
+`default['amzapache']['contact']` - you@yourdomain.com
+`default['amzapache']['servhost']` - Domain you want for your default hostname
+`default['amzapache']['timeout']` - Apache timeout - default: 120
+`default['amzapache']['keepalive']` - Apache keepalive - default: off
+`default['amzapache']['keepaliverequests']` - Apache keep alive requests - default: 100
+`default['amzapache']['keepalivetimeout']` - Apache keepalive timeout - default: 3
Usage ----- #### amzapache::default Installs Apache using the settings as configured in the attributes. This cookbooks is setup to function in combo -with php-pkg (installs PHP) and deployer (which can be used to easily deploy applications from github). +with php-pkg (installs PHP) and deployer (which can easily deploy PHP applications from a repo on github). -To use with OpsWorks - Create a custom layer - add amzapache::default as a custom cookbook to the "setup" event. +To use with OpsWorks (Chef 11) - Create a custom layer - add amzapache::default as a custom cookbook to the "setup" event. Contributing ------------ - 1. Fork the repository on Github 2. Create a named feature branch (like `add_component_x`) 3. Write your change 4. Write tests for your change (if applicable) -5. Run the tests, ensuring they all pass +5. Run the tests via test kitchen, ensuring they all pass 6. Submit a Pull Request using Github License and Authors ------------------- Apache 2.0 License - written by Phil Hendren - github: phendren -gocodeyourself[at]gmail.com - +philip.hendren[at]gmail.com diff --git a/attributes/default.rb b/attributes/default.rb index bb199f0..ad6695e 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -1,19 +1,19 @@ # amzapache attributes -default[:amzapache][:document_root] = '/var/www/html' -default[:amzapache][:dir] = '/etc/httpd' -default[:amzapache][:log_dir] = '/var/log/httpd' -default[:amzapache][:user] = 'apache' -default[:amzapache][:group] = 'apache' -default[:amzapache][:binary] = '/usr/sbin/httpd' -default[:amzapache][:icondir] = '/var/www/icons/' -default[:amzapache][:init_script] = '/etc/init.d/httpd' -default[:amzapache][:http_port] = 80 -default[:amzapache][:ssl_port] = 443 -default[:amzapache][:contact] = 'you@yourdomain.com' -default[:amzapache][:servhost] = 'yourdomain.com' -default[:amzapache][:timeout] = 120 -default[:amzapache][:keepalive] = 'Off' -default[:amzapache][:keepaliverequests] = 100 -default[:amzapache][:keepalivetimeout] = 3 - +default['amzapache']['document_root'] = '/var/www/html' +default['amzapache']['dir'] = '/etc/httpd' +default['amzapache']['log_dir'] = '/var/log/httpd' +default['amzapache']['user'] = 'apache' +default['amzapache']['group'] = 'apache' +default['amzapache']['binary'] = '/usr/sbin/httpd' +default['amzapache']['icon_dir'] = '/var/www/icons' +default['amzapache']['ssl_dir'] = '/etc/httpd/ssl' +default['amzapache']['init_script'] = '/etc/init.d/httpd' +default['amzapache']['http_port'] = 80 +default['amzapache']['ssl_port'] = 443 +default['amzapache']['contact'] = 'you@yourdomain.com' +default['amzapache']['servhost'] = 'yourdomain.com' +default['amzapache']['timeout'] = 120 +default['amzapache']['keepalive'] = false +default['amzapache']['keepaliverequests'] = 100 +default['amzapache']['keepalivetimeout'] = 3 diff --git a/metadata.rb b/metadata.rb index 797c368..9210c9b 100644 --- a/metadata.rb +++ b/metadata.rb @@ -2,6 +2,8 @@ maintainer 'phendren' maintainer_email 'gocodeyourself@gmail.com' license 'Apache License 2.0' -description 'Installs/Configures default apache httpd package on Amazon Linux' +description 'Installs default apache httpd package on Amazon Linux' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '0.1.2' +version '0.1.3' + +supports 'amazon' diff --git a/recipes/default.rb b/recipes/default.rb index dd450c6..8e1f210 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -6,43 +6,52 @@ # package 'httpd' do - package_name 'httpd' action :install end package 'mod_ssl' do - package_name 'mod_ssl' action :install end -cookbook_file "/etc/httpd/conf.d/ssl.conf" do - source "ssl.conf.txt" - mode '0644' - owner 'root' - group 'root' -end - service 'httpd' do + stop_command '/etc/init.d/httpd stop' + start_command '/etc/init.d/httpd start' + restart_command '/etc/init.d/httpd restart' action :enable end -directory "/etc/httpd/ssl" do +# Make sure directories exist +%w( + node['amzapache']['document_root'] + node['amzapache']['icon_dir'] + node['amzapache']['ssl_dir'] +).each do |dir| + directory dir do + owner 'root' + group 'root' + mode 0755 + action :create + end +end + +cookbook_file '/etc/httpd/conf.d/ssl.conf' do + source 'ssl.conf.txt' + mode '0644' owner 'root' group 'root' - mode '0755' - action :create end -# This is some simple bash-fu for creating a module listing -bash "module-listing" do - cwd "/etc/httpd" - user "root" +# Simple bash-fu for creating a module listing +bash 'module-listing' do + cwd '/etc/httpd' + user 'root' code <<-EOH - build="# dynamic module listing\n" + build='# dynamic module listing\n' + build+='# ordered by filename length' echo $build > /tmp/modules.txt.erb cd /etc/httpd/modules; - for list in $(ls); do - base=$(basename $list .so + for list in $(ls |awk '{print length($0)"\t"$0}' |sort -n |cut --complement -f1); do + base=$(basename $list .so) name=$(echo $base | sed -r 's/^.{4}//') part="_module" echo "LoadModule $name$part modules/$list" >> /tmp/modules.txt.erb @@ -51,23 +60,17 @@ end template 'httpd.conf' do - path "/etc/httpd/conf/httpd.conf" + path '/etc/httpd/conf/httpd.conf' source 'httpd.conf.erb' owner 'root' group 'root' - mode '0644' + mode 0644 end -cookbook_file "/var/www/html/index.html" do - source "index.html.txt" - mode "0644" +cookbook_file '/var/www/html/index.html' do + source 'index.html.txt' + mode 0644 owner 'apache' group 'apache' -end - -service "httpd" do - stop_command "/etc/init.d/httpd stop" - start_command "/etc/init.d/httpd start" - restart_command "/etc/init.d/httpd restart" - action :restart + notifies :restart, 'service[httpd]' end diff --git a/templates/default/httpd.conf.erb b/templates/default/httpd.conf.erb index 1c2507d..369b200 100644 --- a/templates/default/httpd.conf.erb +++ b/templates/default/httpd.conf.erb @@ -1,7 +1,7 @@ # This Apache configuration is managed by the amzapache cookbook -# -# Section #1 +# +# Section #1 # # Don't give away too much information about all the subcomponents # we are running. Comment out this line if you don't mind remote sites @@ -19,7 +19,7 @@ ServerTokens OS # # Do NOT add a slash at the end of the directory path. # -ServerRoot "<%= node[:amzapache][:dir] %>" +ServerRoot "<%= node['amzapache']['dir'] %>" # # PidFile: The file in which the server should record its process @@ -32,30 +32,30 @@ PidFile run/httpd.pid # # Timeout: The number of seconds before receives and sends time out. # -Timeout "<%= node[:amzapache][:timeout] %>" +Timeout "<%= node['amzapache']['timeout'] %>" # # KeepAlive: Whether or not to allow persistent connections (more than # one request per connection). Set to "Off" to deactivate. # -KeepAlive "<%= node[:amzapache][:keepalive] %>" +KeepAlive "<%= node['amzapache']['keepalive'] ? 'On' : 'Off'%>" # # MaxKeepAliveRequests: The maximum number of requests to allow # during a persistent connection. Set to 0 to allow an unlimited amount. # We recommend you leave this number high, for maximum performance. # -MaxKeepAliveRequests "<%= node[:amzapache][:keepaliverequests] %>" +MaxKeepAliveRequests "<%= node['amzapache']['keepaliverequests'] %>" # # KeepAliveTimeout: Number of seconds to wait for the next request from the # same client on the same connection. # -KeepAliveTimeout "<%= node[:amzapache][:keepalivetimeout] %>" +KeepAliveTimeout "<%= node['amzapache']['keepalivetimeout'] %>" ## ## Server-Pool Size Regulation (MPM specific) -## +## # prefork MPM # StartServers: number of server processes to start @@ -84,7 +84,7 @@ MaxRequestsPerChild 4000 StartServers 4 MaxClients 300 MinSpareThreads 25 -MaxSpareThreads 75 +MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 @@ -94,16 +94,16 @@ MaxRequestsPerChild 0 # ports, in addition to the default. See also the # directive. # -# Change this to Listen on specific IP addresses as shown below to +# Change this to Listen on specific IP addresses as shown below to # prevent Apache from glomming onto all bound IP addresses (0.0.0.0) # #Listen 12.34.56.78:80 -Listen <%= node[:amzapache][:http_port] %> -NameVirtualHost *:<%= node[:amzapache][:http_port] %> +Listen <%= node['amzapache']['http_port'] %> +NameVirtualHost *:<%= node['amzapache']['http_port'] %> -Listen <%= node[:amzapache][:ssl_port] %> -NameVirtualHost *:<%= node[:amzapache][:ssl_port] %> +Listen <%= node['amzapache']['ssl_port'] %> +NameVirtualHost *:<%= node['amzapache']['ssl_port'] %> @@ -149,18 +149,18 @@ Include conf.d/*.conf # # If you wish httpd to run as a different user or group, you must run -# httpd as root initially and it will switch. +# httpd as root initially and it will switch. # # User/Group: The name (or #number) of the user/group to run httpd as. # . On SCO (ODT 3) use "User nouser" and "Group nogroup". # . On HPUX you may not be able to use shared memory as nobody, and the # suggested workaround is to create a user www and use that user. # NOTE that some kernels refuse to setgid(Group) or semctl(IPC_SET) -# when the value of (unsigned)Group is above 60000; +# when the value of (unsigned)Group is above 60000; # don't use Group #-1 on these systems! # -User <%= node[:amzapache][:user] %> -Group <%= node[:amzapache][:group] %> +User <%= node['amzapache']['user'] %> +Group <%= node['amzapache']['group'] %> ### Section 2: 'Main' server configuration # @@ -179,7 +179,7 @@ Group <%= node[:amzapache][:group] %> # e-mailed. This address appears on some server-generated pages, such # as error documents. e.g. admin@your-domain.com # -ServerAdmin <%= node[:amzapache][:contact] %> +ServerAdmin <%= node['amzapache']['contact'] %> # # ServerName gives the name and port that the server uses to identify itself. @@ -190,13 +190,13 @@ ServerAdmin <%= node[:amzapache][:contact] %> # redirections will not work. See also the UseCanonicalName directive. # # If your host doesn't have a registered DNS name, enter its IP address here. -# You will have to access it by its address anyway, and this will make +# You will have to access it by its address anyway, and this will make # redirections work in a sensible way. # -ServerName 127.0.0.1 +ServerName 127.0.0.1 # -# UseCanonicalName: Determines how Apache constructs self-referencing +# UseCanonicalName: Determines how Apache constructs self-referencing # URLs and the SERVER_NAME and SERVER_PORT variables. # When set "Off", Apache will use the Hostname and Port supplied # by the client. When set "On", Apache will use the value of the @@ -209,15 +209,15 @@ UseCanonicalName On # documents. By default, all requests are taken from this directory, but # symbolic links and aliases may be used to point to other locations. # -DocumentRoot "<%= node[:amzapache][:document_root] %>" +DocumentRoot "<%= node['amzapache']['document_root'] %>" # # Each directory to which Apache has access can be configured with respect # to which services and features are allowed and/or disabled in that -# directory (and its subdirectories). +# directory (and its subdirectories). # -# First, we configure the "default" to be a very restrictive set of -# features. +# First, we configure the "default" to be a very restrictive set of +# features. # Options FollowSymLinks @@ -234,7 +234,7 @@ DocumentRoot "<%= node[:amzapache][:document_root] %>" # # This should be changed to whatever you set DocumentRoot to. # -"> +"> # # Possible values for the Options directive are "None", "All", @@ -289,7 +289,7 @@ DocumentRoot "<%= node[:amzapache][:document_root] %>" # To enable requests to /~user/ to serve the user's public_html # directory, remove the "UserDir disabled" line above, and uncomment # the following line instead: - # + # #UserDir public_html @@ -316,7 +316,7 @@ DocumentRoot "<%= node[:amzapache][:document_root] %>" # is requested. # # The index.html.var file (a type-map) is used to deliver content- -# negotiated documents. The MultiViews Option can be used for the +# negotiated documents. The MultiViews Option can be used for the # same purpose, but it is much slower. # DirectoryIndex index.html index.html.var @@ -329,8 +329,8 @@ DirectoryIndex index.html index.html.var AccessFileName .htaccess # -# The following lines prevent .htaccess and .htpasswd files from being -# viewed by Web clients. +# The following lines prevent .htaccess and .htpasswd files from being +# viewed by Web clients. # Order allow,deny @@ -378,7 +378,7 @@ HostnameLookups Off # # EnableMMAP: Control whether memory-mapping is used to deliver # files (assuming that the underlying OS supports it). -# The default is on; turn this off if you serve from NFS-mounted +# The default is on; turn this off if you serve from NFS-mounted # filesystems. On some systems, turning it off (regardless of # filesystem) can improve performance; for details, please see # http://httpd.apache.org/docs/2.2/mod/core.html#enablemmap @@ -386,9 +386,9 @@ HostnameLookups Off #EnableMMAP off # -# EnableSendfile: Control whether the sendfile kernel support is -# used to deliver files (assuming that the OS supports it). -# The default is on; turn this off if you serve from NFS-mounted +# EnableSendfile: Control whether the sendfile kernel support is +# used to deliver files (assuming that the OS supports it). +# The default is on; turn this off if you serve from NFS-mounted # filesystems. Please see # http://httpd.apache.org/docs/2.2/mod/core.html#enablesendfile # @@ -456,21 +456,21 @@ CustomLog logs/access_log combined ServerSignature On # -# Aliases: Add here as many aliases as you need (with no limit). The format is +# Aliases: Add here as many aliases as you need (with no limit). The format is # Alias fakename realname # # Note that if you include a trailing / on fakename then the server will # require it to be present in the URL. So "/icons" isn't aliased in this -# example, only "/icons/". If the fakename is slash-terminated, then the -# realname must also be slash terminated, and if the fakename omits the +# example, only "/icons/". If the fakename is slash-terminated, then the +# realname must also be slash terminated, and if the fakename omits the # trailing slash, the realname must also omit it. # # We include the /icons/ alias for FancyIndexed directory listings. If you # do not use FancyIndexing, you may comment this out. # -Alias /icons/ "/var/www/icons/" +Alias /icons/ "<%= node['amzapache']['icon_dir'] %>" - +"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order allow,deny @@ -479,7 +479,7 @@ Alias /icons/ "/var/www/icons/" # # WebDAV module configuration section. -# +# # Location of the WebDAV lock database. DAVLockDB /var/lib/dav/lockdb @@ -578,7 +578,7 @@ DefaultIcon /icons/unknown.gif # default, and append to directory listings. # # HeaderName is the name of a file which should be prepended to -# directory indexes. +# directory indexes. ReadmeName README.html HeaderName HEADER.html @@ -589,16 +589,16 @@ HeaderName HEADER.html IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t # -# DefaultLanguage and AddLanguage allows you to specify the language of -# a document. You can then use content negotiation to give a browser a +# DefaultLanguage and AddLanguage allows you to specify the language of +# a document. You can then use content negotiation to give a browser a # file in a language the user can understand. # # Specify a default language. This means that all data -# going out without a specific language tag (see below) will +# going out without a specific language tag (see below) will # be marked with this one. You probably do NOT want to set # this unless you are sure it is correct for all cases. # -# * It is generally better to not mark a page as +# * It is generally better to not mark a page as # * being a certain language than marking it with the wrong # * language! # @@ -609,8 +609,8 @@ IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t # language code is pl) may wish to use "AddLanguage pl .po" to # avoid the ambiguity with the common suffix for perl scripts. # -# Note 2: The example entries below illustrate that in some cases -# the two character 'Language' abbreviation is not identical to +# Note 2: The example entries below illustrate that in some cases +# the two character 'Language' abbreviation is not identical to # the two character 'Country' code for its country, # E.g. 'Danmark/dk' versus 'Danish/da'. # @@ -671,7 +671,7 @@ ForceLanguagePriority Prefer Fallback # # Specify a default charset for all content served; this enables -# interpretation of all content as UTF-8 by default. To use the +# interpretation of all content as UTF-8 by default. To use the # default browser choice (ISO-8859-1), or to allow the META tags # in HTML content to override this choice, comment out this # directive: @@ -759,7 +759,7 @@ AddOutputFilter INCLUDES .shtml # Putting this all together, we can internationalize error responses. # # We use Alias to redirect any /error/HTTP_.html.var response to -# our collection of by-error message multi-language collections. We use +# our collection of by-error message multi-language collections. We use # includes to substitute the appropriate text. # # You can modify the messages' appearance without changing any of the @@ -820,8 +820,8 @@ BrowserMatch "JDK/1\.0" force-response-1.0 # # The following directive disables redirects on non-GET requests for -# a directory that does not include the trailing slash. This fixes a -# problem with Microsoft WebFolders which does not appropriately handle +# a directory that does not include the trailing slash. This fixes a +# problem with Microsoft WebFolders which does not appropriately handle # redirects for folders with DAV methods. # Same deal with Apple's DAV filesystem and Gnome VFS support for DAV. # @@ -897,7 +897,7 @@ BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # -# Please see the documentation at +# Please see the documentation at # # for further details before you try to setup virtual hosts. # @@ -908,7 +908,7 @@ BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully # Use name-based virtual hosting. # # -# NOTE: NameVirtualHost cannot be used without a port specifier +# NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. #