1
|
/* cvs_sync
|
2
|
* Written by Scott Ullrich
|
3
|
* (C)2005-2007 Scott Ullrich
|
4
|
* (C)2010-2012 Erik Fonnesbeck
|
5
|
* Part of the pfSense project pfSsh.php subsystem
|
6
|
*/
|
7
|
|
8
|
require_once("globals.inc");
|
9
|
require_once("filter.inc");
|
10
|
require_once("shaper.inc");
|
11
|
require_once("rrd.inc");
|
12
|
require_once("pfsense-utils.inc");
|
13
|
|
14
|
$GIT_PKG = "git"; // Either "git" or the full package URL
|
15
|
$GIT_BIN= "/usr/local/bin/git";
|
16
|
$GIT_REPO = "git://github.com/pfsense/pfsense.git";
|
17
|
$DEFAULT_BRANCH = "master";
|
18
|
$CODIR = "/root/pfsense";
|
19
|
$GITSYNC_MERGE = "/root/.gitsync_merge";
|
20
|
|
21
|
/* NOTE: Set branches here */
|
22
|
$branches = array(
|
23
|
"master" => "2.3 development branch",
|
24
|
"build_commit" => "The commit originally used to build the image"
|
25
|
);
|
26
|
|
27
|
global $g;
|
28
|
global $argv;
|
29
|
global $command_split;
|
30
|
|
31
|
if (is_array($command_split)) {
|
32
|
$temp_args = array_slice($command_split, 2);
|
33
|
} else {
|
34
|
$temp_args = array_slice($argv, 3);
|
35
|
}
|
36
|
|
37
|
$valid_args = array(
|
38
|
"--minimal" => "\tPerform a minimal copy of only the updated files.\n" .
|
39
|
"\tNot recommended if the system has files modified by any method other\n" .
|
40
|
"\tthan gitsync.\n",
|
41
|
"--help" => "\tDisplay this help list.\n"
|
42
|
);
|
43
|
$args = array();
|
44
|
$arg_count = 0;
|
45
|
while (!empty($temp_args)) {
|
46
|
$arg = array_shift($temp_args);
|
47
|
if ($arg[0] == '-') {
|
48
|
switch ($arg) {
|
49
|
case "--help":
|
50
|
echo "Usage: playback gitsync [options] [[repository] <branch>]\nOptions:\n";
|
51
|
foreach($valid_args as $arg_name => $arg_desc) {
|
52
|
echo $arg_name . "\n" . $arg_desc;
|
53
|
}
|
54
|
exit;
|
55
|
case "--upgrading":
|
56
|
// Disables all interactive functions and neither PHP
|
57
|
// nor the web GUI will be killed or restarted.
|
58
|
$upgrading = true;
|
59
|
case (isset($valid_args[$arg])):
|
60
|
$args[$arg] = true;
|
61
|
break;
|
62
|
default:
|
63
|
echo "Invalid option: {$arg}\nUse --help for usage information.\n";
|
64
|
exit;
|
65
|
}
|
66
|
} else {
|
67
|
$args[$arg_count++] = $arg;
|
68
|
}
|
69
|
}
|
70
|
|
71
|
unlink_if_exists("/tmp/config.cache");
|
72
|
conf_mount_rw();
|
73
|
|
74
|
if (!file_exists($GIT_BIN)) {
|
75
|
require_once("pkg-utils.inc");
|
76
|
|
77
|
echo "Cannot find git, installing...\n";
|
78
|
if (!pkg_call('install -y -q git')) {
|
79
|
echo "\nERROR: Unable to install git pkg.\n";
|
80
|
return;
|
81
|
}
|
82
|
}
|
83
|
|
84
|
# Remove mainline if exists (older)
|
85
|
if (is_dir("/root/pfsense/mainline")) {
|
86
|
exec("rm -rf /root/pfsense/mainline");
|
87
|
}
|
88
|
|
89
|
# Remove RELENG_1_2 if exists (older)
|
90
|
if (is_dir("/root/pfsense/RELENG_1_2")) {
|
91
|
exec("rm -rf /root/pfsense/RELENG_1_2");
|
92
|
}
|
93
|
|
94
|
# Remove HEAD if exists (older)
|
95
|
if (is_dir("/root/pfsense/HEAD")) {
|
96
|
exec("rm -rf /root/pfsense/HEAD");
|
97
|
}
|
98
|
|
99
|
if (file_exists("/root/cvssync_backup.tgz")) {
|
100
|
$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
|
101
|
$tmp = array("RESTORE" => "Restores prior CVSSync backup data performed at {$backup_date}");
|
102
|
$branches = array_merge($branches, $tmp);
|
103
|
}
|
104
|
|
105
|
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
|
106
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url", $output_str, $ret);
|
107
|
if (is_array($output_str) && !empty($output_str[0])) {
|
108
|
$GIT_REPO = $output_str[0];
|
109
|
}
|
110
|
unset($output_str);
|
111
|
}
|
112
|
|
113
|
if (!$args[0] && !$upgrading) {
|
114
|
echo "\nCurrent repository is $GIT_REPO\n";
|
115
|
echo "\nPlease select which branch you would like to sync against:\n\n";
|
116
|
foreach ($branches as $branchname => $branchdesc) {
|
117
|
echo "{$branchname} \t {$branchdesc}\n";
|
118
|
}
|
119
|
echo "\nOr alternatively you may enter a custom RCS branch URL (Git or HTTP).\n\n";
|
120
|
$branch = readline("> ");
|
121
|
echo "\n";
|
122
|
} else {
|
123
|
$branch = $args[0];
|
124
|
}
|
125
|
|
126
|
if ($args[1] == "NOBACKUP") {
|
127
|
$nobackup = true;
|
128
|
} else {
|
129
|
$nobackup = false;
|
130
|
}
|
131
|
|
132
|
// If the repository has been fetched before, build a list of its branches.
|
133
|
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
|
134
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch -r", $branch_list, $ret);
|
135
|
if ($ret == 0 && is_array($branch_list)) {
|
136
|
foreach ($branch_list as $branch_item) {
|
137
|
$branch_item = substr(strrchr($branch_item, "/"), 1);
|
138
|
if (!isset($branches[$branch_item])) {
|
139
|
$branches[$branch_item] = " ";
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
$found = false;
|
146
|
foreach ($branches as $branchname => $branchdesc) {
|
147
|
if ($branchname == $branch) {
|
148
|
$found = true;
|
149
|
break;
|
150
|
}
|
151
|
}
|
152
|
if (!$found) {
|
153
|
if (isURL($branch) && !$upgrading) {
|
154
|
if ($args[1]) {
|
155
|
$GIT_REPO = $branch;
|
156
|
$branch = $args[1];
|
157
|
$found = true;
|
158
|
} else {
|
159
|
echo "\n";
|
160
|
echo "NOTE: $branch was not found.\n\n";
|
161
|
$command = readline("Is this a custom GIT URL? [y]? ");
|
162
|
if (strtolower($command) == "y" or $command == "") {
|
163
|
$GIT_REPO = $branch;
|
164
|
$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
|
165
|
if ($command == "") {
|
166
|
$branch = $DEFAULT_BRANCH;
|
167
|
}
|
168
|
if ($command) {
|
169
|
$branch = $command;
|
170
|
}
|
171
|
$found = true;
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
if (!$found) {
|
176
|
echo "\nNo valid branch found. Exiting.\n\n";
|
177
|
conf_mount_ro();
|
178
|
exit;
|
179
|
}
|
180
|
}
|
181
|
|
182
|
$merge_repos = array();
|
183
|
if (file_exists($GITSYNC_MERGE)) {
|
184
|
$gitsync_merges = file($GITSYNC_MERGE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
185
|
if (!empty($gitsync_merges) && is_array($gitsync_merges)) {
|
186
|
echo "\n===> Automatic merge list read from ${GITSYNC_MERGE}\n";
|
187
|
foreach ($gitsync_merges as $merge_line_num => $merge_line) {
|
188
|
$merge_comments = explode("#", trim($merge_line));
|
189
|
if (empty($merge_comments[0])) {
|
190
|
continue;
|
191
|
}
|
192
|
|
193
|
$merge_line = explode(" ", trim($merge_comments[0]));
|
194
|
if (count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
|
195
|
echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
|
196
|
echo "Expected parameters: repository_url branch\n";
|
197
|
echo "Line read: " . implode(" ", $merge_line) . "\n\n";
|
198
|
echo "Aborting automatic merge.\n\n";
|
199
|
$merge_repos = array();
|
200
|
break;
|
201
|
}
|
202
|
$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
if (!$args[0] && !$upgrading) {
|
207
|
do {
|
208
|
echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
|
209
|
$merge_repo = readline("> ");
|
210
|
if (!empty($merge_repo)) {
|
211
|
$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
|
212
|
if ($merge_branch == "") {
|
213
|
$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
|
214
|
} else if ($merge_branch) {
|
215
|
$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
|
216
|
}
|
217
|
}
|
218
|
} while (!empty($merge_repo));
|
219
|
}
|
220
|
|
221
|
if ($branch == "RESTORE" && $g['platform'] == $g['product_name']) {
|
222
|
if (!file_exists("/root/cvssync_backup.tgz")) {
|
223
|
echo "Sorry, we could not find a previous CVSSync backup file.\n";
|
224
|
conf_mount_ro();
|
225
|
exit();
|
226
|
}
|
227
|
echo "===> Restoring previous CVSSync backup... Please wait...\n";
|
228
|
exec("tar Uxpf /root/cvssync_backup.tgz -C /");
|
229
|
post_cvssync_commands();
|
230
|
conf_mount_ro();
|
231
|
exit();
|
232
|
} else {
|
233
|
$nobackup = true; // do not backup embedded, livecd
|
234
|
}
|
235
|
|
236
|
if ($nobackup == false) {
|
237
|
echo "===> Backing up current pfSense information...\n";
|
238
|
echo "===> Please wait... ";
|
239
|
exec("tar czPf /root/cvssync_backup.tgz --exclude /root --exclude /dev --exclude /tmp --exclude /var/run --exclude /var/empty /");
|
240
|
$size = filesize("/root/cvssync_backup.tgz");
|
241
|
echo "{$size} bytes.\n\n";
|
242
|
sleep(5);
|
243
|
}
|
244
|
|
245
|
echo "===> Checking out $branch\n";
|
246
|
|
247
|
// Git commands for resetting to the specified branch
|
248
|
if ($branch == "build_commit") {
|
249
|
$git_cmd = array(
|
250
|
"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " 2>/dev/null",
|
251
|
"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
|
252
|
"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
|
253
|
);
|
254
|
} else {
|
255
|
$git_cmd = array(
|
256
|
"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
|
257
|
"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
|
258
|
"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg("origin/{$branch}")
|
259
|
);
|
260
|
}
|
261
|
|
262
|
// Git 'er done!
|
263
|
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
|
264
|
echo "===> Fetching updates...\n";
|
265
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url " . escapeshellarg($GIT_REPO));
|
266
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} fetch");
|
267
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} clean -f -x -d");
|
268
|
run_cmds($git_cmd);
|
269
|
} else {
|
270
|
exec("mkdir -p $CODIR/pfSenseGITREPO");
|
271
|
echo "Executing cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone $GIT_REPO pfSenseGITREPO\n";
|
272
|
exec("cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
|
273
|
if (is_dir("$CODIR/pfSenseGITREPO/pfSense")) {
|
274
|
exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
|
275
|
}
|
276
|
if (is_dir("$CODIR/pfSenseGITREPO/mainline")) {
|
277
|
exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
|
278
|
}
|
279
|
run_cmds($git_cmd);
|
280
|
}
|
281
|
|
282
|
foreach ($merge_repos as $merge_repo) {
|
283
|
echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
|
284
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
|
285
|
unset($output_str);
|
286
|
if ($ret <> 0) {
|
287
|
echo "\nMerge failed. Aborting sync.\n\n";
|
288
|
run_cmds($git_cmd);
|
289
|
conf_mount_ro();
|
290
|
exit;
|
291
|
}
|
292
|
}
|
293
|
|
294
|
if (isset($args["--minimal"])) {
|
295
|
if (file_exists("/etc/version.gitsync")) {
|
296
|
$old_revision = trim(file_get_contents("/etc/version.gitsync"));
|
297
|
} else if (file_exists("/etc/version.lastcommit")) {
|
298
|
$old_revision = trim(file_get_contents("/etc/version.lastcommit"));
|
299
|
}
|
300
|
$files_to_copy = strtr(shell_exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} diff --name-only --relative=src " . escapeshellarg($old_revision)), "\n", " ");
|
301
|
} else {
|
302
|
$files_to_copy = '-C ./src .';
|
303
|
}
|
304
|
|
305
|
// Save new commit ID for later minimal file copies
|
306
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} rev-parse -q --verify HEAD > /etc/version.gitsync");
|
307
|
|
308
|
exec("mkdir -p /tmp/lighttpd/cache/compress/");
|
309
|
|
310
|
// Remove files that we do not want to overwrite the system with
|
311
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/crontab");
|
312
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/master.passwd");
|
313
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/passwd");
|
314
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
|
315
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/ttys");
|
316
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/group");
|
317
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
|
318
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/platform");
|
319
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/device.hints");
|
320
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.conf");
|
321
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.rc");
|
322
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/syslog.conf");
|
323
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.shrc");
|
324
|
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.tcshrc");
|
325
|
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/conf*");
|
326
|
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/cf 2>/dev/null");
|
327
|
@chmod("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/tmp", 01777)
|
328
|
|
329
|
echo "===> Installing new files...\n";
|
330
|
|
331
|
if ($g['platform'] == $g['product_name']) {
|
332
|
$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$files_to_copy} | (cd / ; tar -Uxpf -)";
|
333
|
} else {
|
334
|
$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$files_to_copy} | (cd / ; tar -xpf -) 2>/dev/null";
|
335
|
}
|
336
|
|
337
|
if (!empty($files_to_copy)) {
|
338
|
exec($command);
|
339
|
} else {
|
340
|
echo "Already up-to-date.\n";
|
341
|
$upgrading = true;
|
342
|
}
|
343
|
|
344
|
// Reset the repository to restore the deleted files
|
345
|
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard >/dev/null 2>/dev/null");
|
346
|
|
347
|
// Remove obsolete files
|
348
|
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
|
349
|
foreach ($files_to_remove as $file_to_remove) {
|
350
|
if (file_exists($file_to_remove)) {
|
351
|
exec("/bin/rm -f $file_to_remove");
|
352
|
}
|
353
|
}
|
354
|
|
355
|
if (!$upgrading) {
|
356
|
post_cvssync_commands();
|
357
|
}
|
358
|
|
359
|
echo "===> Checkout complete.\n";
|
360
|
echo "\n";
|
361
|
if (!$upgrading) {
|
362
|
echo "Your system is now sync'd and PHP and Lighty will be restarted in 5 seconds.\n\n";
|
363
|
} else {
|
364
|
echo "Your system is now sync'd.\n\n";
|
365
|
}
|
366
|
|
367
|
function post_cvssync_commands() {
|
368
|
echo "===> Removing FAST-CGI temporary files...\n";
|
369
|
exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
|
370
|
exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
|
371
|
|
372
|
exec("rm -rf /tmp/xcache/* 2>/dev/null");
|
373
|
|
374
|
echo "===> Upgrading configuration (if needed)...\n";
|
375
|
convert_config();
|
376
|
|
377
|
echo "===> Configuring filter...";
|
378
|
exec("/etc/rc.filter_configure_sync");
|
379
|
exec("pfctl -f /tmp/rules.debug");
|
380
|
echo "\n";
|
381
|
|
382
|
if (file_exists("/etc/rc.php_ini_setup")) {
|
383
|
echo "===> Running /etc/rc.php_ini_setup...";
|
384
|
exec("/etc/rc.php_ini_setup");
|
385
|
echo "\n";
|
386
|
}
|
387
|
|
388
|
/* lock down console if necessary */
|
389
|
echo "===> Locking down the console if needed...\n";
|
390
|
reload_ttys();
|
391
|
|
392
|
echo "===> Signaling PHP and Lighty restart...";
|
393
|
$fd = fopen("/tmp/restart_lighty", "w");
|
394
|
fwrite($fd, "#!/bin/sh\n");
|
395
|
fwrite($fd, "sleep 5\n");
|
396
|
fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
|
397
|
if (file_exists("/var/etc/lighty-CaptivePortal.conf")) {
|
398
|
fwrite($fd, "/usr/local/sbin/lighttpd -f /var/etc/lighty-CaptivePortal.conf\n");
|
399
|
}
|
400
|
fclose($fd);
|
401
|
mwexec_bg("sh /tmp/restart_lighty");
|
402
|
echo "\n";
|
403
|
|
404
|
}
|
405
|
|
406
|
function isUrl($url = "") {
|
407
|
if ($url) {
|
408
|
if (strstr($url, "rcs.pfsense.org") or
|
409
|
strstr($url, "mainline") or
|
410
|
strstr($url, ".git") or
|
411
|
strstr($url, "git://")) {
|
412
|
return true;
|
413
|
}
|
414
|
}
|
415
|
return false;
|
416
|
}
|
417
|
|
418
|
function run_cmds($cmds) {
|
419
|
global $debug;
|
420
|
foreach ($cmds as $cmd) {
|
421
|
if ($debug) {
|
422
|
echo "Running $cmd";
|
423
|
}
|
424
|
exec($cmd);
|
425
|
}
|
426
|
}
|
427
|
|
428
|
conf_mount_ro();
|