summaryrefslogtreecommitdiffstats
path: root/src/etc/phpshellsessions/gitsync
blob: 26874c75baf349455347cc75e10e418cd71686c0 (plain)
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * gitsync
 *
 * part of pfSense (https://www.pfsense.org)
 * Copyright (c) 2010-2012 Erik Fonnesbeck
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

require_once("globals.inc");
require_once("filter.inc");
require_once("shaper.inc");
require_once("rrd.inc");
require_once("pfsense-utils.inc");

$GIT_PKG = "git"; // Either "git" or the full package URL
$GIT_BIN= "/usr/local/bin/git";
$GIT_REPO = "git://github.com/pfsense/pfsense.git";
$DEFAULT_BRANCH = "master";
$CODIR =  "/root/pfsense";
$GITSYNC_MERGE = "/root/.gitsync_merge";

/* NOTE: Set branches here */
$branches = array(
	"master" => "2.4 development branch",
	"build_commit" => "The commit originally used to build the image"
);

global $g;
global $argv;
global $command_split;

if (is_array($command_split)) {
	$temp_args = array_slice($command_split, 2);
} else {
	$temp_args = array_slice($argv, 3);
}

$valid_args = array(
	"--minimal" => "\tPerform a copy of only the updated files.\n" .
	    "\tNot recommended if the system has files modified by any method other\n" .
	    "\tthan gitsync.\n",
	"--diff" => "\tPerform a copy of only the different and missing files.\n" .
	    "\tRecommended for SSD if system has files modified by any method other\n" .
	    "\tthan gitsync.\n",
	"--show-files" => "\tShow the updated, different and missing files.\n" .
	    "\t(when used with --minimal and --diff options)\n",
	"--show-command" => "\tShow the constructed command.\n",
	"--dry-run" => "\tDry-run only.  No files copied.\n",
	"--help" => "\tDisplay this help list.\n"
	);
$args = array();
$arg_count = 0;
while (!empty($temp_args)) {
	$arg = array_shift($temp_args);
	if ($arg[0] == '-') {
		switch ($arg) {
			case "--help":
				echo "Usage: playback gitsync [options] [[repository] <branch>]\nOptions:\n";
				foreach ($valid_args as $arg_name => $arg_desc) {
					echo $arg_name . "\n" . $arg_desc;
				}
				exit;
			case "--upgrading":
				// Disables all interactive functions and neither PHP
				// nor the web GUI will be killed or restarted.
				$upgrading = true;
			case (isset($valid_args[$arg])):
				$args[$arg] = true;
				break;
			default:
				echo "Invalid option: {$arg}\nUse --help for usage information.\n";
				exit;
		}
	} else {
		$args[$arg_count++] = $arg;
	}
}

if (!function_exists('post_cvssync_commands')) {
function post_cvssync_commands() {
	echo "===> Removing FAST-CGI temporary files...\n";
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");

	exec("rm -rf /tmp/xcache/* 2>/dev/null");

	echo "===> Upgrading configuration (if needed)...\n";
	convert_config();

	echo "===> Configuring filter...";
	exec("/etc/rc.filter_configure_sync");
	exec("pfctl -f /tmp/rules.debug");
	echo "\n";

	if (file_exists("/etc/rc.php_ini_setup")) {
		echo "===> Running /etc/rc.php_ini_setup...";
		exec("/etc/rc.php_ini_setup >/dev/null 2>&1");
		echo "\n";
	}

	/* lock down console if necessary */
	echo "===> Locking down the console if needed...\n";
	reload_ttys();

	echo "===> Signaling PHP and nginx restart...";
	$fd = fopen("/tmp/restart_nginx", "w");
	fwrite($fd, "#!/bin/sh\n");
	fwrite($fd, "sleep 5\n");
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
	fclose($fd);
	mwexec_bg("sh /tmp/restart_nginx");
	echo "\n";
}
}

if (!function_exists('isUrl')) {
function isUrl($url = "") {
	if ($url) {
		if (strstr($url, "rcs.pfsense.org") or
			strstr($url, "mainline") or
			strstr($url, ".git") or
			strstr($url, "git://")) {
			return true;
		}
	}
	return false;
}
}

if (!function_exists('run_cmds')) {
function run_cmds($cmds) {
	global $debug;
	foreach ($cmds as $cmd) {
		if ($debug) {
			echo "Running $cmd";
		}
		exec($cmd);
	}
}
}

unlink_if_exists("/tmp/config.cache");

if (!file_exists($GIT_BIN)) {
	require_once("pkg-utils.inc");

	echo "Cannot find git, installing...\n";
	if (!pkg_call('install -y -q git')) {
		echo "\nERROR: Unable to install git pkg.\n";
		return;
	}
}

# Remove mainline if exists (older)
if (is_dir("/root/pfsense/mainline")) {
	exec("rm -rf /root/pfsense/mainline");
}

# Remove RELENG_1_2 if exists (older)
if (is_dir("/root/pfsense/RELENG_1_2")) {
	exec("rm -rf /root/pfsense/RELENG_1_2");
}

# Remove HEAD if exists (older)
if (is_dir("/root/pfsense/HEAD")) {
	exec("rm -rf /root/pfsense/HEAD");
}

if (file_exists("/root/cvssync_backup.tgz")) {
	$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
	$tmp = array("RESTORE" => "Restores prior CVSSync backup data performed at {$backup_date}");
	$branches = array_merge($branches, $tmp);
}

if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url", $output_str, $ret);
	if (is_array($output_str) && !empty($output_str[0])) {
		$GIT_REPO = $output_str[0];
	}
	unset($output_str);
}

