-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanage.php
203 lines (171 loc) · 5.96 KB
/
manage.php
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/**
* Perform remote management for a website.
*
* Date: Auguest 2020
*
* @author Duncan Sutter
* @author Samantha Tripp
* @author Michael Milette
* @license MIT https://opensource.org/licenses/MIT
*
* This script is the main entry point for remote management.
*
* Coding Guidelines:
* Please follow the PHP Standards Recommendations at https://www.php-fig.org/psr/
*/
// Use the composer PSR-4 autoloader.
$loader = require '/opt/app-root/src/vendor/autoload.php';
$loader->addPsr4('RemoteManage\\', __DIR__ . '/src/');
require_once "helpers.php";
use RemoteManage\Log;
use RemoteManage\S3Cmd;
use RemoteManage\DiskSpace;
use RemoteManage\Drush;
// Set timeout to 3 hours.
set_time_limit(10800);
// Use local timezone
date_default_timezone_set("America/Toronto");
// Long form of valid parameters.
$parameters = ['format::', 'help', 'log-stderr', 'verbose'];
// Get command line options.
$params = getopt('f::hv', $parameters, $opIndex);
// Get the operation and optional args.
$operation = isset($argv[$opIndex]) ? $argv[$opIndex] : '';
$opArgs = isset($argv[$opIndex + 1]) ? array_slice($argv, $opIndex + 1) : [];
// Set boolean options (true or false)
$option['help'] = isset($params['help']) || isset($params['h']);
$option['verbose'] = isset($params['verbose']) || isset($params['v']);
$option['log-stderr'] = isset($params['log-stderr']);
// Set options that may have values. Long options take precedence over short options.
// If this syntax is new to you, search for "php null coalescing operator".
if (!$option['format'] = $params['format'] ?? '') {
$option['format'] = $params['f'] ?? '';
}
// Handle help.
if (empty($operation) || $operation == 'help' || $option['help']) {
fwrite(STDERR, file_get_contents(__DIR__ . '/help.md'));
exit(1);
}
Log::$debugMode = !empty($option['verbose']);
Log::$logStderr = !empty($option['log-stderr']);
// Load .env file which may accompany this package.
if (($env = @file(__DIR__ . '/.env')) !== false) {
foreach ($env as $e) {
if (!empty($e = trim($e))) {
putenv($e);
}
}
}
// Start timer.
Log::stopWatch();
// Get a site object. This will determine the type of site.
$site = getSite();
Log::msg('Site type is: ' . $site->siteType);
Log::msg('Performing ' . $operation . ' operation.');
// Get the application name from the environment
if (empty($site->appEnv = getenv('APP_NAME'))) {
Log::exitError('APP_NAME is undefined.');
}
// Get the requested operation and dispatch.
switch ($operation) {
case 'backup':
$success = $site->backup();
break;
case 'restore':
$filename = array_shift($opArgs);
$exclude = array_shift($opArgs) == '--exclude' ? " --exclude '" . basename(array_shift($opArgs)) . "'" : '';
$success = $site->restore($filename, $exclude);
if (function_exists('opcache_reset')) {
opcache_reset(); // Clear php Opcache in case files changed during restore.
}
break;
case 'download':
$filename = array_shift($opArgs);
// Check to make sure we have a filename.
if (empty($filename)) {
$success = false;
Log::error('Missing filename.');
} else {
$s3 = new S3Cmd();
$success = $s3->getFile($filename, '.' . strstr($filename, '/'));
}
break;
case 'delete':
$appname = array_shift($opArgs);
if ($appname != $site->appEnv) {
Log::exitError('Invalid or missing app-name. Are you sure that you are on the right server?');
}
$site->dropTables(); // No status check because it might have already been empty.
$success = $site->deleteFiles();
break;
case 'cr':
$success = $site->cacheRebuild();
break;
case 'updb':
$success = $site->updateDatabase();
break;
case 'pmlist':
$success = $site->pmList();
break;
case 'app-name':
Log::data('APP_NAME', $site->appEnv);
$success = true;
break;
case 's3list':
$filter = array_shift($opArgs);
$s3 = new S3Cmd();
$success = $s3->getList($filter);
break;
case 'space': // Disk space information.
$success = true;
$format = 'human';
if ($option['format']) {
if (!in_array($option['format'], ['bytes', 'human'])) {
Log::exitError('Invalid format: ' . $option['format']);
}
$format = $option['format'];
}
$volumes = [];
foreach ($site->volumes as $volume) {
// Get disk information.
$disk = new DiskSpace($volume, $format);
// If invalid volume specified, disk total space will be FALSE.
$success = $disk->total !== false;
if ($success === false) {
break;
}
$volumes[] = [
'volume' => $volume,
'totalspace' => $disk->total,
'freespace' => $disk->free,
'usedspace' => $disk->used,
'usedpercentage' => $disk->percentage
];
}
Log::data('volumes', $volumes);
break;
case 'maint': // Set site in production mode.
$mode = strtolower(array_shift($opArgs));
switch ($mode) {
case 'on':
$success = $site->maintMode(true);
break;
case 'off':
$success = $site->maintMode(false);
break;
default: // If no parameter, or an invalid parameter was specified, just return status.
$success = true;
}
$inMaintMode = $site->getMaintMode();
Log::data('maintMode', $inMaintMode ? 'on' : 'off');
$site->cleanup();
break;
default:
Log::exitError('The operation "' . $operation . '" is invalid.');
}
// Stop timer and record elapsed time.
Log::stopWatch('stop');
Log::printData($success ? 'ok' : 'error');
// Complete execution.
exit($success ? 0 : 1);