Project

General

Profile

Download (13.9 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/bsdperimeter/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...";
75
	if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded")) {
76
		$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
77
		$pkgstagingdir = "/root/tmp";
78
		if (!is_dir($pkgstagingdir))
79
			mkdir($pkgstagingdir);
80
		$pkgstaging = "-t {$pkgstagingdir}/instmp.XXXXXX";
81
	}
82
	system("{$pkgtmpdir}/usr/sbin/pkg_add {$pkgstaging} -r {$GIT_PKG}");
83
}
84

    
85
# Remove mainline if exists (older)
86
if(is_dir("/root/pfsense/mainline")) 
87
	exec("rm -rf /root/pfsense/mainline");
88

    
89
# Remove RELENG_1_2 if exists (older)
90
if(is_dir("/root/pfsense/RELENG_1_2")) 
91
	exec("rm -rf /root/pfsense/RELENG_1_2");
92

    
93
# Remove HEAD if exists (older)
94
if(is_dir("/root/pfsense/HEAD")) 
95
	exec("rm -rf /root/pfsense/HEAD");
96

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

    
103
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
104
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url", $output_str, $ret);
105
	if(is_array($output_str) && !empty($output_str[0]))
106
		$GIT_REPO = $output_str[0];
107
	unset($output_str);
108
}
109

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

    
123
if($args[1] == "NOBACKUP") 
124
	$nobackup = true;
125
else 
126
	$nobackup = false;
127

    
128
// If the repository has been fetched before, build a list of its branches.
129
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
130
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch -r", $branch_list, $ret);
131
	if($ret == 0 && is_array($branch_list)) {
132
		foreach ($branch_list as $branch_item) {
133
			$branch_item = substr(strrchr($branch_item, "/"), 1);
134
			if (!isset($branches[$branch_item]))
135
				$branches[$branch_item] = " ";
136
		}
137
	}
138
}
139

    
140
$found = false;
141
foreach($branches as $branchname => $branchdesc) {
142
	if($branchname == $branch) 
143
		$found = true;
144
}
145
if(!$found) {
146
	if(isURL($branch) && !$upgrading) {
147
		if($args[1]) {
148
			$GIT_REPO = $branch;
149
			$branch = $args[1];
150
			$found = true;
151
		}
152
		else {
153
			echo "\n";
154
			echo "NOTE: $branch was not found.\n\n";
155
			$command = readline("Is this a custom GIT URL? [y]? ");
156
			if(strtolower($command) == "y" or $command == "") {
157
				$GIT_REPO = $branch;
158
				$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
159
				if($command == "")
160
					$branch = $DEFAULT_BRANCH;
161
				if($command) 
162
					$branch = $command;
163
				$found = true;
164
			}
165
		}
166
	}
167
	if(!$found) {
168
		echo "\nNo valid branch found.  Exiting.\n\n";
169
		conf_mount_ro();
170
		exit;
171
	}
172
}
173

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

    
184
			$merge_line = explode(" ", trim($merge_comments[0]));
185
			if(count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
186
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
187
				echo "Expected parameters:  repository_url branch\n";
188
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
189
				echo "Aborting automatic merge.\n\n";
190
				$merge_repos = array();
191
				break;
192
			}
193
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
194
		}
195
	}
196
}
197
if(!$args[0] && !$upgrading) {
198
	do {
199
		echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
200
		$merge_repo = readline("> ");
201
		if(!empty($merge_repo)) {
202
			$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
203
			if($merge_branch == "")
204
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
205
			else if($merge_branch)
206
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
207
		}
208
	} while(!empty($merge_repo));
209
}
210

    
211
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
212
	if(!file_exists("/root/cvssync_backup.tgz")) {
213
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
214
		conf_mount_ro();
215
		exit();
216
	}
217
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
218
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
219
	post_cvssync_commands();
220
	conf_mount_ro();
221
	exit();
222
} else {
223
	$nobackup = true; // do not backup embedded, livecd
224
}
225

    
226
if($nobackup == false) {
227
	echo "===> Backing up current pfSense information...\n";
228
	echo "===> Please wait... ";
229
	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 /");
230
	$size = filesize("/root/cvssync_backup.tgz");
231
	echo "{$size} bytes.\n\n";
232
	sleep(5);
233
}
234

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

    
237
// Git commands for resetting to the specified branch
238
if($branch == "build_commit") {
239
	$git_cmd = array(
240
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " 2>/dev/null",
241
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
242
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
243
	);
244
} else {
245
	$git_cmd = array(
246
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
247
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
248
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard " . escapeshellarg("origin/{$branch}")
249
	);
250
}
251

    
252
// Git 'er done!
253
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
254
	echo "===> Fetching updates...\n";
