Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 157 | Revision 159 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php
    //$Id: load.php 158 2009-11-07 21:45:38Z lars $
    /**
     *  @package        openvz_admin
     *  @author         Lars Tiefland <ltiefland@gmail.com> 
     **/
    require ( "include/common.php" );
    $hns = HN::getHNs();
    unset( $hns[-1] );
    foreach ( $hns as $hn_id => $hn_name )
    {
        $configs = load_configs( $hn_id, $hn_name );
        foreach ( $configs as $conf_id => $config )
        {
            $cfg = read_config( $hn_id, $hn_name, $config );
            update_db( $hn_id, $cfg );
        }
    }

    function load_configs( $hn_id, $hn_name )
    {
        unset( $out );
        $cmd = "ping $hn_name -w1";
        exec( $cmd, $out, $ret );
        if ( !$ret )
        {
            echo "Hole Konfigurationen von $hn_name\n";
            unset( $out );
            $cmd = "ssh root@$hn_name ls /etc/vz/conf/*.conf";
            exec( $cmd, $out, $ret );
            return $out;
        }
    }

    function read_config( $hn_id, $hn_name, $config )
    {
        $config = basename( $config );
        $cmd = "scp root@$hn_name:/etc/vz/conf/$config cfgs/$hn_id";
        exec( $cmd, $out, $ret );
        $conf = file( "cfgs/$hn_id/$config" );
        $conf[] = "v_id=\"" . substr( $config, 0, strpos($config, '.') ) . "\"";
        foreach ( $conf as $value )
        {
            if ( $value )
            {
                $value = rtrim( $value );
                if ( $value[0] == " " || $value[0] == "#" )
                {
                    continue;
                }
                $cfg_v = explode( "=", $value );
                $cfg_val[$cfg_v[0]] = trim( $cfg_v[1], '"' );
            }
            else
            {
                continue;
            }
        }
        $cfgs[] = $cfg_val;
        return $cfgs;
    }

    function update_db( $hn_id, $cfgs )
    {
        global $db;

        foreach ( $cfgs as $config )
        {
            if ( isset($config["DISTRIBUTION"]) )
            {
                $dist_name = $config["DISTRIBUTION"];
            } elseif ( in_array($hn_id, array(1, 3)) )
            {
                $dist_name = "gentoo";
            }
            else
            {
                $dist_name = "centos";
            }
            $sql = "
                                SELECT 
                                        * 
                                FROM 
                                        distributions 
                                WHERE 
                                        dist_name='$dist_name'
                        ";
            $res = $db->query( $sql );
            if ( !PEAR::isError($res) )
            {
                if ( $res->numRows() )
                {
                    $row = $res->fetchRow();
                    $dist_id = $row["dist_id"];
                    $sql_v = "
                                                SELECT 
                                                        v_dist, 
                                                        dist_name 
                                                FROM 
                                                        distributions, 
                                                        vservers 
                                                WHERE 
                                                        v_id=" . $config["v_id"] . "
                                                AND 
                                                        dist_id=v_dist 
                                        ";
                    $res_v = $db->query( $sql_v );
                    if ( PEAR::isError($res_v) )
                    {
                        die( $res_v->getUserInfo() );
                    }
                    $row_v = $res_v->fetchRow();
                    $dist_name = $row_v["dist_name"];
                    if ( $dist_name != $config["DISTRIBUTION"] )
                    {
                        $sql = "
                                                        UPDATE 
                                                                vservers 
                                                        SET 
                                                                v_dist=$dist_id 
                                                        WHERE 
                                                                v_id=" . $config["v_id"] . "
                                                ";
                        $res = $db->query( $sql );
                        if ( PEAR::isError($res) )
                        {
                            die( $res->getUserInfo() );
                        }
                    }
                }
                else
                {
                    $sql = "
                                                INSERT INTO 
                                                        distributions 
                                                (
                                                        dist_name, 
                                                        dist_template
                                                )
                                                VALUES
                                                (
                                                        '" . $config["DISTRIBUTION"] . "', 
                                                        '" . $config["OSTEMPLATE"] . "'
                                                )
                                        ";
                    $res = $db->query( $sql );
                    $dist_id = $db->lastinsertID();
                }
            }
            else
            {
                die( $res->getUserInfo() );
            }
            $sql = "
                                SELECT 
                                        * 
                                FROM 
                                        vservers 
                                WHERE 
                                        v_id=" . $config["v_id"] . " 
                                AND 
                                        hn_id=$hn_id
                        ";
            $res = $db->query( $sql );
            if ( !PEAR::isError($res) )
            {
                if ( !$res->numRows() )
                {
                    $sql = "
                                                INSERT INTO 
                                                        vservers 
                                                (
                                                        v_id, 
                                                        v_name, 
                                                        v_dist, 
                                                        hn_id
                                                ) 
                                                VALUES 
                                                (
                                                        " . $config["v_id"] . ", 
                                                        '" . $config["HOSTNAME"] . "', 
                                                        $dist_id, 
                                                        $hn_id
                                                )
                                        ";
                    $res = $db->query( $sql );
                    if ( PEAR::isError($res) )
                    {
                        die( $res->getUserInfo() );
                    }
                } elseif ( $config["HOSTNAME"] )
                {
                    $sql = "
                                                UPDATE 
                                                        vservers 
                                                SET 
                                                        v_name='" . $config["HOSTNAME"] . "' 
                                                WHERE 
                                                        v_id=" . $config["v_id"] . " 
                                                AND 
                                                        hn_id=$hn_id
                                        ";
                    $res = $db->query( $sql );
                    if ( PEAR::isError($res) )
                    {
                        die( $res->getUserInfo() );
                    }
                }
            }
            else
            {
                die( $res->getUserInfo() );
            }
            foreach ( $config as $field => $value )
            {
                $sql = "
                                        SELECT 
                                                * 
                                        FROM 
                                                vserver_config 
                                        WHERE 
                                                vc_name='$field' 
                                        AND 
                                                v_id=" . $config["v_id"] . " 
                                        AND 
                                                hn_id=$hn_id
                        s       ";
                $res = $db->query( $sql );
                if ( !PEAR::isError($res) )
                {
                    if ( $res->numRows() )
                    {
                        $sql = "
                                                        UPDATE 
                                                                vserver_config 
                                                        SET 
                                                                vc_value='$value' 
                                                        WHERE 
                                                                vc_name='$field' 
                                                        AND 
                                                                v_id=" . $config["v_id"] . " 
                                                        AND 
                                                                hn_id=$hn_id
                                                ";
                    }
                    else
                    {
                        $sql = "
                                                        INSERT INTO 
                                                                vserver_config 
                                                        VALUES 
                                                        (
                                                                " . $config["v_id"] . ", 
                                                                $hn_id, 
                                                                '$field', 
                                                                '$value'
                                                        )
                                                ";
                    }
                    $res = $db->query( $sql );
                    if ( PEAR::isError($res) )
                    {
                        die( $res->getUserInfo() );
                    }
                }
                else
                {
                    die( $res->getUserInfo() );
                }
            }
        }
    }
?>