Project

General

Profile

Download (11.6 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
    system("pkg_add -r git");
31
}
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
187
echo "===> Checking out $branch\n";
188

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

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

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

    
234
exec("mkdir -p /tmp/lighttpd/cache/compress/");
235

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

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

    
259
echo "===> Installing new files...\n";
260

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

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

    
270
exec("mv $CODIR/pfSenseGITREPO/gitsync_temp.git $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git");
271

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

    
275
if(!$upgrading)
276
	post_cvssync_commands();
277

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

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

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

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

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

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

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

    
320
}
321

    
322
function isUrl($url = "") {
323
	if($url) 
324
		if(strstr($url, "rcs.pfsense.org") or 
325
			strstr($url, "mainline") or 
326
				strstr($url, ".git"))
327
					return true;
328
	return false;
329
}
330

    
331
function run_cmds($cmds) {
332
	global $debug;
333
	foreach($cmds as $cmd) {
334
		if($debug)
335
			echo "Running $cmd";
336
		exec($cmd);
337
	}
338
}
339

    
340
conf_mount_ro();
(4-4/7)