Subversion-Projekte lars-tiefland.openvz_admin

Revision

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