if (!$args[0] && !$upgrading) {
	echo "\nCurrent repository is $GIT_REPO\n";
	echo "\nPlease select which branch you would like to sync against:\n\n";
	foreach ($branches as $branchname => $branchdesc) {
		echo "{$branchname} \t {$branchdesc}\n";
	}
	echo "\nOr alternatively you may enter a custom RCS branch URL (Git or HTTP).\n\n";
	$branch = readline("> ");
	echo "\n";
} else {
	$branch = $args[0];
}

if ($args[1] == "NOBACKUP") {
	$nobackup = true;
} else {
	$nobackup = false;
}

// If the repository has been fetched before, build a list of its branches.
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch -r", $branch_list, $ret);
	if ($ret == 0 && is_array($branch_list)) {
		foreach ($branch_list as $branch_item) {
			$branch_item = substr(strrchr($branch_item, "/"), 1);
			if (!isset($branches[$branch_item])) {
				$branches[$branch_item] = " ";
			}
		}
	}
}

$found = false;
foreach ($branches as $branchname => $branchdesc) {
	if ($branchname == $branch) {
		$found = true;
		break;
	}
}
if (!$found) {
	if (isURL($branch) && !$upgrading) {
		if ($args[1]) {
			$GIT_REPO = $branch;
			$branch = $args[1];
			$found = true;
		} else {
			echo "\n";
			echo "NOTE: $branch was not found.\n\n";
			$command = readline("Is this a custom GIT URL? [y]? ");
			if (strtolower($command) == "y" or $command == "") {
				$GIT_REPO = $branch;
				$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
				if ($command == "") {
					$branch = $DEFAULT_BRANCH;
				}
				if ($command) {
					$branch = $command;
				}
				$found = true;
			}
		}
	}
	if (!$found) {
		echo "\nNo valid branch found.  Exiting.\n\n";
		exit;
	}
}

