Project

General

Profile

Download (17.2 KB) Statistics
| Branch: | Tag: | Revision:
1 ac24dc24 Renato Botelho
/*
2
 * gitsync
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2010-2012 Erik Fonnesbeck
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 ac24dc24 Renato Botelho
 * All rights reserved.
10
 *
11 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14 ac24dc24 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
16 ac24dc24 Renato Botelho
 *
17 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22 800351cc Scott Ullrich
 */
23
24 680bcb4d Scott Ullrich
require_once("globals.inc");
25 784a0768 sullrich
require_once("filter.inc");
26
require_once("shaper.inc");
27
require_once("rrd.inc");
28 3ffa8318 Renato Botelho
require_once("pfsense-utils.inc");
29 1bfcf6f1 Phil Davis
30 ae26412f Erik Fonnesbeck
$GIT_PKG = "git"; // Either "git" or the full package URL
31 4d4e9b06 Renato Botelho
$GIT_BIN= "/usr/local/bin/git";
32 d5280de6 Chris Buechler
$GIT_REPO = "git://github.com/pfsense/pfsense.git";
33 26b89905 Erik Fonnesbeck
$DEFAULT_BRANCH = "master";
34 da1d0165 Marcello Coutinho
$CODIR =  "/root/pfsense";
35 26b89905 Erik Fonnesbeck
$GITSYNC_MERGE = "/root/.gitsync_merge";
36
37
/* NOTE: Set branches here */
38
$branches = array(
39 4a3c0547 Renato Botelho
	"master" => "2.5.0 development branch",
40 d6f8f730 Renato Botelho
	"build_commit" => "The commit originally used to build the image"
41 26b89905 Erik Fonnesbeck
);
42 3f179a55 Scott Ullrich
43 153e3cb5 jim-p
global $g;
44 81fa1cc3 Scott Ullrich
global $argv;
45
global $command_split;
46 3f179a55 Scott Ullrich
47 1bfcf6f1 Phil Davis
if (is_array($command_split)) {
48 00c0720a Erik Fonnesbeck
	$temp_args = array_slice($command_split, 2);
49 1bfcf6f1 Phil Davis
} else {
50 00c0720a Erik Fonnesbeck
	$temp_args = array_slice($argv, 3);
51 1bfcf6f1 Phil Davis
}
52 00c0720a Erik Fonnesbeck
53
$valid_args = array(
54 b19c8033 NOYB
	"--minimal" => "\tPerform a copy of only the updated files.\n" .
55 1b875052 Renato Botelho
	    "\tNot recommended if the system has files modified by any method other\n" .
56
	    "\tthan gitsync.\n",
57 b19c8033 NOYB
	"--diff" => "\tPerform a copy of only the different and missing files.\n" .
58 d20f76bc NOYB
	    "\tRecommended for SSD if system has files modified by any method other\n" .
59
	    "\tthan gitsync.\n",
60 ef30fa51 NOYB
	"--show-files" => "\tShow the updated, different and missing files.\n" .
61 b19c8033 NOYB
	    "\t(when used with --minimal and --diff options)\n",
62 ef30fa51 NOYB
	"--show-command" => "\tShow the constructed command.\n",
63 d20f76bc NOYB
	"--dry-run" => "\tDry-run only.  No files copied.\n",
64 00c0720a Erik Fonnesbeck
	"--help" => "\tDisplay this help list.\n"
65
	);
