Project

General

Profile

Download (12.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/*   cvs_sync
2
 *   Written by Scott Ullrich
3
 *   (C)2005-2007 Scott Ullrich
4
 *   (C)2010 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
conf_mount_rw();
15

    
16
$GIT_REPO="git://github.com/bsdperimeter/pfsense.git";
17
$CODIR =  "/root/pfsense/";
18

    
19
global $g;
20
global $argv;
21
global $command_split;
22

    
23
// If this parameter is set, all interactive functions are disabled
24
// and neither PHP nor the web gui will be killed or restarted.
25
$upgrading = in_array("--upgrading", $argv);
26

    
27
unlink_if_exists("/tmp/config.cache");
28

    
29
if(!file_exists("/usr/local/bin/git")) {
30
	echo "Cannot find git, fetching...";
31
	if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded")) {
32
		$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
33
		$pkgstagingdir = "/root/tmp";
34
		if (!is_dir($pkgstagingdir))
35
			mkdir($pkgstagingdir);
36
		$pkgstaging = "-t {$pkgstagingdir}/instmp.XXXXXX";
37
	}
38
	system("{$pkgtmpdir}/usr/sbin/pkg_add {$pkgstaging} -r http://ftp-archive.freebsd.org/pub/FreeBSD-Archive/ports/`uname -m`/packages-8.1-release/All/git-1.7.1.1.tbz");
39
}
40

    
41
# Remove mainline if exists (older)
42
if(is_dir("/root/pfsense/mainline")) 
43
	exec("rm -rf /root/pfsense/mainline");
44

    
45
# Remove RELENG_1_2 if exists (older)
46
if(is_dir("/root/pfsense/RELENG_1_2")) 
47
	exec("rm -rf /root/pfsense/RELENG_1_2");
48

    
49
# Remove HEAD if exists (older)
50
if(is_dir("/root/pfsense/HEAD")) 
51
	exec("rm -rf /root/pfsense/HEAD");
52

    
53
/* NOTE: Set branches here */
54
$branches = array(
55
	"master" => "2.x development branch",
56
	"RELENG_2_0" => "2.0 RC branch",
57
	"RELENG_1_2" => "1.2* release branch",
58
	"build_commit" => "The commit originally used to build the image"
59
);
60

    
61
if(file_exists("/root/cvssync_backup.tgz")) {
62
	$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
63
	$tmp = array("RESTORE" => "Restores prior CVSSync backup data performed at {$backup_date}");
64
	$branches = array_merge($branches, $tmp);
65
}
66

    
67
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
68
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url", $output_str, $ret);
69
	if(is_array($output_str) && !empty($output_str[0]))
70
		$GIT_REPO = $output_str[0];
71
	unset($output_str);
72
}
73

    
74
if($command_split[2]) {
75
	$branch = $command_split[2];
76
} else {
77
	if(!$argv[3] && !$upgrading) {
78
		echo "\nCurrent repository is $GIT_REPO\n";
79
		echo "\nPlease select which branch you would like to sync against:\n\n";
80
		foreach($branches as $branchname => $branchdesc) {
81
			echo "{$branchname} \t {$branchdesc}\n";
82
		}
83
		echo "\nOr alternatively you may enter a custom RCS branch URL (HTTP).\n\n";
84
		$branch = readline("> ");
85
		echo "\n";
86
	} else {
87
		$branch = $argv[3];
88
	}
89
}
90

    
91
if($argv[4] == "NOBACKUP") 
92
	$nobackup = true;
93
else 
94
	$nobackup = false;
95

    
96
// If the repository has been fetched before, build a list of its branches.
97
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
98
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch -r", $branch_list, $ret);
99
	if($ret == 0 && is_array($branch_list)) {
100
		foreach ($branch_list as $branch_item) {
101
			$branch_item = substr(strrchr($branch_item, "/"), 1);
102
			if (!isset($branches[$branch_item]))
103
				$branches[$branch_item] = " ";
104
		}
105
	}
106
}
107

    
108
$found = false;
109
foreach($branches as $branchname => $branchdesc) {
110
	if($branchname == $branch) 
111
		$found = true;
112
}
113
if(!$found) {
114
	if(isURL($branch) && !$upgrading) {
115
		echo "\n";
116
		echo "NOTE: $branch was not found.\n\n";
117
		$command = readline("Is this a custom GIT URL? [y]? ");
118
		if(strtolower($command) == "y" or $command == "") {
119
			$GIT_REPO = $branch;
120
			$command = readline("Checkout which branch [RELENG_2_0]? ");
121
			if($command == "")
122
				$branch = "RELENG_2_0";
123
			if($command) 
124
				$branch = $command;
125
			$found = true;
126
		}
127
	}
128
	if(!$found) {
129
		echo "\nNo valid branch found.  Exiting.\n\n";
130
		conf_mount_ro();
131
		exit;
132
	}
133
}
134

    
135
$merge_repos = array();
136
if(file_exists("/root/.gitsync_merge")) {
137
	$gitsync_merges = file("/root/.gitsync_merge", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
138
	if(!empty($gitsync_merges) && is_array($gitsync_merges)) {
139
		echo "\n===> Automatic merge list read from /root/.gitsync_merge\n";
140
		foreach($gitsync_merges as $merge_line_num => $merge_line) {
141
			$merge_comments = explode("#", trim($merge_line));
142
			if(empty($merge_comments[0]))
143
				continue;
144

    
145
			$merge_line = explode(" ", trim($merge_comments[0]));
146
			if(count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
147
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
148
				echo "Expected parameters:  repository_url branch\n";
149
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
150
				echo "Aborting automatic merge.\n\n";
151
				$merge_repos = array();
152
				break;
153
			}
154
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
155
		}
156
	}
157
}
158
if(!$command_split[2] && !$argv[3] && !$upgrading) {
159
	do {
160
		echo "\nAdd a custom RCS branch URL (HTTP) to merge in or press enter if done.\n\n";
161
		$merge_repo = readline("> ");
162
		if(!empty($merge_repo)) {
163
			$merge_branch = readline("Merge which branch [RELENG_2_0]? ");
164
			if($merge_branch == "")
165
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => 'RELENG_2_0');
166
			else if($merge_branch)
167
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
168
		}
169
	} while(!empty($merge_repo));
170
}
171

    
172
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
173
	if(!file_exists("/root/cvssync_backup.tgz")) {
174
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
175
		conf_mount_ro();
176
		exit();
177
	}
