Subversion-Projekte lars-tiefland.openvz_admin

Revision

Revision 157 | Revision 159 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
3 lars 1
<?php
157 lars 2
    //$Id: load.php 158 2009-11-07 21:45:38Z lars $
3
    /**
4
     * 	@package	openvz_admin
5
     * 	@author		Lars Tiefland <ltiefland@gmail.com>
6
     **/
7
    require ( "include/common.php" );
8
    $hns = HN::getHNs();
9
    unset( $hns[-1] );
10
    foreach ( $hns as $hn_id => $hn_name )
11
    {
12
        $configs = load_configs( $hn_id, $hn_name );
13
        foreach ( $configs as $conf_id => $config )
14
        {
15
            $cfg = read_config( $hn_id, $hn_name, $config );
16
            update_db( $hn_id, $cfg );
17
        }
18
    }
122 lars 19
 
157 lars 20
    function load_configs( $hn_id, $hn_name )
21
    {
22
        unset( $out );
23
        $cmd = "ping $hn_name -w1";
24
        exec( $cmd, $out, $ret );
25
        if ( !$ret )
26
        {
27
            echo "Hole Konfigurationen von $hn_name\n";
28
            unset( $out );
29
            $cmd = "ssh root@$hn_name ls /etc/vz/conf/*.conf";
30
            exec( $cmd, $out, $ret );
31
            return $out;
32
        }
33
    }
122 lars 34
 
157 lars 35
    function read_config( $hn_id, $hn_name, $config )
36
    {
37
        $config = basename( $config );
38
        $cmd = "scp root@$hn_name:/etc/vz/conf/$config cfgs/$hn_id";
39
        exec( $cmd, $out, $ret );
40
        $conf = file( "cfgs/$hn_id/$config" );
41
        $conf[] = "v_id=\"" . substr( $config, 0, strpos($config, '.') ) . "\"";
42
        foreach ( $conf as $value )
43
        {
44
            if ( $value )
45
            {
46
                $value = rtrim( $value );
47
                if ( $value[0] == " " || $value[0] == "#" )
48
                {
49
                    continue;
50
                }
51
                $cfg_v = explode( "=", $value );
52
                $cfg_val[$cfg_v[0]] = trim( $cfg_v[1], '"' );
53
            }
54
            else
55
            {
56
                continue;
57
            }
58
        }
59
        $cfgs[] = $cfg_val;
60
        return $cfgs;
61
    }
122 lars 62
 
157 lars 63
    function update_db( $hn_id, $cfgs )
