Project

General

Profile

Download (14.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/*   cvs_sync
2
 *   Written by Scott Ullrich
3
 *   (C)2005-2007 Scott Ullrich
4
 *   (C)2010-2012 Erik Fonnesbeck
5
 *   Part of the pfSense project pfSsh.php subsystem
6
 */
7

    
8
require_once("globals.inc");
9
require_once("filter.inc");
10
require_once("shaper.inc");
11
require_once("rrd.inc");
12
require_once("pfsense-utils.inc");
13
	
14
$GIT_PKG = "git"; // Either "git" or the full package URL
15
$GIT_BIN= "/usr/pbi/bin/git";
16
$GIT_REPO = "git://github.com/pfsense/pfsense.git";
17
$DEFAULT_BRANCH = "master";
18
$CODIR =  "/root/pfsense";
19
$GITSYNC_MERGE = "/root/.gitsync_merge";
20

    
21
/* NOTE: Set branches here */
22
$branches = array(
23
	"master" => "2.2 development branch",
24
	"RELENG_2_2" => "2.2.* release branch",
25
	"RELENG_2_1" => "2.1.* release branch",
26
	"RELENG_2_0" => "2.0.* release branch",
27
	"RELENG_1_2" => "1.2.* release branch",
28
	"build_commit" => "The commit originally used to build the image"
29
);
30

    
31
global $g;
32
global $argv;
33
global $command_split;
34

    
35
if(is_array($command_split))
36
	$temp_args = array_slice($command_split, 2);
37
else
38
	$temp_args = array_slice($argv, 3);
39

    
40
$valid_args = array(
41
	"--minimal" => "\tPerform a minimal copy of only the updated files.\n" .
42
	               "\tNot recommended if the system has files modified by any method other\n" .
43
	               "\tthan gitsync.\n",
44
	"--help" => "\tDisplay this help list.\n"
45
	);
46
$args = array();
47
$arg_count = 0;
48
while(!empty($temp_args)) {
49
	$arg = array_shift($temp_args);
50
	if($arg[0] == '-') {
51
		switch($arg) {
52
			case "--help":
53
				echo "Usage: playback gitsync [options] [[repository] <branch>]\nOptions:\n";
54
				foreach($valid_args as $arg_name => $arg_desc)
55
					echo $arg_name . "\n" . $arg_desc;
56
				exit;
57
			case "--upgrading":
58
				// Disables all interactive functions and neither PHP
59
				// nor the web GUI will be killed or restarted.
60
				$upgrading = true;
61
			case (isset($valid_args[$arg])):
62
				$args[$arg] = true;
63
				break;
64
			default:
65
				echo "Invalid option: {$arg}\nUse --help for usage information.\n";
66
				exit;
67
		}
68
	} else {
69
		$args[$arg_count++] = $arg;
70
	}
71
}
72

    
73
unlink_if_exists("/tmp/config.cache");
74
conf_mount_rw();
75

    
76
if(!file_exists($GIT_BIN)) {
77
	echo "Cannot find git, fetching...\n";
78
	require_once("config.inc");
79
	require_once("util.inc");
80
	require_once("pkg-utils.inc");
81

    
82
	echo "Trying to fetch package info...";
83
	$pkg_info = get_pkg_info();
84
	if ($pkg_info) {
85
		echo " Done.\n";
86
	} else {
87
		$xmlrpc_base_url = get_active_xml_rpc_base_url();
88
		echo "\n" . sprintf(gettext(' >>> Unable to communicate with %1$s. Please verify DNS and interface configuration, and that %2$s has functional Internet connectivity.'), $xmlrpc_base_url, $g['product_name']) . "\n";
89
		return;
90
	}
91
	if (empty($pkg_info["git"])) {
92
		echo "Can't locate git package in pfSense repo. Using FreeBSD pkg repo..";
93

    
94
		if (($g['platform'] == "nanobsd")) {
95
			$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
96
			$pkgstagingdir = "/root/tmp";
97
			if (!is_dir($pkgstagingdir))
98
				mkdir($pkgstagingdir);
99
			$pkgstaging = "-t {$pkgstagingdir}/instmp.XXXXXX";
100
		}
101
		system("{$pkgtmpdir}/usr/sbin/pkg_add {$pkgstaging} -r {$GIT_PKG}");
102

    
103
	} else {
104
		install_package("git", $pkg_info["git"], true);
105
		echo "Done.\n";
106
	}
107
}
108

    
109
# Remove mainline if exists (older)
110
if(is_dir("/root/pfsense/mainline")) 
111
	exec("rm -rf /root/pfsense/mainline");
112

    
113
# Remove RELENG_1_2 if exists (older)
114
if(is_dir("/root/pfsense/RELENG_1_2")) 
115
	exec("rm -rf /root/pfsense/RELENG_1_2");
116

    
117
# Remove HEAD if exists (older)
118
if(is_dir("/root/pfsense/HEAD")) 
119
	exec("rm -rf /root/pfsense/HEAD");
120

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

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

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

    
147
if($args[1] == "NOBACKUP") 
148
	$nobackup = true;
149
else 
150
	$nobackup = false;
151

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

    
164
$found = false;
165
foreach($branches as $branchname => $branchdesc) {
166
	if($branchname == $branch) 
167
		$found = true;
168
}
169
if(!$found) {
170
	if(isURL($branch) && !$upgrading) {
171
		if($args[1]) {
172
			$GIT_REPO = $branch;
173
			$branch = $args[1];
174
			$found = true;
175
		}
176
		else {
177
			echo "\n";
178
			echo "NOTE: $branch was not found.\n\n";
179
			$command = readline("Is this a custom GIT URL? [y]? ");
180
			if(strtolower($command) == "y" or $command == "") {
181
				$GIT_REPO = $branch;
182
				$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
183
				if($command == "")
184
					$branch = $DEFAULT_BRANCH;
185
				if($command) 
186
					$branch = $command;
187
				$found = true;
188
			}
189
		}
190
	}
191
	if(!$found) {
192
		echo "\nNo valid branch found.  Exiting.\n\n";
193
		conf_mount_ro();
194
		exit;
195
	}
196
}
197

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

    
208
			$merge_line = explode(" ", trim($merge_comments[0]));
209
			if(count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
210
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
211
				echo "Expected parameters:  repository_url branch\n";
212
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
213
				echo "Aborting automatic merge.\n\n";
214
				$merge_repos = array();
215
				break;
216
			}
217
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
218
		}