255
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url " . escapeshellarg($GIT_REPO));
256
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
257
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d");
258
	run_cmds($git_cmd);
259
} else {
260
    exec("mkdir -p $CODIR/pfSenseGITREPO");
261
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
262
	exec("cd $CODIR/pfSenseGITREPO && git clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
263
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
264
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
265
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
266
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
267
	run_cmds($git_cmd);
268
}
269

    
270
foreach($merge_repos as $merge_repo) {
271
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
272
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
273
	unset($output_str);
274
	if($ret <> 0) {
275
		echo "\nMerge failed.  Aborting sync.\n\n";
276
		run_cmds($git_cmd);
277
		conf_mount_ro();
278
		exit;
279
	}
280
}
281

    
282
if(isset($args["--minimal"])) {
283
	if(file_exists("/etc/version.gitsync"))
284
		$old_revision = trim(file_get_contents("/etc/version.gitsync"));
285
	else if(file_exists("/etc/version.lastcommit"))
286
		$old_revision = trim(file_get_contents("/etc/version.lastcommit"));
287
	$files_to_copy = strtr(shell_exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git diff --name-only " . escapeshellarg($old_revision)), "\n", " ");
288
} else
289
	$files_to_copy = '--exclude .git .';
290

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

    
294
exec("mkdir -p /tmp/lighttpd/cache/compress/");
295

    
296
// Nuke CVS and pfSense tarballs
297
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
298
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
299

    
300
// Remove files that we do not want to overwrite the system with 
301
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
302
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
303
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
304
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
305
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
306
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
307
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
308
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
309
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
310
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
311
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
312
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
313
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
314
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
315
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
316
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
317
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
318

    
319
echo "===> Installing new files...\n";
320

    
321
if($g['platform'] == "pfSense") 
322
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$files_to_copy} | (cd / ; tar -Uxpf -)";
323
else 
324
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$files_to_copy} | (cd / ; tar -xpf -) 2>/dev/null";
325
if(!empty($files_to_copy))
326
	exec($command);
327
else {
328
	echo "Already up-to-date.\n";
329
	$upgrading = true;
330
}
331

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

    
335
// Remove obsolete files
336
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
337
foreach($files_to_remove as $file_to_remove) 
338
	if(file_exists($file_to_remove)) 
339
		exec("/bin/rm -f $file_to_remove");
340

    
341
if(!$upgrading)
342
	post_cvssync_commands();
343

    
344
echo "===> Checkout complete.\n";
345
echo "\n";
346
if(!$upgrading)
347
	echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
348
else
349
	echo "Your system is now sync'd.\n\n";
350

    
351
function post_cvssync_commands() {
352
	echo "===> Removing FAST-CGI temporary files...\n";
353
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
354
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
355

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

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

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

    
366
	if(file_exists("/etc/rc.php_ini_setup")) {
367
		echo "===> Running /etc/rc.php_ini_setup...";
368
		exec("/etc/rc.php_ini_setup");
369
		echo "\n";
370
	}
371

    
372
	/* lock down console if necessary */
373
	echo "===> Locking down the console if needed...\n";
374
	auto_login();
375
	
376
	echo "===> Signaling PHP and Lighty restart...";
377
	$fd = fopen("/tmp/restart_lighty", "w");
378
	fwrite($fd, "#!/bin/sh\n");
379
	fwrite($fd, "sleep 5\n");
380
	fwrite($fd, "killall php\n");
381
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
382
	if(file_exists("/var/etc/lighty-CaptivePortal.conf"))
383
		fwrite($fd, "/usr/local/sbin/lighttpd -f /var/etc/lighty-CaptivePortal.conf\n");
384
	fclose($fd);
385
	mwexec_bg("sh /tmp/restart_lighty");
386
	echo "\n";
387

    
388
}
389

    
390
function isUrl($url = "") {
391
	if($url) 
392
		if(strstr($url, "rcs.pfsense.org") or 
393
			strstr($url, "mainline") or 
394
				strstr($url, ".git") or strstr($url, "git://"))
395
					return true;
396
	return false;
397
}
398

    
399
function run_cmds($cmds) {
400
	global $debug;
401
	foreach($cmds as $cmd) {
402
		if($debug)
403
			echo "Running $cmd";
404
		exec($cmd);
405
	}
406
}
407

    
408
conf_mount_ro();
(6-6/9)