Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 134 | Revision 143 | Zur aktuellen Revision | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

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