tbrehm
2012-06-11 897af06af9522ded99b1e0f46730299e89856ffe
commit | author | age
87a4a6 1 <?php
T 2 /*
039b46 3   Copyright (c) 2007-2011, Till Brehm, projektfarm Gmbh
eed6b3 4   All rights reserved.
87a4a6 5
eed6b3 6   Redistribution and use in source and binary forms, with or without modification,
V 7   are permitted provided that the following conditions are met:
87a4a6 8
eed6b3 9  * Redistributions of source code must retain the above copyright notice,
V 10   this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice,
12   this list of conditions and the following disclaimer in the documentation
13   and/or other materials provided with the distribution.
14  * Neither the name of ISPConfig nor the names of its contributors
15   may be used to endorse or promote products derived from this software without
16   specific prior written permission.
87a4a6 17
eed6b3 18   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
V 19   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21   IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25   OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27   EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
87a4a6 29
8e9a5f 30 define('SCRIPT_PATH', dirname($_SERVER["SCRIPT_FILENAME"]));
N 31 require(SCRIPT_PATH."/lib/config.inc.php");
32 require(SCRIPT_PATH."/lib/app.inc.php");
87a4a6 33
T 34 set_time_limit(0);
eed6b3 35 ini_set('error_reporting', E_ALL & ~E_NOTICE);
87a4a6 36
T 37 // make sure server_id is always an int
be76b0 38 $conf['server_id'] = intval($conf['server_id']);
87a4a6 39
32b40d 40 /*
eed6b3 41  * Try to Load the server configuration from the master-db
V 42  */
43 if ($app->dbmaster->connect()) {
44     $server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = " . $conf['server_id']);
039b46 45
eed6b3 46     $conf['last_datalog_id'] = (int) $server_db_record['updated'];
V 47     $conf['mirror_server_id'] = (int) $server_db_record['mirror_server_id'];
039b46 48
db0a6f 49     // Load the ini_parser
T 50     $app->uses('ini_parser');
039b46 51
db0a6f 52     // Get server configuration
2a4cc6 53     $conf['serverconfig'] = $app->ini_parser->parse_ini_string(stripslashes($server_db_record['config']));
039b46 54
db0a6f 55     // Set the loglevel
2a4cc6 56     $conf['log_priority'] = intval($conf['serverconfig']['server']['loglevel']);
eed6b3 57
039b46 58     // we do not need this variable anymore
9adcf5 59     unset($server_db_record);
039b46 60     
V 61     /*
62      * Save the rescue-config, maybe we need it (because the database is down)
63      */
64     $tmp['serverconfig']['server']['loglevel'] = $conf['log_priority'];
65     $tmp['serverconfig']['rescue'] = $conf['serverconfig']['rescue'];
66     file_put_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", serialize($tmp));
67     unset($tmp);
68
69     // protect the file
70     chmod(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", 0600);
71     
eed6b3 72 } else {
V 73     /*
74      * The master-db is not available.
75      * Problem: because we need to start the rescue-module (to rescue the DB if this IS the
76      * server, the master-db is running at) we have to initialize some config...
77      */
039b46 78     
V 79     /*
80      * If there is a temp-file with the data we could get from the database, then we use it
81      */
82     $tmp = array();
83     if (file_exists(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt")){
84         $tmp = unserialize(file_get_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt"));
85     }
86     
87     // maxint at 32 and 64 bit systems
88     $conf['last_datalog_id'] = intval('9223372036854775807'); 
89
90     // no mirror
91     $conf['mirror_server_id'] = 0; 
92
93     // Set the loglevel 
94     $conf['log_priority'] = (isset($tmp['serverconfig']['server']['loglevel']))? $tmp['serverconfig']['server']['loglevel'] : LOGLEVEL_ERROR;
eed6b3 95     /*
V 96      * Set the configuration to rescue the database
97      */
039b46 98     if (isset($tmp['serverconfig']['rescue'])){
V 99         $conf['serverconfig']['rescue'] = $tmp['serverconfig']['rescue'];
100     }
101     else{
102         $conf['serverconfig']['rescue']['try_rescue'] = 'n';
103     }
104     // we do not need this variable anymore
105     unset($tmp);
db0a6f 106 }
T 107
3d4630 108
9b82d2 109 // Check whether another instance of this script is already running
eed6b3 110 if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock')) {
V 111     clearstatcache();
112     for ($i = 0; $i < 120; $i++) { // Wait max. 1200 sec, then retry
113         if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock')) {
114             exec("ps aux | grep '/usr/local/ispconfig/server/[s]erver.php' | wc -l", $check);
115             if (intval($check[0]) > 1) { // 1 because this is 2nd instance!
116                 $app->log('There is already an instance of server.php running. Exiting.', LOGLEVEL_DEBUG);
117                 exit;
118             }
119             $app->log('There is already a lockfile set. Waiting another 10 seconds...', LOGLEVEL_DEBUG);
120             sleep(10);
121             clearstatcache();
122         }
123     }
87a4a6 124 }
T 125
126 // Set Lockfile
eed6b3 127 @touch($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
V 128 $app->log('Set Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 129
eed6b3 130 /** Do we need to start the core-modules */
V 131 $needStartCore = true;
87a4a6 132
eed6b3 133 /*
V 134  * Next we try to process the datalog
135  */
136 if ($app->db->connect() && $app->dbmaster->connect()) {
9eff6c 137
d2d3b9 138     // Check if there is anything to update
eed6b3 139     if ($conf['mirror_server_id'] > 0) {
V 140         $tmp_rec = $app->dbmaster->queryOneRecord("SELECT count(server_id) as number from sys_datalog WHERE datalog_id > " . $conf['last_datalog_id'] . " AND (server_id = " . $conf['server_id'] . " OR server_id = " . $conf['mirror_server_id'] . " OR server_id = 0)");
9adcf5 141     } else {
eed6b3 142         $tmp_rec = $app->dbmaster->queryOneRecord("SELECT count(server_id) as number from sys_datalog WHERE datalog_id > " . $conf['last_datalog_id'] . " AND (server_id = " . $conf['server_id'] . " OR server_id = 0)");
9adcf5 143     }
eed6b3 144
2a4cc6 145     $tmp_num_records = $tmp_rec['number'];
d2d3b9 146     unset($tmp_rec);
T 147
eed6b3 148     if ($tmp_num_records > 0) {
d2d3b9 149         /*
eed6b3 150           There is something to do, triggert by the database -> do it!
V 151          */
d2d3b9 152         // Write the Log
T 153         $app->log("Found $tmp_num_records changes, starting update process.", LOGLEVEL_DEBUG);
154         // Load required base-classes
db0a6f 155         $app->uses('modules,plugins,file,services');
2a4cc6 156         // Load the modules that are in the mods-enabled folder
d2d3b9 157         $app->modules->loadModules('all');
T 158         // Load the plugins that are in the plugins-enabled folder
159         $app->plugins->loadPlugins('all');
2a4cc6 160         // Go through the sys_datalog table and call the processing functions
J 161         // from the modules that are hooked on to the table actions
d2d3b9 162         $app->modules->processDatalog();
2a4cc6 163         // Restart services that need to after configuration
d2d3b9 164         $app->services->processDelayedActions();
eed6b3 165         // All modules are already loaded and processed, so there is NO NEED to load the core once again...
V 166         $needStartCore = false;
d2d3b9 167     }
32b40d 168 } else {
eed6b3 169     if (!$app->db->connect()) {
V 170         $app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN);
0e381b 171     } else {
eed6b3 172         $app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN);
0e381b 173     }
782b02 174 }
3d4630 175
eed6b3 176 /*
V 177  * Under normal circumstances the system was loaded and all updates are done.
178  * but if we do not have to update anything or if the database is not accessible, then we
179  * have to start the core-system (if the database is accessible, we need the core because of the
180  * monitoring. If the databse is NOT accessible, we need the core because of rescue the db...
181  */
182 if ($needStartCore) {
183     // Write the log
184     $app->log('No Updated records found, starting only the core.', LOGLEVEL_DEBUG);
185     // Load required base-classes
186     $app->uses('modules,plugins,file,services');
187     // Load the modules that are im the mods-core folder
188     $app->modules->loadModules('core');
189     // Load the plugins that are in the plugins-core folder
190     $app->plugins->loadPlugins('core');
191 }
192
193
87a4a6 194 // Remove lock
eed6b3 195 @unlink($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
V 196 $app->log('Remove Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 197
32b40d 198
ba747c 199 die("finished.\n");
c30aaf 200 ?>