Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 168 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
169 lars 1
<?php
165 lars 2
    //$Id: vServer.class.php 169 2011-07-10 19:12:37Z lars $
3
    /**
4
     *     @package    openvz_admin
5
     *     @author        Lars Tiefland <ltiefland@gmail.com>
6
     **/
7
    class vServer
8
    {
9
        protected $status;
10
        protected $name;
11
        protected $hn_id;
12
        protected $dist;
13
        protected $dist_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
        }
60
 
61
        function getStatus()
62
        {
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
                    {
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
                }
114
                $out_v["status"] = "<div class=\"$class\">" . $out_v["status"] .
115
                    "</div>";
116
                $this->status = $out_v;
117
 
118
            }
119
            return $this->status;
120
        }
121
 
122
        static function getvServers( $hn_id = 1 )
123
        {
124
            global $db;
125
            $sql = "SELECT * FROM vservers WHERE hn_id=$hn_id ORDER BY v_id";
126
            $res = $db->query( $sql );
127
            $id = 0;
128
            while ( $row = $res->fetchRow() )
129
            {
130
                $vservers[$id] = $row;
131
                $v = new vServer( $row["v_id"], $row["hn_id"] );
132
                $ret = $v->getStatus();
133
                $dist = new Dist( $row["v_dist"] );
134
                $dist_info = $dist->toArray();
135
                $vservers[$id]["id"] = $row["id"];
136
                $vservers[$id]["v_dist_name"] = $dist_info["dist_name"];
137
                $vservers[$id]["v_status"] = $ret["status"];
138
                $vservers[$id]["code"] = $ret["code"];
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";
195
                        break;
196
                }
197
            }
198
            return $out_v;
199
        }
200
 
201
        function save( $v_name, $v_dist, $v_ip, $v_r_pw )
202
        {
203
            global $db, $smarty, $title, $tpl_file;
204
            $this->name = $v_name;
205
            $this->dist_id = $v_dist;
206
            unset( $out );
207
            $d = new Dist( $v_dist );
208
            $dist = $d->toArray();
209
            $ip = IP::getIP( $v_ip );
210
            $d_name = $dist["dist_name"];
211
            $this->dist = $d_name;
212
            $d_template = $dist["template"];
213
            $db->beginTransaction();
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)";
215
            $res = $db->query( $sql );
216
            if ( ! PEAR::isError( $res ) )
217
            {
218
                $v_id = $db->lastinsertid();
219
                $sql = "INSERT INTO host_2_ip (ve_id, ip) VALUES ($v_id,$v_ip)";
220
                $res = $db->query( $sql );
221
                if ( ! PEAR::isError( $res ) )
222
                {
223
                    $cmd = "ssh root@mainframe vzctl create $this->v_id --hostname $v_name --ostemplate $d_template --config vps.$d_name --ipadd " .
224
                        $ip["ip"];
225
                    exec( $cmd, $out, $ret );
226
                    if ( ! $ret )
227
                    {
228
                        $smarty->assign( "meld", "vServer erfolgreich angelegt!" );
229
                        $smarty->assign( "error", false );
230
                        $db->commit();
231
                    }
232
                    else
233
                    {
234
                        $smarty->assign( "error", true );
235
                        $smarty->assign( "meld", "Ein Fehler ist aufgetreten!" );
236
                        $smarty->assign( "db_meld", $cmd . "<br>" . implde( "<br>",
237
                            $out ) );
238
                        $db->rollback();
239
                    }
240
                }
241
                else
242
                {
243
                    $smarty->assign( "error", true );
244
                    $smarty->assign( "meld", "Ein Fehler ist aufgetreten!" );
245
                    $smarty->assign( "db_meld", $res->getUserInfo() );
246
                    $tpl_file = "meld.tpl";
247
                    $title = "Fehler";
248
                    $db->rollback();
249
                }
250
            }
251
            else
252
            {
253
                $smarty->assign( "error", true );
254
                $smarty->assign( "meld", "Ein Fehler ist aufgetreten!" );
255
                $smarty->assign( "db_meld", $res->getUserInfo() );
256
                $db->rollback();
257
            }
258
        }
259
    }
260
?>