Revision 165 | Zur aktuellen Revision | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed
<?phpphp//$Id: vServer.class.php 168 2011-07-10 19:11:02Z lars $/*** @package openvz_admin* @author Lars Tiefland <ltiefland@gmail.com>**/class vServer{protected $status;protected $name;protected $hn_id;protected $dist;protected $dist_id;protected $v_id;function toArray(){$ret["hn_id"] = $this->hn_id;$ret["v_id"] = $this->v_id;$ret["dist_id"] = $this->dist_id;$ret["dist"] = $this->dist;$ret["name"] = $this->name;$ret["status"] = $this->status;return $ret;}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;$hn = HN::getHN( $this->hn_id );$cmd = "ssh root@" . $hn["name"] . " vzctl status $id";exec( $cmd, $out, $ret );if ( ! $ret ){$out_v["status"] = $out[0];}else{$out_v["status"] = "unknown";}$out_v["code"] = $ret;}else{$id = $this->v_id;$hn = HN::getHN( $this->hn_id );$hn_status = $hn["status"];$out_v["status"] = "Hardware node is $hn_status";if ( $hn_status == "online" ){$out_v["code"] = 0;}else{$out_v["code"] = 1;}}if ( eregi( "running", $out_v["status"] ) || eregi( "online", $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 ORDER BY v_id";$res = $db->query( $sql );$id = 0;while ( $row = $res->fetchRow() ){$vservers[$id] = $row;$v = new vServer( $row["v_id"], $row["hn_id"] );$ret = $v->getStatus();$dist = new Dist( $row["v_dist"] );$dist_info = $dist->toArray();$vservers[$id]["id"] = $row["id"];$vservers[$id]["v_dist_name"] = $dist_info["dist_name"];$vservers[$id]["v_status"] = $ret["status"];$vservers[$id]["code"] = $ret["code"];$vservers[$id]["started"] = $ret["started"];$id++;}return $vservers;}function control( $cmd = "" ){$cmds = array( "start", "stop", "restart", "destroy" );if ( $cmd == "" || ! in_array( $cmd, $cmds ) ){return false;}unset( $out );if ( $this->v_id != 0 ){$id = $this->v_id;$hn = HN::getHN( $this->hn_id );$cmd = "ssh root@" . $hn["name"] . " 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{$hn = HN::getHN( $this->hn_id );switch ( $cmd ){case "restart":$cmd = "ssh root@" . $hn["name"] . " reboot";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;break;default:$out_v["code"] = 255;$out_v["error"] = true;$out_v["status"] = "n/a";break;}}return $out_v;}function save( $v_name, $v_dist, $v_ip, $v_r_pw ){global $db, $smarty, $title, $tpl_file;$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 = $dist["template"];$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 ) ){$v_id = $db->lastinsertid();$sql = "INSERT INTO host_2_ip (ve_id, ip) VALUES ($v_id,$v_ip)";$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 . "<br>" . implde( "<br>",$out ) );$db->rollback();}}else{$smarty->assign( "error", true );$smarty->assign( "meld", "Ein Fehler ist aufgetreten!" );$smarty->assign( "db_meld", $res->getUserInfo() );$tpl_file = "meld.tpl";$title = "Fehler";$db->rollback();}}else{$smarty->assign( "error", true );$smarty->assign( "meld", "Ein Fehler ist aufgetreten!" );$smarty->assign( "db_meld", $res->getUserInfo() );$db->rollback();}}}?>