From beaf991d584ea893916a68ae8548f05e99db1453 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Mon, 18 Jul 2016 12:52:04 -0700 Subject: [PATCH 1/2] Added capability for domain aliases The purpose of these is _not_ to replace the canonical domain, but to allow the genesis site in question to serve requests for any number of _additional_ domains. * stored in ansible's group_vars * properly handled in htaccess rewrite conditions for spam reduction * added as ServerAlias in apache vhosts --- generator/app/index.js | 36 +++++++++++++++++++ .../provisioning/group_vars/webservers | 2 ++ generator/app/templates/web/htaccess | 9 ++++- .../wordpress/templates/vhosts/000-local | 4 +++ .../wordpress/templates/vhosts/001-staging | 4 +++ .../wordpress/templates/vhosts/002-branch | 4 +++ .../wordpress/templates/vhosts/003-production | 4 +++ 7 files changed, 62 insertions(+), 1 deletion(-) diff --git a/generator/app/index.js b/generator/app/index.js index f45f998..021b03b 100644 --- a/generator/app/index.js +++ b/generator/app/index.js @@ -84,6 +84,42 @@ WordpressGenerator.prototype.promptForDomain = function() { }); }; +WordpressGenerator.prototype.promptForAliases = function() { + var existing = function() { + try { + var groupVars = this.readFileAsString(path.join(this.env.cwd, 'provisioning', 'group_vars', 'webservers')); + + var matches = groupVars.match(/^aliases:(?:\s+-[ ]+\S+)+/m); + + if (matches) { + aliases = matches[0].split(/\s+-[ ]+/); + aliases.shift(); + + return aliases.join(' '); + } + } catch(e) {}; + }.bind(this); + + this.prompts.push({ + required: true, + type: 'text', + name: 'aliases', + message: 'Domain aliases, space delimited (e.g. mysite.net mysite.org)', + default: function() { + return existing() || ''; + }.bind(this), + validate: function(input) { + if (/^([\w-]+\.\w+(?:\.\w{2,3})?(?:[ ]+)?)+$/.test(input)) { + return true; + } else if (!input) { + return true; + } + + return chalk.yellow(input) + ' does not match the example'; + } + }); +}; + WordpressGenerator.prototype.promptForGenesis = function() { this.prompts.push({ type: 'text', diff --git a/generator/app/templates/provisioning/group_vars/webservers b/generator/app/templates/provisioning/group_vars/webservers index c47f2df..624e224 100644 --- a/generator/app/templates/provisioning/group_vars/webservers +++ b/generator/app/templates/provisioning/group_vars/webservers @@ -1,5 +1,7 @@ --- domain: <%= props.domain %> +aliases:<% if (props.aliases) { props.aliases.split(/\s+/).forEach(function (alias) { %> + - <%= alias %><% }); } else { %> []<% } %> mysql: name: <%= props.DB_NAME %> user: <%= props.DB_USER %> diff --git a/generator/app/templates/web/htaccess b/generator/app/templates/web/htaccess index d501765..b15850b 100644 --- a/generator/app/templates/web/htaccess +++ b/generator/app/templates/web/htaccess @@ -126,7 +126,14 @@ SetEnvIf Host ^local\. allow_wp_install # Rules to help reduce spam RewriteCond %{REQUEST_METHOD} POST RewriteCond %{REQUEST_URI} ^(.*)wp-comments-post\.php* - RewriteCond %{HTTP_REFERER} !^(.*)<%= props.domain %>.* + RewriteCond %{HTTP_REFERER} !^(.*)<% +if (props.aliases) { + %>(<%= props.domain %>|<% + %><%= props.aliases.split(/\s+/).join('|') %>)<% +} else { + %><%= props.domain %><% +} +%>.* RewriteCond %{HTTP_REFERER} !^http://jetpack\.wordpress\.com/jetpack-comment/ [OR] RewriteCond %{HTTP_USER_AGENT} ^$ RewriteRule ^(.*)$ - [F] diff --git a/provisioning/roles/wordpress/templates/vhosts/000-local b/provisioning/roles/wordpress/templates/vhosts/000-local index 8951e01..4d89d90 100644 --- a/provisioning/roles/wordpress/templates/vhosts/000-local +++ b/provisioning/roles/wordpress/templates/vhosts/000-local @@ -3,6 +3,10 @@ ServerName local.{{ domain }} +{% for alias in aliases %} + ServerAlias local.{{ alias }} +{% endfor %} + DocumentRoot /vagrant/web Options FollowSymLinks diff --git a/provisioning/roles/wordpress/templates/vhosts/001-staging b/provisioning/roles/wordpress/templates/vhosts/001-staging index 452f68e..6ded2dc 100644 --- a/provisioning/roles/wordpress/templates/vhosts/001-staging +++ b/provisioning/roles/wordpress/templates/vhosts/001-staging @@ -3,6 +3,10 @@ ServerName staging.{{ domain }} +{% for alias in aliases %} + ServerAlias staging.{{ alias }} +{% endfor %} + DocumentRoot /var/www/{{ domain }}/master/current/web Options FollowSymLinks diff --git a/provisioning/roles/wordpress/templates/vhosts/002-branch b/provisioning/roles/wordpress/templates/vhosts/002-branch index f7b3874..e6ae362 100644 --- a/provisioning/roles/wordpress/templates/vhosts/002-branch +++ b/provisioning/roles/wordpress/templates/vhosts/002-branch @@ -4,6 +4,10 @@ ServerName branch.staging.{{ domain }} ServerAlias *.staging.{{ domain }} +{% for alias in aliases %} + ServerAlias branch.staging.{{ alias }} *.staging.{{ alias }} +{% endfor %} + VirtualDocumentRoot /var/www/{{ domain }}/%1/current/web Options FollowSymLinks diff --git a/provisioning/roles/wordpress/templates/vhosts/003-production b/provisioning/roles/wordpress/templates/vhosts/003-production index f84a6d9..7be98f2 100644 --- a/provisioning/roles/wordpress/templates/vhosts/003-production +++ b/provisioning/roles/wordpress/templates/vhosts/003-production @@ -7,6 +7,10 @@ ServerAlias www.{{ domain }} ServerAlias *.{{ domain }} +{% for alias in aliases %} + ServerAlias {{ alias }} production.{{ alias }} www.{{ alias }} *.{{ alias }} +{% endfor %} + DocumentRoot /var/www/{{ domain }}/master/current/web Options FollowSymLinks From a92e31db481af76c4b9377084c212d1361ffe1e2 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Mon, 18 Jul 2016 14:42:40 -0700 Subject: [PATCH 2/2] Fixed detection of existing domain + aliases; added aliases to vagrant hostmanager --- generator/app/index.js | 18 ++++++++++++++++-- generator/app/templates/Vagrantfile | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/generator/app/index.js b/generator/app/index.js index 021b03b..75155a8 100644 --- a/generator/app/index.js +++ b/generator/app/index.js @@ -66,12 +66,26 @@ WordpressGenerator.prototype.promptForName = function() { }; WordpressGenerator.prototype.promptForDomain = function() { + var existing = function() { + try { + var groupVars = this.readFileAsString(path.join(this.env.cwd, 'provisioning', 'group_vars', 'webservers')); + + var matches = groupVars.match(/^domain:[\t ]*(\S+)\s/m); + + if (matches) { + return matches[1]; + } + } catch(e) {}; + }.bind(this); + this.prompts.push({ required: true, type: 'text', name: 'domain', message: 'Domain name (e.g. mysite.com)', - default: path.basename(this.env.cwd).toLowerCase(), + default: function() { + return existing() || path.basename(this.env.cwd).toLowerCase(); + }.bind(this), validate: function(input) { if (/^[\w-]+\.\w+(?:\.\w{2,3})?$/.test(input)) { return true; @@ -92,7 +106,7 @@ WordpressGenerator.prototype.promptForAliases = function() { var matches = groupVars.match(/^aliases:(?:\s+-[ ]+\S+)+/m); if (matches) { - aliases = matches[0].split(/\s+-[ ]+/); + var aliases = matches[0].split(/\s+-[ ]+/); aliases.shift(); return aliases.join(' '); diff --git a/generator/app/templates/Vagrantfile b/generator/app/templates/Vagrantfile index 7b65242..0f60f92 100644 --- a/generator/app/templates/Vagrantfile +++ b/generator/app/templates/Vagrantfile @@ -18,7 +18,8 @@ Vagrant.configure("2") do |config| config.vm.define :local do |box| box.vm.hostname = "local.<%= props.domain %>" - +<% if (props.aliases) { %> box.hostmanager.aliases = %w(<%= props.aliases.split(/\s+/).map(function (alias) { return 'local.' + alias; }).join(' ') %>) +<% } %> # Static IP for testing. box.vm.network :private_network, ip: "<%= props.ip %>" box.vm.network :forwarded_port, guest: 22, host: <%= props.ip.split('.').join('').split('0').join('').slice(-4) %>, auto_correct: true