66
$args = array();
67
$arg_count = 0;
68 1bfcf6f1 Phil Davis
while (!empty($temp_args)) {
69 00c0720a Erik Fonnesbeck
	$arg = array_shift($temp_args);
70 1bfcf6f1 Phil Davis
	if ($arg[0] == '-') {
71
		switch ($arg) {
72 00c0720a Erik Fonnesbeck
			case "--help":
73
				echo "Usage: playback gitsync [options] [[repository] <branch>]\nOptions:\n";
74 4e322e2c Phil Davis
				foreach ($valid_args as $arg_name => $arg_desc) {
75 00c0720a Erik Fonnesbeck
					echo $arg_name . "\n" . $arg_desc;
76 1bfcf6f1 Phil Davis
				}
77 00c0720a Erik Fonnesbeck
				exit;
78
			case "--upgrading":
79
				// Disables all interactive functions and neither PHP
80
				// nor the web GUI will be killed or restarted.
81
				$upgrading = true;
82
			case (isset($valid_args[$arg])):
83
				$args[$arg] = true;
84
				break;
85
			default:
86
				echo "Invalid option: {$arg}\nUse --help for usage information.\n";
87
				exit;
88
		}
89
	} else {
90
		$args[$arg_count++] = $arg;
91
	}
92
}
93 f0ce6758 Erik Fonnesbeck
94 d20f76bc NOYB
if (!function_exists('post_cvssync_commands')) {
95
function post_cvssync_commands() {
96
	echo "===> Removing FAST-CGI temporary files...\n";
97
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
98
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
99
100
	exec("rm -rf /tmp/xcache/* 2>/dev/null");
101
102
	echo "===> Upgrading configuration (if needed)...\n";
103
	convert_config();
104
105
	echo "===> Configuring filter...";
106
	exec("/etc/rc.filter_configure_sync");
107
	exec("pfctl -f /tmp/rules.debug");
108
	echo "\n";
109
110
	if (file_exists("/etc/rc.php_ini_setup")) {
111
		echo "===> Running /etc/rc.php_ini_setup...";
112
		exec("/etc/rc.php_ini_setup >/dev/null 2>&1");
113
		echo "\n";
114
	}
115
116
	/* lock down console if necessary */
117
	echo "===> Locking down the console if needed...\n";
118
	reload_ttys();
119
120
	echo "===> Signaling PHP and nginx restart...";
121
	$fd = fopen("/tmp/restart_nginx", "w");
122
	fwrite($fd, "#!/bin/sh\n");
123
	fwrite($fd, "sleep 5\n");
124
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
125
	fclose($fd);
126
	mwexec_bg("sh /tmp/restart_nginx");
127
	echo "\n";
128
}
129
}
130
131
if (!function_exists('isUrl')) {
132
function isUrl($url = "") {
133
	if ($url) {
134
		if (strstr($url, "rcs.pfsense.org") or
135
			strstr($url, "mainline") or
136
			strstr($url, ".git") or
137
			strstr($url, "git://")) {
138
			return true;
139
		}
140
	}
141
	return false;
142
}
143
}
144
145
if (!function_exists('run_cmds')) {
146
function run_cmds($cmds) {
147
	global $debug;
148
	foreach ($cmds as $cmd) {
149
		if ($debug) {
150
			echo "Running $cmd";
151
		}
152
		exec($cmd);
153
	}
154
}
155
}
156
157 784cca4d Scott Ullrich
unlink_if_exists("/tmp/config.cache");
158 800351cc Scott Ullrich
159 1bfcf6f1 Phil Davis
if (!file_exists($GIT_BIN)) {
160 c3ebb2f8 jim-p
	require_once("pkg-utils.inc");
161
162 4d4e9b06 Renato Botelho
	echo "Cannot find git, installing...\n";
163 c833432e Renato Botelho
	if (!pkg_call('install -y -q git')) {
164 4d4e9b06 Renato Botelho
		echo "\nERROR: Unable to install git pkg.\n";
165 c3ebb2f8 jim-p
		return;
166
	}
167 2b065943 Scott Ullrich
}
168
169 74fb85b8 Scott Ullrich
# Remove mainline if exists (older)
170 1bfcf6f1 Phil Davis
if (is_dir("/root/pfsense/mainline")) {
171 2ac39ce3 Scott Ullrich
	exec("rm -rf /root/pfsense/mainline");
172 1bfcf6f1 Phil Davis
}
173 2ac39ce3 Scott Ullrich
174 74fb85b8 Scott Ullrich
# Remove RELENG_1_2 if exists (older)
175 1bfcf6f1 Phil Davis
if (is_dir("/root/pfsense/RELENG_1_2")) {
176 74fb85b8 Scott Ullrich
	exec("rm -rf /root/pfsense/RELENG_1_2");
177 1bfcf6f1 Phil Davis
}
178 74fb85b8 Scott Ullrich
179
# Remove HEAD if exists (older)
180 1bfcf6f1 Phil Davis
if (is_dir("/root/pfsense/HEAD")) {
181 74fb85b8 Scott Ullrich
	exec("rm -rf /root/pfsense/HEAD");
182 1bfcf6f1 Phil Davis
}
183 74fb85b8 Scott Ullrich
184 1bfcf6f1 Phil Davis
if (file_exists("/root/cvssync_backup.tgz")) {
185 d56e44ba Scott Ullrich
	$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
186 81fa1cc3 Scott Ullrich
	$tmp = array("RESTORE" => "Restores prior CVSSync backup data performed at {$backup_date}");
187 76e74f41 Scott Ullrich
	$branches = array_merge($branches, $tmp);
188
}
189 537bc12e Scott Ullrich
190 1bfcf6f1 Phil Davis
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
191 da1d0165 Marcello Coutinho
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url", $output_str, $ret);
192 1bfcf6f1 Phil Davis
	if (is_array($output_str) && !empty($output_str[0])) {
193 932364e7 Erik Fonnesbeck
		$GIT_REPO = $output_str[0];
194 1bfcf6f1 Phil Davis
	}
