|
78 | 78 | <title>Simple PHP GIT deploy script</title>
|
79 | 79 | <style>
|
80 | 80 | body { padding: 0 1em; background: #222; color: #fff; }
|
81 |
| -h2 { color: #c33; } |
| 81 | +h2, .error { color: #c33; } |
82 | 82 | .prompt { color: #6be234; }
|
83 | 83 | .command { color: #729fcf; }
|
| 84 | +.output { color: #999; } |
84 | 85 | </style>
|
85 | 86 | </head>
|
86 | 87 | <body>
|
|
104 | 105 | foreach (array('git', 'rsync') as $command) {
|
105 | 106 | $path = trim(shell_exec('which '.$command));
|
106 | 107 | if ($path == '') {
|
107 |
| - die(sprintf('<b>%s</b> not available. It need to be installed on the server for this script to work.', $command)); |
| 108 | + die(sprintf('<div class="error"><b>%s</b> not available. It need to be installed on the server for this script to work.</div>', $command)); |
108 | 109 | } else {
|
109 | 110 | $binaries[$command] = $path;
|
110 | 111 | $version = explode("\n", shell_exec($path.' --version'));
|
|
174 | 175 | );
|
175 | 176 |
|
176 | 177 | // Remove the TMP_DIR
|
177 |
| -$commands[] = sprintf( |
| 178 | +$commands['cleanup'] = sprintf( |
178 | 179 | 'rm -rf %s'
|
179 | 180 | , TMP_DIR
|
180 | 181 | );
|
|
185 | 186 | if (file_exists(TMP_DIR) && is_dir(TMP_DIR)) {
|
186 | 187 | chdir(TMP_DIR); // Ensure that we're in the right directory
|
187 | 188 | }
|
188 |
| - $tmp = shell_exec($command.' 2>&1'); // Execute the command |
| 189 | + $tmp = array(); |
| 190 | + exec($command.' 2>&1', $tmp, $return_code); // Execute the command |
189 | 191 | // Output the result
|
190 | 192 | printf('
|
191 | 193 | <span class="prompt">$</span> <span class="command">%s</span>
|
192 |
| -%s |
| 194 | +<div class="output">%s</div> |
193 | 195 | '
|
194 | 196 | , htmlentities(trim($command))
|
195 |
| - , htmlentities(trim($tmp)) |
| 197 | + , htmlentities(trim(implode("\n", $tmp))) |
196 | 198 | );
|
197 | 199 | flush(); // Try to output everything as it happens
|
| 200 | + |
| 201 | + // Error handling and cleanup |
| 202 | + if ($return_code !== 0) { |
| 203 | + $tmp = shell_exec($commands['cleanup']); |
| 204 | + printf(' |
| 205 | +<div class="error"> |
| 206 | +Error encountered! |
| 207 | +Stopping the script to prevent possible data loss. |
| 208 | +CHECK THE DATA IN YOUR TARGET DIR! |
| 209 | +</div> |
| 210 | +
|
| 211 | +
|
| 212 | +Cleaning up temporary files ... |
| 213 | +
|
| 214 | +<span class="prompt">$</span> <span class="command">%s</span> |
| 215 | +<div class="output">%s</div> |
| 216 | +' |
| 217 | + , htmlentities(trim($commands['cleanup'])) |
| 218 | + , htmlentities(trim($tmp)) |
| 219 | + ); |
| 220 | + error_log(sprintf( |
| 221 | + 'Deployment error! %s' |
| 222 | + , __FILE__ |
| 223 | + )); |
| 224 | + break; |
| 225 | + } |
198 | 226 | }
|
199 | 227 | ?>
|
200 | 228 |
|
|
0 commit comments