Simple configuration et installation

This commit is contained in:
Michael RICOIS 2012-02-13 09:49:16 +00:00
parent 4edb8da2f8
commit ae8bcce45d

View File

@ -1,93 +1,82 @@
#!/usr/bin/php
<?php
Class configure
// Paramètres
if ( count($argc)<1 || in_array($argv[1], array('--help', '-help', '-h', '-?')) ) {
?>
Utilisation : <?php echo $argv[0]; ?> [actions]
Installation :
- install list
- install version
Configuration :
- config initscript
- config logrotate
- config list
- config server *.conf
<?php
exit;
}
$hostname = exec('echo $HOSTNAME');
$SPHINX_VERSION = array(
'0.9.9' => array('archive'=>'sphinx-0.9.9.tar.gz', 'dir'=>'sphinx-0.9.9'),
'1.10' => array('archive'=>'sphinx-1.10.tar.gz', 'dir'=>'sphinx-1.10'),
'2.0.2' => array('archive'=>'sphinx-2.0.2.tar.gz', 'dir'=>'sphinx-2.0.2'),
'2.0.3' => array('archive'=>'sphinx-2.0.3.tar.gz', 'dir'=>'sphinx-2.0.3'),
);
if ($argv[1]=='install')
{
protected $dirname;
protected $sphinx;
protected $hostname;
protected $sql;
protected $indexes;
public function __construct()
{
$this->hostname = exec('echo $(hostname)');
$this->sphinx = 'sphinx.conf';
$this->dirname = 'config/'.$this->hostname.'/';
(file_exists($this->sphinx))?unlink($this->sphinx):'';
//Version
if (!array_key_exists($argv[2], $SPHINX_VERSION)){
exit;
}
public function printIndexes($nbServer)
{
$dir = opendir($this->dirname.$this->sql[$nbServer].'/');
$i = 1;
while($file = readdir($dir)) {
$extension = explode('.', $file);
if($file != '.' and $file != '..' and $extension[1] == 'conf') {
echo "\t\t".'['.$i.'] '. $file."\n";
$this->indexes[$i] = $file;$i++;
}
}
if(count($this->indexes) == 0) {
echo "\tAucun indexes dans le repertoire : '".$this->dirname.$this->sql[$nbServer]."' \n";
exit;
}
}
public function printMysqlServer()
{
$dir = opendir($this->dirname);
$i = 1;
while($rep = readdir($dir)) {
if($rep != '.' and $rep != '..' and is_dir($this->dirname.$rep)) {
echo "\t".'['.$i.'] '.$rep."\n";
$this->sql[$i] = $rep;
$this->printIndexes($i);
$i++;
}
}
if(count($this->sql) == 0) {
echo "\tAucun serveurs dans le repertoire : '".$this->dirname."' \n";
exit;
}
}
public function createConfig($mySqlServer, $index)
{
$dirname = $this->dirname.$mySqlServer.'/';
$dir = opendir($dirname);
$version = $argv[2];
if(file_exists($dirname.$index)) {
file_put_contents($this->sphinx, file_get_contents($dirname.$index), FILE_APPEND);
return (true);
}
return (false);
//Compilation
$cmd = array();
$cmd[] = "tar xzvf ".$SPHINX_VERSION[$version]['archive'];
$cmd[] = "cp libstemmer_c.tgz ".$SPHINX_VERSION[$version]['dir']."/";
$cmd[] = "cd ".$SPHINX_VERSION[$version]['dir']."/";
$cmd[] = "tar xzvf libstemmer_c.tgz";
$cmd[] = "./configure --with-libstemmer --prefix=/usr/local/sphinx";
$cmd[] = "make";
$cmd[] = "make install";
foreach($cmd as $c){
passthru($c);
}
public function inviteDeCommande()
{
$handle = fopen('.', 'r');
$server = '';
fwrite(STDOUT, "\n - Serveurs SQL disponnible :\n");
$this->printMysqlServer();
while($server != "exit\n") {
echo "Selectionnez Server et Indexe : ";
$server = fgets(STDIN);
$indexes = explode(' ', $server);
if(isset($this->sql[intval($indexes[0])]) and isset($this->indexes[intval($indexes[1])])) {
$this->createConfig($this->sql[intval($indexes[0])], $this->indexes[intval($indexes[1])]);
echo 'Indexe ajouté !'."\n";
} else
echo 'Cet indexe n\'existe pas !'."\n";
}
file_put_contents($this->sphinx, file_get_contents($this->dirname.$this->sphinx), FILE_APPEND);
exec('sudo mv '.$this->sphinx.' /usr/local/sphinx/etc/'.$this->sphinx);
}
}
$a = new configure();
$a->inviteDeCommande();
if ($argv[1]=='config')
{
//Initscript
if ($argv[2]=='initscript') {
exec("cp -v initscript/sphinxsearch /etc/init.d/");
exit;
}
//Logrotate
if ($argv[2]=='logrotate') {
echo exec("which logrotate");
exec("cp -v logrotate/searchd /etc/logrotate.d/");
exit;
}
//Liste des configuration
if ($argv[2]=='list') {
echo passthru("tree config/".$hostname."/");
exit;
}
//Configuration
if (!file_exist("config/".$hostname."/".$argv[2])){
echo "Erreur\n"; exit;
}
$server = $argv[2];
$concat = '';
for($i=3;$i<count($argc);$i++){
$concat.= "config/".$hostname."/".$server."/".$argv[$i];
}
exec("cat config/".$hostname."/".$server."/sphinx.conf ".$concact." > /usr/local/sphinx/etc/sphinx.conf");
}