Project

General

Profile

Download (12.5 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_PKG = "git"; // Either "git" or the full package URL
17
$GIT_REPO = "git://github.com/bsdperimeter/pfsense.git";
18
$DEFAULT_BRANCH = "master";
19
$CODIR =  "/root/pfsense/";
20
$GITSYNC_MERGE = "/root/.gitsync_merge";
21

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

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

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

    
38
unlink_if_exists("/tmp/config.cache");
39

    
40
if(!file_exists("/usr/local/bin/git")) {
41
	echo "Cannot find git, fetching...";
42
	if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded")) {
43
		$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
44
		$pkgstagingdir = "/root/tmp";
45
		if (!is_dir($pkgstagingdir))
46
			mkdir($pkgstagingdir);
47
		$pkgstaging = "-t {$pkgstagingdir}/instmp.XXXXXX";
48
	}
49
	system("{$pkgtmpdir}/usr/sbin/pkg_add {$pkgstaging} -r {$GIT_PKG}");
50
}
51

    
52
# Remove mainline if exists (older)
53
if(is_dir("/root/pfsense/mainline")) 
54
	exec("rm -rf /root/pfsense/mainline");
55

    
56
# Remove RELENG_1_2 if exists (older)
57
if(is_dir("/root/pfsense/RELENG_1_2")) 
58
	exec("rm -rf /root/pfsense/RELENG_1_2");
59

    
60
# Remove HEAD if exists (older)
61
if(is_dir("/root/pfsense/HEAD")) 
62
	exec("rm -rf /root/pfsense/HEAD");
63

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

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

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

    
94
if($argv[4] == "NOBACKUP") 
95
	$nobackup = true;
96
else 
97
	$nobackup = false;
98

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

    
111
$found = false;
112
foreach($branches as $branchname => $branchdesc) {
113
	if($branchname == $branch) 
114
		$found = true;
115
}
116
if(!$found) {
117
	if(isURL($branch) && !$upgrading) {
118
		if($command_split[3]) {
119
			$GIT_REPO = $branch;
120
			$branch = $command_split[3];
121
			$found = true;
122
		}
123
		else if($argv[4]) {
124
			$GIT_REPO = $branch;
125
			$branch = $argv[4];
126
			$found = true;
127
		}
128
		else {
129
			echo "\n";
130
			echo "NOTE: $branch was not found.\n\n";
131
			$command = readline("Is this a custom GIT URL? [y]? ");
132
			if(strtolower($command) == "y" or $command == "") {
133
				$GIT_REPO = $branch;
134
				$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
135
				if($command == "")
136
					$branch = $DEFAULT_BRANCH;
137
				if($command) 
138
					$branch = $command;
139
				$found = true;
140
			}
141
		}
142
	}
143
	if(!$found) {
144
		echo "\nNo valid branch found.  Exiting.\n\n";
145
		conf_mount_ro();
146
		exit;
147
	}
148
}
149

    
150
$merge_repos = array();
151
if(file_exists($GITSYNC_MERGE)) {
152
	$gitsync_merges = file($GITSYNC_MERGE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
153
	if(!empty($gitsync_merges) && is_array($gitsync_merges)) {
154
		echo "\n===> Automatic merge list read from ${GITSYNC_MERGE}\n";
155
		foreach($gitsync_merges as $merge_line_num => $merge_line) {
156
			$merge_comments = explode("#", trim($merge_line));
157
			if(empty($merge_comments[0]))
158
				continue;
159

    
160
			$merge_line = explode(" ", trim($merge_comments[0]));
161
			if(count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
162
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
163
				echo "Expected parameters:  repository_url branch\n";
164
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
165
				echo "Aborting automatic merge.\n\n";
166
				$merge_repos = array();
167
				break;
168
			}
169
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
170
		}
171
	}
172
}
173
if(!$command_split[2] && !$argv[3] && !$upgrading) {
174
	do {
175
		echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
176
		$merge_repo = readline("> ");
177
		if(!empty($merge_repo)) {
178
			$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
179
			if($merge_branch == "")
180
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
181
			else if($merge_branch)
182
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
183
		}
184
	} while(!empty($merge_repo));
185
}
186

    
187
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
188
	if(!file_exists("/root/cvssync_backup.tgz")) {
189
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
190
		conf_mount_ro();
191
		exit();
192
	}
193
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
194
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
195
	post_cvssync_commands();
196
	conf_mount_ro();
197
	exit();
198
} else {
199
	$nobackup = true; // do not backup embedded, livecd
200
}
201

    
202
if($nobackup == false) {
203
	echo "===> Backing up current pfSense information...\n";
204
	echo "===> Please wait... ";
205
	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 /");
206
	$size = filesize("/root/cvssync_backup.tgz");
207
	echo "{$size} bytes.\n\n";
208
	sleep(5);
209
}
210

    
211
echo "===> Checking out $branch\n";
212

    
213
// Git commands for resetting to the specified branch
214
if($branch == "build_commit") {
215
	$git_cmd = array(
216
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " 2>/dev/null",
217
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
218
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
219
	);
220
} else {
221
	$git_cmd = array(
222
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
223
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
224
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard " . escapeshellarg("origin/{$branch}")
225
	);
226
}
227

    
228
// Git 'er done!
229
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
230
	echo "===> Fetching updates...\n";
