Subversion-Projekte lars-tiefland.openvz_admin

Revision

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