Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 60 | Revision 68 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
13 lars 1
<?php
2
	//$Id: vServer.class.php 61 2007-10-03 14:30:16Z lars $
3
	class vServer
4
	{
5
	 	protected $status;
6
	 	protected $name;
7
	 	protected $hn_id;
8
	 	protected $dist;
9
	 	protected $dist_id;
10
	 	protected $v_id;
24 lars 11
	 	function __construct($v_id=NULL, $hn_id=1)
13 lars 12
	 	{
13
	 	 	global $db;
24 lars 14
			if(is_numeric($v_id))
13 lars 15
			{
16
				$sql="SELECT * FROM vservers, distributions WHERE v_id=$v_id AND hn_id=$hn_id AND dist_id=v_dist";
17
				$res=$db->query($sql);
18
				if(!PEAR::isError($res))
19
				{
20
					$row=$res->fetchRow();
21
					$this->v_id=$v_id;
22
					$this->dist=$row["dist_name"];
23
					$this->dist_id=$row["dist_id"];
24
					$this->name=$row["v_name"];
25
					$this->hn_id=$hn_id;
26
					$this->status=$this->getStatus();
27
				}
28
				else
29
				{
30
					echo $res->getUserInfo();
31
					return false;
32
				}
33
			}
34
			else
35
			{
36
				$this->v_id=0;
37
				$this->dist_id=1;
38
				$this->dist="gentoo";
39
				$this->hn_id=1;
40
				$this->name="";
24 lars 41
				$this->status["status"]="";
42
				$this->status["code"]="0";
13 lars 43
			}
44
		}
45
 
46
		function getStatus()
47
		{
48
			if(!$this->status)
49
			{
50
 
51
				unset($out);
52
				if($this->v_id!=0)
53
				{
54
					$id=$this->v_id;
55
					$cmd="ssh root@mainframe vzctl status $id";
56
					exec($cmd,$out,$ret);
57
					if(!$ret)
58
					{
15 lars 59
						$out_v["status"]=$out[0];
13 lars 60
					}
61
					else
62
					{
63
						$out_v["status"]="unknown";
64
					}
65
					$out_v["code"]=$ret;
66
				}
67
				else
68
				{
69
					$out_v["status"]="Hardware node is running";
70
					$out_v["code"]=0;
71
				}
72
				if(eregi("running",$out_v["status"]))
73
				{
74
					$out_v["started"]=true;
75
				}
76
				else
77
				{
78
					$out_v["started"]=false;
79
				}
80
				if(!$out_v["code"]&&$out_v["started"])
81
				{
82
					$class="success_upd";
83
				}
84
				else
85
				{
20 lars 86
					$class="error_upd";
13 lars 87
				}
88
				$out_v["status"]="<div class=\"$class\">".$out_v["status"]."</div>";
89
				$this->status=$out_v;
90
 
91
			}
92
			return $this->status;
93
		}
94
 
18 lars 95
		static function getvServers($hn_id=1)
13 lars 96
		{
97
			global $db;
18 lars 98
			$sql="SELECT * FROM vservers WHERE hn_id=$hn_id";
13 lars 99
			$res=$db->query($sql);
29 lars 100
			while($row=$res->fetchRow())
101
			{
102
				$vservers[$row["id"]]=$row;
103
				$v=new vServer($row["v_id"], $row["hn_id"]);
104
				$ret=$v->getStatus();
105
				$vservers[$row["id"]]["v_status"]=$ret["status"];
106
				$vservers[$row["id"]]["code"]=$ret["code"];
107
				$vservers[$row["id"]]["started"]=$ret["started"];
108
			}
109
			return $vservers;
13 lars 110
		}
15 lars 111
 
112
		function control($cmd="")
113
		{
114
			$cmds=array("start","stop","restart");
115
			if($cmd==""||!in_array($cmd, $cmds))
116
			{
117
				return false;
118
			}
119
			unset($out);
120
			if($this->v_id!=0)
121
			{
122
				$id=$this->v_id;
123
				$cmd="ssh root@mainframe vzctl $cmd $id";
124
				exec($cmd,$out,$ret);
125
				if(!$ret)
126
				{
127
					$out_v["status"]=implode("<br>",$out);
16 lars 128
					$out_v["error"]=false;
15 lars 129
				}
130
				else
131
				{
132
					$out_v["status"]="unknown";
16 lars 133
					$out_v["error"]=true;
15 lars 134
				}
135
				$out_v["code"]=$ret;
136
			}
137
			else
138
			{
139
				$out_v=false;
140
			}
141
			return $out_v;
142
		}
44 lars 143
 
144
		function save($v_name, $v_dist, $v_ip, $v_r_pw)
145
		{
146
			global $db, $smarty;
147
			$this->name=$v_name;
148
			$this->dist_id=$v_dist;
149
			unset($out);
150
			$d=new Dist($v_dist);
151
			$dist=$d->toArray();
56 lars 152
			$ip=IP::getIP($v_ip);
44 lars 153
			$d_name=$dist["dist_name"];
154
			$this->dist=$d_name;
60 lars 155
			$d_template=$dist["template"];
44 lars 156
			$db->beginTransaction();
48 lars 157
			$sql="INSERT into vservers (v_id, hn_id, v_name, v_dist) VALUES ($this->v_id, $this->hn_id, '$this->name', $this->dist_id)";
44 lars 158
			$res=$db->query($sql);
159
			if(!PEAR::isError($res))
160
			{
56 lars 161
				$cmd="ssh root@mainframe vzctl create $this->v_id --hostname $v_name --ostemplate $d_template --config vps.$d_name --ipadd ".$ip["ip"];
44 lars 162
				exec($cmd,$out,$ret);
163
				if(!$ret)
164
				{
165
					$smarty->assign("meld","vServer erfolgreich angelegt!");
166
					$smarty->assign("error", false);
167
					$db->commit();
168
				}
169
				else
170
				{
171
					$smarty->assign("error", true);
172
					$smarty->assign("meld","Ein Fehler ist aufgetreten!");
51 lars 173
					$smarty->assign("db_meld",$cmd);
44 lars 174
					$db->rollback();
175
				}
176
			}
177
			else
178
			{
179
				$smarty->assign("error", true);
180
				$smarty->assign("meld","Ein Fehler ist aufgetreten!");
181
				$smarty->assign("db_meld",$res->getUserInfo());
182
				$db->rollback();
183
			}
184
		}
13 lars 185
	}
186
?>