Project

General

Profile

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