219
	}
220
}
221
if(!$args[0] && !$upgrading) {
222
	do {
223
		echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
224
		$merge_repo = readline("> ");
225
		if(!empty($merge_repo)) {
226
			$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
227
			if($merge_branch == "")
228
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
229
			else if($merge_branch)
230
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
231
		}
232
	} while(!empty($merge_repo));
233
}
234

    
235
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
236
	if(!file_exists("/root/cvssync_backup.tgz")) {
237
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
238
		conf_mount_ro();
239
		exit();
240
	}
241
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
242
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
243
	post_cvssync_commands();
244
	conf_mount_ro();
245
	exit();
246
} else {
247
	$nobackup = true; // do not backup embedded, livecd
248
}
249

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

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

    
261
// Git commands for resetting to the specified branch
262
if($branch == "build_commit") {
263
	$git_cmd = array(
264
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " 2>/dev/null",
265
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
266
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
267
	);
268
} else {
269
	$git_cmd = array(
270
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
271
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
272
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg("origin/{$branch}")
273
	);
274
}
275

    
276
// Git 'er done!
277
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
278
	echo "===> Fetching updates...\n";
279
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url " . escapeshellarg($GIT_REPO));
280
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} fetch");
281
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} clean -f -f -x -d");
282
	run_cmds($git_cmd);
