예제 #1
0
function gs_asterisks_reload($host_ids, $dialplan_only)
{
    $dialplan_only = !!$dialplan_only;
    if (!$host_ids || !is_array($host_ids)) {
        $host_ids = false;
    }
    # connect to db
    #
    $db = gs_db_master_connect();
    if (!$db) {
        return new GsError('Could not connect to database.');
    }
    # get hosts
    #
    $hosts = @gs_hosts_get();
    if (isGsError($hosts)) {
        return new GsError($hosts->getMsg());
    }
    if (!is_array($hosts)) {
        return new GsError('Failed to get hosts.');
    }
    $GS_INSTALLATION_TYPE_SINGLE = gs_get_conf('GS_INSTALLATION_TYPE_SINGLE');
    if (!$GS_INSTALLATION_TYPE_SINGLE) {
        # get our host IDs
        #
        $our_host_ids = @gs_get_listen_to_ids();
        if (isGsError($our_host_ids)) {
            return new GsError($our_host_ids->getMsg());
        }
        if (!is_array($our_host_ids)) {
            return new GsError('Failed to get our host IDs.');
        }
    }
    # are we root? do we have to sudo?
    #
    $uid = @posix_geteuid();
    $uinfo = @posix_getPwUid($uid);
    $uname = @$uinfo['name'];
    $sudo = $uname == 'root' ? '' : 'sudo ';
    $ok = true;
    foreach ($hosts as $host) {
        if (!$host_ids || in_array($host['id'], $host_ids)) {
            $cmd = '/opt/gemeinschaft/sbin/start-asterisk' . ($dialplan_only ? ' --dialplan' : '');
            if (!$GS_INSTALLATION_TYPE_SINGLE && !in_array($host['id'], $our_host_ids)) {
                # this is not the local node
                $cmd = $sudo . 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($host['host']) . ' ' . qsa($cmd);
            }
            @exec($sudo . $cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err);
            $ok = $ok && $err == 0;
        }
    }
    if (!$ok) {
        return new GsError('Failed to reload Asterisks.');
    }
    return true;
}
function gs_asterisks_prune_peer($peer, $host_ids = false)
{
    if (!$host_ids || !is_array($host_ids)) {
        $host_ids = false;
    }
    # check peer
    if ($peer === 'all' || $peer == '') {
        $peer = 'all';
    } elseif (!preg_match('/^[1-9][0-9]{1,9}$/', $peer)) {
        return new GsError('Invalid peer name.');
    }
    # connect to db
    #
    $db = gs_db_master_connect();
    if (!$db) {
        return new GsError('Could not connect to database.');
    }
    # get hosts
    #
    $hosts = @gs_hosts_get();
    if (isGsError($hosts)) {
        return new GsError($hosts->getMsg());
    }
    if (!is_array($hosts)) {
        return new GsError('Failed to get hosts.');
    }
    $GS_INSTALLATION_TYPE_SINGLE = gs_get_conf('GS_INSTALLATION_TYPE_SINGLE');
    if (!$GS_INSTALLATION_TYPE_SINGLE) {
        # get our host IDs
        #
        $our_host_ids = @gs_get_listen_to_ids();
        if (isGsError($our_host_ids)) {
            return new GsError($our_host_ids->getMsg());
        }
        if (!is_array($our_host_ids)) {
            return new GsError('Failed to get our host IDs.');
        }
    }
    # are we root? do we have to sudo?
    #
    $uid = @posix_geteuid();
    $uinfo = @posix_getPwUid($uid);
    $uname = @$uinfo['name'];
    $sudo = $uname == 'root' ? '' : 'sudo ';
    $ok = true;
    foreach ($hosts as $host) {
        if (!$host_ids || in_array($host['id'], $host_ids)) {
            $cmd = 'asterisk -rx \'sip prune realtime ' . $peer . '\' ';
            if (!$GS_INSTALLATION_TYPE_SINGLE && !in_array($host['id'], $our_host_ids)) {
                # this is not the local node
                $cmd = $sudo . 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes -l root ' . qsa($host['host']) . ' ' . qsa($cmd);
            }
            @exec($sudo . $cmd . ' 1>>/dev/null 2>>/dev/null', $out, $err);
            $ok = $ok && $err == 0;
        }
    }
    if (!$ok) {
        return new GsError('Failed to prune peer "' . $peer . '".');
    }
    return true;
}
예제 #3
0
function gs_db_setup_replication($master_host, $slave_host, $user, $pass)
{
    if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) {
        return new GsError('Not allowed on single server systems.');
    }
    # are we root? do we have to sudo?
    $uid = @posix_geteuid();
    $uinfo = @posix_getPwUid($uid);
    $uname = @$uinfo['name'];
    $sudo = $uname === 'root' ? '' : 'sudo ';
    # get binlog position
    #
    $master = null;
    $ok = gs_db_connect($master, 'master', $master_host, $user, $pass, GS_DB_MASTER_DB, 1);
    if (!$ok) {
        return new GsError('Failed to connect to master database.');
    }
    $rs = $master->execute('SHOW MASTER STATUS');
    if (!$rs) {
        return new GsError('DB error.');
    }
    $master_status = $rs->fetchRow();
    if (!$master_status) {
        return new GsError('DB error.');
    }
    # Stop Slave
    #
    $slave = null;
    $ok = gs_db_connect($slave, 'slave', $slave_host, $user, $pass, GS_DB_SLAVE_DB, 1);
    if (!$ok) {
        return new GsError('Failed to connect to slave database.');
    }
    $ok = $slave->execute('STOP SLAVE');
    if (!$ok) {
        return new GsError('Failed to stop database slave replication');
    }
    $dump_filename = '/tmp/gs-db-resync-dump-' . rand() . '.sql';
    # dump Master database
    #
    $cmd = 'mysqldump --opt --lock-tables --databases asterisk';
    $cmd = 'ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . $master_host) . ' ' . qsa($cmd) . ' > ' . qsa($dump_filename) . ' 2>>/dev/null';
    $err = 0;
    $out = array();
    @exec($sudo . $cmd, $out, $err);
    if ($err != 0) {
        return new GsError('Failed to save dump of master database!');
    }
    # restore dump on Slave
    #
    $cmd = 'cat ' . qsa($dump_filename) . ' | ssh -o StrictHostKeyChecking=no -o BatchMode=yes ' . qsa('root@' . $slave_host) . ' ' . qsa('mysql asterisk');
    //FIXME - instead of cat, copy the dump to the slave first
    $err = 0;
    $out = array();
    @exec($sudo . $cmd, $out, $err);
    if ($err != 0) {
        return new GsError('Failed to restore database dump on slave!');
    }
    # start replication on Slave
    #
    $query = 'CHANGE MASTER TO ' . 'MASTER_HOST=\'' . $master->escape($master_host) . '\', ' . 'MASTER_USER=\'' . $master->escape($user) . '\', ' . 'MASTER_PASSWORD=\'' . $master->escape($pass) . '\', ' . 'MASTER_LOG_FILE=\'' . $master->escape($master_status['File']) . '\', ' . 'MASTER_LOG_POS=' . (int) $master_status['Position'];
    $ok = $slave->execute($query);
    if (!$ok) {
        return new GsError('Failed to Change Master on Slave!');
    }
    $ok = $slave->execute('START SLAVE');
    if (!$ok) {
        return new GsError('Failed to Start Slave Replication Process');
    }
    return true;
}