64
    {
65
        global $db;
125 lars 66
 
157 lars 67
        foreach ( $cfgs as $config )
68
        {
69
            if ( isset($config["DISTRIBUTION"]) )
70
            {
71
                $dist_name = $config["DISTRIBUTION"];
72
            } elseif ( in_array($hn_id, array(1, 3)) )
73
            {
74
                $dist_name = "gentoo";
75
            }
76
            else
77
            {
78
                $dist_name = "centos";
79
            }
80
            $sql = "
81
				SELECT
82
					*
83
				FROM
84
					distributions
85
				WHERE
86
					dist_name='$dist_name'
87
			";
88
            $res = $db->query( $sql );
89
            if ( !PEAR::isError($res) )
90
            {
91
                if ( $res->numRows() )
92
                {
93
                    $row = $res->fetchRow();
94
                    $dist_id = $row["dist_id"];
95
                    $sql_v = "
96
						SELECT
97
							v_dist,
98
							dist_name
99
						FROM
100
							distributions,
101
							vservers
102
						WHERE
103
							v_id=" . $config["v_id"] . "
104
						AND
105
							dist_id=v_dist
106
					";
107
                    $res_v = $db->query( $sql_v );
108
                    if ( PEAR::isError($res_v) )
109
                    {
110
                        die( $res_v->getUserInfo() );
111
                    }
112
                    $row_v = $res_v->fetchRow();
113
                    $dist_name = $row_v["dist_name"];
114
                    if ( $dist_name != $config["DISTRIBUTION"] )
115
                    {
116
                        $sql = "
117
							UPDATE
118
								vservers
119
							SET
120
								v_dist=$dist_id
121
							WHERE
122
								v_id=" . $config["v_id"] . "
123
						";
124
                        $res = $db->query( $sql );
125
                        if ( PEAR::isError($res) )
126
                        {
127
                            die( $res->getUserInfo() );
128
                        }
129
                    }
130
                }
131
                else
132
                {
133
                    $sql = "
134
						INSERT INTO
135
							distributions
136
						(
137
							dist_name,
138
							dist_template
139
						)
140
						VALUES
141
						(
142
							'" . $config["DISTRIBUTION"] . "',
143
							'" . $config["OSTEMPLATE"] . "'
144
						)
145
					";
146
                    $res = $db->query( $sql );
147
                    $dist_id = $db->lastinsertID();
148
                }
149
            }
150
            else
151
            {
152
                die( $res->getUserInfo() );
153
            }
154
            $sql = "
155
				SELECT
156
					*
157
				FROM
158
					vservers
159
				WHERE
160
					v_id=" . $config["v_id"] . "
161
				AND
162
					hn_id=$hn_id
163
			";
164
            $res = $db->query( $sql );
165
            if ( !PEAR::isError($res) )
166
            {
167
                if ( !$res->numRows() )
168
                {
169
                    $sql = "
170
						INSERT INTO
171
							vservers
172
						(
173
							v_id,
174
							v_name,
175
							v_dist,
176
							hn_id
177
						)
178
						VALUES
179
						(
180
							" . $config["v_id"] . ",
181
							'" . $config["HOSTNAME"] . "',
182
							$dist_id,
183
							$hn_id
184
						)
185
					";
186
                    $res = $db->query( $sql );
187
                    if ( PEAR::isError($res) )
188
                    {
158 lars 189
                        die( $res->getUserInfo() );
157 lars 190
                    }
191
                } elseif ( $config["HOSTNAME"] )
192
                {
193
                    $sql = "
194
						UPDATE
195
							vservers
196
						SET
197
							v_name='" . $config["HOSTNAME"] . "'
198
						WHERE
199
							v_id=" . $config["v_id"] . "
200
						AND
201
							hn_id=$hn_id
202
					";
203
                    $res = $db->query( $sql );
204
                    if ( PEAR::isError($res) )
205
                    {
158 lars 206
                        die( $res->getUserInfo() );
157 lars 207
                    }
208
                }
209
            }
210
            else
211
            {
212
                die( $res->getUserInfo() );
213
            }
214
            foreach ( $config as $field => $value )
215
            {
216
                $sql = "
217
					SELECT
218
						*
219
					FROM
220
						vserver_config
221
					WHERE
222
						vc_name='$field'
223
					AND
224
						v_id=" . $config["v_id"] . "
225
					AND
226
						hn_id=$hn_id
227
			s	";
228
                $res = $db->query( $sql );
229
                if ( !PEAR::isError($res) )
230
                {
231
                    if ( $res->numRows() )
232
                    {
233
                        $sql = "
234
							UPDATE
235
								vserver_config
236
							SET
237
								vc_value='$value'
238
							WHERE
239
								vc_name='$field'
240
							AND
241
								v_id=" . $config["v_id"] . "
242
							AND
243
								hn_id=$hn_id
244
						";
245
                    }
246
                    else
247
                    {
248
                        $sql = "
249
							INSERT INTO
250
								vserver_config
251
							VALUES
252
							(
253
								" . $config["v_id"] . ",
254
								$hn_id,
255
								'$field',
256
								'$value'
257
							)
258
						";
259
                    }
260
                    $res = $db->query( $sql );
261
                    if ( PEAR::isError($res) )
262
                    {
158 lars 263
                        die( $res->getUserInfo() );
157 lars 264
                    }
265
                }
266
                else
267
                {
158 lars 268
                    die( $res->getUserInfo() );
157 lars 269
                }
270
            }
271
        }
272
    }
136 lars 273
?>