Project

General

Profile

Download (9.86 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
require_once("pfsense-utils.inc");
12
	
13
conf_mount_rw();
14

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

    
18
global $argv;
19
global $command_split;
20

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

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

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

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

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

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

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

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

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

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

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

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

    
121
$merge_repos = array();
122
if(!$command_split[2] && !$argv[3]) {
123
	do {
124
		echo "\nAdd a custom RCS branch URL (HTTP) to merge in or press enter for none.\n\n";
125
		$merge_repo = readline("> ");
126
		if(!empty($merge_repo)) {
127
			$merge_branch = readline("Merge which branch [master]? ");
128
			if($merge_branch == "")
129
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => 'master');
130
			else if($merge_branch)
131
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
132
		}
133
	} while(!empty($merge_repo));
134
}
135

    
136
if($branch == "RESTORE" && $g['platform'] == "pfSense") {
137
	if(!file_exists("/root/cvssync_backup.tgz")) {
138
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
139
		conf_mount_ro();
140
		exit();
141
	}
142
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
143
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
144
	post_cvssync_commands();
145
	conf_mount_ro();
146
	exit();
147
} else {
148
	$nobackup = true; // do not backup embedded, livecd
149
}
150

    
151
if($nobackup == false) {
152
	echo "===> Backing up current pfSense information...\n";
153
	echo "===> Please wait... ";
154
	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 /");
155
	$size = filesize("/root/cvssync_backup.tgz");
156
	echo "{$size} bytes.\n\n";
157
	sleep(5);
158
}
159

    
160
echo "===> Checking out $branch\n";
161

    
162
// Git commands for resetting to the specified branch
163
if($branch == "build_commit") {
164
	$git_cmd = array(
165
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch $branch 2>/dev/null",
166
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f $branch 2>/dev/null",
167
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard `cat /etc/version.lastcommit`"
168
	);
169
} else {
170
	$git_cmd = array(
171
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch $branch origin/$branch 2>/dev/null",
172
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f $branch 2>/dev/null",
173
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard origin/$branch"
174
	);
175
}
176

    
177
// Git 'er done!
178
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
179
	echo "===> Fetching updates...\n";
180
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url $GIT_REPO");
181
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
182
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d");
183
	run_cmds($git_cmd);
184
} else {
185
    exec("mkdir -p $CODIR/pfSenseGITREPO");
186
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
187
	exec("cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO");
188
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
189
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
190
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
191
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
192
	run_cmds($git_cmd);
193
}
194

    
195
foreach($merge_repos as $merge_repo) {
196
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
197
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git pull {$merge_repo['repo']} {$merge_repo['branch']}", $output_str, $ret);
198
	unset($output_str);
199
	if($ret <> 0) {
200
		echo "\nMerge failed.  Aborting sync.\n\n";
201
		run_cmds($git_cmd);
202
		conf_mount_ro();
203
		exit;
204
	}
205
}
206

    
207
exec("mkdir -p /tmp/lighttpd/cache/compress/");
208

    
209
// Nuke CVS and pfSense tarballs
210
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name CVS -exec rm -rf {} \; 2>/dev/null");
211
exec("cd ${CODIR}/pfSenseGITREPO/pfSenseGITREPO && find . -name pfSense.tgz -exec rm {} \; 2>/dev/null");
212

    
213
// Remove files that we do not want to overwrite the system with 
214
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
215
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
216
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
217
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
218
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
219
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
220
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
221
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
222
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
223
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
224
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
225
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
226
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
227
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
228
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
229
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
230
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");
231

    
232
echo "===> Installing new files...\n";
233

    
234
if($g['platform'] == "pfSense") 
235
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -Uxpf -)";
236
else 
237
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - . | (cd / ; tar -xpf -) 2>/dev/null";
238
exec($command);
239

    
240
post_cvssync_commands();
241

    
242
run_cmds($git_cmd);
243

    
244
echo "===> Checkout complete.\n";
245
echo "\n";
246
echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
247

    
248
function post_cvssync_commands() {
249
	echo "===> Removing FAST-CGI temporary files...\n";
250
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
251
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
252

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

    
255
	echo "===> Upgrading configuration (if needed)...\n";
256
	convert_config();
257

    
258
	echo "===> Restarting check_reload_status...\n";
259
	exec("killall check_reload_status");
260
	mwexec_bg("nohup /usr/bin/nice -n20 /usr/local/sbin/check_reload_status");
261

    
262
	echo "===> Configuring filter...";
263
	exec("/etc/rc.filter_configure_sync");
264
	exec("pfctl -f /tmp/rules.debug");
265
	echo "\n";
266

    
267
	if(file_exists("/etc/rc.php_ini_setup")) {
268
		echo "===> Running /etc/rc.php_ini_setup...";
269
		exec("/etc/rc.php_ini_setup");
270
		echo "\n";
271
	}
272

    
273
	/* lock down console if necessary */
274
	echo "===> Locking down the console if needed...\n";
275
	auto_login();
276
	
277
	echo "===> Signaling PHP and Lighty restart...";
278
	$fd = fopen("/tmp/restart_lighty", "w");
279
	fwrite($fd, "#!/bin/sh\n");
280
	fwrite($fd, "sleep 5\n");
281
	fwrite($fd, "killall php\n");
282
	fwrite($fd, "touch /tmp/restart_webgui\n");
283
	fclose($fd);
284
	mwexec_bg("sh /tmp/restart_lighty");
285
	echo "\n";
286

    
287
}
288

    
289
function isUrl($url = "") {
290
	if($url) 
291
		if(strstr($url, "rcs.pfsense.org") or 
292
			strstr($url, "mainline") or 
293
				strstr($url, ".git"))
294
					return true;
295
	return false;
296
}
297

    
298
function run_cmds($cmds) {
299
	global $debug;
300
	foreach($cmds as $cmd) {
301
		if($debug)
302
			echo "Running $cmd";
303
		exec($cmd);
304
	}
305
}
306

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