Marius Cramer
2014-09-26 132081c0e69f0313ccb0d23637499e940d29f470
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"]));
7fe908 31 require SCRIPT_PATH."/lib/config.inc.php";
MC 32 require SCRIPT_PATH."/lib/app.inc.php";
87a4a6 33
132081 34 $app->setCaller('server');
MC 35
87a4a6 36 set_time_limit(0);
eed6b3 37 ini_set('error_reporting', E_ALL & ~E_NOTICE);
87a4a6 38
T 39 // make sure server_id is always an int
be76b0 40 $conf['server_id'] = intval($conf['server_id']);
87a4a6 41
32b40d 42 /*
eed6b3 43  * Try to Load the server configuration from the master-db
V 44  */
1d8f7f 45 if ($app->dbmaster->connect_error == NULL) {
eed6b3 46     $server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = " . $conf['server_id']);
7fe908 47
5be510 48     if(!is_array($server_db_record)) die('Unable to load the server configuration from database.');
7fe908 49
MC 50     //* Get the number of the last processed datalog_id, if the id of the local server
72695f 51     //* is > then the one of the remote system, then use the local ID as we might not have
T 52     //* reached the remote server during the last run then.
53     $local_server_db_record = $app->db->queryOneRecord("SELECT * FROM server WHERE server_id = " . $conf['server_id']);
7fe908 54     $conf['last_datalog_id'] = (int) max($server_db_record['updated'], $local_server_db_record['updated']);
72695f 55     unset($local_server_db_record);
7fe908 56
eed6b3 57     $conf['mirror_server_id'] = (int) $server_db_record['mirror_server_id'];
039b46 58
db0a6f 59     // Load the ini_parser
T 60     $app->uses('ini_parser');
039b46 61
db0a6f 62     // Get server configuration
2a4cc6 63     $conf['serverconfig'] = $app->ini_parser->parse_ini_string(stripslashes($server_db_record['config']));
039b46 64
db0a6f 65     // Set the loglevel
2a4cc6 66     $conf['log_priority'] = intval($conf['serverconfig']['server']['loglevel']);
7fe908 67
615a0a 68     // Set level from which admin should be notified by email
T 69     if(!isset($conf['serverconfig']['server']['admin_notify_events']) || $conf['serverconfig']['server']['admin_notify_events'] == '') $conf['serverconfig']['server']['admin_notify_events'] = 3;
70     $conf['admin_notify_priority'] = intval($conf['serverconfig']['server']['admin_notify_events']);
eed6b3 71
039b46 72     // we do not need this variable anymore
9adcf5 73     unset($server_db_record);
7fe908 74
615a0a 75     // retrieve admin email address for notifications
7b47c0 76     //$sys_ini = $app->dbmaster->queryOneRecord("SELECT * FROM sys_ini WHERE sysini_id = 1");
T 77     $sys_ini = $app->db->queryOneRecord("SELECT * FROM sys_ini WHERE sysini_id = 1");
615a0a 78     $conf['sys_ini'] = $app->ini_parser->parse_ini_string(stripslashes($sys_ini['config']));
T 79     $conf['admin_mail'] = $conf['sys_ini']['mail']['admin_mail'];
80     unset($sys_ini);
7fe908 81
039b46 82     /*
V 83      * Save the rescue-config, maybe we need it (because the database is down)
84      */
85     $tmp['serverconfig']['server']['loglevel'] = $conf['log_priority'];
86     $tmp['serverconfig']['rescue'] = $conf['serverconfig']['rescue'];
87     file_put_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", serialize($tmp));
88     unset($tmp);
89
90     // protect the file
91     chmod(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt", 0600);
7fe908 92
eed6b3 93 } else {
V 94     /*
95      * The master-db is not available.
96      * Problem: because we need to start the rescue-module (to rescue the DB if this IS the
97      * server, the master-db is running at) we have to initialize some config...
98      */
7fe908 99
039b46 100     /*
V 101      * If there is a temp-file with the data we could get from the database, then we use it
102      */
103     $tmp = array();
104     if (file_exists(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt")){
105         $tmp = unserialize(file_get_contents(dirname(__FILE__) . "/temp/rescue_module_serverconfig.ser.txt"));
106     }
7fe908 107
039b46 108     // maxint at 32 and 64 bit systems
7fe908 109     $conf['last_datalog_id'] = intval('9223372036854775807');
039b46 110
V 111     // no mirror
7fe908 112     $conf['mirror_server_id'] = 0;
039b46 113
7fe908 114     // Set the loglevel
039b46 115     $conf['log_priority'] = (isset($tmp['serverconfig']['server']['loglevel']))? $tmp['serverconfig']['server']['loglevel'] : LOGLEVEL_ERROR;
eed6b3 116     /*
V 117      * Set the configuration to rescue the database
118      */
039b46 119     if (isset($tmp['serverconfig']['rescue'])){
V 120         $conf['serverconfig']['rescue'] = $tmp['serverconfig']['rescue'];
121     }
122     else{
123         $conf['serverconfig']['rescue']['try_rescue'] = 'n';
124     }
125     // we do not need this variable anymore
126     unset($tmp);
db0a6f 127 }
T 128
3d4630 129
9b82d2 130 // Check whether another instance of this script is already running
eed6b3 131 if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock')) {
V 132     clearstatcache();
133     for ($i = 0; $i < 120; $i++) { // Wait max. 1200 sec, then retry
134         if (is_file($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock')) {
135             exec("ps aux | grep '/usr/local/ispconfig/server/[s]erver.php' | wc -l", $check);
136             if (intval($check[0]) > 1) { // 1 because this is 2nd instance!
137                 $app->log('There is already an instance of server.php running. Exiting.', LOGLEVEL_DEBUG);
138                 exit;
139             }
140             $app->log('There is already a lockfile set. Waiting another 10 seconds...', LOGLEVEL_DEBUG);
141             sleep(10);
142             clearstatcache();
143         }
144     }
87a4a6 145 }
T 146
147 // Set Lockfile
eed6b3 148 @touch($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
V 149 $app->log('Set Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 150
eed6b3 151 /** Do we need to start the core-modules */
7fe908 152
MC 153
eed6b3 154 $needStartCore = true;
87a4a6 155
eed6b3 156 /*
V 157  * Next we try to process the datalog
158  */
1d8f7f 159 if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) {
9eff6c 160
d2d3b9 161     // Check if there is anything to update
eed6b3 162     if ($conf['mirror_server_id'] > 0) {
V 163         $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 164     } else {
eed6b3 165         $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 166     }
eed6b3 167
2a4cc6 168     $tmp_num_records = $tmp_rec['number'];
d2d3b9 169     unset($tmp_rec);
7fe908 170
5a43e7 171     //** Load required base-classes
ff6a68 172     $app->uses('modules,plugins,file,services,system');
5a43e7 173     //** Load the modules that are in the mods-enabled folder
T 174     $app->modules->loadModules('all');
175     //** Load the plugins that are in the plugins-enabled folder
176     $app->plugins->loadPlugins('all');
ab6e69 177     
MC 178     $app->plugins->raiseAction('server_plugins_loaded', '');
179     
eed6b3 180     if ($tmp_num_records > 0) {
d2d3b9 181         $app->log("Found $tmp_num_records changes, starting update process.", LOGLEVEL_DEBUG);
5a43e7 182         //** Go through the sys_datalog table and call the processing functions
T 183         //** from the modules that are hooked on to the table actions
d2d3b9 184         $app->modules->processDatalog();
T 185     }
5a43e7 186     //** Process actions from sys_remoteaction table
T 187     $app->modules->processActions();
188     //** Restart services that need to after configuration
189     $app->services->processDelayedActions();
190     //** All modules are already loaded and processed, so there is NO NEED to load the core once again...
191     $needStartCore = false;
7fe908 192
32b40d 193 } else {
1d8f7f 194     if ($app->db->connect->connect_error == NULL) {
eed6b3 195         $app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN);
0e381b 196     } else {
eed6b3 197         $app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN);
0e381b 198     }
782b02 199 }
3d4630 200
eed6b3 201 /*
V 202  * Under normal circumstances the system was loaded and all updates are done.
203  * but if we do not have to update anything or if the database is not accessible, then we
204  * have to start the core-system (if the database is accessible, we need the core because of the
205  * monitoring. If the databse is NOT accessible, we need the core because of rescue the db...
206  */
207 if ($needStartCore) {
208     // Write the log
209     $app->log('No Updated records found, starting only the core.', LOGLEVEL_DEBUG);
210     // Load required base-classes
211     $app->uses('modules,plugins,file,services');
212     // Load the modules that are im the mods-core folder
213     $app->modules->loadModules('core');
cc6568 214     // Load the plugins that are in the f folder
H 215     //$app->plugins->loadPlugins('core');
eed6b3 216 }
V 217
218
87a4a6 219 // Remove lock
eed6b3 220 @unlink($conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock');
V 221 $app->log('Remove Lock: ' . $conf['temppath'] . $conf['fs_div'] . '.ispconfig_lock', LOGLEVEL_DEBUG);
3d4630 222
32b40d 223
ba747c 224 die("finished.\n");
c30aaf 225 ?>