195 932364e7 Erik Fonnesbeck
	unset($output_str);
196
}
197
198 1bfcf6f1 Phil Davis
if (!$args[0] && !$upgrading) {
199 00c0720a Erik Fonnesbeck
	echo "\nCurrent repository is $GIT_REPO\n";
200
	echo "\nPlease select which branch you would like to sync against:\n\n";
201 1bfcf6f1 Phil Davis
	foreach ($branches as $branchname => $branchdesc) {
202 00c0720a Erik Fonnesbeck
		echo "{$branchname} \t {$branchdesc}\n";
203 71845120 Scott Ullrich
	}
204 00c0720a Erik Fonnesbeck
	echo "\nOr alternatively you may enter a custom RCS branch URL (Git or HTTP).\n\n";
205
	$branch = readline("> ");
206
	echo "\n";
207
} else {
208
	$branch = $args[0];
209 96ebb0a6 Scott Ullrich
}
210 784cca4d Scott Ullrich
211 1bfcf6f1 Phil Davis
if ($args[1] == "NOBACKUP") {
212 680bcb4d Scott Ullrich
	$nobackup = true;
213 1bfcf6f1 Phil Davis
} else {
214 7a7371d1 Scott Ullrich
	$nobackup = false;
215 1bfcf6f1 Phil Davis
}
216 84b5f39b Scott Ullrich
217 fb95b324 Erik Fonnesbeck
// If the repository has been fetched before, build a list of its branches.
218 1bfcf6f1 Phil Davis
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
219 da1d0165 Marcello Coutinho
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch -r", $branch_list, $ret);
220 1bfcf6f1 Phil Davis
	if ($ret == 0 && is_array($branch_list)) {
221 fb95b324 Erik Fonnesbeck
		foreach ($branch_list as $branch_item) {
222
			$branch_item = substr(strrchr($branch_item, "/"), 1);
223 1bfcf6f1 Phil Davis
			if (!isset($branches[$branch_item])) {
224 fb95b324 Erik Fonnesbeck
				$branches[$branch_item] = " ";
225 1bfcf6f1 Phil Davis
			}
226 fb95b324 Erik Fonnesbeck
		}
227
	}