$merge_repos = array();
if (file_exists($GITSYNC_MERGE)) {
	$gitsync_merges = file($GITSYNC_MERGE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	if (!empty($gitsync_merges) && is_array($gitsync_merges)) {
		echo "\n===> Automatic merge list read from ${GITSYNC_MERGE}\n";
		foreach ($gitsync_merges as $merge_line_num => $merge_line) {
			$merge_comments = explode("#", trim($merge_line));
			if (empty($merge_comments[0])) {
				continue;
			}

			$merge_line = explode(" ", trim($merge_comments[0]));
			if (count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
				echo "Expected parameters:  repository_url branch\n";
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
				echo "Aborting automatic merge.\n\n";
				$merge_repos = array();
				break;
			}
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
		}
	}
}
if (!$args[0] && !$upgrading) {
	do {
		echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
		$merge_repo = readline("> ");
		if (!empty($merge_repo)) {
			$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
			if ($merge_branch == "") {
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
			} else if ($merge_branch) {
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
			}
		}
	} while (!empty($merge_repo));
}

if ($branch == "RESTORE") {
	if (!file_exists("/root/cvssync_backup.tgz")) {
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
		exit();
	}
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
	post_cvssync_commands();
	exit();
} else {
	$nobackup = true; // do not backup embedded
}

if ($nobackup == false) {
	echo "===> Backing up current pfSense information...\n";
	echo "===> Please wait... ";
	exec("tar czPf /root/cvssync_backup.tgz --exclude /root --exclude /dev --exclude /tmp --exclude /var/run --exclude /var/empty /");
	$size = filesize("/root/cvssync_backup.tgz");
	echo "{$size} bytes.\n\n";
	sleep(5);
}

echo "===> Checking out $branch\n";

// Git commands for resetting to the specified branch
if ($branch == "build_commit") {
	$git_cmd = array(
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " 2>/dev/null",
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
	);
} else {
	$git_cmd = array(
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg("origin/{$branch}")
	);
}

// Git 'er done!
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
	echo "===> Fetching updates...\n";
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url " . escapeshellarg($GIT_REPO));
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} fetch");
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} clean -f -x -d");
	run_cmds($git_cmd);
} else {
	exec("mkdir -p $CODIR/pfSenseGITREPO");
	echo "Executing cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone $GIT_REPO pfSenseGITREPO\n";
	exec("cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
	if (is_dir("$CODIR/pfSenseGITREPO/pfSense")) {
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
	}
	if (is_dir("$CODIR/pfSenseGITREPO/mainline")) {
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
	}
	run_cmds($git_cmd);
}

foreach ($merge_repos as $merge_repo) {
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
	unset($output_str);
	if ($ret <> 0) {
		echo "\nMerge failed.  Aborting sync.\n\n";
		run_cmds($git_cmd);
		exit;
	}
}

$updated_files_array[1] = array();
if (isset($args["--minimal"])) {
	if (file_exists("/etc/version.gitsync")) {
		$old_revision = trim(file_get_contents("/etc/version.gitsync"));
	} else if (file_exists("/etc/version.lastcommit")) {
		$old_revision = trim(file_get_contents("/etc/version.lastcommit"));
	}

	# Find updated files.
	$updated_files = shell_exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} diff --name-only --relative=src " . escapeshellarg($old_revision));

	# Get the path of each updated file.
	preg_match_all('@(?:^(.+?)$)@sim', $updated_files, $updated_files_array, PREG_PATTERN_ORDER);

	if(isset($args["--show-files"])) {
		echo "===> Updated Files: \n";
		print_r($updated_files_array[1]);
	}
}

// Save new commit ID for later minimal file copies
if(!isset($args["--dry-run"])) {
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} rev-parse -q --verify HEAD > /etc/version.gitsync");
}

// Remove files that we do not want to overwrite the system with
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/crontab");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/master.passwd");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/passwd");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/ttys");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/group");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/platform");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/device.hints");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.conf");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.rc");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/syslog.conf");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.shrc");
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.tcshrc");
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/conf*");
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/cf 2>/dev/null");
@chmod("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/tmp", 01777);

