Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
05b5256
Changing WP_HOME and WP_SITEURL to absolute urls, dynamically set fro…
EvanK Apr 22, 2015
247a761
Removing absolute urls from sql dump on db:{up,down}
EvanK Apr 22, 2015
ac8b369
Removed useless arg
EvanK Apr 22, 2015
616803b
Merge branch 'master' into 35-url-constants
EvanK Apr 23, 2015
c20c059
Absolute url regex is more specific in what it matches
EvanK Apr 26, 2015
254a0cf
Regex filtering is now _replacing_ stage subdomains in absolute urls,…
EvanK Apr 27, 2015
7ac2fe5
Normalizing absolute urls based on current stage
EvanK Apr 28, 2015
488b2b2
Turn off canonical name for wildcard branch vhost
EvanK Apr 28, 2015
2326681
Canonical config for apache 2.2
EvanK Apr 28, 2015
97b23b9
Fixed phpunit test and updated composer autoload path
EvanK Apr 28, 2015
c6a7a4f
Merge branch 'master' into 35-url-constants
EvanK Apr 28, 2015
f7f7dc2
Added `www` generator option, saved in groupvars; rewrote htaccess ge…
EvanK Apr 29, 2015
191ecd3
Merge branch 'master' into 35-url-constants
EvanK Apr 30, 2015
b2b566d
Merge branch 'master' into 35-url-constants
EvanK Apr 30, 2015
8f416df
Added www setting to generated deploy.rb; updated db rakefile to chec…
EvanK Apr 30, 2015
c6a63c4
Merge branch 'master' into 35-url-constants
EvanK May 3, 2015
2f91499
Replaced perl regex filter with wp-cli search-replace command
EvanK May 6, 2015
104a1d5
Merge branch 'master' into 35-url-constants
EvanK May 6, 2015
5d3052f
Merge branch 'master' into 35-url-constants
EvanK May 7, 2015
478576f
Moved Evolution.php from web dir to project root
EvanK May 8, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "",
"autoload": {
"psr-0": {
"Evolution": "lib/wordpress"
"Evolution": "temp"
}
},
"require-dev": {
Expand Down
7 changes: 5 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/ansible/roles/apache/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/ansible/roles/apache/templates/canonical
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
UseCanonicalName On
ServerName {{ domain }}
21 changes: 21 additions & 0 deletions lib/capistrano/tasks/db.rake
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)}"
Expand Down
30 changes: 0 additions & 30 deletions lib/wordpress/Evolution.php

This file was deleted.

66 changes: 66 additions & 0 deletions lib/yeoman/templates/Evolution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

class Evolution
{
const DOMAIN = '<%= props.domain %>';

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;
}

}
6 changes: 3 additions & 3 deletions lib/yeoman/templates/web/wp-config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* Include Evolution to use with WordPress */
require_once(dirname(__FILE__) . '/../bower_components/evolution-wordpress/lib/wordpress/Evolution.php');
require_once(dirname(__FILE__) . '/../Evolution.php');
<%= wpConfigFile
// Already started PHP
.replace('<?php', '')
Expand Down Expand Up @@ -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://'.Evolution::getHostname().'/');",
"define('WP_POST_REVISIONS', 5);",
"define('WP_SITEURL', '/wp');",
"define('WP_SITEURL', 'http://'.Evolution::getHostname().'/wp');",
"/* That's all,",
].join('\n')
)
Expand Down
61 changes: 31 additions & 30 deletions test/unit/wordpress/EvolutionTest.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
<?php

if (!defined('EVOLUTION_WWW_MODE')) {
$content = file_get_contents(dirname(__FILE__).'/../../../temp/lib/ansible/group_vars/all');
define('EVOLUTION_WWW_MODE', preg_match('/www[ ]*:[ ]*(true)/', $content) ? 'www.' : '');
}

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);
}

/**
* @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)
);
}
}