228
}
229
230 9fa76728 Scott Ullrich
$found = false;
231 1bfcf6f1 Phil Davis
foreach ($branches as $branchname => $branchdesc) {
232
	if ($branchname == $branch) {
233 9fa76728 Scott Ullrich
		$found = true;
234 4b4db04b Renato Botelho
		break;
235 1bfcf6f1 Phil Davis
	}
236 81fa1cc3 Scott Ullrich
}
237 1bfcf6f1 Phil Davis
if (!$found) {
238
	if (isURL($branch) && !$upgrading) {
239
		if ($args[1]) {
240 e623dd70 Scott Ullrich
			$GIT_REPO = $branch;
241 00c0720a Erik Fonnesbeck
			$branch = $args[1];
242 e623dd70 Scott Ullrich
			$found = true;
243 1bfcf6f1 Phil Davis
		} else {
244 2044cb61 Erik Fonnesbeck
			echo "\n";
245
			echo "NOTE: $branch was not found.\n\n";
246
			$command = readline("Is this a custom GIT URL? [y]? ");
247 1bfcf6f1 Phil Davis
			if (strtolower($command) == "y" or $command == "") {
248 2044cb61 Erik Fonnesbeck
				$GIT_REPO = $branch;
249 26b89905 Erik Fonnesbeck
				$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
250 1bfcf6f1 Phil Davis
				if ($command == "") {
251 26b89905 Erik Fonnesbeck
					$branch = $DEFAULT_BRANCH;
252 1bfcf6f1 Phil Davis
				}
253
				if ($command) {
254 2044cb61 Erik Fonnesbeck
					$branch = $command;
255 1bfcf6f1 Phil Davis
				}
256 2044cb61 Erik Fonnesbeck
				$found = true;
257
			}
258
		}
259 e623dd70 Scott Ullrich
	}
260 1bfcf6f1 Phil Davis
	if (!$found) {
261 e623dd70 Scott Ullrich
		echo "\nNo valid branch found.  Exiting.\n\n";
262
		exit;
263
	}
264 800351cc Scott Ullrich
}
265
266 b6964228 Erik Fonnesbeck
$merge_repos = array();
267 1bfcf6f1 Phil Davis
if (file_exists($GITSYNC_MERGE)) {
268 26b89905 Erik Fonnesbeck
	$gitsync_merges = file($GITSYNC_MERGE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
269 1bfcf6f1 Phil Davis
	if (!empty($gitsync_merges) && is_array($gitsync_merges)) {
270 26b89905 Erik Fonnesbeck
		echo "\n===> Automatic merge list read from ${GITSYNC_MERGE}\n";
271 1bfcf6f1 Phil Davis
		foreach ($gitsync_merges as $merge_line_num => $merge_line) {
272 97e6256f Erik Fonnesbeck
			$merge_comments = explode("#", trim($merge_line));
273 1bfcf6f1 Phil Davis
			if (empty($merge_comments[0])) {
274 97e6256f Erik Fonnesbeck
				continue;
275 1bfcf6f1 Phil Davis
			}
276 97e6256f Erik Fonnesbeck
277
			$merge_line = explode(" ", trim($merge_comments[0]));
278 1bfcf6f1 Phil Davis
			if (count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
279 97e6256f Erik Fonnesbeck
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
280
				echo "Expected parameters:  repository_url branch\n";
281
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
282
				echo "Aborting automatic merge.\n\n";
283
				$merge_repos = array();
284
				break;
285
			}
286
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
287
		}
288
	}
289
}
290 1bfcf6f1 Phil Davis
if (!$args[0] && !$upgrading) {
291 b6964228 Erik Fonnesbeck
	do {
292 26b89905 Erik Fonnesbeck
		echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
293 b6964228 Erik Fonnesbeck
		$merge_repo = readline("> ");
294 1bfcf6f1 Phil Davis
		if (!empty($merge_repo)) {
295 26b89905 Erik Fonnesbeck
			$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
296 1bfcf6f1 Phil Davis
			if ($merge_branch == "") {
297 26b89905 Erik Fonnesbeck
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
298 1bfcf6f1 Phil Davis
			} else if ($merge_branch) {
299 b6964228 Erik Fonnesbeck
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
300 1bfcf6f1 Phil Davis
			}
301 b6964228 Erik Fonnesbeck
		}
302 1bfcf6f1 Phil Davis
	} while (!empty($merge_repo));
303 b6964228 Erik Fonnesbeck
}
304
305 dc61252a Renato Botelho
if ($branch == "RESTORE") {
306 1bfcf6f1 Phil Davis
	if (!file_exists("/root/cvssync_backup.tgz")) {
307 ff33e21f Scott Ullrich
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
308
		exit();
309
	}
310
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
311 9226ab39 Scott Ullrich
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
312 ff33e21f Scott Ullrich
	post_cvssync_commands();
313
	exit();
314 81fa1cc3 Scott Ullrich
} else {
315 987712e8 jim-p
	$nobackup = true; // do not backup embedded
316 ff33e21f Scott Ullrich
}
317
318 1bfcf6f1 Phil Davis
if ($nobackup == false) {
319 84b5f39b Scott Ullrich
	echo "===> Backing up current pfSense information...\n";
320
	echo "===> Please wait... ";
321 4881e5a9 Ermal
	exec("tar czPf /root/cvssync_backup.tgz --exclude /root --exclude /dev --exclude /tmp --exclude /var/run --exclude /var/empty /");
322 84b5f39b Scott Ullrich
	$size = filesize("/root/cvssync_backup.tgz");
323 81fa1cc3 Scott Ullrich
	echo "{$size} bytes.\n\n";
324
	sleep(5);
325 84b5f39b Scott Ullrich
}
326 ff33e21f Scott Ullrich
327 9c55f824 Scott Ullrich
echo "===> Checking out $branch\n";
328 81fa1cc3 Scott Ullrich
329 43141617 Erik Fonnesbeck
// Git commands for resetting to the specified branch
330 1bfcf6f1 Phil Davis
if ($branch == "build_commit") {
331 43141617 Erik Fonnesbeck
	$git_cmd = array(
332 da1d0165 Marcello Coutinho
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " 2>/dev/null",
333
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
334
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
335 43141617 Erik Fonnesbeck
	);
336
} else {
337
	$git_cmd = array(
338 da1d0165 Marcello Coutinho
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
339
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
340
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg("origin/{$branch}")
341 43141617 Erik Fonnesbeck
	);
342
}
343
344 81fa1cc3 Scott Ullrich
// Git 'er done!
345 1bfcf6f1 Phil Davis
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
346 e2dc4289 sullrich
	echo "===> Fetching updates...\n";
347 da1d0165 Marcello Coutinho
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url " . escapeshellarg($GIT_REPO));
348
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} fetch");
349 bcee456c Renato Botelho
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} clean -f -x -d");
350 ecac770c Erik Fonnesbeck
	run_cmds($git_cmd);
