Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 136 | Revision 154 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php
        //$Id: load.php 138 2008-07-15 11:26:24Z 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 = "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"]))
                        {
                                $sql = "SELECT * FROM distributions WHERE dist_name='" . $config["DISTRIBUTION"] .
                                        "'";
                                $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 dist_id=v_dist AND v_id=".$config["v_id"];
                                                $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))
                                        {
                                                echo $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";
                                $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))
                                        {
                                                echo $res->getUserInfo();
                                        }
                                }
                                else
                                {
                                        echo $res->getUserInfo();
                                }
                        }
                }
        }
?>