Subversion-Projekte lars-tiefland.openvz_admin

Revision

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