351 35a98a24 Scott Ullrich
} else {
352 1bfcf6f1 Phil Davis
	exec("mkdir -p $CODIR/pfSenseGITREPO");
353
	echo "Executing cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone $GIT_REPO pfSenseGITREPO\n";
354 da1d0165 Marcello Coutinho
	exec("cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
355 1bfcf6f1 Phil Davis
	if (is_dir("$CODIR/pfSenseGITREPO/pfSense")) {
356 72374d82 Scott Ullrich
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
357 1bfcf6f1 Phil Davis
	}
358
	if (is_dir("$CODIR/pfSenseGITREPO/mainline")) {
359 72374d82 Scott Ullrich
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
360 1bfcf6f1 Phil Davis
	}
361 43141617 Erik Fonnesbeck
	run_cmds($git_cmd);
362 35a98a24 Scott Ullrich
}
363 4c1ed4a7 Scott Ullrich
364 1bfcf6f1 Phil Davis
foreach ($merge_repos as $merge_repo) {
365 b6964228 Erik Fonnesbeck
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
366 da1d0165 Marcello Coutinho
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
367 b6964228 Erik Fonnesbeck
	unset($output_str);
368 1bfcf6f1 Phil Davis
	if ($ret <> 0) {
369 b6964228 Erik Fonnesbeck
		echo "\nMerge failed.  Aborting sync.\n\n";
370
		run_cmds($git_cmd);
371
		exit;
372
	}
373
}
374
375 b19c8033 NOYB
$updated_files_array[1] = array();
376 1bfcf6f1 Phil Davis
if (isset($args["--minimal"])) {
377
	if (file_exists("/etc/version.gitsync")) {
378 147b2be1 Erik Fonnesbeck
		$old_revision = trim(file_get_contents("/etc/version.gitsync"));
379 1bfcf6f1 Phil Davis
	} else if (file_exists("/etc/version.lastcommit")) {
380 147b2be1 Erik Fonnesbeck
		$old_revision = trim(file_get_contents("/etc/version.lastcommit"));
381 1bfcf6f1 Phil Davis
	}
382 b19c8033 NOYB
383
	# Find updated files.
384
	$updated_files = shell_exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} diff --name-only --relative=src " . escapeshellarg($old_revision));
385
386
	# Get the path of each updated file.
387
	preg_match_all('@(?:^(.+?)$)@sim', $updated_files, $updated_files_array, PREG_PATTERN_ORDER);
388
389 ef30fa51 NOYB
	if(isset($args["--show-files"])) {
390 b19c8033 NOYB
		echo "===> Updated Files: \n";
391
		print_r($updated_files_array[1]);
392
	}
393 1bfcf6f1 Phil Davis
}
394 147b2be1 Erik Fonnesbeck
395
// Save new commit ID for later minimal file copies
396 b19c8033 NOYB
if(!isset($args["--dry-run"])) {
397
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} rev-parse -q --verify HEAD > /etc/version.gitsync");
398
}
399 147b2be1 Erik Fonnesbeck
400 1bfcf6f1 Phil Davis
// Remove files that we do not want to overwrite the system with
401 61af87f6 Renato Botelho
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/crontab");
402
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/master.passwd");
403
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/passwd");
404
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
405
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/ttys");
406
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/group");
407
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
408
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/platform");
409
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/device.hints");
410
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.conf");
411
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.rc");
412
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/syslog.conf");
413
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.shrc");
414
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.tcshrc");
415
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/conf*");
416
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/cf 2>/dev/null");
417 4758017e Renato Botelho
@chmod("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/tmp", 01777);
418 81fa1cc3 Scott Ullrich
419 b19c8033 NOYB
$different_files_array[1] = array();
420
$missing_files_array[1] = array();
421 d20f76bc NOYB
if(isset($args["--diff"])) {
422
	# Find different and missing files.
423
	$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/)'");
