-
Notifications
You must be signed in to change notification settings - Fork 25
/
acsf_tools_stage_domains.drush.inc
132 lines (106 loc) · 3.63 KB
/
acsf_tools_stage_domains.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Implements hook_drush_command().
*/
function acsf_tools_stage_domains_drush_command() {
$items = array();
$items['acsf-tools-stage-domains'] = array(
'description' => dt('Automatically stage the production Factories\' vanity domains to a lower environment.'),
'arguments' => array(
'env' => 'The environment we\'re staging domains to. E.g. dev\test',
),
'required-arguments' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'examples' => array(
'drush @mysite.local acsf-stage-domains dev',
),
'aliases' => ['sfdo'],
);
return $items;
}
/**
* Drush callback to copy custom domain names from production down to
* a lower environment (dev/stage).
*
* @param $env
* @return bool
*/
function drush_acsf_tools_stage_domains($env) {
if (!in_array($env, array('dev','test'))) {
return drush_set_error('Invalid staging environment.');
}
$config = acsf_tools_get_rest_config();
// Get Sites in the prod factory.
$sites = get_factory_sites($config, 'prod');
foreach ($sites as $site) {
post_vanity_domain($config, $site, $env);
}
}
/**
* Helper function to get a list of sites in the production factory.
*
* @param $username
* @param $password
* @return mixed
*/
function get_factory_sites($config, $env = 'prod') {
// TODO: What happens when more than 100 sites? Implement paging.
$sites_url = acsf_tools_get_factory_url($config, '/api/v1/sites?limit=100', $env);
return acsf_tools_curl_wrapper($config->username, $config->password, $sites_url)->sites;
}
function get_prod_vanity_domain_for_site($config, $site) {
$domain_url = acsf_tools_get_factory_url($config,"/api/v1/domains/$site->id");
$result = acsf_tools_curl_wrapper($config->username, $config->password, $domain_url);
return $result->domains->custom_domains[0];
}
/**
* Helper function to post a vanity domain to a staging environment.
*
* @param $config
* @param $site
* @param $env
*/
function post_vanity_domain($config, $site, $env) {
$post_domain_url = acsf_tools_get_factory_url($config,"/api/v1/domains/$site->id/add", $env);
if ($stage_domain = get_stage_domain($config, $site, $env)) {
$data = array(
'domain_name' => $stage_domain
);
$result = acsf_tools_curl_wrapper($config->username, $config->password, $post_domain_url, $data);
drush_print("$stage_domain set OK.");
}
}
/**
* Utility function to parse and set the staging domain based on a user defined
* pattern set in acsf_tools_config.yml.
*
* @param $config
* @param $site
* @param $env
* @return string
*/
function get_stage_domain($config, $site, $env) {
// Set the url_env string to 'stage' if env is test.
$url_env = ($env == 'test') ? 'stage' : $env;
// Find the existing prod vanity domain.
if ($prod_domain = get_prod_vanity_domain_for_site($config, $site)) {
// Get the subdomain off the prod vanity url. E.g., 'www.foo.com'
// is 'www'. Or, 'coolsite.domain.com' is 'coolsite'.
$parts = explode('.', $prod_domain, 2);
$subdomain = $parts[0];
$prod_root_domain = $parts[1];
if ($subdomain == 'www') {
// If subdomain is www, then stage pattern should be dev.foo.com,
// test.foo.com, etc.
$new_subdomain = $url_env;
} else {
// If subdomain is custom, e.g., 'coolsite.foo.com', then follow
// the pattern set in acsf_tools_config.yml - default is {subdomain}-{env},
// e.g., coolsite-dev.foo.com.
$new_subdomain = str_replace('{subdomain}', $subdomain, $config->subdomain_pattern);
$new_subdomain = str_replace('{env}', $url_env, $new_subdomain);
}
return "$new_subdomain.$prod_root_domain";
}
return FALSE;
}