Project

General

Profile

Download (11.4 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
unlink_if_exists("/tmp/config.cache");
23

    
24
if(!file_exists("/usr/local/bin/git")) {
25
	echo "Cannot find git, fetching...";
26
    system("pkg_add -r git");
27
}
28

    
29
# Remove mainline if exists (older)
30
if(is_dir("/root/pfsense/mainline")) 
31
	exec("rm -rf /root/pfsense/mainline");
32

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

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

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

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

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

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

    
78
if($argv[4] == "NOBACKUP") 
79
	$nobackup = true;
80
else 
81
	$nobackup = false;
82

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

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

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

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

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

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

    
183
echo "===> Checking out $branch\n";
184

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

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

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

    
230
exec("mkdir -p /tmp/lighttpd/cache/compress/");
231

    
232
// Nuke CVS and pfSense tarballs
233
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
234
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
235

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

    
255
echo "===> Installing new files...\n";
256

    
257
// Don't include the .git directory in the copy
258
exec("mv $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git $CODIR/pfSenseGITREPO/gitsync_temp.git");
259

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

    
266
exec("mv $CODIR/pfSenseGITREPO/gitsync_temp.git $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git");
267

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

    
271
post_cvssync_commands();
272

    
273
echo "===> Checkout complete.\n";
274
echo "\n";
275
echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
276

    
277
function post_cvssync_commands() {
278
	echo "===> Removing FAST-CGI temporary files...\n";
279
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
280
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
281

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

    
284
	echo "===> Upgrading configuration (if needed)...\n";
285
	convert_config();
286

    
287
	echo "===> Restarting check_reload_status...\n";
288
	exec("killall check_reload_status");
289
	mwexec_bg("nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status");
290

    
291
	echo "===> Configuring filter...";
292
	exec("/etc/rc.filter_configure_sync");
293
	exec("pfctl -f /tmp/rules.debug");
294
	echo "\n";
295

    
296
	if(file_exists("/etc/rc.php_ini_setup")) {
297
		echo "===> Running /etc/rc.php_ini_setup...";
298
		exec("/etc/rc.php_ini_setup");
299
		echo "\n";
300
	}
301

    
302
	/* lock down console if necessary */
303
	echo "===> Locking down the console if needed...\n";
304
	auto_login();
305
	
306
	echo "===> Signaling PHP and Lighty restart...";
307
	$fd = fopen("/tmp/restart_lighty", "w");
308
	fwrite($fd, "#!/bin/sh\n");
309
	fwrite($fd, "sleep 5\n");
310
	fwrite($fd, "killall php\n");
311
	fwrite($fd, "touch /tmp/restart_webgui\n");
312
	fclose($fd);
313
	mwexec_bg("sh /tmp/restart_lighty");
314
	echo "\n";
315

    
316
}
317

    
318
function isUrl($url = "") {
319
	if($url) 
320
		if(strstr($url, "rcs.pfsense.org") or 
321
			strstr($url, "mainline") or 
322
				strstr($url, ".git"))
323
					return true;
324
	return false;
325
}
326

    
327
function run_cmds($cmds) {
328
	global $debug;
329
	foreach($cmds as $cmd) {
330
		if($debug)
331
			echo "Running $cmd";
332
		exec($cmd);
333
	}
334
}
335

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