Skip to content

Commit e75d45b

Browse files
lcatlettgrasmash
authored andcommitted
Add Cloud hooks for ACSF (acquia#1209)
* Add ACSF drush utility commands. * Add support for multisite cloud hooks and BLT deploy tasks on ACSF. * Enable BLT deploy updates on ACSF by default. * Documenting post-drupal-scaffold-cmd to applying patches (acquia#1208) Plus minor grammer change to "Contributing.md" : are -> our Signed-off-by: Justin Winter <[email protected]> * Updating README.md for patches.
1 parent acf91fc commit e75d45b

File tree

4 files changed

+277
-9
lines changed

4 files changed

+277
-9
lines changed

scripts/cloud-hooks/functions.sh

+46-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,53 @@
33

44
status=0
55

6+
drush_alias=${site}'.'${target_env}
7+
68
deploy_updates() {
79

10+
case $target_env in
11+
[01]*)
12+
acsf_deploy
13+
;;
14+
*)
15+
ace_deploy
16+
;;
17+
esac
18+
}
19+
20+
21+
acsf_deploy() {
22+
sites=()
23+
# Prep for BLT commands.
24+
repo_root="/var/www/html/$site.$target_env"
25+
export PATH=$repo_root/vendor/bin:$PATH
26+
cd $repo_root
27+
28+
echo "Running updates for environment: $target_env"
29+
30+
# Generate an array of all site URIs on the Factory from parsed output of Drush utility.
31+
while IFS=$'\n' read -r line; do
32+
sites[i++]="$line"
33+
done < <(drush @"${drush_alias}" --include=./drush acsf-tools-list | grep domains: -A 1 | grep 0: | sed -e 's/^[0: ]*//')
34+
unset IFS
35+
36+
# Loop through each available site uri and run BLT deploy updates.
37+
for uri in "${sites[@]}"; do
38+
#Override BLT default deploy uri.
39+
blt deploy:update -Denvironment=$target_env -Ddrush.uri="$uri"
40+
if [ $? -ne 0 ]; then
41+
echo "Update errored."
42+
status=1;
43+
fi
44+
45+
echo "Finished updates for site: $uri."
46+
done
47+
48+
echo "Finished updates for all $target_env sites."
49+
}
50+
51+
ace_deploy() {
52+
853
echo "Running updates for environment: $target_env"
954

1055
# Prep for BLT commands.
@@ -20,7 +65,7 @@ deploy_updates() {
2065

2166
echo "Finished updates for environment: $target_env"
2267
}
23-
68+
2469
deploy_install() {
2570

2671
echo "Installing site for environment: $target_env"

template/drush/acsf_tools.drush.inc

+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
/**
3+
* @file
4+
* Utility commands for ACSF sites.
5+
*/
6+
7+
/**
8+
* Implements hook_drush_command().
9+
*/
10+
function acsf_tools_drush_command() {
11+
$items = array();
12+
13+
$items['acsf-tools-list'] = array(
14+
'description' => dt('List the sites of the factory.'),
15+
'options' => array(
16+
'fields' => 'The list of fields to display (comma separated list).',
17+
),
18+
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
19+
'examples' => array(
20+
'drush acsf-tools-list' => 'Get all details for all the sites of the factory.',
21+
'drush acsf-tools-list --fields' => 'Get prefix for all the sites of the factory.',
22+
'drush acsf-tools-list --fields=name,domains' => 'Get prefix, name and domains for all the sites of the factory.',
23+
),
24+
);
25+
26+
$items['acsf-tools-ml'] = array(
27+
'description' => dt('Runs the passed drush command against all the sites of the factory (ml stands for multiple -l option).'),
28+
'allow-additional-options' => TRUE,
29+
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
30+
'examples' => array(
31+
'drush8 acsf-tools-ml cget system.site page.front' => 'Get value of page.front config for all the sites.',
32+
'drush8 acsf-tools-ml sqlq "select status from system where name=\'php\'"' => 'Check status of php module on all the sites.',
33+
),
34+
);
35+
36+
$items['acsf-tools-dump'] = array(
37+
'description' => dt('Make a DB dump for each site of the factory).'),
38+
'options' => array(
39+
'result-folder' => 'The folder in which the dumps will be written. Defaults to ~/drush-backups.',
40+
),
41+
'allow-additional-options' => TRUE,
42+
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
43+
'examples' => array(
44+
'drush8 acsf-tools-dump' => 'Create DB dumps for the sites of the factory. Default result folder will be used.',
45+
'drush8 acsf-tools-dump --result-folder=/home/project/backup/2017' => 'Create DB dumps for the sites of the factory and store them in the specified folder. If folder does not exist the command will try to create it.',
46+
'drush8 acsf-tools-dump --result-folder=/home/project/backup/2017 --gzip' => 'Same as above but using options of sql-dump command.',
47+
),
48+
);
49+
50+
return $items;
51+
}
52+
53+
/**
54+
* Action callback for acsf-tools-list command.
55+
*/
56+
function drush_acsf_tools_list() {
57+
// Look for list of sites and loop over it.
58+
if ($sites = _drush_acsf_tools_get_sites()) {
59+
// Render the info.
60+
$fields = drush_get_option('fields');
61+
if (isset($fields)) {
62+
$expected_attributes = array_flip(explode(',', $fields));
63+
}
64+
65+
foreach ($sites as $name => $details) {
66+
// Get site prefix from main domain.
67+
$prefix = explode('.', $details['domains'][0])[0];
68+
drush_print($prefix);
69+
70+
// Filter attributes.
71+
if (isset($expected_attributes)) {
72+
$details = array_intersect_key($details, $expected_attributes);
73+
}
74+
75+
// Print attributes.
76+
_drush_acsf_tools_recursive_print($details, 2);
77+
}
78+
}
79+
}
80+
81+
/**
82+
* Validate callback for acsf-tools-ml command.
83+
*/
84+
function drush_acsf_tools_ml_validate() {
85+
$arguments = drush_get_arguments();
86+
if (count($arguments) == 1) {
87+
return drush_set_error('WRONG_PARAMETERS', dt('This command expects at least one parameter.'));
88+
}
89+
}
90+
91+
/**
92+
* Action callback for acsf-tools-ml command.
93+
*/
94+
function drush_acsf_tools_ml() {
95+
// Ask for confirmation before running the command.
96+
if (!_drush_acsf_tools_prompt_confirm()) {
97+
return;
98+
}
99+
100+
// Look for list of sites and loop over it.
101+
if ($sites = _drush_acsf_tools_get_sites()) {
102+
$arguments = drush_get_arguments();
103+
unset($arguments[0]);
104+
$command = array_shift($arguments);
105+
106+
$options = drush_get_context('cli');
107+
unset($options['php']);
108+
unset($options['php-options']);
109+
110+
$processed = array();
111+
foreach ($sites as $details) {
112+
$domain = $details['domains'][0];
113+
114+
drush_log("\n=> Running command on $domain", 'ok');
115+
drush_invoke_process('@self', $command, $arguments, $options + array('l' => $domain));
116+
}
117+
}
118+
}
119+
120+
/**
121+
* Action callback for acsf-tools-dump command.
122+
*/
123+
function drush_acsf_tools_dump() {
124+
// Ask for confirmation before running the command.
125+
if (!_drush_acsf_tools_prompt_confirm()) {
126+
return;
127+
}
128+
129+
// Identify target folder.
130+
$result_folder = drush_get_option('result-folder');
131+
if (!isset($result_folder)) {
132+
$result_folder = '~/drush-backups';
133+
}
134+
135+
if (!is_dir($result_folder) || !is_writable($result_folder)) {
136+
// Target folder does not exist. Try to create it.
137+
if (!mkdir($result_folder, 0777, TRUE)) {
138+
drush_log("\nImpossible to write to $result_folder folder.", 'error');
139+
return;
140+
}
141+
}
142+
143+
// Look for list of sites and loop over it.
144+
if ($sites = _drush_acsf_tools_get_sites()) {
145+
$arguments = drush_get_arguments();
146+
$command = 'sql-dump';
147+
148+
$options = drush_get_context('cli');
149+
unset($options['php']);
150+
unset($options['php-options']);
151+
152+
unset($options['result-folder']);
153+
154+
$processed = array();
155+
foreach ($sites as $details) {
156+
$domain = $details['domains'][0];
157+
$prefix = explode('.', $domain)[0];
158+
159+
$options['result-file'] = $result_folder . '/' . $prefix . '.sql';
160+
161+
drush_log("\n=> Running command on $domain", 'ok');
162+
drush_invoke_process('@self', $command, $arguments, $options + array('l' => $domain));
163+
}
164+
}
165+
}
166+
167+
/**
168+
* Utility function to retrieve sites information.
169+
*/
170+
function _drush_acsf_tools_get_sites() {
171+
$sites = FALSE;
172+
173+
// Look for list of sites and loop over it.
174+
if (($map = gardens_site_data_load_file()) && isset($map['sites'])) {
175+
// Acquire sites info.
176+
$sites = array();
177+
foreach ($map['sites'] as $domain => $site_details) {
178+
if (!isset($sites[$site_details['name']])) {
179+
$sites[$site_details['name']] = $site_details;
180+
}
181+
$sites[$site_details['name']]['domains'][] = $domain;
182+
}
183+
}
184+
else {
185+
drush_log("\nFailed to retrieve the list of sites of the factory.", 'error');
186+
}
187+
188+
return $sites;
189+
}
190+
191+
/**
192+
* Utility function to print arrays.
193+
*/
194+
function _drush_acsf_tools_recursive_print($variable, $indent) {
195+
foreach ($variable as $key => $value) {
196+
if (!is_array($value)) {
197+
drush_print($key . ': ' . $value, $indent);
198+
}
199+
else {
200+
drush_print($key . ':', $indent);
201+
_drush_acsf_tools_recursive_print($value, $indent + 2);
202+
}
203+
}
204+
}
205+
206+
/**
207+
* Utility function to ask for confirmation.
208+
*/
209+
function _drush_acsf_tools_prompt_confirm() {
210+
// Ask for confirmation before running the command.
211+
// Special care for -y option to avoid drush_prompt default behaviour.
212+
$yes = drush_get_context('DRUSH_AFFIRMATIVE');
213+
if ($yes) {
214+
drush_set_context('DRUSH_AFFIRMATIVE', FALSE);
215+
}
216+
217+
$input = drush_prompt(
218+
dt('You are about to run a command on all the sites of your factory.
219+
Do you confirm you want to do that? If yes, type \'ok\'')
220+
);
221+
if ($input != 'ok') {
222+
return FALSE;
223+
}
224+
225+
if ($yes) {
226+
drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
227+
}
228+
229+
return TRUE;
230+
}

template/hooks/common/post-code-deploy/post-code-deploy.sh

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ deployed_tag="$4"
1717
repo_url="$5"
1818
repo_type="$6"
1919

20-
acsf_file="/mnt/files/$AH_SITE_GROUP.$AH_SITE_ENVIRONMENT/files-private/sites.json"
21-
if [ ! -f $acsf_file ]; then
20+
2221
. /var/www/html/$site.$target_env/vendor/acquia/blt/scripts/cloud-hooks/functions.sh
2322
deploy_updates
2423
# Send notifications to Slack, if configured. See readme/deploy.md for setup instructions.
2524
. `dirname $0`/../slack.sh
26-
exit $status
27-
fi

template/hooks/common/post-code-update/post-code-update.sh

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ deployed_tag="$4"
2020
repo_url="$5"
2121
repo_type="$6"
2222

23-
acsf_file="/mnt/files/$AH_SITE_GROUP.$AH_SITE_ENVIRONMENT/files-private/sites.json"
24-
if [ ! -f $acsf_file ]; then
2523
. /var/www/html/$site.$target_env/vendor/acquia/blt/scripts/cloud-hooks/functions.sh
2624
deploy_updates
2725
. `dirname $0`/../slack.sh
28-
exit $status
29-
fi

0 commit comments

Comments
 (0)