Project

General

Profile

Download (7.23 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
/*
8
 * Functions
9
 */
10
function post_cvssync_commands() {
11
	echo "===> Removing FAST-CGI temporary files...\n";
12
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
13
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
14

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

    
17
	check_php_status();
18

    
19
	echo "===> Upgrading configuration (if needed)...\n";
20
	convert_config();
21

    
22
	echo "===> Syncing system passwords...\n";
23
	local_sync_accounts();
24

    
25
	echo "===> Restarting check_reload_status...\n";
26
	exec("killall check_reload_status");
27
	mwexec_bg("nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status");
28

    
29
	echo "===> Configuring filter...";
30
	exec("/etc/rc.filter_configure_sync");
31
	exec("pfctl -f /tmp/rules.debug");
32
	echo "\n";
33

    
34
	echo "===> Signaling PHP and Lighty restart...";
35
	$fd = fopen("/tmp/restart_lighty", "w");
36
	fwrite($fd, "#!/bin/sh\n");
37
	fwrite($fd, "sleep 5\n");
38
	fwrite($fd, "killall php\n");
39
	fwrite($fd, "touch /tmp/restart_webgui\n");
40
	fclose($fd);
41
	mwexec_bg("sh /tmp/restart_lighty");
42
	echo "done.\n";
43
}
44

    
45
function check_php_status() {
46
	echo "===> Ensuring that PHP is working before going further...";
47
	$test_php = `php -v`;
48
	if(!strstr($test_php, "PHP")) {
49
		echo "OH NOES.\n+++> Ruh roh, you're PHP is now toast.\n";
50
		if(file_exists("/root/cvssync_backup.tgz")) {
51
			echo "+++> Found previous CVSSync backup.  Restoring...";
52
			mwexec("cd / && tar xzpfU /root/cvssync_backup.tgz -C /");
53
			echo "done.\n";
54
			echo "\nWe'll exit now, try not to do whatever you did, again.\n";
55
			exit;
56
		} else {
57
			echo "+++> Sorry, we could not locate a cvssync backup to restore from. Good luck rescuing your system! :( \n";
58
			exit;
59
		}
60
	} else {
61
		echo "it's good!\n";
62
	}
63
}
64

    
65
function create_supfile($branch) {
66

    
67
	if($branch == "HEAD") 
68
		$BRANCHTAG = ".";
69
	else
70
		$BRANCHTAG = $branch;
71

    
72
	$supfile = "
73
*default host=cvs.pfsense.com
74
*default base=/root/pfsense/$branch
75
*default release=cvs tag=$BRANCHTAG
76
*default delete use-rel-suffix
77
pfSense
78
*default compress
79
";
80

    
81
	file_put_contents("/var/etc/pfSense-supfile", $supfile);
82
}
83

    
84
/*
85
 *
86
 * 
87
 * cvssync script starts here 
88
 *
89
 * 
90
 */
91

    
92
echo "===> cvssync is starting at " . exec(date) . "\n";
93

    
94
conf_mount_rw();
95

    
96
echo "===> Backing up original rules.debug...";
97
exec("cp /tmp/rules.debug /tmp/rules.beforecvssync");
98
echo "done.\n";
99

    
100
if(is_dir("/home/pfsense")) {
101
	echo "\nRelocating downloaded cvssync data, please wait...";
102
	exec("rm -rf /home/pfsense");
103
	echo " done.\n";
104
}
105

    
106
unlink_if_exists("/tmp/config.cache");
107

    
108
if(!file_exists("/usr/local/bin/cvsup")) {
109
	echo "Cannot find cvsup, pkg_add in progress...";
110
	system("/usr/sbin/pkg_add -r http://files.pfsense.org/packages/7/All/cvsup-without-gui-16.1h_4.tbz");
111
}
112

    
113
if(!file_exists("/usr/bin/cvs")) {
114
	echo "Cannot find cvs, fetching static cvs...";
115
	system("fetch -o /usr/bin/cvs http://files.pfsense.org/extras/7/cvs");
116
	system("chmod a+rx /usr/bin/cvs");
117
}
118

    
119
if(!file_exists("/usr/bin/cvs") or !file_exists("/usr/local/bin/cvsup")) {
120
	echo "Could not fetch cvs or cvsup.   exiting.";
121
	exit;
122
}
123

    
124
global $argv;
125
global $command_split;
126

    
127
/* NOTE: Set branches here */
128
$branches = array(
129
				  "RELENG_1" => "1.3 development branch",
130
				  "RELENG_1_2" => "1.2 release branch"
131
				);
132

    
133
if(file_exists("/root/cvssync_backup.tgz")) {
134
	$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
135
	$tmp = array("RESTORE" => "Restores prior CVSSync backup from {$backup_date}");
136
	$branches = array_merge($branches, $tmp);
137
}
138

    
139
if($command_split[2]) {
140
	$branch = $command_split[2];
141
} else {
142
	if(!$argv[3]) {
143
		echo "\nPlease select which branch you would like to sync against:\n\n";
144
		foreach($branches as $branchname => $branchdesc) {
145
			echo "{$branchname} \t {$branchdesc}\n";
146
		}
147
		$branch = readline("> ");
148
		echo "\n";
149
	} else {
150
		$branch = $argv[3];
151
	}
152
}
153
create_supfile($branch);
154

    
155
// Specify if we should backup system
156
if($argv[4] == "NOBACKUP") 
157
	$nobackup = true;
158
else 
159
	$nobackup = false;
160

    
161
if($g['platform'] == "embedded" || $g['platform'] == "livecd" || $g['platform'] == "wrap") 
162
	$nobackup = true; // do not backup embedded, livecd
163

    
164
$CODIR =  "/root/pfsense/$branch";
165

    
166
exec("mkdir -p /root/pfsense/$BRANCHTAG");
167

    
168
$found = false;
169
foreach($branches as $branchname => $branchdesc) 
170
	if($branchname == $branch) 
171
		$found = true;
172
if(!$found) {
173
	echo "\nInvalid branch {$branch}.\n";
174
	exit;
175
}
176

    
177
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
178
	if(!file_exists("/root/cvssync_backup.tgz")) {
179
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
180
		exit();
181
	}
182
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
183
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
184
	post_cvssync_commands();
185
	exit();
186
}
187

    
188
if($nobackup == false) {
189
	echo "===> Backing up current pfSense information...\n";
190
	echo "===> Specify NOBACKUP at the end of pfSsh.php command to turn this feature off.\n";
191
	echo "===> Please wait... ";
192
	$exclude  = " --exclude /var/dhcpd/dev/";
193
	$exclude .= " --exclude /root";
194
	$exclude .= " --exclude /dev";
195
	$exclude .= " --exclude /var/db/racoon/racoon.sock";
196
	$exclude .= " --exclude /tmp";
197
	$exclude .= " --exclude /var/run";
198
	$exclude .= " --exclude /var/empty";
199
	exec("tar czPf /root/cvssync_backup.tgz {$exclude} /");
200
	$size = filesize("/root/cvssync_backup.tgz");
201
	echo "{$size} bytes.\n\n";
202
}
203

    
204
echo "===> Checking out $branch\n";
205
exec("mkdir -p /root/pfsense/$branch");
206
system("cvsup /var/etc/pfSense-supfile");
207
exec("rm -rf /usr/local/www/includes/*");
208
exec("rm -rf /usr/local/www/javascript/*");
209
exec("find /usr/local/www -name \"*.js\" -exec rm {} \;");
210

    
211
exec("mkdir -p /tmp/lighttpd/cache/compress/");
212

    
213
exec("cd ${CODIR}/ && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
214
exec("cd ${CODIR}/ && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
215
exec("rm -rf ${CODIR}/conf*");
216
exec("rm ${CODIR}/pfSense/usr/local/www/trigger_initial_wizard 2>/dev/null");
217
exec("rm ${CODIR}/pfSense/etc/crontab 2>/dev/null");
218
exec("rm ${CODIR}/pfSense/etc/master.passwd 2>/dev/null");
219
exec("rm ${CODIR}/pfSense/etc/passwd 2>/dev/null");
220
exec("rm ${CODIR}/pfSense/etc/fstab 2>/dev/null");
221
exec("rm ${CODIR}/pfSense/etc/ttys 2>/dev/null");
222
exec("rm ${CODIR}/pfSense/etc/group 2>/dev/null");
223
exec("rm ${CODIR}/pfSense/etc/fstab 2>/dev/null");
224
exec("rm ${CODIR}/pfSense/etc/platform 2>/dev/null");
225
exec("rm ${CODIR}/pfSense/boot/device.hints 2>/dev/null");
226
exec("rm ${CODIR}/pfSense/boot/loader.conf 2>/dev/null");
227
exec("rm ${CODIR}/pfSense/boot/loader.rc 2>/dev/null");
228
exec("rm -rf ${CODIR}/pfSense/conf/ 2>/dev/null");
229
exec("rm -rf ${CODIR}/pfSense/cf/ 2>/dev/null");
230
exec("rm -rf ${CODIR}/pfSense/root/.shrc");
231
exec("rm -rf ${CODIR}/pfSense/root/.tcshrc");
232
exec("rm -f ${CODIR}/pfSense/etc/syslog.conf 2>/dev/null");
233

    
234
echo "===> Installing new files...";
235
if($g['platform'] == "pfSense") 
236
	$command = "cd $CODIR/pfSense ; tar -cpf - . | (cd / ; tar -Uxpf -)";
237
else 
238
	$command = "cd $CODIR/pfSense ; tar -cpf - . | (cd / ; tar -xpf -) 2>/dev/null";
239
exec($command);
240
echo "done.\n";
241

    
242
post_cvssync_commands();
243

    
244
echo "===> Checkout and file installation complete.\n";
245
echo "===> Signaling 5 second PHP and lighttpd reload\n";
246

    
247
conf_mount_ro();
248

    
249
echo "===> Showing rules.debug differences\n";
250
system("diff -u /tmp/rules.beforecvssync /tmp/rules.debug");
251

    
252
echo "===> Waiting for PHP/lighttpd restart...";
253
sleep(10);
254

    
(1-1/7)