424
425
	# Get the path of each different or missing file.
426 b19c8033 NOYB
	preg_match_all('@(?:^Files .*? and '.$CODIR.'/pfSenseGITREPO/pfSenseGITREPO/src/(.+?) differ.*?$)@sim', $different_missing_files, $different_files_array, PREG_PATTERN_ORDER);
427
	preg_match_all('@(?:^Only in '.$CODIR.'/pfSenseGITREPO/pfSenseGITREPO/src/(.+?)$)@sim', $different_missing_files, $missing_files_array, PREG_PATTERN_ORDER);
428 d20f76bc NOYB
429
	# Deal with diff's output format of missing files (path: missing_file).
430
	foreach ($missing_files_array[1] as $key => $file) {
431
432
		# Most of the time there will be only the one ': ' injected by diff output.  So global replace with dir delimiter (/) is fine.
433
		$tmp = str_replace(": ", "/", $file, $count);
434
		if ($count == 1)
435
			$file = ltrim($tmp, "/");
436
437
		# 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.
438
		else {
439
			$tmp = $file;
440
			do {
441
				$pos = strrpos($tmp, ": ");
442
				if ($pos) {
443
					$tmp = substr($tmp, 0, $pos);
444
					$res = is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO/src/$tmp/");
445
				}
446
			} while (!$res && $pos);
447
448
			if ($res)
449
				$file = ltrim($tmp . "/" . substr($file, $pos+2), "/");
450
		}
451
452
		$missing_files_array[1][$key] = $file;
453
	}
