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_REPO = "git://github.com/pfsense/pfsense.git";
16
$DEFAULT_BRANCH = "master";
17
$CODIR =  "/root/pfsense/";
18
$GITSYNC_MERGE = "/root/.gitsync_merge";
19

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

    
28
global $g;
29
global $argv;
30
global $command_split;
31

    
32
if(is_array($command_split))
33
	$temp_args = array_slice($command_split, 2);
34
else
35
	$temp_args = array_slice($argv, 3);
36

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

    
70
unlink_if_exists("/tmp/config.cache");
71
conf_mount_rw();
72

    
73
if(!file_exists("/usr/local/bin/git")) {
74
	echo "Cannot find git, fetching...\n";
75
	require_once("config.inc");
76
	require_once("util.inc");
77
	require_once("pkg-utils.inc");
78

    
79
	echo "Trying to fetch package info...";
80
	$pkg_info = get_pkg_info();
81
	if ($pkg_info) {
82
		echo " Done.\n";
83
	} else {
84
		$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
85
		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";
86
		return;
87
	}
88
	if (empty($pkg_info["git"])) {
89
		echo "Can't locate git package in pfSense repo. Using FreeBSD pkg repo..";
90

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

    
100
	} else {
101
		install_package("git", $pkg_info["git"], true);
102
		echo "Done.\n";
103
	}
104
}
105

    
106
# Remove mainline if exists (older)
107
if(is_dir("/root/pfsense/mainline")) 
108
	exec("rm -rf /root/pfsense/mainline");
109

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

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

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

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

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

    
144
if($args[1] == "NOBACKUP") 
145
	$nobackup = true;
146
else 
147
	$nobackup = false;
148

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

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

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

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

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

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

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

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

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

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

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

    
312
// Save new commit ID for later minimal file copies
313
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git rev-parse -q --verify HEAD > /etc/version.gitsync");
314

    
315
exec("mkdir -p /tmp/lighttpd/cache/compress/");
316

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

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

    
340
echo "===> Installing new files...\n";
341

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

    
353
// Reset the repository to restore the deleted files
354
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard >/dev/null 2>/dev/null");
355

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

    
362
if(!$upgrading)
363
	post_cvssync_commands();
364

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

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

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

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

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

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

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

    
409
}
410

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

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

    
429
conf_mount_ro();
(8-8/15)