Project

General

Profile

Download (11.9 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 $argv;
20
global $command_split;
21

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

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

    
28
if(!file_exists("/usr/local/bin/git")) {
29
	echo "Cannot find git, fetching...";
30
	if (($g['platform'] == "nanobsd") || ($g['platform'] == "embedded"))
31
		$pkgtmpdir = "/usr/bin/env PKG_TMPDIR=/root/ ";
32
	system("{$pkgtmpdir}/usr/sbin/pkg_add -r git");
33
}
34

    
35
# Remove mainline if exists (older)
36
if(is_dir("/root/pfsense/mainline")) 
37
	exec("rm -rf /root/pfsense/mainline");
38

    
39
# Remove RELENG_1_2 if exists (older)
40
if(is_dir("/root/pfsense/RELENG_1_2")) 
41
	exec("rm -rf /root/pfsense/RELENG_1_2");
42

    
43
# Remove HEAD if exists (older)
44
if(is_dir("/root/pfsense/HEAD")) 
45
	exec("rm -rf /root/pfsense/HEAD");
46

    
47
/* NOTE: Set branches here */
48
$branches = array(
49
	"master" => "2.0 development branch",
50
	"RELENG_1_2" => "1.2* release branch",
51
	"build_commit" => "The commit originally used to build the image"
52
);
53

    
54
if(file_exists("/root/cvssync_backup.tgz")) {
55
	$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
56
	$tmp = array("RESTORE" => "Restores prior CVSSync backup data performed at {$backup_date}");
57
	$branches = array_merge($branches, $tmp);
58
}
59

    
60
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
61
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url", $output_str, $ret);
62
	if(is_array($output_str) && !empty($output_str[0]))
63
		$GIT_REPO = $output_str[0];
64
	unset($output_str);
65
}
66

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

    
84
if($argv[4] == "NOBACKUP") 
85
	$nobackup = true;
86
else 
87
	$nobackup = false;
88

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

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

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

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

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

    
180
if($nobackup == false) {
181
	echo "===> Backing up current pfSense information...\n";
182
	echo "===> Please wait... ";
183
	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 /");
184
	$size = filesize("/root/cvssync_backup.tgz");
185
	echo "{$size} bytes.\n\n";
186
	sleep(5);
187
}
188

    
189
echo "===> Checking out $branch\n";
190

    
191
// Git commands for resetting to the specified branch
192
if($branch == "build_commit") {
193
	$git_cmd = array(
194
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " 2>/dev/null",
195
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
196
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard `cat /etc/version.lastcommit`"
197
	);
198
} else {
199
	$git_cmd = array(
200
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$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 " . escapeshellarg("origin/{$branch}")
203
	);
204
}
205

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

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

    
236
exec("mkdir -p /tmp/lighttpd/cache/compress/");
237

    
238
// Nuke CVS and pfSense tarballs
239
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
240
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
241

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

    
261
echo "===> Installing new files...\n";
262

    
263
// Don't include the .git directory in the copy
264
exec("mv $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git $CODIR/pfSenseGITREPO/gitsync_temp.git");
265

    
266
if($g['platform'] == "pfSense") 
267
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -Uxpf -)";
268
else 
269
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -xpf -) 2>/dev/null";
270
exec($command);
271

    
272
exec("mv $CODIR/pfSenseGITREPO/gitsync_temp.git $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git");
273

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

    
277
if(!$upgrading)
278
	post_cvssync_commands();
279

    
280
echo "===> Checkout complete.\n";
281
echo "\n";
282
if(!$upgrading)
283
	echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
284
else
285
	echo "Your system is now sync'd.\n\n";
286

    
287
function post_cvssync_commands() {
288
	echo "===> Removing FAST-CGI temporary files...\n";
289
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
290
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
291

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

    
294
	echo "===> Upgrading configuration (if needed)...\n";
295
	convert_config();
296

    
297
	echo "===> Configuring filter...";
298
	exec("/etc/rc.filter_configure_sync");
299
	exec("pfctl -f /tmp/rules.debug");
300
	echo "\n";
301

    
302
	if(file_exists("/etc/rc.php_ini_setup")) {
303
		echo "===> Running /etc/rc.php_ini_setup...";
304
		exec("/etc/rc.php_ini_setup");
305
		echo "\n";
306
	}
307

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

    
324
}
325

    
326
function isUrl($url = "") {
327
	if($url) 
328
		if(strstr($url, "rcs.pfsense.org") or 
329
			strstr($url, "mainline") or 
330
				strstr($url, ".git"))
331
					return true;
332
	return false;
333
}
334

    
335
function run_cmds($cmds) {
336
	global $debug;
337
	foreach($cmds as $cmd) {
338
		if($debug)
339
			echo "Running $cmd";
340
		exec($cmd);
341
	}
342
}
343

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