454
455 ef30fa51 NOYB
	if(isset($args["--show-files"])) {
456 d20f76bc NOYB
		echo "===> Different Files: \n";
457
		print_r($different_files_array[1]);
458
		echo "===> Missing Files: \n";
459
		print_r($missing_files_array[1]);
460
	}
461
}
462
463 b19c8033 NOYB
# Files to be copied.
464
if(isset($args["--minimal"]) || isset($args["--diff"])) {
465
	$files_to_copy_array = array_merge($updated_files_array[1], $different_files_array[1], $missing_files_array[1]);
466
	$files_to_copy_array = array_unique($files_to_copy_array);
467
468 53b9a2ac NOYB
	unset($updated_files_array, $different_files_array, $missing_files_array);
469 b19c8033 NOYB
470
	# Convert the list from an array to a space separated quoted string.  Quoted for white space file name support.
471
	if (count($files_to_copy_array) > 0) {	# Leave the string empty/unset if there is nothing to copy.
472
		$files_to_copy = '"' . implode('" "', $files_to_copy_array) . '"';
473
	}
474 53b9a2ac NOYB
	$qty_files_to_copy = count($files_to_copy_array);
475 b19c8033 NOYB
	unset($files_to_copy_array);
476
} else {
477
	$files_to_copy = '.';
478 53b9a2ac NOYB
	$qty_files_to_copy = chr(8);
479 b19c8033 NOYB
}
480
481
$tar_options = '-C ./src';
482
483 53b9a2ac NOYB
echo "===> Installing $qty_files_to_copy new files...\n";
484 81fa1cc3 Scott Ullrich
485 dc61252a Renato Botelho
$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$tar_options} {$files_to_copy} | (cd / ; tar -Uxpf -)";
486 1bfcf6f1 Phil Davis
487
if (!empty($files_to_copy)) {
488 ef30fa51 NOYB
	if(isset($args["--show-command"])) {
489 d20f76bc NOYB
		echo "===> Command: \n$command\n";
490
	}
491
	if(!isset($args["--dry-run"])) {
492
		exec($command);
493
	}
494 1bfcf6f1 Phil Davis
} else {
495 147b2be1 Erik Fonnesbeck
	echo "Already up-to-date.\n";
496
	$upgrading = true;
497
}
498 800351cc Scott Ullrich
499 97e6256f Erik Fonnesbeck
// Reset the repository to restore the deleted files
500 da1d0165 Marcello Coutinho
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard >/dev/null 2>/dev/null");
501 800351cc Scott Ullrich
502 feef287e Erik Fonnesbeck
// Remove obsolete files
503
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
504 1bfcf6f1 Phil Davis
foreach ($files_to_remove as $file_to_remove) {
505
	if (file_exists($file_to_remove)) {
506 feef287e Erik Fonnesbeck
		exec("/bin/rm -f $file_to_remove");
507 1bfcf6f1 Phil Davis
	}
508
}
509 feef287e Erik Fonnesbeck
510 1bfcf6f1 Phil Davis
if (!$upgrading) {
511 f0ce6758 Erik Fonnesbeck
	post_cvssync_commands();
512 1bfcf6f1 Phil Davis
}
513 51a2e892 sullrich
514 81fa1cc3 Scott Ullrich
echo "===> Checkout complete.\n";
515
echo "\n";
516 1bfcf6f1 Phil Davis
if (!$upgrading) {
517 f1981374 NOYB
	echo "The system is now sync'd and PHP and nginx will be restarted in 5 seconds.\n\n";
518 1bfcf6f1 Phil Davis
} else {
519 f1981374 NOYB
	echo "The system is now sync'd.\n\n";
520 1bfcf6f1 Phil Davis
}