Project

General

Profile

Download (8.38 KB) Statistics
| Branch: | Tag: | Revision:
1
/*   cvs_sync
2
 *   Written by Scott Ullrich
3
 *   (C)2005-2007 Scott Ullrich
4
 *   Part of the pfSense project pfSsh.php subsystem
5
 */
6

    
7
require_once("globals.inc");
8
require_once("filter.inc");
9
require_once("shaper.inc");
10
require_once("rrd.inc");
11
	
12
conf_mount_rw();
13

    
14
$GIT_REPO="http://gitweb.pfsense.org/pfsense/mainline.git";
15
$CODIR =  "/root/pfsense/";
16

    
17
global $argv;
18
global $command_split;
19

    
20
unlink_if_exists("/tmp/config.cache");
21

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

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

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

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

    
39
/* NOTE: Set branches here */
40
$branches = array(
41
	"master" => "2.0 development branch",
42
	"RELENG_1_2" => "1.2* release branch"
43
);
44

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

    
51
if($command_split[2]) {
52
	$branch = $command_split[2];
53
} else {
54
	if(!$argv[3]) {
55
		echo "\nPlease select which branch you would like to sync against:\n\n";
56
		foreach($branches as $branchname => $branchdesc) {
57
			echo "{$branchname} \t {$branchdesc}\n";
58
		}
59
		echo "\nOr alternatively you may enter a custom RCS branch URL (HTTP).\n\n";
60
		$branch = readline("> ");
61
		echo "\n";
62
	} else {
63
		$branch = $argv[3];
64
	}
65
}
66

    
67
if($argv[4] == "NOBACKUP") 
68
	$nobackup = true;
69
else 
70
	$nobackup = false;
71

    
72
exec("mkdir -p /root/pfsense/$BRANCHTAG");
73

    
74
$found = false;
75
foreach($branches as $branchname => $branchdesc) {
76
	if($branchname == $branch) 
77
		$found = true;
78
}
79
if(!$found) {
80
	if(isURL($branch)) {
81
		echo "\n";
82
		echo "NOTE: $branch was not found.\n\n";
83
		$command = readline("Is this a custom GIT URL? [y]? ");
84
		if(strtolower($command) == "y" or $command == "") {
85
			$GIT_REPO = $branch;
86
			$command = readline("Checkout which branch [master]? ");
87
			if($command == "")
88
				$branch = "master";
89
			if($command) 
90
				$branch = $command;
91
			$found = true;
92
		}
93
	}
94
	if(!$found) {
95
		echo "\nNo valid branch found.  Exiting.\n\n";
96
		exit;
97
	}
98
}
99

    
100
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
101
	if(!file_exists("/root/cvssync_backup.tgz")) {
102
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
103
		exit();
104
	}
105
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
106
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
107
	post_cvssync_commands();
108
	exit();
109
} else {
110
	$nobackup = true; // do not backup embedded, livecd
111
}
112

    
113
if($nobackup == false) {
114
	echo "===> Backing up current pfSense information...\n";
115
	echo "===> Please wait... ";
116
	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 /");
117
	$size = filesize("/root/cvssync_backup.tgz");
118
	echo "{$size} bytes.\n\n";
119
	sleep(5);
120
}
121

    
122
echo "===> Checking out $branch\n";
123
exec("mkdir -p /root/pfsense/$branch");
124

    
125
// Git 'er done!
126
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
127
	echo "===> Fetching updates...\n";
128
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
129
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d");
130
	if($branch == "master") {
131
		$git_cmd = array(
132
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset HEAD --hard", 
133
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git rebase origin"
134
		);
135
		run_cmds($git_cmd);
136
	} else {
137
		$git_cmd = array(
138
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset HEAD --hard",
139
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout master",
140
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch -D $branch",
141
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch",
142
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git rebase origin",
143
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -b $branch origin/$branch"
144
		);
145
		run_cmds($git_cmd);
146
	}
147
} else {
148
    exec("mkdir -p $CODIR/pfSenseGITREPO");
149
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
150
	exec("cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO");
151
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
152
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
153
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
154
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
155
	if($branch == "master") {
156
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout master");
157
	} else {
158
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -b $branch origin/$branch");
159
	}
160
}
161

    
162
exec("mkdir -p /tmp/lighttpd/cache/compress/");
163

    
164
// Nuke CVS and pfSense tarballs
165
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
166
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
167

    
168
// Remove files that we do not want to overwrite the system with 
169
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
170
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
171
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
172
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
173
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
174
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
175
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
176
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
177
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
178
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
179
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
180
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
181
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
182
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
183
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
184
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
185
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
186

    
187
echo "===> Installing new files...\n";
188

    
189
if($g['platform'] == "pfSense") 
190
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -Uxpf -)";
191
else 
192
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -xpf -) 2>/dev/null";
193
exec($command);
194

    
195
post_cvssync_commands();
196

    
197
run_cmds($git_cmd);
198

    
199
echo "===> Checkout complete.\n";
200
echo "\n";
201
echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
202

    
203
function post_cvssync_commands() {
204
	echo "===> Removing FAST-CGI temporary files...\n";
205
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
206
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
207

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

    
210
	echo "===> Upgrading configuration (if needed)...\n";
211
	convert_config();
212

    
213
	echo "===> Restarting check_reload_status...\n";
214
	exec("killall check_reload_status");
215
	mwexec_bg("nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status");
216

    
217
	echo "===> Configuring filter...";
218
	exec("/etc/rc.filter_configure_sync");
219
	exec("pfctl -f /tmp/rules.debug");
220
	echo "\n";
221

    
222
	if(file_exists("/etc/rc.php_ini_setup")) {
223
		echo "===> Running /etc/rc.php_ini_setup...";
224
		exec("/etc/rc.php_ini_setup");
225
		echo "\n";
226
	}
227

    
228
	/* lock down console if necessary */
229
	echo "===> Locking down the console if needed...\n";
230
	if(isset($config['system']['disableconsolemenu'])) {
231
		auto_login(false);
232
	} else {
233
		auto_login(true);
234
	}
235
	
236
	echo "===> Signaling PHP and Lighty restart...";
237
	$fd = fopen("/tmp/restart_lighty", "w");
238
	fwrite($fd, "#!/bin/sh\n");
239
	fwrite($fd, "sleep 5\n");
240
	fwrite($fd, "killall php\n");
241
	fwrite($fd, "touch /tmp/restart_webgui\n");
242
	fclose($fd);
243
	mwexec_bg("sh /tmp/restart_lighty");
244
	echo "\n";
245

    
246
}
247

    
248
function isUrl($url = "") {
249
	if($url) 
250
		if(strstr($url, "rcs.pfsense.org") or 
251
			strstr($url, "mainline") or 
252
				strstr($url, ".git"))
253
					return true;
254
	return false;
255
}
256

    
257
function run_cmds($cmds) {
258
	global $debug;
259
	foreach($cmds as $cmd) {
260
		if($debug)
261
			echo "Running $cmd";
262
		exec($cmd);
263
	}
264
}
265

    
266
conf_mount_ro();
(3-3/6)