diff --git a/composer.json b/composer.json index 93a40fd4..7d46abf5 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "", "autoload": { "psr-0": { - "Evolution": "lib/wordpress" + "Evolution": "temp" } }, "require-dev": { diff --git a/composer.lock b/composer.lock index 8c6bfcf1..46d67bc3 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": "d69b7ab33d905f8cacec6c516a0fb4de", "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/lib/ansible/roles/apache/tasks/main.yml b/lib/ansible/roles/apache/tasks/main.yml index a7cd6d69..0fe7ced8 100644 --- a/lib/ansible/roles/apache/tasks/main.yml +++ b/lib/ansible/roles/apache/tasks/main.yml @@ -22,6 +22,23 @@ - debug: msg={{ apache_version.stdout }} +- 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 (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) 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 77b45e72..4cea4f7f 100644 --- a/lib/capistrano/tasks/db.rake +++ b/lib/capistrano/tasks/db.rake @@ -1,5 +1,22 @@ namespace :evolve do namespace :db do + 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 :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" 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,11 +56,13 @@ namespace :evolve do task :down do |task| begin invoke "evolve:db:backup" + invoke "evolve:db:prepare", fetch(:stage), "local" run_locally do execute :gzip, "-d", fetch(:db_gzip_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 @@ -57,6 +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", "local", fetch(:stage) run_locally do execute :vagrant, :up @@ -70,6 +90,7 @@ namespace :evolve do 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)}" 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/Evolution.php b/lib/yeoman/templates/Evolution.php new file mode 100644 index 00000000..9d88d691 --- /dev/null +++ b/lib/yeoman/templates/Evolution.php @@ -0,0 +1,66 @@ +'; + + public static $hostname; + public static $env; + + public static function getDbName($name) + { + return sprintf('%s_%s', $name, static::getEnv()); + } + + public static function getHostname() + { + if (isset(self::$hostname)) { + return self::$hostname; + } + + $env = self::getEnv(); + + if (in_array($env, array('local', 'staging'))) { + self::$hostname = $env.'.'.self::DOMAIN; + } + else { + $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'); + + self::$env = in_array($match, $known) ? $match : 'production'; + + return self::$env; + } + + 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 802d9521..d2d7a9d1 100644 --- a/lib/yeoman/templates/web/wp-config.php +++ b/lib/yeoman/templates/web/wp-config.php @@ -1,7 +1,7 @@ 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); + } + + /** + * @dataProvider serverNameProvider + */ + public function testGetHostname($servername, $env, $hostname) + { + $_SERVER['SERVER_NAME'] = $servername; + + $this->assertEquals($hostname, Evolution::getHostname()); } - public function httpHostProvider() + 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_WWW_MODE . Evolution::DOMAIN), + array('www.'.Evolution::DOMAIN, 'production', EVOLUTION_WWW_MODE . Evolution::DOMAIN), + array('local.'.Evolution::DOMAIN, 'local', 'local.'.Evolution::DOMAIN), + array('staging.'.Evolution::DOMAIN, 'staging', 'staging.'.Evolution::DOMAIN), + array('production.'.Evolution::DOMAIN, 'production', EVOLUTION_WWW_MODE . Evolution::DOMAIN), + array('local.thewrongdomain.com', 'production', EVOLUTION_WWW_MODE . Evolution::DOMAIN) ); } }