178
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
179
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
180
	post_cvssync_commands();
181
	conf_mount_ro();
182
	exit();
183
} else {
184
	$nobackup = true; // do not backup embedded, livecd
185
}
186

    
187
if($nobackup == false) {
188
	echo "===> Backing up current pfSense information...\n";
189
	echo "===> Please wait... ";
190
	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 /");
191
	$size = filesize("/root/cvssync_backup.tgz");
192
	echo "{$size} bytes.\n\n";
193
	sleep(5);
194
}
195

    
196
echo "===> Checking out $branch\n";
197

    
198
// Git commands for resetting to the specified branch
199
if($branch == "build_commit") {
200
	$git_cmd = array(
201
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " 2>/dev/null",
202
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
203
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard `cat /etc/version.lastcommit`"
204
	);
205
} else {
206
	$git_cmd = array(
207
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
208
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
209
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard " . escapeshellarg("origin/{$branch}")
210
	);
211
}
212

    
213
// Git 'er done!
214
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
215
	echo "===> Fetching updates...\n";
216
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url " . escapeshellarg($GIT_REPO));
217
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
218
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d");
219
	run_cmds($git_cmd);
220
} else {
221
    exec("mkdir -p $CODIR/pfSenseGITREPO");
222
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
223
	exec("cd $CODIR/pfSenseGITREPO && git clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
224
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
225
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
226
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
227
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
228
	run_cmds($git_cmd);
229
}
230

    
231
foreach($merge_repos as $merge_repo) {
232
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
233
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
234
	unset($output_str);
235
	if($ret <> 0) {
236
		echo "\nMerge failed.  Aborting sync.\n\n";
237
		run_cmds($git_cmd);
238
		conf_mount_ro();
239
		exit;
240
	}
241
}
242

    
243
exec("mkdir -p /tmp/lighttpd/cache/compress/");
244

    
245
// Nuke CVS and pfSense tarballs
246
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
247
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
248

    
249
// Remove files that we do not want to overwrite the system with 
250
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
251
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
252
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
253
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
254
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
255
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
256
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
257
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
258
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
259
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
260
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
261
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
262
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
263
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
264
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
265
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
266
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
267

    
268
echo "===> Installing new files...\n";
269

    
270
// Don't include the .git directory in the copy
271
exec("mv $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git $CODIR/pfSenseGITREPO/gitsync_temp.git");
272

    
273
if($g['platform'] == "pfSense") 
274
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -Uxpf -)";
275
else 
276
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -xpf -) 2>/dev/null";
277
exec($command);
278

    
279
exec("mv $CODIR/pfSenseGITREPO/gitsync_temp.git $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git");
280

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

    
284
if(!$upgrading)
285
	post_cvssync_commands();
286

    
287
echo "===> Checkout complete.\n";
288
echo "\n";
289
if(!$upgrading)
290
	echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
291
else
292
	echo "Your system is now sync'd.\n\n";
293

    
294
function post_cvssync_commands() {
295
	echo "===> Removing FAST-CGI temporary files...\n";
296
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
297
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
298

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

    
301
	echo "===> Upgrading configuration (if needed)...\n";
302
	convert_config();
303

    
304
	echo "===> Configuring filter...";
305
	exec("/etc/rc.filter_configure_sync");
306
	exec("pfctl -f /tmp/rules.debug");
307
	echo "\n";
308

    
309
	if(file_exists("/etc/rc.php_ini_setup")) {
310
		echo "===> Running /etc/rc.php_ini_setup...";
311
		exec("/etc/rc.php_ini_setup");
312
		echo "\n";
313
	}
314

    
315
	/* lock down console if necessary */
316
	echo "===> Locking down the console if needed...\n";
317
	auto_login();
318
	
319
	echo "===> Signaling PHP and Lighty restart...";
320
	$fd = fopen("/tmp/restart_lighty", "w");
321
	fwrite($fd, "#!/bin/sh\n");
322
	fwrite($fd, "sleep 5\n");
323
	fwrite($fd, "killall php\n");
324
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
325
	if(file_exists("/var/etc/lighty-CaptivePortal.conf"))
326
		fwrite($fd, "/usr/local/sbin/lighttpd -f /var/etc/lighty-CaptivePortal.conf\n");
327
	fclose($fd);
328
	mwexec_bg("sh /tmp/restart_lighty");
329
	echo "\n";
330

    
331
}
332

    
333
function isUrl($url = "") {
334
	if($url) 
335
		if(strstr($url, "rcs.pfsense.org") or 
336
			strstr($url, "mainline") or 
337
				strstr($url, ".git"))
338
					return true;
339
	return false;
340
}
341

    
342
function run_cmds($cmds) {
343
	global $debug;
344
	foreach($cmds as $cmd) {
345
		if($debug)
346
			echo "Running $cmd";
347
		exec($cmd);
348
	}
349
}
350

    
351
conf_mount_ro();
(5-5/8)