Location: PHPKode > projects > GForge Community Edition > gforge-ce-571-src/change-hostname.php
#!/usr/bin/php -f
<?php
/**
 * Copyright (c) 2005-2008 GForge, LLC
 *
 * @version   $Id$
 * @author Ruben Gutierrez hide@address.com
 * @date 2006-09-07
 *
 * This file is part of GForge.
 *
 * See LICENSE for details. This file is part of GForge AS and may not be
 * redistributed without written permission of GForge, LLC
 */


define ('GREEN', "\033[01;32m" );
define ('NORMAL', "\033[00m" );
define ('RED', "\033[01;31m" );

function changeHostname() {
	// Where the PHP code will live
	$gforge_lib_dir = '/opt/gforge5';
	if (!is_dir($gforge_lib_dir)) {
		show(RED."Error: GForge5 folder doesn't exist.");
		die();
	}

	$gforge_etc_dir = '/etc/gforge';
	if (!is_dir($gforge_etc_dir)) {
		show(RED."Error: GForge5 config folder doesn't exist.");
		die();
	}
	
	show(' * Enter the new hostname (example: gforge5.company.com): ');
	$hostname = trim(fgets(STDIN));
	if (strlen($hostname) == 0) {
		show(RED."Error: you have to enter a hostname.");
		die();
	}

	# gforge.con
	$gforge_conf = array("\$config['hostName'] *="=>"\$config['hostName']='$hostname';");
	makeConfig($gforge_etc_dir.'/gforge.conf', $gforge_conf);

	# mailman.conf
	$mailman_conf = array("\$config['listsHost'] *="=>"\$config['listsHost']='$hostname';");
	makeConfig($gforge_etc_dir.'/plugins/mailman/mailman.conf', $mailman_conf);

	# httpd.conf
	$httpd_conf = array('ServerName'=>"ServerName	$hostname");
	foreach($httpd_conf as $key => $val) {
		$key = str_replace("/", "\\/", $key);
		$key = str_replace('"', '\\"', $key);
		$val = str_replace("/", "\\/", $val);
		$val = str_replace('"', '\\"', $val);
		run("perl -pi -e \"s/$key.*/$val/gi\" $gforge_etc_dir/httpd.conf");
	}

	show(' * Updating Config Cache...');
	run("cd $gforge_lib_dir/bin && php create_config_cache.php");

	$db_config = include($gforge_etc_dir.'/gforge5-db-conf.php');
	$gforge_db = $db_config['propel']['datasources']['gf5']['connection']['database'];

	show(' * Updating GForge DB');
	run('su - postgres -c "psql '.$gforge_db.' -c \\"UPDATE scm_server SET hostname=\''.$hostname.'\' WHERE scm_type=\'CVS\' OR scm_type=\'SVN\';\\""');
	
	show(' * Restarting Apache...');
	if (is_file('/etc/init.d/httpd')) {
		run('/etc/init.d/httpd restart',true);
	} elseif (is_file('/etc/init.d/apache2')) {
		run('/etc/init.d/apache2 restart',true);
	} elseif (is_file('/etc/init.d/cswapache2')) {
		run('/etc/init.d/cswapache2 restart',true);
	}

	finish();
}

function makeConfig($file, $config) {
	foreach($config as $key => $val) {
		$key = str_replace("/", "\\/", $key);
		$key = str_replace("'", "\\'", $key);
		$key = str_replace("[", "\\[", $key);
		$key = str_replace("]", "\\]", $key);
		$key = str_replace("$", "\\$", $key);
		
		$val = str_replace("/", "\\/", $val);
		$val = str_replace("'", "\\'", $val);
		$val = str_replace("[", "\\[", $val);
		$val = str_replace("$", "\\$", $val);
		$val = str_replace("]", "\\]", $val);
		run("perl -pi -e \"s/$key.*/$val/gi\" $file");
	}
}


function finish() {
	show("Done.\n");
}

function show($text, $newLine = true) {
	if ($newLine) {
		$text = GREEN.$text .NORMAL."\n";
	}
	fwrite(STDOUT, $text);
}

function run($command, $ignore = false) {
	system($command, $ret);
	if ($ret != 0 && $ignore == false) {
		echo RED.'An error ocurred running the last command... aborting.'.NORMAL."\n";
		die();
	}
}

changeHostname();
?>
Return current item: GForge Community Edition