Subversion-Projekte lars-tiefland.openvz_admin

Revision

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

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