summaryrefslogtreecommitdiffstats
path: root/etc/phpshellsessions/gitsync
blob: 156545e31c73fbd0aa5f2614ddf6c4059e28dac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*   cvs_sync
 *   Written by Scott Ullrich
 *   (C)2005-2007 Scott Ullrich
 *   Part of the pfSense project pfSsh.php subsystem
 */

conf_mount_rw();

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

global $argv;
global $command_split;

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

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

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

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

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

/* NOTE: Set branches here */
$branches = array(
	"master" => "2.0 development branch",
	"RELENG_1_2" => "1.2* release branch"
);

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

if($command_split[2]) {
	$branch = $command_split[2];
} else {
	if(!$argv[3]) {
		echo "\nPlease select which branch you would like to sync against:\n\n";
		foreach($branches as $branchname => $branchdesc) {
			echo "{$branchname} \t {$branchdesc}\n";
		}
		echo "\nOr alternatively you may enter a custom RCS branch URL (HTTP).\n\n";
		$branch = readline("> ");
		echo "\n";
	} else {
		$branch = $argv[3];
	}
}

if($argv[4] == "NOBACKUP") 
	$nobackup=true;
else 
	$nobackup = false;

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

$found = false;
foreach($branches as $branchname => $branchdesc) {
	if($branchname == $branch) 
		$found = true;
}
if(!$found) {
	if(isURL($branch)) {
		echo "\n";
		echo "NOTE: $branch was not found.\n\n";
		$command = readline("Is this a custom GIT URL? [y]? ");
		if(strtolower($command) == "y" or $command == "") {
			$GIT_REPO = $branch;
			$command = readline("Checkout which branch [master]? ");
			if($command == "")
				$branch = "master";
			if($command) 
				$branch = $command;
			$found = true;
		}
	}
	if(!$found) {
		echo "\nNo valid branch found.  Exiting.\n\n";
		exit;
	}
}

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

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

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

// Git 'er done!
if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
	echo "===> Fetching updates...\n";
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch");
	if($branch == "master") {
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard HEAD");
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git rebase origin");
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard $branch");
	} else {
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard HEAD");
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git rebase origin/$branch");
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard origin/$branch");
	}
} else {
    exec("mkdir -p $CODIR/pfSenseGITREPO");
    echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n";
	exec("cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO");
	if(is_dir("$CODIR/pfSenseGITREPO/pfSense")) 
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
	if(is_dir("$CODIR/pfSenseGITREPO/mainline")) 
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
	if($branch == "master") {
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout master");
	} else {
		exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -b $branch origin/$branch");
	}
}

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

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

// Remove files that we do not want to overwrite the system with 
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/usr/local/www/trigger_initial_wizard 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/crontab 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/master.passwd 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/passwd 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/ttys 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/group 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/fstab 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/platform 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/device.hints 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.conf 2>/dev/null");
exec("rm ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/boot/loader.rc 2>/dev/null");
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/conf*");
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/cf 2>/dev/null");
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.shrc");
exec("rm -rf ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/root/.tcshrc");
exec("rm -f ${CODIR}/pfSenseGITREPO/pfSenseGITREPO/etc/syslog.conf 2>/dev/null");

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

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

post_cvssync_commands();

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

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

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

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

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

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

	if(file_exists("/etc/rc.php_ini_setup")) {
		echo "===> Running /etc/rc.php_ini_setup...";
		exec("/etc/rc.php_ini_setup");
		echo "\n";
	}

	/* lock down console if necessary */
	echo "===> Locking down the console if needed...\n";
	if(isset($config['system']['disableconsolemenu'])) {
		auto_login(false);
	} else {
		auto_login(true);
	}
	
	echo "===> Signaling PHP and Lighty restart...";
	$fd = fopen("/tmp/restart_lighty", "w");
	fwrite($fd, "#!/bin/sh\n");
	fwrite($fd, "sleep 5\n");
	fwrite($fd, "killall php\n");
	fwrite($fd, "touch /tmp/restart_webgui\n");
	fclose($fd);
	mwexec_bg("sh /tmp/restart_lighty");
	echo "\n";

}

function isUrl($url = "") {
	if($url) 
		if(strstr($url, "rcs.pfsense.org") or 
			strstr($url, "mainline") or 
				strstr($url, ".git"))
					return true;
	return false;
}

conf_mount_ro();
OpenPOWER on IntegriCloud