Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 109 | Revision 134 | 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 133 2008-05-22 16:48:38Z lars $
109 lars 3
	/**
4
	 * 	@package	openvz_admin
5
	 * 	@author		Lars Tiefland <ltiefland@gmail.com>
6
	 **/
13 lars 7
	class vServer
8
	{
9
	 	protected $status;
10
	 	protected $name;
11
	 	protected $hn_id;
12
	 	protected $dist;
13
	 	protected $dist_id;
14
	 	protected $v_id;
24 lars 15
	 	function __construct($v_id=NULL, $hn_id=1)
13 lars 16
	 	{
17
	 	 	global $db;
24 lars 18
			if(is_numeric($v_id))
13 lars 19
			{
20
				$sql="SELECT * FROM vservers, distributions WHERE v_id=$v_id AND hn_id=$hn_id AND dist_id=v_dist";
21
				$res=$db->query($sql);
22
				if(!PEAR::isError($res))
23
				{
24
					$row=$res->fetchRow();
25
					$this->v_id=$v_id;
26
					$this->dist=$row["dist_name"];
27
					$this->dist_id=$row["dist_id"];
28
					$this->name=$row["v_name"];
29
					$this->hn_id=$hn_id;
30
					$this->status=$this->getStatus();
31
				}
32
				else
33
				{
34
					echo $res->getUserInfo();
35
					return false;
36
				}
37
			}
38
			else
39
			{
40
				$this->v_id=0;
41
				$this->dist_id=1;
42
				$this->dist="gentoo";
43
				$this->hn_id=1;
44
				$this->name="";
24 lars 45
				$this->status["status"]="";
46
				$this->status["code"]="0";
13 lars 47
			}
48
		}
49
 
87 lars 50
		function getStatus()
13 lars 51
		{
52
			if(!$this->status)
53
			{
54
 
55
				unset($out);
56
				if($this->v_id!=0)
57
				{
58
					$id=$this->v_id;
87 lars 59
					$hn=HN::getHN($this->hn_id);
82 lars 60
					$cmd="ssh root@".$hn["name"]." vzctl status $id";
13 lars 61
					exec($cmd,$out,$ret);
62
					if(!$ret)
63
					{
15 lars 64
						$out_v["status"]=$out[0];
13 lars 65
					}
66
					else
67
					{
68
						$out_v["status"]="unknown";
69
					}
70
					$out_v["code"]=$ret;
71
				}
72
				else
73
				{
133 lars 74
					$id=$this->v_id;
75
					$hn=HN::getHN($this->hn_id);
76
					$hn_status=$hn->status;
77
					$out_v["status"]="Hardware node is $hn_status";
78
					if($hn_status=="online")
79
					{
80
						$out_v["code"]=0;
81
					}
82
					else
83
					{
84
						$out_v["code"]=1;
85
					}
13 lars 86
				}
133 lars 87
				if(eregi("running",$out_v["status"])||eregi("online",$out_v["status"]))
13 lars 88
				{
89
					$out_v["started"]=true;
90
				}
91
				else
92
				{
93
					$out_v["started"]=false;
94
				}
95
				if(!$out_v["code"]&&$out_v["started"])
96
				{
97
					$class="success_upd";
98
				}
99
				else
100
				{
20 lars 101
					$class="error_upd";
13 lars 102
				}
103
				$out_v["status"]="<div class=\"$class\">".$out_v["status"]."</div>";
104
				$this->status=$out_v;
105
 
106
			}
107
			return $this->status;
108
		}
109
 
18 lars 110
		static function getvServers($hn_id=1)
13 lars 111
		{
112
			global $db;
68 lars 113
			$sql="SELECT * FROM vservers WHERE hn_id=$hn_id ORDER BY v_id";
13 lars 114
			$res=$db->query($sql);
68 lars 115
			$id=0;
29 lars 116
			while($row=$res->fetchRow())
117
			{
87 lars 118
				$vservers[$id]=$row;
29 lars 119
				$v=new vServer($row["v_id"], $row["hn_id"]);
87 lars 120
				$ret=$v->getStatus();
71 lars 121
				$dist=new Dist($row["v_dist"]);
122
				$dist_info=$dist->toArray();
97 lars 123
				$vservers[$id]["id"]=$row["id"];
71 lars 124
				$vservers[$id]["v_dist_name"]=$dist_info["dist_name"];
68 lars 125
				$vservers[$id]["v_status"]=$ret["status"];
126
				$vservers[$id]["code"]=$ret["code"];
127
				$vservers[$id]["started"]=$ret["started"];
128
				$id++;
29 lars 129
			}
130
			return $vservers;
13 lars 131
		}
15 lars 132
 
133
		function control($cmd="")
134
		{
68 lars 135
			$cmds=array("start","stop","restart","destroy");
15 lars 136
			if($cmd==""||!in_array($cmd, $cmds))
137
			{
138
				return false;
139
			}
140
			unset($out);
141
			if($this->v_id!=0)
142
			{
143
				$id=$this->v_id;
102 lars 144
				$hn=HN::getHN($this->hn_id);
145
				$cmd="ssh root@".$hn["name"]." vzctl $cmd $id";
15 lars 146
				exec($cmd,$out,$ret);
147
				if(!$ret)
148
				{
149
					$out_v["status"]=implode("<br>",$out);
16 lars 150
					$out_v["error"]=false;
15 lars 151
				}
152
				else
153
				{
154
					$out_v["status"]="unknown";
16 lars 155
					$out_v["error"]=true;
15 lars 156
				}
157
				$out_v["code"]=$ret;
158
			}
159
			else
160
			{
161
				$out_v=false;
162
			}
163
			return $out_v;
164
		}
44 lars 165
 
166
		function save($v_name, $v_dist, $v_ip, $v_r_pw)
167
		{
168
			global $db, $smarty;
169
			$this->name=$v_name;
170
			$this->dist_id=$v_dist;
171
			unset($out);
172
			$d=new Dist($v_dist);
173
			$dist=$d->toArray();
56 lars 174
			$ip=IP::getIP($v_ip);
44 lars 175
			$d_name=$dist["dist_name"];
176
			$this->dist=$d_name;
60 lars 177
			$d_template=$dist["template"];
44 lars 178
			$db->beginTransaction();
48 lars 179
			$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 180
			$res=$db->query($sql);
181
			if(!PEAR::isError($res))
182
			{
108 lars 183
				$v_id=$db->lasinsertid();
109 lars 184
				$sql_ip="SELECT id FROM ips WHERE ip='$v_ip'";
185
				$res_ip=$db->query($sql_ip);
186
				if(!PEAR::isError($res_ip))
44 lars 187
				{
109 lars 188
					$row_ip=$res->fetchRow();
189
					$ip_id=$row_ip["id"];
190
					$sql="INSERT INTO host_2_ip (ve_id, ip) VALUES ($v_id,$ip_id)";
72 lars 191
					$res=$db->query($sql);
192
					if(!PEAR::isError($res))
193
					{
194
						$cmd="ssh root@mainframe vzctl create $this->v_id --hostname $v_name --ostemplate $d_template --config vps.$d_name --ipadd ".$ip["ip"];
195
						exec($cmd,$out,$ret);
196
						if(!$ret)
197
						{
198
							$smarty->assign("meld","vServer erfolgreich angelegt!");
199
							$smarty->assign("error", false);
200
							$db->commit();
201
						}
202
						else
203
						{
204
							$smarty->assign("error", true);
205
							$smarty->assign("meld","Ein Fehler ist aufgetreten!");
107 lars 206
							$smarty->assign("db_meld",$cmd."<br>".implde("<br>",$out));
72 lars 207
							$db->rollback();
208
						}
209
					}
210
					else
211
					{
212
						$smarty->assign("error", true);
213
						$smarty->assign("meld","Ein Fehler ist aufgetreten!");
214
						$smarty->assign("db_meld",$res->getUserInfo());
215
						$db->rollback();
216
					}
44 lars 217
				}
218
				else
219
				{
220
					$smarty->assign("error", true);
221
					$smarty->assign("meld","Ein Fehler ist aufgetreten!");
109 lars 222
					$smarty->assign("db_meld",$res_ip->getUserInfo());
44 lars 223
					$db->rollback();
224
				}
225
			}
226
			else
227
			{
228
				$smarty->assign("error", true);
229
				$smarty->assign("meld","Ein Fehler ist aufgetreten!");
230
				$smarty->assign("db_meld",$res->getUserInfo());
231
				$db->rollback();
232
			}
233
		}
13 lars 234
	}
70 lars 235
?>