-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpusher.php
377 lines (352 loc) · 11.9 KB
/
pusher.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
$time_start = microtime(true);
ini_set("date.timezone", "Europe/Amsterdam"); // https://www.youtube.com/watch?v=rzOzUHYwPms
$runDate = date("YmdHis");
$options = parseParameters(
array (
"help",
"site:",
"commit::",
"backup",
"branch::",
"deploy",
"prepare",
"directory::"
)
);
if (isset($options['directory']))
$runDate = $options['directory'];
echo "\n";
echo "Batch deployment tool\n";
echo "Assigned directory: {$runDate}\n";
echo "\n";
echo "===========================\n";
if (count($options) == 0 or isset($options['help']) or (!isset($options['site'])))
{
echo "Usage: pusher.php --deploy <action>\n";
echo "Arguments:\n";
echo " --site <sitename>: Select specific site\n";
echo "Optional:\n";
echo " --branch <branchname>: Use selected git branchname\n";
echo " --backup: Create a backup \n";
echo " --commit <git id>: Use selected commitid as HEAD\n";
echo " --deploy: deployment\n";
echo " --directory: Use assigned deployment directory\n";
echo " --prepare: generate package for deployment\n";
echo " --help: Display this helptext\n";
die();
}
require_once('pusher.config.php');
require_once('housekeeping.php');
if (isset($options['site']))
{
if (!isset($Configuration['sites'][$options['site']]))
{
echo 'Unknown site specified.';
die();
}
echo "Site ".$options['site']." selected\n";
// Do checks
$SiteName = $options['site'];
$SiteConfig = $Configuration['sites'][$options['site']];
if ($SiteConfig['deploymethod'] == 'FTP' and !isset ($SiteConfig['FTP']))
{
echo 'Error: Deploy method FTP is used but no FTP configuration found, exiting.';
die();
}
if ($SiteConfig['backup'] or isset($options['backup']))
{
$backupRootDir = $Configuration['general']['backupdir'].$SiteName.'/'.$runDate."/";
echo "Creating backup...\n";
echo " Backup location: ".$backupRootDir."\n";
mkdir($backupRootDir, 775, true);
foreach ($SiteConfig['backuppaths'] as $remote => $local)
{
if ($SiteConfig['deploymethod'] == 'FTP' and isset ($SiteConfig['FTP']))
{
$localPath = $backupRootDir.$local;
$remotePath = $remote;
$excludePath = '';
if (isset($SiteConfig['excludebackuppaths']))
$excludePath = " -X ".implode(",", $SiteConfig['excludebackuppaths']);
if (strlen($excludePath > 0))
$excludePath = substr($excludePath, 0, -1);
echo "\t".$remotePath."... ";
exec($Configuration['general']['wget']." -q -N -r -l inf {$excludePath} -nH --ftp-user={$SiteConfig['FTP']['username']} --ftp-password={$SiteConfig['FTP']['password']} -P \"{$localPath}\" ftp://{$SiteConfig['FTP']['hostname']}:{$SiteConfig['FTP']['port']}{$remotePath}");
echo " Done!\n";
}
elseif ($SiteConfig['deploymethod'] == 'SSH' and isset ($SiteConfig['SSH']))
{
$localPath = $backupRootDir.$local;
$remotePath = $remote;
$excludePath = '';
if (isset($SiteConfig['excludebackuppaths']))
{
foreach ($SiteConfig['excludebackuppaths'] as $path)
{
$excludePath = " --exclude \"{$path}\"";
}
}
echo "\t".$remotePath."... ";
exec($Configuration['general']['rsync']." --rsh \"ssh -p {$SiteConfig['FTP']['port']}\" --recursive --archive {$excludePath} {$SiteConfig['FTP']['username']}@{$SiteConfig['FTP']['hostname']}:{$remotePath} {$localPath}");
echo "Done!\n";
}
else {
echo 'Error: Deploy method '.$SiteConfig['deploymethod'].' is but we don\'t have backup instructions for that, exiting.';
die();
}
}
echo "Backup done!\n";
echo currenttime()." ===========================\n";
sleep(2);
}
if (isset($options['deploy']) or isset($options['prepare']))
{
echo "Preparing source {$options['site']} from {$SiteConfig['deploysource']}\n";
echo "Preparing source data..";
$deployDirectory = $Configuration['general']['deploydir'].$SiteName.'/'.$runDate."/";
@mkdir($deployDirectory, 775, true);
if ($SiteConfig['deploysource'] == "folder")
{
copyfolder($SiteConfig['sourcefolder'], $deployDirectory);
}
elseif ($SiteConfig['deploysource'] == "git")
{
$gitBranch = '';
if (isset($options['branch']))
$SiteConfig['gitbranch'] = $options['branch'];
if (isset($SiteConfig['gitbranch']) and strlen($SiteConfig['gitbranch']) > 0)
$gitBranch = ' --branch '.$options['branch'].' ';
echo "Pulling from git... ";
exec ($Configuration['general']['gitexecutable']." clone {$gitBranch} {$SiteConfig['giturl']} \"{$deployDirectory}\"");
if (isset($options['commit']))
{
$ourDir = __DIR__;
chdir($deployDirectory);
echo "Applying specific commit... ";
exec("{$Configuration['general']['gitexecutable']} checkout {$options['commit']}");
chdir($ourDir);
}
}
else {
echo 'Error: Deploy source '.$SiteConfig['deploysource'].' is not available, exiting.';
die();
}
sleep(10);
echo " Done!\n";
echo currenttime()." ===========================\n";
sleep(2);
echo "Housekeeping..\n";
deleteDirectory($deployDirectory.'.git');
rdir_cleanup($deployDirectory, true);
echo currenttime()." ===========================\n";
sleep(2);
}
if (isset($options['deploy']))
{
if (isset($SiteConfig['excludecopypaths']) and count($SiteConfig['excludecopypaths']) > 0)
{
echo "Processing exclude paths:\n";
foreach ($SiteConfig['excludecopypaths'] as $exclude)
{
if (substr($exclude, 0, 1) == "/")
{ // Remove first char slash
$exclude = substr($exclude, 1);
}
if (substr($exclude, -1) == "/") // Directory
{
echo "\tDirectory /".$exclude."\n";
deleteDirectory($deployDirectory.$exclude);
}
else {
echo "\tFile /".$exclude."\n";
unlink($deployDirectory.$exclude);
}
}
echo " Done!\n";
echo currenttime()." ===========================\n";
sleep(2);
}
if (isset($SiteConfig['replacefiles']) AND count($SiteConfig['replacefiles']) > 0)
{
echo "Replacing files:\n";
foreach ($SiteConfig['replacefiles'] as $source => $dest)
{
if (substr($source, 0, 1) == "/")
{ // Remove first char slash
$source = substr($source, 1);
}
if (substr($dest, 0, 1) == "/")
{ // Remove first char slash
$dest = substr($dest, 1);
}
if (substr($exclude, -1) == "/") // Directory
{
echo "\tDirectory /".$source."\n";
echo "\t\t => /".$dest."\n";
copy(__DIR__.'/'.$source, $deployDirectory . $dest);
}
else {
echo "\tFile /".$source."\n";
echo "\t\t => /".$dest."\n";
copy(__DIR__.'/'.$source, $deployDirectory . $dest);
}
}
echo " Done!\n";
echo currenttime()." ===========================\n";
sleep(2);
}
if (isset($SiteConfig['replacelines']) AND count($SiteConfig['replacelines']) > 0)
{
echo "Editing files (lines):\n";
foreach ($SiteConfig['replacelines'] as $file => $edits)
{
if (substr($file, 0, 1) == "/")
{ // Remove first char slash
$file = substr($file, 1);
}
echo "\tFile /".$file."\n";
$fileContents = file_get_contents( $deployDirectory . $file );
if (!$fileContents)
{
echo 'ERROR@replacelines: Cannot load '.$file;
die();
}
$fileSplit = explode("\n", $fileContents);
foreach ($edits as $lineNumber => $newText)
{
echo "\t{$lineNumber}. {$newText}\n";
$fileSplit [ $lineNumber - 1 ] = $newText;
}
$fileContents = implode("\n", $fileSplit);
file_put_contents($deployDirectory . $file, $fileContents);
}
echo " Done!\n";
echo currenttime()." ===========================\n";
sleep(2);
}
echo "Transfering files...\n";
foreach ($SiteConfig['copypaths'] as $local => $remote)
{
if ($SiteConfig['deploymethod'] == 'FTP' and isset ($SiteConfig['FTP']))
{
if (substr($local, 0, 1) == "/")
{ // Remove first char slash
$local = substr($local, 1);
}
if (substr($local, -1) == "/")
{ // Directory? add a *
$local = $local.'*';
}
$deployDirectory = $Configuration['general']['deploydir'].$SiteName.'/'.$runDate."/";
$localPath = $deployDirectory.$local;
$remotePath = $remote;
echo "\t/".$local."... ";
exec($Configuration['general']['ncftpput']." -u {$SiteConfig['FTP']['username']} -p {$SiteConfig['FTP']['password']} -P {$SiteConfig['FTP']['port']} -R -F {$SiteConfig['FTP']['hostname']} \"{$remotePath}\" \"{$localPath}\"");
echo "\n";
}
elseif ($SiteConfig['deploymethod'] == 'SSH' and isset ($SiteConfig['SSH']))
{
if (substr($local, 0, 1) == "/")
{ // Remove first char slash
$local = substr($local, 1);
}
if (substr($local, -1) == "/")
{ // Directory? add a *
$local = $local.'*';
}
$deployDirectory = $Configuration['general']['deploydir'].$SiteName.'/'.$runDate."/";
$localPath = $deployDirectory.$local;
$remotePath = $remote;
echo "\t/".$local."... ";
exec($Configuration['general']['rsync']." --rsh \"ssh -p {$SiteConfig['FTP']['port']}\" --recursive --archive {$localPath} {$SiteConfig['FTP']['username']}@{$SiteConfig['FTP']['hostname']}:{$remotePath}");
echo "\n";
}
else {
echo 'Error: Deploy method '.$SiteConfig['deploymethod'].' is but we don\'t have deploy instructions for that, exiting.';
die();
}
}
echo "Transfering files Done!\n";
echo currenttime()." ===========================\n";
sleep(2);
}
echo currenttime().": All tasks are finished!\n\n";
}
function copyfolder($source, $dest)
{
if(is_dir($source)) {
$dir_handle=opendir($source);
while($file=readdir($dir_handle)){
if($file!="." && $file!=".." && $file!=".git" && $file!=".svn" && $file!=".gitignore"){
if(is_dir($source."/".$file)){
if(!is_dir($dest."/".$file)){
mkdir($dest."/".$file);
}
copyfolder($source."/".$file, $dest."/".$file);
} else {
copy($source."/".$file, $dest."/".$file);
}
}
}
closedir($dir_handle);
} else {
copy($source, $dest);
}
}
function deleteDirectory($dirPath) {
if (is_dir($dirPath)) {
$objects = scandir($dirPath);
foreach ($objects as $object) {
if ($object != "." && $object !="..") {
if (filetype($dirPath . DIRECTORY_SEPARATOR . $object) == "dir") {
deleteDirectory($dirPath . DIRECTORY_SEPARATOR . $object);
} else {
unlink($dirPath . DIRECTORY_SEPARATOR . $object);
}
}
}
reset($objects);
rmdir($dirPath);
}
}
function parseParameters($noopt = array()) {
$result = array();
$params = $GLOBALS['argv'];
// could use getopt() here (since PHP 5.3.0), but it doesn't work relyingly
reset($params);
while (list($tmp, $p) = each($params)) {
if ($p{0} == '-') {
$pname = substr($p, 1);
$value = true;
if ($pname{0} == '-') {
// long-opt (--<param>)
$pname = substr($pname, 1);
if (strpos($p, '=') !== false) {
// value specified inline (--<param>=<value>)
list($pname, $value) = explode('=', substr($p, 2), 2);
}
}
// check if next parameter is a descriptor or a value
$nextparm = current($params);
if (!in_array($pname, $noopt) && $value === true && $nextparm !== false && $nextparm{0} != '-') list($tmp, $value) = each($params);
$result[$pname] = $value;
} else {
// param doesn't belong to any option
$result[] = $p;
}
}
return $result;
}
function currenttime()
{
global $time_start;
$seconds = microtime(true) - $time_start;
$minutes = 0;
while ($seconds >=60 )
{
$seconds = $seconds - 60;
$minutes++;
}
return sprintf("%1d:%2$02d", $minutes, $seconds);
}