Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 123 | Revision 125 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php
        //$Id: load.php 124 2008-05-22 13:50:21Z lars $
        /**
         *      @package        openvz_admin
         *      @author         Lars Tiefland <ltiefland@gmail.com> 
         **/
        require ("include/common.php");
        $hns = HN::getHNs();
        unset($hns[-1]);
        #       $hns[3]="gate.dynalias.net";
        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($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)
        {
                $conf = basename($config);
                $cmd = "scp root@$hn_name:/etc/vz/conf/$conf cfgs/$hn_id";
                exec($cmd, $out, $ret);
                $cont = file("cfgs/$hn_id/$conf");
                $cont[] = "v_id=\"" . substr($conf, 0, strpos($conf, '.')) . "\"";
                return $cont;
        }

        function update_db($hn_id, $cfgs)
        {
                global $db;
                foreach ($cfgs as $config)
                {
                        $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"];
                                }
                                else
                                {
                                        $sql = "INSERT INTO distributions (dist_name, dist_template) VALUES('" . $config["DISTRIBUTION"] .
                                                "', '" . $config["OSTEMPLATE"] . "')";
                                        $res = $db->query($sql);
                                        $dist_id = $res->lastinsertID();
                                }
                                $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();
                                                }
                                        }
                                }
                                foreach ($conf 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();
                                        }
                                }
                        }
                        else
                        {
                                echo $res->getUserInfo();
                        }
                }
        }
?>