283
} else {
284
    exec("mkdir -p $CODIR/pfSenseGITREPO");
285
    echo "Executing cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone $GIT_REPO pfSenseGITREPO\n";
286
	exec("cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
287
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
288
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
289
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
290
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
291
	run_cmds($git_cmd);
292
}
293

    
294
foreach($merge_repos as $merge_repo) {
295
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
296
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
297
	unset($output_str);
298
	if($ret <> 0) {
299
		echo "\nMerge failed.  Aborting sync.\n\n";
300
		run_cmds($git_cmd);
301
		conf_mount_ro();
302
		exit;
303
	}
304
}
305

    
306
if(isset($args["--minimal"])) {
307
	if(file_exists("/etc/version.gitsync"))
308
		$old_revision = trim(file_get_contents("/etc/version.gitsync"));
309
	else if(file_exists("/etc/version.lastcommit"))
310
		$old_revision = trim(file_get_contents("/etc/version.lastcommit"));
311
	$files_to_copy = strtr(shell_exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} diff --name-only " . escapeshellarg($old_revision)), "\n", " ");
312
} else
313
	$files_to_copy = '--exclude .git .';
314

    
315
// Save new commit ID for later minimal file copies
316
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} rev-parse -q --verify HEAD > /etc/version.gitsync");
317

    
318
exec("mkdir -p /tmp/lighttpd/cache/compress/");
319

    
320
// Nuke CVS and pfSense tarballs
321
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
322
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
323

    
324
// Remove files that we do not want to overwrite the system with 
325
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
326
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
327
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
328
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
329
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
330
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
331
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
332
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
333
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
334
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
335
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
336
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
337
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
338
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
339
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
340
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
341

    
342
echo "===> Installing new files...\n";
343

    
344
if($g['platform'] == "pfSense") 
345
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$files_to_copy} | (cd / ; tar -Uxpf -)";
346
else 
347
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$files_to_copy} | (cd / ; tar -xpf -) 2>/dev/null";
348
if(!empty($files_to_copy))
349
	exec($command);
350
else {
351
	echo "Already up-to-date.\n";
352
	$upgrading = true;
353
}
354

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

    
358
// Remove obsolete files
359
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
360
foreach($files_to_remove as $file_to_remove) 
361
	if(file_exists($file_to_remove)) 
362
		exec("/bin/rm -f $file_to_remove");
363

    
364
if(!$upgrading)
365
	post_cvssync_commands();
366

    
367
echo "===> Checkout complete.\n";
368
echo "\n";
369
if(!$upgrading)
370
	echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
371
else
372
	echo "Your system is now sync'd.\n\n";
373

    
374
function post_cvssync_commands() {
375
	echo "===> Removing FAST-CGI temporary files...\n";
376
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
377
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
378

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

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

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

    
389
	if(file_exists("/etc/rc.php_ini_setup")) {
390
		echo "===> Running /etc/rc.php_ini_setup...";
391
		exec("/etc/rc.php_ini_setup");
392
		echo "\n";
393
	}
394

    
395
	/* lock down console if necessary */
396
	echo "===> Locking down the console if needed...\n";
397
	reload_ttys();
398
	
399
	echo "===> Signaling PHP and Lighty restart...";
400
	$fd = fopen("/tmp/restart_lighty", "w");
401
	fwrite($fd, "#!/bin/sh\n");
402
	fwrite($fd, "sleep 5\n");
403
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
404
	if(file_exists("/var/etc/lighty-CaptivePortal.conf"))
405
		fwrite($fd, "/usr/local/sbin/lighttpd -f /var/etc/lighty-CaptivePortal.conf\n");
406
	fclose($fd);
407
	mwexec_bg("sh /tmp/restart_lighty");
408
	echo "\n";
409

    
410
}
411

    
412
function isUrl($url = "") {
413
	if($url) 
414
		if(strstr($url, "rcs.pfsense.org") or 
415
			strstr($url, "mainline") or 
416
				strstr($url, ".git") or strstr($url, "git://"))
417
					return true;
418
	return false;
419
}
420

    
421
function run_cmds($cmds) {
422
	global $debug;
423
	foreach($cmds as $cmd) {
424
		if($debug)
425
			echo "Running $cmd";
426
		exec($cmd);
427
	}
428
}
429

    
430
conf_mount_ro();
(10-10/18)