Project

General

Profile

Download (9.7 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
	"build_commit" => "The commit originally used to build the image"
44
);
45

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

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

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

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

    
81
exec("mkdir -p /root/pfsense/$BRANCHTAG");
82

    
83
$found = false;
84
foreach($branches as $branchname => $branchdesc) {
85
	if($branchname == $branch) 
86
		$found = true;
87
}
88
if(!$found) {
89
	if(isURL($branch)) {
90
		echo "\n";
91
		echo "NOTE: $branch was not found.\n\n";
92
		$command = readline("Is this a custom GIT URL? [y]? ");
93
		if(strtolower($command) == "y" or $command == "") {
94
			$GIT_REPO = $branch;
95
			$command = readline("Checkout which branch [master]? ");
96
			if($command == "")
97
				$branch = "master";
98
			if($command) 
99
				$branch = $command;
100
			$found = true;
101
		}
102
	}
103
	if(!$found) {
104
		echo "\nNo valid branch found.  Exiting.\n\n";
105
		exit;
106
	}
107
}
108

    
109
$merge_repos = array();
110
if(!$command_split[2] && !$argv[3]) {
111
	do {
112
		echo "\nAdd a custom RCS branch URL (HTTP) to merge in or press enter for none.\n\n";
113
		$merge_repo = readline("> ");
114
		if(!empty($merge_repo)) {
115
			$merge_branch = readline("Merge which branch [master]? ");
116
			if($merge_branch == "")
117
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => 'master');
118
			else if($merge_branch)
119
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
120
		}
121
	} while(!empty($merge_repo));
122
}
123

    
124
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
125
	if(!file_exists("/root/cvssync_backup.tgz")) {
126
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
127
		exit();
128
	}
129
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
130
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
131
	post_cvssync_commands();
132
	exit();
133
} else {
134
	$nobackup = true; // do not backup embedded, livecd
135
}
136

    
137
if($nobackup == false) {
138
	echo "===> Backing up current pfSense information...\n";
139
	echo "===> Please wait... ";
140
	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 /");
141
	$size = filesize("/root/cvssync_backup.tgz");
142
	echo "{$size} bytes.\n\n";
143
	sleep(5);
144
}
145

    
146
echo "===> Checking out $branch\n";
147
exec("mkdir -p /root/pfsense/$branch");
148

    
149
// Git 'er done!
150
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
151
	echo "===> Fetching updates...\n";
152
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url $GIT_REPO");
153
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
154
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d");
155
	if($branch == "build_commit") {
156
		$git_cmd = array(
157
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch $branch 2>/dev/null",
158
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f $branch 2>/dev/null",
159
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard `cat /etc/version.lastcommit`"
160
		);
161
	} else {
162
		$git_cmd = array(
163
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch $branch origin/$branch 2>/dev/null",
164
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f $branch 2>/dev/null",
165
			"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard origin/$branch"
166
		);
167
	}
168
	run_cmds($git_cmd);
169
} else {
170
    exec("mkdir -p $CODIR/pfSenseGITREPO");
171
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
172
	exec("cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO");
173
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
174
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
175
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
176
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
177
	if($branch == "master") {
178
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout master");
179
	} else if($branch == "build_commit") {
180
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -b build_commit `cat /etc/version.lastcommit`");
181
	} else {
182
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -b $branch origin/$branch");
183
	}
184
}
185

    
186
foreach($merge_repos as $merge_repo) {
187
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
188
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git pull {$merge_repo['repo']} {$merge_repo['branch']}", $output_str, $ret);
189
	unset($output_str);
190
	if($ret <> 0) {
191
		echo "\nMerge failed.  Aborting sync.\n\n";
192
		run_cmds($git_cmd);
193
		exit;
194
	}
195
}
196

    
197
exec("mkdir -p /tmp/lighttpd/cache/compress/");
198

    
199
// Nuke CVS and pfSense tarballs
200
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
201
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
202

    
203
// Remove files that we do not want to overwrite the system with 
204
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
205
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
206
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
207
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
208
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
209
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
210
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
211
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
212
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
213
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
214
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
215
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
216
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
217
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
218
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
219
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
220
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
221

    
222
echo "===> Installing new files...\n";
223

    
224
if($g['platform'] == "pfSense") 
225
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -Uxpf -)";
226
else 
227
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -xpf -) 2>/dev/null";
228
exec($command);
229

    
230
post_cvssync_commands();
231

    
232
run_cmds($git_cmd);
233

    
234
echo "===> Checkout complete.\n";
235
echo "\n";
236
echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
237

    
238
function post_cvssync_commands() {
239
	echo "===> Removing FAST-CGI temporary files...\n";
240
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
241
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
242

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

    
245
	echo "===> Upgrading configuration (if needed)...\n";
246
	convert_config();
247

    
248
	echo "===> Restarting check_reload_status...\n";
249
	exec("killall check_reload_status");
250
	mwexec_bg("nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status");
251

    
252
	echo "===> Configuring filter...";
253
	exec("/etc/rc.filter_configure_sync");
254
	exec("pfctl -f /tmp/rules.debug");
255
	echo "\n";
256

    
257
	if(file_exists("/etc/rc.php_ini_setup")) {
258
		echo "===> Running /etc/rc.php_ini_setup...";
259
		exec("/etc/rc.php_ini_setup");
260
		echo "\n";
261
	}
262

    
263
	/* lock down console if necessary */
264
	echo "===> Locking down the console if needed...\n";
265
	auto_login();
266
	
267
	echo "===> Signaling PHP and Lighty restart...";
268
	$fd = fopen("/tmp/restart_lighty", "w");
269
	fwrite($fd, "#!/bin/sh\n");
270
	fwrite($fd, "sleep 5\n");
271
	fwrite($fd, "killall php\n");
272
	fwrite($fd, "touch /tmp/restart_webgui\n");
273
	fclose($fd);
274
	mwexec_bg("sh /tmp/restart_lighty");
275
	echo "\n";
276

    
277
}
278

    
279
function isUrl($url = "") {
280
	if($url) 
281
		if(strstr($url, "rcs.pfsense.org") or 
282
			strstr($url, "mainline") or 
283
				strstr($url, ".git"))
284
					return true;
285
	return false;
286
}
287

    
288
function run_cmds($cmds) {
289
	global $debug;
290
	foreach($cmds as $cmd) {
291
		if($debug)
292
			echo "Running $cmd";
293
		exec($cmd);
294
	}
295
}
296

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