From 05b525651c6e9ee1db6dbe381ff002094d2bda81 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Wed, 22 Apr 2015 16:07:51 -0500 Subject: [PATCH 01/13] Changing WP_HOME and WP_SITEURL to absolute urls, dynamically set from SERVER[HTTP_HOST] --- lib/yeoman/templates/web/wp-config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/yeoman/templates/web/wp-config.php b/lib/yeoman/templates/web/wp-config.php index 802d9521..5f78bd17 100644 --- a/lib/yeoman/templates/web/wp-config.php +++ b/lib/yeoman/templates/web/wp-config.php @@ -37,9 +37,9 @@ "define('WP_CONTENT_DIR', dirname(__FILE__) . CONTENT_DIR);", "define('WP_CONTENT_URL', CONTENT_DIR);", "define('WP_ENV', Evolution::getEnv());", - "define('WP_HOME', '/');", + "define('WP_HOME', 'http://'.$_SERVER['HTTP_HOST'].'/');", "define('WP_POST_REVISIONS', 5);", - "define('WP_SITEURL', '/wp');", + "define('WP_SITEURL', 'http://'.$_SERVER['HTTP_HOST'].'/wp');", "/* That's all,", ].join('\n') ) From 247a7610dbc299694419889e53e743501ffe8804 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Wed, 22 Apr 2015 16:08:41 -0500 Subject: [PATCH 02/13] Removing absolute urls from sql dump on db:{up,down} --- lib/capistrano/tasks/db.rake | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index 77b45e72..80684947 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,5 +1,9 @@ namespace :evolve do namespace :db do + task :prepare do |task, args| + set :perl_cmd, '-pi -e \'s!https?://(?:[^/]+\.)?' + Regexp.escape(fetch(:domain)) + '/!/!ig\'' + end + desc "Create backup of remote DB" task :backup, :skip_download do |task, args| set :db_backup_file, DateTime.now.strftime("#{fetch(:wp_config)['name']}.%Y-%m-%d.%H%M%S.sql") @@ -39,9 +43,11 @@ namespace :evolve do task :down do |task| begin invoke "evolve:db:backup" + invoke "evolve:db:prepare" run_locally do execute :gzip, "-d", fetch(:db_gzip_file) + execute :perl, fetch(:perl_cmd), fetch(:db_backup_file) execute :vagrant, :up execute :vagrant, :ssh, :local, "-c 'cd /vagrant && mysql -uroot -D \"#{fetch(:wp_config)['name']}_local\" < #{fetch(:db_backup_file)}'" execute :rm, fetch(:db_backup_file) @@ -57,6 +63,7 @@ namespace :evolve do begin invoke "evolve:confirm", "You are about to destroy & override the \"#{fetch(:stage)}\" database!" invoke "evolve:db:backup", true + invoke "evolve:db:prepare" run_locally do execute :vagrant, :up @@ -67,6 +74,7 @@ namespace :evolve do on release_roles(:db) do upload! fetch(:db_gzip_file), "/tmp/#{fetch(:db_gzip_file)}" execute :gzip, "-d", "/tmp/#{fetch(:db_gzip_file)}" + execute :perl, fetch(:perl_cmd), "/tmp/#{fetch(:db_backup_file)}" within fetch(:wp_path) do execute :wp, :db, :import, "/tmp/#{fetch(:db_backup_file)}", "--path=\"#{fetch(:wp_path)}\"", "--url=\"http://#{fetch(:stage)}.#{fetch(:domain)}/\"" From ac8b369c6afebc3e996294a0043fe88f6d038b62 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Wed, 22 Apr 2015 16:16:00 -0500 Subject: [PATCH 03/13] Removed useless arg --- lib/capistrano/tasks/db.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index 80684947..c64375ca 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,6 +1,6 @@ namespace :evolve do namespace :db do - task :prepare do |task, args| + task :prepare do |task| set :perl_cmd, '-pi -e \'s!https?://(?:[^/]+\.)?' + Regexp.escape(fetch(:domain)) + '/!/!ig\'' end From c20c0597acd8cadeb9e66cf980caa3a148f3b38d Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Sun, 26 Apr 2015 00:28:06 -0500 Subject: [PATCH 04/13] Absolute url regex is more specific in what it matches --- lib/capistrano/tasks/db.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index c64375ca..9c8b8f98 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,7 +1,7 @@ namespace :evolve do namespace :db do task :prepare do |task| - set :perl_cmd, '-pi -e \'s!https?://(?:[^/]+\.)?' + Regexp.escape(fetch(:domain)) + '/!/!ig\'' + set :perl_cmd, '-pi -e \'s!https?://(([^/]+\.)?(local|staging|production|www)\.)?' + Regexp.escape(fetch(:domain)) + '/!/!ig\'' end desc "Create backup of remote DB" From 254a0cf9b7ae035e5a6d81d48135832207cc79ea Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Mon, 27 Apr 2015 16:28:36 -0500 Subject: [PATCH 05/13] Regex filtering is now _replacing_ stage subdomains in absolute urls, instead of making them entirely relative --- lib/capistrano/tasks/db.rake | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index 9c8b8f98..e56cef65 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,7 +1,11 @@ namespace :evolve do namespace :db do - task :prepare do |task| - set :perl_cmd, '-pi -e \'s!https?://(([^/]+\.)?(local|staging|production|www)\.)?' + Regexp.escape(fetch(:domain)) + '/!/!ig\'' + task :prepare, :target_stage do |task, args| + set :perl_cmd, '-pi -e \'s!(https?://)(?:(?:[^/]+\.)?(?:local|staging|production|www)\.)?(' + + Regexp.escape(fetch(:domain)) + + '/)!$1' + + Regexp.escape(args[:target_stage]) + + '\.$2!ig\'' end desc "Create backup of remote DB" @@ -43,7 +47,7 @@ namespace :evolve do task :down do |task| begin invoke "evolve:db:backup" - invoke "evolve:db:prepare" + invoke "evolve:db:prepare", "local" run_locally do execute :gzip, "-d", fetch(:db_gzip_file) @@ -63,7 +67,7 @@ namespace :evolve do begin invoke "evolve:confirm", "You are about to destroy & override the \"#{fetch(:stage)}\" database!" invoke "evolve:db:backup", true - invoke "evolve:db:prepare" + invoke "evolve:db:prepare", fetch(:stage) run_locally do execute :vagrant, :up From 7ac2fe56f99010c16773838135c242faf0c3866d Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Tue, 28 Apr 2015 10:30:24 -0500 Subject: [PATCH 06/13] Normalizing absolute urls based on current stage * Added canonical ServerName for apache (as bare domain, eg "example.com") * Moved Evolution class into scaffolded project with a DOMAIN const, added `getHostname` method * Wp config now setting WP_HOME and WP_SITEURL via `Evolution::getHostname` * DB rake task is translating urls for production to bare domain "example.com", all others as "$STAGE.example.com" --- lib/ansible/roles/apache/tasks/main.yml | 9 ++++ lib/ansible/roles/apache/templates/canonical | 2 + lib/capistrano/tasks/db.rake | 7 +++- lib/wordpress/Evolution.php | 30 ------------- lib/yeoman/templates/web/Evolution.php | 44 ++++++++++++++++++++ lib/yeoman/templates/web/wp-config.php | 6 +-- 6 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 lib/ansible/roles/apache/templates/canonical delete mode 100644 lib/wordpress/Evolution.php create mode 100644 lib/yeoman/templates/web/Evolution.php diff --git a/lib/ansible/roles/apache/tasks/main.yml b/lib/ansible/roles/apache/tasks/main.yml index a9fad607..c87e5705 100644 --- a/lib/ansible/roles/apache/tasks/main.yml +++ b/lib/ansible/roles/apache/tasks/main.yml @@ -16,6 +16,15 @@ - debug: msg={{ apache_version.stdout }} +- name: Configure Apache canonical server name + template: src=canonical dest=/etc/apache2/conf-available/canonical-servername.conf mode=0644 + sudo: true + +- name: Enable canonical config + command: a2enconf canonical-servername + notify: restart apache + sudo: yes + - name: Ensure Apache `NameVirtualHost` is 80 (2.2) lineinfile: regexp='^NameVirtualHost' line='NameVirtualHost *:80' dest=/etc/apache2/ports.conf backup=yes notify: restart apache diff --git a/lib/ansible/roles/apache/templates/canonical b/lib/ansible/roles/apache/templates/canonical new file mode 100644 index 00000000..04485cb4 --- /dev/null +++ b/lib/ansible/roles/apache/templates/canonical @@ -0,0 +1,2 @@ +UseCanonicalName On +ServerName {{ domain }} diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index e56cef65..d1ffa165 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,11 +1,14 @@ namespace :evolve do namespace :db do task :prepare, :target_stage do |task, args| + stage_subdomain = args[:target_stage].to_s + stage_subdomain = stage_subdomain == 'production' ? '' : stage_subdomain + '.' + set :perl_cmd, '-pi -e \'s!(https?://)(?:(?:[^/]+\.)?(?:local|staging|production|www)\.)?(' + Regexp.escape(fetch(:domain)) + '/)!$1' + - Regexp.escape(args[:target_stage]) + - '\.$2!ig\'' + Regexp.escape(stage_subdomain) + + '$2!ig\'' end desc "Create backup of remote DB" diff --git a/lib/wordpress/Evolution.php b/lib/wordpress/Evolution.php deleted file mode 100644 index 984bcbf3..00000000 --- a/lib/wordpress/Evolution.php +++ /dev/null @@ -1,30 +0,0 @@ - 'staging') - if (!defined('WP_ENV')) { - define('WP_ENV', Evolution::getEnv()); - } - - return WP_ENV; - } - -} diff --git a/lib/yeoman/templates/web/Evolution.php b/lib/yeoman/templates/web/Evolution.php new file mode 100644 index 00000000..b55876e2 --- /dev/null +++ b/lib/yeoman/templates/web/Evolution.php @@ -0,0 +1,44 @@ +'; + + public static function getDbName($name) + { + return sprintf('%s_%s', $name, static::getEnv()); + } + + public static function getHostname() + { + $env = self::getEnv(); + + if (in_array($env, array('local', 'staging'))) { + return $env.'.'.self::DOMAIN; + } + else { + return self::DOMAIN; + } + } + + public static function getEnv() + { + preg_match('/(?:(local|staging|production)\.)?'.preg_quote(self::DOMAIN).'$/', $_SERVER['SERVER_NAME'], $matches); + + $match = count($matches) > 1 ? array_pop($matches) : 'production'; + $known = array('local', 'staging', 'production'); + + return in_array($match, $known) ? $match : 'production'; + } + + public static function initEnv() + { + // Set environment to the last sub-domain (e.g. foo.staging.site.com => 'staging') + if (!defined('WP_ENV')) { + define('WP_ENV', Evolution::getEnv()); + } + + return WP_ENV; + } + +} diff --git a/lib/yeoman/templates/web/wp-config.php b/lib/yeoman/templates/web/wp-config.php index 5f78bd17..e26bcda3 100644 --- a/lib/yeoman/templates/web/wp-config.php +++ b/lib/yeoman/templates/web/wp-config.php @@ -1,7 +1,7 @@ Date: Tue, 28 Apr 2015 10:50:15 -0500 Subject: [PATCH 07/13] Turn off canonical name for wildcard branch vhost --- lib/ansible/roles/apache/templates/vhosts/002-branch | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ansible/roles/apache/templates/vhosts/002-branch b/lib/ansible/roles/apache/templates/vhosts/002-branch index 7c49f692..e95afdc0 100644 --- a/lib/ansible/roles/apache/templates/vhosts/002-branch +++ b/lib/ansible/roles/apache/templates/vhosts/002-branch @@ -1,4 +1,5 @@ + UseCanonicalName Off ServerAdmin webmaster@localhost ServerName staging.{{ domain }} From 2326681e1d65b2fc101d80e365088c43fe542642 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Tue, 28 Apr 2015 11:25:41 -0500 Subject: [PATCH 08/13] Canonical config for apache 2.2 --- lib/ansible/roles/apache/tasks/main.yml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/ansible/roles/apache/tasks/main.yml b/lib/ansible/roles/apache/tasks/main.yml index c87e5705..b06079ba 100644 --- a/lib/ansible/roles/apache/tasks/main.yml +++ b/lib/ansible/roles/apache/tasks/main.yml @@ -16,13 +16,21 @@ - debug: msg={{ apache_version.stdout }} -- name: Configure Apache canonical server name +- name: Configure Apache canonical server name (2.2) + template: src=canonical dest=/etc/apache2/conf.d/canonical-servername.conf mode=0644 + notify: restart apache + when: apache_version.stdout == '2.2' + sudo: true + +- name: Configure Apache canonical server name (2.4) template: src=canonical dest=/etc/apache2/conf-available/canonical-servername.conf mode=0644 + when: apache_version.stdout == '2.4' sudo: true -- name: Enable canonical config +- name: Enable canonical config (2.4) command: a2enconf canonical-servername notify: restart apache + when: apache_version.stdout == '2.4' sudo: yes - name: Ensure Apache `NameVirtualHost` is 80 (2.2) From 97b23b9f869966a9e682d712313b40a420685703 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Tue, 28 Apr 2015 13:44:44 -0500 Subject: [PATCH 09/13] Fixed phpunit test and updated composer autoload path --- composer.json | 2 +- composer.lock | 7 +++- test/unit/wordpress/EvolutionTest.php | 56 +++++++++++++-------------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/composer.json b/composer.json index 93a40fd4..2b8e71af 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "", "autoload": { "psr-0": { - "Evolution": "lib/wordpress" + "Evolution": "temp/web" } }, "require-dev": { diff --git a/composer.lock b/composer.lock index 8c6bfcf1..e183ed46 100644 --- a/composer.lock +++ b/composer.lock @@ -1,9 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" ], - "hash": "3ce64f1bef543c0d01250f69b19cb382", + "hash": "b07772e26f05b1e772dd3ed95c242aef", "packages": [], "packages-dev": [ { @@ -703,6 +704,8 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, "platform": [], "platform-dev": [] } diff --git a/test/unit/wordpress/EvolutionTest.php b/test/unit/wordpress/EvolutionTest.php index 4e543476..8a433ed7 100644 --- a/test/unit/wordpress/EvolutionTest.php +++ b/test/unit/wordpress/EvolutionTest.php @@ -4,57 +4,53 @@ class EvolutionTest extends PHPUnit_Framework_TestCase { public function testGetDbName() { - $_SERVER['HTTP_HOST'] = 'local.example.com'; + $_SERVER['SERVER_NAME'] = 'local.'.Evolution::DOMAIN; $this->assertEquals('test_local', Evolution::getDbName('test')); } /** - * @dataProvider httpHostProvider + * @dataProvider serverNameProvider */ - public function testGetEnv($host, $expected) + public function testGetEnv($servername, $env, $hostname) { - $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SERVER_NAME'] = $servername; - $this->assertEquals($expected, Evolution::getEnv()); + $this->assertEquals($env, Evolution::getEnv()); } /** - * @dataProvider httpHostProvider + * @dataProvider serverNameProvider */ - public function testInitEnv($host, $expected) + public function testInitEnv($servername, $env, $hostname) { - $_SERVER['HTTP_HOST'] = $host; + $_SERVER['SERVER_NAME'] = $servername; Evolution::initEnv(); $this->assertTrue(defined('WP_ENV'), '`Evolution::initEnv()` should define `WP_ENV`'); - $this->assertEquals($expected, WP_ENV); + $this->assertEquals($env, WP_ENV); } - public function httpHostProvider() + /** + * @dataProvider serverNameProvider + */ + public function testGetHostname($servername, $env, $hostname) + { + $_SERVER['SERVER_NAME'] = $servername; + + $this->assertEquals($hostname, Evolution::getHostname()); + } + + public function serverNameProvider() { return array( - array('example.com', 'production'), - array('example.net', 'production'), - array('example.org', 'production'), - array('example.co.uk', 'production'), - array('www.example.com', 'production'), - array('www.example.org', 'production'), - array('www.example.net', 'production'), - array('www.example.co.uk', 'production'), - array('local.example.com', 'local'), - array('local.example.net', 'local'), - array('local.example.org', 'local'), - array('local.example.co.uk', 'local'), - array('staging.example.com', 'staging'), - array('staging.example.net', 'staging'), - array('staging.example.org', 'staging'), - array('staging.example.co.uk', 'staging'), - array('production.example.com', 'production'), - array('production.example.net', 'production'), - array('production.example.org', 'production'), - array('production.example.co.uk', 'production'), + array(Evolution::DOMAIN, 'production', Evolution::DOMAIN), + array('www.'.Evolution::DOMAIN, 'production', Evolution::DOMAIN), + array('local.'.Evolution::DOMAIN, 'local', 'local.'.Evolution::DOMAIN), + array('staging.'.Evolution::DOMAIN, 'staging', 'staging.'.Evolution::DOMAIN), + array('production.'.Evolution::DOMAIN, 'production', Evolution::DOMAIN), + array('local.thewrongdomain.com', 'production', Evolution::DOMAIN) ); } } From f7f7dc2fbe7a5cda9857863f2e97bab45877b2b1 Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Wed, 29 Apr 2015 18:27:59 -0500 Subject: [PATCH 10/13] Added `www` generator option, saved in groupvars; rewrote htaccess generation to preserve customizations outside the Evolution block --- bin/mock | 1 + lib/yeoman/index.js | 47 +++++++++++++++++++ .../templates/lib/ansible/group_vars/all | 1 + lib/yeoman/templates/web/_htaccess | 14 +++++- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/bin/mock b/bin/mock index 522e3d9e..79fd89d7 100755 --- a/bin/mock +++ b/bin/mock @@ -47,6 +47,7 @@ helpers.testDirectory(outputDir, function(err) { ssl: true, name: 'Example.com', domain: 'example.com', + www: false, roles: WordpressGenerator.roles.map(function(role) { return role.value; }), ip: '192.168.137.137', DB_NAME: 'example_db', diff --git a/lib/yeoman/index.js b/lib/yeoman/index.js index c51d0cad..6cf55fd4 100644 --- a/lib/yeoman/index.js +++ b/lib/yeoman/index.js @@ -223,6 +223,39 @@ var WordpressGenerator = yeoman.generators.Base.extend({ }); }, + promptForWww: function() { + var choices = [ + { + name: 'No, just the domain', + value: false, + }, + { + name: 'Yes, www is preferred', + value: true, + }, + ]; + var existing = function() { + try { + var match = this.readFileAsString(path.join(this.env.cwd, 'lib', 'ansible', 'group_vars', 'all')) + .match(/www[ ]*:[ ]*(true)/i); + + return match[1]; + } catch(e) {}; + }.bind(this); + + if(existing()) { + choices.reverse(); + } + + this.prompts.push({ + required: true, + type: 'list', + name: 'www', + message: 'Should this site enforce a www subdomain in production? (e.g. www.mysite.com)', + choices: choices + }); + }, + promptForRoles: function() { try { var provisionYml = this.readFileAsString(path.join(this.env.cwd, 'lib', 'ansible', 'provision.yml')); @@ -409,6 +442,20 @@ var WordpressGenerator = yeoman.generators.Base.extend({ } }, + prepareHtaccess: function() { + try { + this.htaccessFile = this.readFileAsString(path.join(this.env.cwd, 'web', '.htaccess')); + this.htaccessFile = this.htaccessFile + .replace(/# BEGIN Evolution WordPress(?:.|[\r\n]+)+?# END Evolution WordPress[\r\n]*/i, '') + ; + + this.htaccessWpBlock = !!this.htaccessFile.match(/# BEGIN WordPress(?:.|[\r\n]+)+?# END WordPress[\r\n]*/i); + } catch(e) { + this.htaccessFile = ''; + this.htaccessWpBlock = false; + } + }, + prepareRoles: function() { this.props.roles = WordpressGenerator.roles.map(function(role) { role.checked = !!~this.props.roles.indexOf(role.value); diff --git a/lib/yeoman/templates/lib/ansible/group_vars/all b/lib/yeoman/templates/lib/ansible/group_vars/all index f5c85ba9..d05eb36d 100644 --- a/lib/yeoman/templates/lib/ansible/group_vars/all +++ b/lib/yeoman/templates/lib/ansible/group_vars/all @@ -1,6 +1,7 @@ --- evolve_version: <%= pkg.version %> domain: <%= props.domain %> +www: <%= props.www %> mysql: name: <%= props.DB_NAME %> diff --git a/lib/yeoman/templates/web/_htaccess b/lib/yeoman/templates/web/_htaccess index 77e1e877..d67c174a 100644 --- a/lib/yeoman/templates/web/_htaccess +++ b/lib/yeoman/templates/web/_htaccess @@ -1,4 +1,14 @@ -# BEGIN WordPress +# BEGIN Evolution Wordpress + +RewriteEngine On +# Force <%= props.www ? '' : 'non-' %>www for production +RewriteCond %{HTTP_HOST} ^<%= props.www ? '' : 'www\\.' %><%= props.domain.replace('.', '\\.') %>$ [NC] +RewriteCond %{HTTP:X-Forwarded-Proto} ^http(s)| [NC] +RewriteRule ^(.*)$ http%1://<%= props.www ? 'www.' : '' %><%= props.domain %>/$1 [L,R=301,NC] + +# END Evolution Wordpress +<%= htaccessFile %> +<% if (!htaccessWpBlock) { %># BEGIN WordPress RewriteEngine On RewriteBase / @@ -7,4 +17,4 @@ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] -# END WordPress +# END WordPress<% } %> From 8f416df1ad7072c71bf625d58e30b71e83cb2d7e Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Thu, 30 Apr 2015 11:52:20 -0500 Subject: [PATCH 11/13] Added www setting to generated deploy.rb; updated db rakefile to check www setting; updated Evolution php class to check www setting --- lib/capistrano/tasks/db.rake | 6 ++-- lib/yeoman/templates/lib/capistrano/deploy.rb | 1 + lib/yeoman/templates/web/Evolution.php | 28 +++++++++++++++++-- test/unit/wordpress/EvolutionTest.php | 13 ++++++--- 4 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index d1ffa165..22b53d5e 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,8 +1,10 @@ namespace :evolve do namespace :db do task :prepare, :target_stage do |task, args| - stage_subdomain = args[:target_stage].to_s - stage_subdomain = stage_subdomain == 'production' ? '' : stage_subdomain + '.' + stage_subdomain = args[:target_stage].to_s + '.' + if stage_subdomain == 'production.' + stage_subdomain = fetch(:www) ? 'www.' : '' + end set :perl_cmd, '-pi -e \'s!(https?://)(?:(?:[^/]+\.)?(?:local|staging|production|www)\.)?(' + Regexp.escape(fetch(:domain)) + diff --git a/lib/yeoman/templates/lib/capistrano/deploy.rb b/lib/yeoman/templates/lib/capistrano/deploy.rb index da63ea46..21851acb 100644 --- a/lib/yeoman/templates/lib/capistrano/deploy.rb +++ b/lib/yeoman/templates/lib/capistrano/deploy.rb @@ -15,6 +15,7 @@ set :deploy_to, "/var/www/#{fetch(:domain)}/#{fetch(:stage)}/#{fetch(:branch)}" set :linked_dirs, %w{web/wp-content/uploads} set :wp_path, "#{release_path}/web/wp" +set :www, <%= props.www ? 'true' : 'false' %> namespace :deploy do after :updated, :bower_install do diff --git a/lib/yeoman/templates/web/Evolution.php b/lib/yeoman/templates/web/Evolution.php index b55876e2..cda26490 100644 --- a/lib/yeoman/templates/web/Evolution.php +++ b/lib/yeoman/templates/web/Evolution.php @@ -4,6 +4,9 @@ class Evolution { const DOMAIN = '<%= props.domain %>'; + public static $hostname; + public static $env; + public static function getDbName($name) { return sprintf('%s_%s', $name, static::getEnv()); @@ -11,24 +14,43 @@ public static function getDbName($name) public static function getHostname() { + if (isset(self::$hostname)) { + return self::$hostname; + } + $env = self::getEnv(); if (in_array($env, array('local', 'staging'))) { - return $env.'.'.self::DOMAIN; + self::$hostname = $env.'.'.self::DOMAIN; } else { - return self::DOMAIN; + $content = file_get_contents(dirname(__FILE__).'/../lib/ansible/group_vars/all'); + + if (preg_match('/www[ ]*:[ ]*(true)/', $content)) { + self::$hostname = 'www.'.self::DOMAIN; + } + else { + self::$hostname = self::DOMAIN; + } } + + return self::$hostname; } public static function getEnv() { + if (isset(self::$env)) { + return self::$env; + } + preg_match('/(?:(local|staging|production)\.)?'.preg_quote(self::DOMAIN).'$/', $_SERVER['SERVER_NAME'], $matches); $match = count($matches) > 1 ? array_pop($matches) : 'production'; $known = array('local', 'staging', 'production'); - return in_array($match, $known) ? $match : 'production'; + self::$env = in_array($match, $known) ? $match : 'production'; + + return self::$env; } public static function initEnv() diff --git a/test/unit/wordpress/EvolutionTest.php b/test/unit/wordpress/EvolutionTest.php index 8a433ed7..b5affb5e 100644 --- a/test/unit/wordpress/EvolutionTest.php +++ b/test/unit/wordpress/EvolutionTest.php @@ -1,5 +1,10 @@ Date: Wed, 6 May 2015 11:34:26 -0500 Subject: [PATCH 12/13] Replaced perl regex filter with wp-cli search-replace command --- lib/capistrano/tasks/db.rake | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/capistrano/tasks/db.rake b/lib/capistrano/tasks/db.rake index 22b53d5e..4cea4f7f 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,16 +1,20 @@ namespace :evolve do namespace :db do - task :prepare, :target_stage do |task, args| - stage_subdomain = args[:target_stage].to_s + '.' - if stage_subdomain == 'production.' - stage_subdomain = fetch(:www) ? 'www.' : '' + task :prepare, :source, :target do |task, args| + def filter_stage(stage) + if stage == 'production' + return (fetch(:www) ? 'www.' : '') + fetch(:domain) + else + return stage + '.' + fetch(:domain) + end end - set :perl_cmd, '-pi -e \'s!(https?://)(?:(?:[^/]+\.)?(?:local|staging|production|www)\.)?(' + - Regexp.escape(fetch(:domain)) + - '/)!$1' + - Regexp.escape(stage_subdomain) + - '$2!ig\'' + set :wp_cmd, [ + '"://' + filter_stage(args[:source].to_s) + '/"', + '"://' + filter_stage(args[:target].to_s) + '/"', + '--path="' + fetch(:wp_path) + '"', + '--url="http://' + args[:target].to_s + '.' + fetch(:domain) + '/"', + ].join(' ') end desc "Create backup of remote DB" @@ -52,13 +56,13 @@ namespace :evolve do task :down do |task| begin invoke "evolve:db:backup" - invoke "evolve:db:prepare", "local" + invoke "evolve:db:prepare", fetch(:stage), "local" run_locally do execute :gzip, "-d", fetch(:db_gzip_file) - execute :perl, fetch(:perl_cmd), fetch(:db_backup_file) execute :vagrant, :up execute :vagrant, :ssh, :local, "-c 'cd /vagrant && mysql -uroot -D \"#{fetch(:wp_config)['name']}_local\" < #{fetch(:db_backup_file)}'" + execute :vagrant, :ssh, :local, "-c 'cd #{fetch(:wp_path)} && wp search-replace #{fetch(:wp_cmd)}'" execute :rm, fetch(:db_backup_file) end @@ -72,7 +76,7 @@ namespace :evolve do begin invoke "evolve:confirm", "You are about to destroy & override the \"#{fetch(:stage)}\" database!" invoke "evolve:db:backup", true - invoke "evolve:db:prepare", fetch(:stage) + invoke "evolve:db:prepare", "local", fetch(:stage) run_locally do execute :vagrant, :up @@ -83,10 +87,10 @@ namespace :evolve do on release_roles(:db) do upload! fetch(:db_gzip_file), "/tmp/#{fetch(:db_gzip_file)}" execute :gzip, "-d", "/tmp/#{fetch(:db_gzip_file)}" - execute :perl, fetch(:perl_cmd), "/tmp/#{fetch(:db_backup_file)}" within fetch(:wp_path) do execute :wp, :db, :import, "/tmp/#{fetch(:db_backup_file)}", "--path=\"#{fetch(:wp_path)}\"", "--url=\"http://#{fetch(:stage)}.#{fetch(:domain)}/\"" + execute :wp, :'search-replace', fetch(:wp_cmd) end execute :rm, "/tmp/#{fetch(:db_backup_file)}" From 478576ffbf0e5a43659b791745f58d84c7b3ebbd Mon Sep 17 00:00:00 2001 From: Evan Kaufman Date: Fri, 8 May 2015 12:37:31 -0500 Subject: [PATCH 13/13] Moved Evolution.php from web dir to project root --- composer.json | 2 +- composer.lock | 2 +- lib/yeoman/templates/{web => }/Evolution.php | 2 +- lib/yeoman/templates/web/wp-config.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename lib/yeoman/templates/{web => }/Evolution.php (94%) diff --git a/composer.json b/composer.json index 2b8e71af..7d46abf5 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "", "autoload": { "psr-0": { - "Evolution": "temp/web" + "Evolution": "temp" } }, "require-dev": { diff --git a/composer.lock b/composer.lock index e183ed46..46d67bc3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "b07772e26f05b1e772dd3ed95c242aef", + "hash": "d69b7ab33d905f8cacec6c516a0fb4de", "packages": [], "packages-dev": [ { diff --git a/lib/yeoman/templates/web/Evolution.php b/lib/yeoman/templates/Evolution.php similarity index 94% rename from lib/yeoman/templates/web/Evolution.php rename to lib/yeoman/templates/Evolution.php index cda26490..9d88d691 100644 --- a/lib/yeoman/templates/web/Evolution.php +++ b/lib/yeoman/templates/Evolution.php @@ -24,7 +24,7 @@ public static function getHostname() self::$hostname = $env.'.'.self::DOMAIN; } else { - $content = file_get_contents(dirname(__FILE__).'/../lib/ansible/group_vars/all'); + $content = file_get_contents(dirname(__FILE__).'/lib/ansible/group_vars/all'); if (preg_match('/www[ ]*:[ ]*(true)/', $content)) { self::$hostname = 'www.'.self::DOMAIN; diff --git a/lib/yeoman/templates/web/wp-config.php b/lib/yeoman/templates/web/wp-config.php index e26bcda3..d2d7a9d1 100644 --- a/lib/yeoman/templates/web/wp-config.php +++ b/lib/yeoman/templates/web/wp-config.php @@ -1,7 +1,7 @@