$different_files_array[1] = array();
$missing_files_array[1] = array();
if(isset($args["--diff"])) {
	# Find different and missing files.
	$different_missing_files = shell_exec("/usr/bin/diff -qr / $CODIR/pfSenseGITREPO/pfSenseGITREPO/src/ | /usr/bin/grep -E '^(Files .*? and $CODIR/pfSenseGITREPO/pfSenseGITREPO/src/.*? differ)|(Only in $CODIR/pfSenseGITREPO/pfSenseGITREPO/src/)'");

	# Get the path of each different or missing file.
	preg_match_all('@(?:^Files .*? and '.$CODIR.'/pfSenseGITREPO/pfSenseGITREPO/src/(.+?) differ.*?$)@sim', $different_missing_files, $different_files_array, PREG_PATTERN_ORDER);
	preg_match_all('@(?:^Only in '.$CODIR.'/pfSenseGITREPO/pfSenseGITREPO/src/(.+?)$)@sim', $different_missing_files, $missing_files_array, PREG_PATTERN_ORDER);

	# Deal with diff's output format of missing files (path: missing_file).
	foreach ($missing_files_array[1] as $key => $file) {

		# Most of the time there will be only the one ': ' injected by diff output.  So global replace with dir delimiter (/) is fine.
		$tmp = str_replace(": ", "/", $file, $count);
		if ($count == 1)
			$file = ltrim($tmp, "/");

		# For the very rare case a path component (dir or file) contains ': ' as well, then need to find and replace only the ': ' injected by diff output.
		else {
			$tmp = $file;
			do {
				$pos = strrpos($tmp, ": ");
				if ($pos) {
					$tmp = substr($tmp, 0, $pos);
					$res = is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO/src/$tmp/");
				}
			} while (!$res && $pos);

			if ($res)
				$file = ltrim($tmp . "/" . substr($file, $pos+2), "/");
		}

		$missing_files_array[1][$key] = $file;
	}

	if(isset($args["--show-files"])) {
		echo "===> Different Files: \n";
		print_r($different_files_array[1]);
		echo "===> Missing Files: \n";
		print_r($missing_files_array[1]);
	}
}

# Files to be copied.
if(isset($args["--minimal"]) || isset($args["--diff"])) {
	$files_to_copy_array = array_merge($updated_files_array[1], $different_files_array[1], $missing_files_array[1]);
	$files_to_copy_array = array_unique($files_to_copy_array);

	unset($updated_files_array, $different_files_array, $missing_files_array);

	# Convert the list from an array to a space separated quoted string.  Quoted for white space file name support.
	if (count($files_to_copy_array) > 0) {	# Leave the string empty/unset if there is nothing to copy.
		$files_to_copy = '"' . implode('" "', $files_to_copy_array) . '"';
	}
	$qty_files_to_copy = count($files_to_copy_array);
	unset($files_to_copy_array);
} else {
	$files_to_copy = '.';
	$qty_files_to_copy = chr(8);
}

$tar_options = '-C ./src';

echo "===> Installing $qty_files_to_copy new files...\n";

$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$tar_options} {$files_to_copy} | (cd / ; tar -Uxpf -)";

if (!empty($files_to_copy)) {
	if(isset($args["--show-command"])) {
		echo "===> Command: \n$command\n";
	}
	if(!isset($args["--dry-run"])) {
		exec($command);
	}
} else {
	echo "Already up-to-date.\n";
	$upgrading = true;
}

// Reset the repository to restore the deleted files
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard >/dev/null 2>/dev/null");

// Remove obsolete files
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
foreach ($files_to_remove as $file_to_remove) {
	if (file_exists($file_to_remove)) {
		exec("/bin/rm -f $file_to_remove");
	}
}

if (!$upgrading) {
	post_cvssync_commands();
}

echo "===> Checkout complete.\n";
echo "\n";
if (!$upgrading) {
	echo "Your system is now sync'd and PHP and nginx will be restarted in 5 seconds.\n\n";
} else {
	echo "Your system is now sync'd.\n\n";
}

OpenPOWER on IntegriCloud