231
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url " . escapeshellarg($GIT_REPO));
232
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
233
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d");
234
	run_cmds($git_cmd);
235
} else {
236
    exec("mkdir -p $CODIR/pfSenseGITREPO");
237
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
238
	exec("cd $CODIR/pfSenseGITREPO && git clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
239
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
240
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
241
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
242
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
243
	run_cmds($git_cmd);
244
}
245

    
246
foreach($merge_repos as $merge_repo) {
247
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
248
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
249
	unset($output_str);
250
	if($ret <> 0) {
251
		echo "\nMerge failed.  Aborting sync.\n\n";
252
		run_cmds($git_cmd);
253
		conf_mount_ro();
254
		exit;
255
	}
256
}
257

    
258
exec("mkdir -p /tmp/lighttpd/cache/compress/");
259

    
260
// Nuke CVS and pfSense tarballs
261
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
262
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
263

    
264
// Remove files that we do not want to overwrite the system with 
265
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
266
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
267
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
268
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
269
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
270
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
271
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
272
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
273
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
274
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
275
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
276
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
277
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
278
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
279
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
280
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
281
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
282

    
283
echo "===> Installing new files...\n";
284

    
285
if($g['platform'] == "pfSense") 
286
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - --exclude .git . | (cd / ; tar -Uxpf -)";
287
else 
288
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - --exclude .git . | (cd / ; tar -xpf -) 2>/dev/null";
289
exec($command);
290

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

    
294
// Remove obsolete files
295
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
296
foreach($files_to_remove as $file_to_remove) 
297
	if(file_exists($file_to_remove)) 
298
		exec("/bin/rm -f $file_to_remove");
299

    
300
if(!$upgrading)
301
	post_cvssync_commands();
302

    
303
echo "===> Checkout complete.\n";
304
echo "\n";
305
if(!$upgrading)
306
	echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
307
else
308
	echo "Your system is now sync'd.\n\n";
309

    
310
function post_cvssync_commands() {
311
	echo "===> Removing FAST-CGI temporary files...\n";
312
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
313
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
314

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

    
317
	echo "===> Upgrading configuration (if needed)...\n";
318
	convert_config();
319

    
320
	echo "===> Configuring filter...";
321
	exec("/etc/rc.filter_configure_sync");
322
	exec("pfctl -f /tmp/rules.debug");
323
	echo "\n";
324

    
325
	if(file_exists("/etc/rc.php_ini_setup")) {
326
		echo "===> Running /etc/rc.php_ini_setup...";
327
		exec("/etc/rc.php_ini_setup");
328
		echo "\n";
329
	}
330

    
331
	/* lock down console if necessary */
332
	echo "===> Locking down the console if needed...\n";
333
	auto_login();
334
	
335
	echo "===> Signaling PHP and Lighty restart...";
336
	$fd = fopen("/tmp/restart_lighty", "w");
337
	fwrite($fd, "#!/bin/sh\n");
338
	fwrite($fd, "sleep 5\n");
339
	fwrite($fd, "killall php\n");
340
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
341
	if(file_exists("/var/etc/lighty-CaptivePortal.conf"))
342
		fwrite($fd, "/usr/local/sbin/lighttpd -f /var/etc/lighty-CaptivePortal.conf\n");
343
	fclose($fd);
344
	mwexec_bg("sh /tmp/restart_lighty");
345
	echo "\n";
346

    
347
}
348

    
349
function isUrl($url = "") {
350
	if($url) 
351
		if(strstr($url, "rcs.pfsense.org") or 
352
			strstr($url, "mainline") or 
353
				strstr($url, ".git") or strstr($url, "git://"))
354
					return true;
355
	return false;
356
}
357

    
358
function run_cmds($cmds) {
359
	global $debug;
360
	foreach($cmds as $cmd) {
361
		if($debug)
362
			echo "Running $cmd";
363
		exec($cmd);
364
	}
365
}
366

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