Project

General

Profile

Download (12 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="http://gitweb.pfsense.org/pfsense/mainline.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 git");
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.0 development branch",
56
	"RELENG_1_2" => "1.2* release branch",
57
	"build_commit" => "The commit originally used to build the image"
58
);
59

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
330
}
331

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

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

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