|
| 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 | +} |
0 commit comments