Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 61 | Revision 70 | 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 68 2007-10-25 14:11:23Z 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();
68 lars 106
				$vservers[$id]["v_status"]=$ret["status"];
107
				$vservers[$id]["code"]=$ret["code"];
108
				$vservers[$id]["started"]=$ret["started"];
109
				$id++;
29 lars 110
			}
111
			return $vservers;
13 lars 112
		}
15 lars 113
 
114
		function control($cmd="")
115
		{
68 lars 116
			$cmds=array("start","stop","restart","destroy");
15 lars 117
			if($cmd==""||!in_array($cmd, $cmds))
118
			{
119
				return false;
120
			}
121
			unset($out);
122
			if($this->v_id!=0)
123
			{
124
				$id=$this->v_id;
125
				$cmd="ssh root@mainframe vzctl $cmd $id";
126
				exec($cmd,$out,$ret);
127
				if(!$ret)
128
				{
129
					$out_v["status"]=implode("<br>",$out);
16 lars 130
					$out_v["error"]=false;
15 lars 131
				}
132
				else
133
				{
134
					$out_v["status"]="unknown";
16 lars 135
					$out_v["error"]=true;
15 lars 136
				}
137
				$out_v["code"]=$ret;
138
			}
139
			else
140
			{
141
				$out_v=false;
142
			}
143
			return $out_v;
144
		}
44 lars 145
 
146
		function save($v_name, $v_dist, $v_ip, $v_r_pw)
147
		{
148
			global $db, $smarty;
149
			$this->name=$v_name;
150
			$this->dist_id=$v_dist;
151
			unset($out);
152
			$d=new Dist($v_dist);
153
			$dist=$d->toArray();
56 lars 154
			$ip=IP::getIP($v_ip);
44 lars 155
			$d_name=$dist["dist_name"];
156
			$this->dist=$d_name;
60 lars 157
			$d_template=$dist["template"];
44 lars 158
			$db->beginTransaction();
48 lars 159
			$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 160
			$res=$db->query($sql);
161
			if(!PEAR::isError($res))
162
			{
56 lars 163
				$cmd="ssh root@mainframe vzctl create $this->v_id --hostname $v_name --ostemplate $d_template --config vps.$d_name --ipadd ".$ip["ip"];
44 lars 164
				exec($cmd,$out,$ret);
165
				if(!$ret)
166
				{
167
					$smarty->assign("meld","vServer erfolgreich angelegt!");
168
					$smarty->assign("error", false);
169
					$db->commit();
170
				}
171
				else
172
				{
173
					$smarty->assign("error", true);
174
					$smarty->assign("meld","Ein Fehler ist aufgetreten!");
51 lars 175
					$smarty->assign("db_meld",$cmd);
44 lars 176
					$db->rollback();
177
				}
178
			}
179
			else
180
			{
181
				$smarty->assign("error", true);
182
				$smarty->assign("meld","Ein Fehler ist aufgetreten!");
183
				$smarty->assign("db_meld",$res->getUserInfo());
184
				$db->rollback();
185
			}
186
		}
13 lars 187
	}
188
?>