Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 56 | Revision 60 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

<?php
        //$Id$
        class vServer
        {
                protected $status;
                protected $name;
                protected $hn_id;
                protected $dist;
                protected $dist_id;
                protected $v_id;
                function __construct($v_id=NULL, $hn_id=1)
                {
                        global $db;
                        if(is_numeric($v_id))
                        {
                                $sql="SELECT * FROM vservers, distributions WHERE v_id=$v_id AND hn_id=$hn_id AND dist_id=v_dist";
                                $res=$db->query($sql);
                                if(!PEAR::isError($res))
                                {
                                        $row=$res->fetchRow();
                                        $this->v_id=$v_id;
                                        $this->dist=$row["dist_name"];
                                        $this->dist_id=$row["dist_id"];
                                        $this->name=$row["v_name"];
                                        $this->hn_id=$hn_id;
                                        $this->status=$this->getStatus();
                                }
                                else
                                {
                                        echo $res->getUserInfo();
                                        return false;
                                }
                        }
                        else
                        {
                                $this->v_id=0;
                                $this->dist_id=1;
                                $this->dist="gentoo";
                                $this->hn_id=1;
                                $this->name="";
                                $this->status["status"]="";
                                $this->status["code"]="0";
                        }
                }
                
                function getStatus()
                {
                        if(!$this->status)
                        {

                                unset($out);
                                if($this->v_id!=0)
                                {
                                        $id=$this->v_id;
                                        $cmd="ssh root@mainframe vzctl status $id";
                                        exec($cmd,$out,$ret);
                                        if(!$ret)
                                        {
                                                $out_v["status"]=$out[0];
                                        }
                                        else
                                        {
                                                $out_v["status"]="unknown";
                                        }
                                        $out_v["code"]=$ret;
                                }
                                else
                                {
                                        $out_v["status"]="Hardware node is running";
                                        $out_v["code"]=0;
                                }
                                if(eregi("running",$out_v["status"]))
                                {
                                        $out_v["started"]=true;
                                }
                                else
                                {
                                        $out_v["started"]=false;
                                }
                                if(!$out_v["code"]&&$out_v["started"])
                                {
                                        $class="success_upd";
                                }
                                else
                                {
                                        $class="error_upd";
                                }
                                $out_v["status"]="<div class=\"$class\">".$out_v["status"]."</div>";
                                $this->status=$out_v;

                        }
                        return $this->status;
                }
                
                static function getvServers($hn_id=1)
                {
                        global $db;
                        $sql="SELECT * FROM vservers WHERE hn_id=$hn_id";
                        $res=$db->query($sql);
                        while($row=$res->fetchRow())
                        {
                                $vservers[$row["id"]]=$row;
                                $v=new vServer($row["v_id"], $row["hn_id"]);
                                $ret=$v->getStatus();
                                $vservers[$row["id"]]["v_status"]=$ret["status"];
                                $vservers[$row["id"]]["code"]=$ret["code"];
                                $vservers[$row["id"]]["started"]=$ret["started"];
                        }
                        return $vservers;
                }
                
                function control($cmd="")
                {
                        $cmds=array("start","stop","restart");
                        if($cmd==""||!in_array($cmd, $cmds))
                        {
                                return false;
                        }
                        unset($out);
                        if($this->v_id!=0)
                        {
                                $id=$this->v_id;
                                $cmd="ssh root@mainframe vzctl $cmd $id";
                                exec($cmd,$out,$ret);
                                if(!$ret)
                                {
                                        $out_v["status"]=implode("<br>",$out);
                                        $out_v["error"]=false;
                                }
                                else
                                {
                                        $out_v["status"]="unknown";
                                        $out_v["error"]=true;
                                }
                                $out_v["code"]=$ret;
                        }
                        else
                        {
                                $out_v=false;
                        }
                        return $out_v;
                }
                
                function save($v_name, $v_dist, $v_ip, $v_r_pw)
                {
                        global $db, $smarty;
                        $this->name=$v_name;
                        $this->dist_id=$v_dist;
                        unset($out);
                        $d=new Dist($v_dist);
                        $dist=$d->toArray();
                        $ip=IP::getIP($v_ip);
                        $d_name=$dist["dist_name"];
                        $this->dist=$d_name;
                        $d_template="/vz/template/cache/".$dist["template"].".tar.gz";
                        $db->beginTransaction();
                        $sql="INSERT into vservers (v_id, hn_id, v_name, v_dist) VALUES ($this->v_id, $this->hn_id, '$this->name', $this->dist_id)";
                        $res=$db->query($sql);
                        if(!PEAR::isError($res))
                        {
                                $cmd="ssh root@mainframe vzctl create $this->v_id --hostname $v_name --ostemplate $d_template --config vps.$d_name --ipadd ".$ip["ip"];
                                exec($cmd,$out,$ret);
                                if(!$ret)
                                {
                                        $smarty->assign("meld","vServer erfolgreich angelegt!");
                                        $smarty->assign("error", false);
                                        $db->commit();
                                }
                                else
                                {
                                        $smarty->assign("error", true);
                                        $smarty->assign("meld","Ein Fehler ist aufgetreten!");
                                        $smarty->assign("db_meld",$cmd);
                                        $db->rollback();
                                }
                        }
                        else
                        {
                                $smarty->assign("error", true);
                                $smarty->assign("meld","Ein Fehler ist aufgetreten!");
                                $smarty->assign("db_meld",$res->getUserInfo());
                                $db->rollback();
                        }
                }
        }
?>