/* cvs_sync * Written by Scott Ullrich * (C)2005-2007 Scott Ullrich * (C)2010 Erik Fonnesbeck * Part of the pfSense project pfSsh.php subsystem */ require_once("globals.inc"); require_once("filter.inc"); require_once("shaper.inc"); require_once("rrd.inc"); require_once("pfsense-utils.inc"); 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..."; 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", "build_commit" => "The commit originally used to build the image" ); 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(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) { exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url", $output_str, $ret); if(is_array($output_str) && !empty($output_str[0])) $GIT_REPO = $output_str[0]; unset($output_str); } if($command_split[2]) { $branch = $command_split[2]; } else { if(!$argv[3]) { echo "\nCurrent repository is $GIT_REPO\n"; 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; // If the repository has been fetched before, build a list of its branches. if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) { exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch -r", $branch_list, $ret); if($ret == 0 && is_array($branch_list)) { foreach ($branch_list as $branch_item) { $branch_item = substr(strrchr($branch_item, "/"), 1); if (!isset($branches[$branch_item])) $branches[$branch_item] = " "; } } } $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"; conf_mount_ro(); exit; } } $merge_repos = array(); if(file_exists("/root/.gitsync_merge")) { $gitsync_merges = file("/root/.gitsync_merge", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if(!empty($gitsync_merges) && is_array($gitsync_merges)) { echo "\n===> Automatic merge list read from /root/.gitsync_merge\n"; foreach($gitsync_merges as $merge_line_num => $merge_line) { $merge_comments = explode("#", trim($merge_line)); if(empty($merge_comments[0])) continue; $merge_line = explode(" ", trim($merge_comments[0])); if(count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) { echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n"; echo "Expected parameters: repository_url branch\n"; echo "Line read: " . implode(" ", $merge_line) . "\n\n"; echo "Aborting automatic merge.\n\n"; $merge_repos = array(); break; } $merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]); } } } if(!$command_split[2] && !$argv[3]) { do { echo "\nAdd a custom RCS branch URL (HTTP) to merge in or press enter if done.\n\n"; $merge_repo = readline("> "); if(!empty($merge_repo)) { $merge_branch = readline("Merge which branch [master]? "); if($merge_branch == "") $merge_repos[] = array('repo' => $merge_repo, 'branch' => 'master'); else if($merge_branch) $merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch); } } while(!empty($merge_repo)); } 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"; conf_mount_ro(); exit(); } echo "===> Restoring previous CVSSync backup... Please wait...\n"; exec("tar Uxpf /root/cvssync_backup.tgz -C /"); post_cvssync_commands(); conf_mount_ro(); 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"; // Git commands for resetting to the specified branch if($branch == "build_commit") { $git_cmd = array( "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " 2>/dev/null", "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null", "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard `cat /etc/version.lastcommit`" ); } else { $git_cmd = array( "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null", "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git checkout -f " . escapeshellarg($branch) . " 2>/dev/null", "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard " . escapeshellarg("origin/{$branch}") ); } // Git 'er done! if(is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) { echo "===> Fetching updates...\n"; exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git config remote.origin.url " . escapeshellarg($GIT_REPO)); exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git fetch"); exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git clean -f -f -x -d"); run_cmds($git_cmd); } else { exec("mkdir -p $CODIR/pfSenseGITREPO"); echo "Executing cd $CODIR/pfSenseGITREPO && git clone $GIT_REPO pfSenseGITREPO\n"; exec("cd $CODIR/pfSenseGITREPO && git clone " . escapeshellarg($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"); run_cmds($git_cmd); } foreach($merge_repos as $merge_repo) { echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n"; exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret); unset($output_str); if($ret <> 0) { echo "\nMerge failed. Aborting sync.\n\n"; run_cmds($git_cmd); conf_mount_ro(); exit; } } 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"; // Don't include the .git directory in the copy exec("mv $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git $CODIR/pfSenseGITREPO/gitsync_temp.git"); 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); exec("mv $CODIR/pfSenseGITREPO/gitsync_temp.git $CODIR/pfSenseGITREPO/pfSenseGITREPO/.git"); // Reset the repository to restore the deleted files exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && git reset --hard >/dev/null 2>/dev/null"); 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 "===> 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"; auto_login(); 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, "/usr/local/sbin/pfSctl -c 'service 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; } function run_cmds($cmds) { global $debug; foreach($cmds as $cmd) { if($debug) echo "Running $cmd"; exec($cmd); } } conf_mount_ro();