Esempio n. 1
0
function start($aspid = false)
{
    $unix = new unix();
    $sock = new sockets();
    $pidfile = "/etc/artica-postfix/pids/zarafa-search-starter.pid";
    if (!$aspid) {
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Engine Artica Task Already running PID {$pid} since {$time}mn\n";
            }
            return;
        }
    }
    @file_put_contents($pidfile, getmypid());
    $serverbin = $unix->find_program("zarafa-search");
    if (!is_file($serverbin)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Engine is not installed...\n";
        }
        return;
    }
    $pid = XZARAFA_SERVER_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Engine Failed, zarafa-server is not running...\n";
        }
        return;
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Engine zarafa-server is running...\n";
        }
    }
    $EnableZarafaSearch = $sock->GET_INFO("EnableZarafaSearch");
    if (!is_numeric($EnableZarafaSearch)) {
        $EnableZarafaSearch = 1;
    }
    if ($EnableZarafaSearch == 0) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Zarafa-search is disabled ( see EnableZarafaSearch )\n";
        }
        return;
    }
    $ZarafaIndexPath = $sock->GET_INFO("ZarafaIndexPath");
    if ($ZarafaIndexPath == null) {
        $ZarafaIndexPath = "/var/lib/zarafa/index";
    }
    $pid = ZARAFA_SEARCH_PID();
    @mkdir($ZarafaIndexPath, 0755, true);
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Engine already running pid {$pid} since {$time}mn\n";
        }
        return;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} Engine...\n";
    }
    $f[] = $serverbin;
    $f[] = "-c /etc/zarafa/search.cfg";
    $f[] = "--ignore-unknown-config-options";
    $cmdline = @implode(" ", $f);
    if ($GLOBALS["VERBOSE"]) {
        echo $cmdline . "\n";
    }
    shell_exec("{$cmdline} 2>&1");
    sleep(1);
    for ($i = 0; $i < 5; $i++) {
        $pid = ZARAFA_SEARCH_PID();
        if ($unix->process_exists($pid)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} daemon started pid .{$pid}..\n";
            }
            break;
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} daemon wait {$i}/5\n";
        }
        sleep(1);
    }
    $pid = ZARAFA_SEARCH_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} daemon failed to start\n";
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$cmdline}\n";
        } else {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} daemon success PID {$pid}\n";
            }
        }
    }
}
Esempio n. 2
0
function reload()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Reloading.....: [INIT]: Already task running PID {$pid} since {$time}mn\n";
        }
        return;
    }
    $pid = XZARAFA_SERVER_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Reloading.....: [INIT]: zarafa-server stopped...\n";
        }
        $php5 = $unix->LOCATE_PHP5_BIN();
        shell_exec("{$php5} " . __FILE__ . " --start");
        return;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Reloading.....: [INIT]: zarafa-server reconfigure...\n";
    }
    system("/usr/share/artica-postfix/bin/artica-install --zarafa-reconfigure");
    if ($GLOBALS["OUTPUT"]) {
        echo "Reloading.....: [INIT]: zarafa-server reloading PID {$pid}...\n";
    }
    $kill = $unix->find_program("kill");
    unix_system_HUP($pid);
}
Esempio n. 3
0
function RestoreFromBackup($backuppath)
{
    $unix = new unix();
    $PidRestore = "/etc/artica-postfix/pids/zarafaRestore.pid";
    $rm = $unix->find_program("rm");
    $pid = $unix->get_pid_from_file($PidRestore);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n";
        }
        return;
    }
    $pid = $unix->PIDOF_PATTERN("exec.zarafa-db.php --restorefrom");
    if ($pid != getmypid()) {
        if ($unix->process_exists($pid)) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n";
            }
            return;
        }
    }
    $mysql = $unix->find_program("mysql");
    $pid = $unix->PIDOF_PATTERN("{$mysql}\\s+.*?--socket=/var/run/mysqld/zarafa-db.sock.*?database=zarafa");
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: Restore Task Already running PID {$pid} since {$time}mn\n";
        }
        return;
    }
    @file_put_contents($PidRestore, getmypid());
    $sock = new sockets();
    $SourceDir = dirname($backuppath);
    $WORKDIR = $sock->GET_INFO("ZarafaDedicateMySQLWorkDir");
    if ($WORKDIR == null) {
        $WORKDIR = "/home/zarafa-db";
    }
    if (is_file("{$SourceDir}/ldap.ldif")) {
        RestoreFromBackup_progress("{restore_ldap_database}", 10);
        RestoreFromBackup_ldap("{$SourceDir}/ldap.ldif");
    }
    $unix = new unix();
    if (!is_file($backuppath)) {
        echo "Action: `{$backuppath}` no such file: ABORT!\n";
        RestoreFromBackup_progress("{failed}", 100);
        return;
    }
    echo "Action: Removing Zarafa Database MySQL client `{$mysql}`....\n";
    RestoreFromBackup_progress("Removing Zarafa Database", 30);
    $cmd = "{$mysql} --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa --execute=\"DROP DATABASE zarafa\" 2>&1";
    $results = array();
    exec("{$cmd}", $results);
    while (list($num, $ligne) = each($results)) {
        echo "MySQL: (Delete Database) {$ligne}\n";
    }
    RestoreFromBackup_progress("Removing all content", 32);
    if (is_dir("{$WORKDIR}/data/zarafa")) {
        recursive_remove_directory("{$WORKDIR}");
    }
    RestoreFromBackup_progress("Restarting MySQL service (recovery)", 40);
    echo "Action: Restarting MySQL service...\n";
    echo "Action: Stopping MySQL service...\n";
    stop(true);
    echo "Action: Starting MySQL service (InnoDB recovery mode)...\n";
    start(true, true);
    while (list($num, $ligne) = each($results)) {
        echo "Service: {$ligne}\n";
    }
    sleep(5);
    $ZARAFADB_PID = ZARAFADB_PID();
    if (!$unix->process_exists($ZARAFADB_PID)) {
        RestoreFromBackup_progress("Failed to restart dedicated MySQL", 100);
        return;
    }
    RestoreFromBackup_progress("Stopping Zarafa server", 43);
    @unlink("/tmp/zarafa-upgrade-lock");
    shell_exec("/etc/init.d/zarafa-server stop --force");
    $pid = XZARAFA_SERVER_PID();
    if ($unix->process_exists($pid)) {
        $kill = $unix->find_program("kill");
        unix_system_kill_force($pid);
    }
    RestoreFromBackup_progress("Restarting MySQL service (normal)", 45);
    echo "Action: Restarting MySQL service...\n";
    echo "Action: Stopping MySQL service...\n";
    stop(true);
    echo "Action: Starting MySQL service (InnoDB normal mode)...\n";
    start(true, false);
    while (list($num, $ligne) = each($results)) {
        echo "Service: {$ligne}\n";
    }
    sleep(2);
    $ZARAFADB_PID = ZARAFADB_PID();
    if (!$unix->process_exists($ZARAFADB_PID)) {
        RestoreFromBackup_progress("Failed to restart dedicated MySQL", 100);
        return;
    }
    if (!$unix->is_socket("/var/run/mysqld/zarafa-db.sock")) {
        echo "Action: /var/run/mysqld/zarafa-db.sock waiting socket\n";
        for ($i = 0; $i < 5; $i++) {
            if ($unix->is_socket("/var/run/mysqld/zarafa-db.sock")) {
                break;
            }
            echo "Action: Waiting zarafa-db.sock {$i}/4\n";
            sleep(1);
        }
    }
    if (!$unix->is_socket("/var/run/mysqld/zarafa-db.sock")) {
        echo "Action: /var/run/mysqld/zarafa-db.sock no such socket\n";
        RestoreFromBackup_progress("zarafa-db.sock no such socket", 100);
        return;
    }
    echo "Action: /var/run/mysqld/zarafa-db.sock OK\n";
    echo "Action: create a freshed Zarafa database\n";
    $ZarafaIndexPath = $sock->GET_INFO("ZarafaIndexPath");
    if ($ZarafaIndexPath == null) {
        $ZarafaIndexPath = "/var/lib/zarafa/index";
    }
    RestoreFromBackup_progress("Cleaning/Stopping Zarafa search DBs", 50);
    if (is_dir($ZarafaIndexPath)) {
        recursive_remove_directory("{$ZarafaIndexPath}");
        shell_exec("/etc/init.d/zarafa-search stop");
    }
    RestoreFromBackup_progress("Create a freshed Zarafa database", 50);
    $results = array();
    $cmd = "{$mysql} --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --execute=\"CREATE DATABASE zarafa\" 2>&1";
    $results = array();
    exec("{$cmd}", $results);
    while (list($num, $ligne) = each($results)) {
        echo "MySQL: (Create Database) {$ligne}\n";
    }
    RestoreFromBackup_progress("Testing Database...", 51);
    if (!is_dir("{$WORKDIR}/data/zarafa")) {
        echo "Action: FAILED TO create a freshed Zarafa database: ABORT!!\n";
        echo "Action: {$WORKDIR}/data/zarafa no such directory\n";
        RestoreFromBackup_progress("FAILED to create a freshed Zarafa database", 100);
        return;
    }
    RestoreFromBackup_progress("Checks Database size", 53);
    databasesize(true);
    $gunzip = $unix->find_program("gunzip");
    $SourceFileBase = basename($backuppath);
    $file_ext = $unix->file_ext($SourceFileBase);
    $tStart = time();
    $nohup = $unix->find_program("nohup");
    $backuppath1 = $unix->shellEscapeChars($backuppath);
    $cmd = "{$nohup} {$mysql} --show-warnings --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa < {$backuppath1} >/root/mysqllog.txt 2>&1 &";
    echo "Action: {$SourceFileBase} extension {$file_ext}\n";
    echo "Action: Restoring From {$backuppath1}\n";
    if ($file_ext == "gz") {
        echo "Action: Restoring From {$backuppath1} with uncompress..\n";
        $cmd = "{$nohup} {$gunzip} -c {$backuppath1} |{$mysql} --show-warnings --socket=/var/run/mysqld/zarafa-db.sock --protocol=socket --user=root --batch --debug-info --database=zarafa >/root/mysqllog.txt 2>&1 &";
    }
    $size = @filesize($backuppath);
    $size = FormatBytes($size / 1024);
    echo "Action: Please wait, it should take time...\nAction: Do not shutdown the computer or restart the MySQL service!\n";
    $results = array();
    RestoreFromBackup_progress("{restoring_data} {$size} {please_wait} !", 70);
    $lastmd5 = null;
    $continue = true;
    shell_exec($cmd);
    $ALRDLO = array();
    while ($continue) {
        $fileMD5 = @md5_file("/root/mysqllog.txt");
        if ($fileMD5 != $lastmd5) {
            $LOGS = explode("\n", @file_get_contents("/root/mysqllog.txt"));
            while (list($num, $ligne) = each($LOGS)) {
                if (trim($ligne) == null) {
                    continue;
                }
                if (isset($ALRDLO[md5($ligne)])) {
                    continue;
                }
                $ALRDLO[md5($ligne)] = true;
                if (preg_match("#ERROR\\s+([0-9]+)\\s+\\(#", $ligne, $re)) {
                    echo date("Y-m-d H:i:s") . " MySQL: FAILED !!! {$ligne}\n";
                    RestoreFromBackup_progress("{failed} {error} {$re[1]} ", 100);
                    return;
                }
                echo date("Y-m-d H:i:s") . " MySQL: {$ligne}\n";
            }
            $lastmd5 = $fileMD5;
        }
        $pid = $unix->PIDOF_PATTERN("{$mysql}\\s+.*?--socket=/var/run/mysqld/zarafa-db.sock.*?database=zarafa");
        echo "Action: PID: {$pid}\n";
        if (!$unix->process_exists($pid)) {
            echo "Action: injection stopped running since " . $unix->distanceOfTimeInWords($tStart, time(), true) . "\n";
            $continue = false;
            break;
        }
        echo "Action: PID {$pid} running since " . $unix->distanceOfTimeInWords($tStart, time(), true) . ", please wait...\n";
        RestoreFromBackup_progress($unix->distanceOfTimeInWords($tStart, time(), true) . " {please_wait} !", 71);
        $continue = true;
        sleep(30);
        continue;
    }
    echo "Action: Done, took: " . $unix->distanceOfTimeInWords($tStart, time(), true) . "\n";
    echo "Action: Please wait, Checks Database size\n";
    RestoreFromBackup_progress("Checks Database size", 75);
    databasesize(true);
    RestoreFromBackup_progress("{restoring_data} {success}", 80);
    echo "Action: restart_services\n";
    RestoreFromBackup_progress("{restart_services}", 90);
    $unix->THREAD_COMMAND_SET("/etc/init.d/zarafa-server restart");
    echo "Action: Restore task done...\n";
    echo "Action: You can close the windows now...\n";
    RestoreFromBackup_progress("{done}", 100);
    die;
}
Esempio n. 4
0
function stop($aspid = false)
{
    $unix = new unix();
    $suffix = null;
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    if (!$aspid) {
        $pid = $unix->get_pid_from_file($pidfile);
        if ($unix->process_exists($pid, basename(__FILE__))) {
            $time = $unix->PROCCESS_TIME_MIN($pid);
            if ($GLOBALS["OUTPUT"]) {
                echo "Stopping......: " . date("H:i:s") . " [INIT]: Already task running PID {$pid} since {$time}mn\n";
            }
            return;
        }
    }
    @file_put_contents($pidfile, getmypid());
    $pid = XZARAFA_SERVER_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server already stopped...\n";
        }
        return;
    }
    system_admin_events("Warning, Ordered to stop Zarafa service", __FUNCTION__, __FILE__, __LINE__, "mailboxes");
    if ($GLOBALS["KILL"]) {
        $killopt = " -9";
        @unlink("/tmp/zarafa-upgrade-lock");
        $suffix = " (forced)";
    }
    if (is_file("/tmp/zarafa-upgrade-lock")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server database upgrade is taking place.\n";
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Stopping......: " . date("H:i:s") . " [INIT]: Do not stop this process bacause it may render your database unusable..\n";
        }
        return;
    }
    $time = $unix->PROCCESS_TIME_MIN($pid);
    if ($GLOBALS["OUTPUT"]) {
        echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server Daemon with a ttl of {$time}mn{$suffix}\n";
    }
    $kill = $unix->find_program("kill");
    if ($GLOBALS["OUTPUT"]) {
        echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server killing smoothly PID {$pid}...{$suffix}\n";
    }
    shell_exec("{$kill}{$killopt} {$pid}");
    sleep(1);
    for ($i = 1; $i < 60; $i++) {
        $pid = XZARAFA_SERVER_PID();
        if (!$unix->process_exists($pid)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server pid {$pid} successfully stopped ...{$suffix}\n";
            }
            break;
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server wait {$i}/60{$suffix}\n";
        }
        shell_exec("{$kill}{$killopt} {$pid}");
        sleep(1);
    }
    if ($GLOBALS["KILL"]) {
        $zarafadmin = $unix->find_program("zarafa-admin");
        $pid = $unix->PIDOF($zarafadmin);
        if ($unix->process_exists($pid)) {
            for ($i = 1; $i < 60; $i++) {
                if (!$unix->process_exists($pid)) {
                    break;
                }
                if ($GLOBALS["OUTPUT"]) {
                    echo "Stopping......: " . date("H:i:s") . " [INIT]: Stopping zarafa-admin PID {$pid}{$suffix}\n";
                }
                unix_system_kill_force($pid);
                $pid = $unix->PIDOF($zarafadmin);
            }
        }
        $createuser_pid = $unix->PIDOF_PATTERN("createuser.d");
        if ($unix->process_exists($createuser_pid)) {
            for ($i = 1; $i < 60; $i++) {
                if (!$unix->process_exists($createuser_pid)) {
                    break;
                }
                if ($GLOBALS["OUTPUT"]) {
                    echo "Stopping......: " . date("H:i:s") . " [INIT]: Stopping createuser.d PID {$createuser_pid}{$suffix}\n";
                }
                unix_system_kill_force($createuser_pid);
                $createuser_pid = $unix->PIDOF_PATTERN("createuser.d");
            }
        }
    }
    $pid = XZARAFA_SERVER_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server daemon success...{$suffix}\n";
        }
        return;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Stopping......: " . date("H:i:s") . " [INIT]: zarafa-server daemon failed...{$suffix}\n";
    }
}