Skip to content

Commit f2124f2

Browse files
author
Marius Burkard
committed
- added some logging
1 parent 02e3ef6 commit f2124f2

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

server/lib/classes/addon_installer.inc.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ private function extractPackage($package_file) {
1313
$ret = null;
1414
$retval = 0;
1515

16+
$app->log('Extracting addon package ' . $package_file, 0, false);
17+
1618
$cmd = 'which unzip';
1719
$tmp = explode("\n", exec($cmd, $ret, $retval));
1820
if($retval != 0) {
21+
$app->log('The unzip command was not found on the server.', 2, false);
1922
throw new AddonInstallerException('unzip tool not found.');
2023
}
2124
$unzip = reset($tmp);
2225
unset($tmp);
2326
if(!$unzip) {
27+
$app->log('Unzip tool was not found.', 2, false);
2428
throw new AddonInstallerException('unzip tool not found.');
2529
}
2630

2731
$temp_dir = $app->system->tempdir(sys_get_temp_dir(), 'addon_', 0700);
2832
if(!$temp_dir) {
33+
$app->log('Could not create the temp dir.', 2, false);
2934
throw new AddonInstallerException('Could not create temp dir.');
3035
}
3136

@@ -34,9 +39,12 @@ private function extractPackage($package_file) {
3439
$cmd = $unzip . ' -d ' . escapeshellarg($temp_dir) . ' ' . escapeshellarg($package_file);
3540
exec($cmd, $ret, $retval);
3641
if($retval != 0) {
42+
$app->log('Package extraction failed.', 2, false);
3743
throw new AddonInstallerException('Package extraction failed.');
3844
}
3945

46+
$app->log('Extracted to ' . $temp_dir, 0, false);
47+
4048
return $temp_dir;
4149
}
4250

@@ -46,36 +54,48 @@ private function extractPackage($package_file) {
4654
* @throws AddonInstallerValidationException
4755
*/
4856
private function validatePackage($path) {
57+
$app->log('Validating extracted addon at ' . $path, 0, false);
58+
4959
if(!is_dir($path)) {
60+
$app->log('Invalid path.', 2, false);
5061
throw new AddonInstallerValidationException('Invalid path.');
5162
}
5263

5364
$ini_file = $path . '/addon.ini';
5465
if(!is_file($ini_file)) {
66+
$app->log('Addon ini file missing.', 2, false);
5567
throw new AddonInstallerValidationException('Addon ini file missing.');
5668
}
5769

70+
$app->log('Parsing ini ' . $ini_file, 0, false);
5871
$ini = parse_ini_file($ini_file, true);
5972
if(!$ini || !isset($ini['addon'])) {
73+
$app->log('Ini file could not be read.', 2, false);
6074
throw new AddonInstallerValidationException('Ini file is missing addon section.');
6175
}
6276

6377
$addon = $ini['addon'];
6478
if(!isset($addon['ident']) || !isset($addon['name']) || !isset($addon['version'])) {
79+
$app->log('Addon data in ini file missing or invalid.', 2, false);
6580
throw new AddonInstallerValidationException('Ini file is missing addon ident/name/version.');
6681
}
6782

6883
$class_file = $path . '/' . $addon['ident'] . '.addon.php';
6984
if(!is_file($class_file)) {
85+
$app->log('Base class file in addon not found', 2, false);
7086
throw new AddonInstallerValidationException('Package is missing main addon class.');
7187
}
7288

7389
if(isset($ini['ispconfig']['version.min']) && $ini['ispconfig']['version.min'] && version_compare($ini['ispconfig']['version.min'], ISPC_APP_VERSION, '>')) {
90+
$app->log('ISPConfig version too low for this addon.', 2, false);
7491
throw new AddonInstallerValidationException('Addon requires at least ISPConfig version ' . $ini['ispconfig']['version.min'] . '.');
7592
} elseif(isset($ini['ispconfig']['version.max']) && $ini['ispconfig']['version.max'] && version_compare($ini['ispconfig']['version.min'], ISPC_APP_VERSION, '<')) {
93+
$app->log('ISPConfig version too high for this addon.', 2, false);
7694
throw new AddonInstallerValidationException('Addon allows at max ISPConfig version ' . $ini['ispconfig']['version.max'] . '.');
7795
}
7896

97+
$app->log('Loaded addon installer ' . $class_file, 0, false);
98+
7999
$addon['class_file'] = $class_file;
80100
$addon['class_name'] = substr(basename($class_file), 0, -10) . '_addon_installer';
81101

@@ -92,21 +112,30 @@ private function getInstalledAddonVersion($ident) {
92112
// check for previous version
93113
if(is_dir($addon_path . '/' . $ident) && is_file($addon_path . '/' . $ident . '/addon.ini')) {
94114
$addon = parse_ini_file($addon_path . '/' . $ident . '/addon.ini', true);
115+
if($addon && isset($addon['addon'])) {
116+
$addon = $addon['addon']; // ini section
117+
} else {
118+
$addon = false;
119+
}
95120
if(!$addon || !isset($addon['version']) || !isset($addon['ident']) || $addon['ident'] != $ident) {
96-
throw new AddonInstallerException('Installed app found but it is invalid.');
121+
$app->log('Could not get version of installed addon.', 2, false);
122+
throw new AddonInstallerException('Installed app ' . $ident . ' found but it is invalid.');
97123
}
98124

99125
$file_version = $addon['version'];
126+
$app->log('Installed version of addon ' . $ident . ' is ' . $file_version, 0, false);
100127
}
101128

102129
$check = $app->db->queryOneRecord('SELECT `addon_version` FROM `addons` WHERE `addon_ident` = ?', $ident);
103130
if($check && $check['addon_version']) {
104131
$db_version = $check['addon_version'];
132+
$app->log('Installed version of addon ' . $ident . ' (in db) is ' . $db_version . '.', 0, false);
105133
}
106134

107135
if(!$file_version && !$db_version) {
108136
return false;
109137
} elseif($file_version != $db_version) {
138+
$app->log('Version mismatch between ini file and database (' . $file_version . ' != ' . $db_version . ').', 0, false);
110139
throw new AddonInstallerException('Addon version mismatch in database (' . $db_version . ') and file system (' . $file_version . ').');
111140
}
112141

@@ -126,40 +155,52 @@ public function installAddon($package_file, $force = false) {
126155
$app->load('ispconfig_addon_installer_base');
127156

128157
if(!is_file($package_file)) {
158+
$app->log('Package file not found: ' . $package_file, 2, false);
129159
throw new AddonInstallerException('Package file not found.');
130160
} elseif(substr($package_file, -4) !== '.pkg') {
161+
$app->log('Invalid package file: ' . $package_file, 2, false);
131162
throw new AddonInstallerException('Invalid package file.');
132163
}
133164

134165
$tmp_dir = $this->extractPackage($package_file);
135166
if(!$tmp_dir) {
136167
// extracting failed
168+
$app->log('Package extraction failed.', 2, false);
137169
throw new AddonInstallerException('Package extraction failed.');
138170
}
139171

140172
$addon = $this->validatePackage($tmp_dir);
141173
if(!$addon) {
142174
throw new AddonInstallerException('Package validation failed.');
143175
}
176+
$app->log('Package validated.', 0, false);
144177

145178
$is_update = false;
146179
$previous = $this->getInstalledAddonVersion($addon['ident']);
147180
if($previous !== false) {
148181
// this is an update
149182
if(version_compare($previous, $addon['version'], '>') && $force !== true) {
183+
$app->log('Installed version is newer than the one to install and --force not used.', 2, false);
150184
throw new AddonInstallerException('Installed version is newer than the one to install.');
151185
} elseif(version_compare($previous, $addon['version'], '=') && $force !== true) {
186+
$app->log('Installed version is the same as the one to install and --force not used.', 2, false);
152187
throw new AddonInstallerException('Installed version is the same as the one to install.');
153188
}
154189
$is_update = true;
155190
}
191+
192+
$app->log('Including package class file ' . $addon['class_file'], 0, false);
193+
156194
include $addon['class_file'];
157-
if(!class_exists($addon['class_name'])) {
195+
$class_name = $addon['class_name'];
196+
if(!class_exists($class_name)) {
197+
$app->log('Class name ' . $class_name . ' not found in class file ' . $addon['class_file'], 2, false);
158198
throw new AddonInstallerException('Could not find main class in addon file.');
159199
}
160200

161-
$class_name = $addon['class_name'];
162201
/* @var $inst ispconfig_addon_installer_base */
202+
$app->log('Instanciating installer class ' . $class_name, 0, false);
203+
163204
$inst = new $class_name();
164205
$inst->setAddonName($addon['name']);
165206
$inst->setAddonIdent($addon['ident']);
@@ -178,6 +219,7 @@ public function installAddon($package_file, $force = false) {
178219

179220
exec('rm -rf ' . escapeshellarg($tmp_dir));
180221

222+
$app->log('Installation completed.', 0, false);
181223
return true;
182224
}
183225

server/lib/classes/ispconfig_addon_installer_base.inc.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function setAddonTempDir($path) {
3636
protected function copyInterfaceFiles() {
3737
global $conf;
3838

39+
$app->log('Copying interface files.', 0, false);
3940
$install_dir = realpath($conf['rootpath'] . '/..');
4041

4142
if(is_dir($this->temp_dir . '/interface')) {
@@ -44,6 +45,7 @@ protected function copyInterfaceFiles() {
4445
$command = 'cp -rf ' . escapeshellarg($this->temp_dir . '/interface') . ' ' . escapeshellarg($install_dir . '/');
4546
exec($command, $ret, $retval);
4647
if($retval != 0) {
48+
$app->log('Copying interface files failed.', 2, false);
4749
throw new AddonInstallerException('Command ' . $command . ' failed with code ' . $retval);
4850
}
4951

@@ -56,6 +58,8 @@ protected function copyInterfaceFiles() {
5658
protected function copyServerFiles() {
5759
global $conf;
5860

61+
$app->log('Copying server files.', 0, false);
62+
5963
$install_dir = realpath($conf['rootpath'] . '/..');
6064

6165
if(is_dir($this->temp_dir . '/server')) {
@@ -64,6 +68,7 @@ protected function copyServerFiles() {
6468
$command = 'cp -rf ' . escapeshellarg($this->temp_dir . '/server'). ' ' . escapeshellarg($install_dir . '/');
6569
exec($command, $ret, $retval);
6670
if($retval != 0) {
71+
$app->log('Copying interface files failed.', 2, false);
6772
throw new AddonInstallerException('Command ' . $command . ' failed with code ' . $retval);
6873
}
6974
return true;
@@ -75,9 +80,12 @@ protected function copyServerFiles() {
7580
protected function copyAddonFiles() {
7681
global $app, $conf;
7782

83+
$app->log('Copying addon files.', 0, false);
84+
7885
$install_dir = realpath($conf['rootpath'] . '/..') . '/addons/' . $this->addon_ident;
7986
if(!is_dir($install_dir)) {
8087
if(!$app->system->mkdir($install_dir, false, 0750, true)) {
88+
$app->log('Addons dir missing and could not be created.', 2, false);
8189
throw new AddonInstallerException('Could not create addons dir ' . $install_dir);
8290
}
8391
}
@@ -89,6 +97,7 @@ protected function copyAddonFiles() {
8997
exec($command, $ret, $retval);
9098
if($retval != 0) {
9199
/* TODO: logging */
100+
$app->log('Warning or error on copying addon files. Returncode of command ' . $command . ' was: ' . $retval .'.', 0, false);
92101
}
93102

94103
return true;
@@ -100,6 +109,7 @@ protected function copyAddonFiles() {
100109
protected function executeSqlStatements() {
101110
global $app, $conf;
102111

112+
$app->log('Adding addon entry to db.', 0, false);
103113
// create addon entry if not existing
104114
$qry = 'INSERT IGNORE INTO `addons` (`addon_ident`, `addon_version`, `addon_name`, `db_version`) VALUES (?, ?, ?, ?)';
105115
$app->db->query($qry, $this->addon_ident, $this->addon_version, $this->addon_name, 0);
@@ -110,6 +120,7 @@ protected function executeSqlStatements() {
110120
$incremental = 0;
111121
if($check['db_version']) {
112122
$incremental = $check['db_version'];
123+
$app->log('Current db version is ' . $incremental . '.', 0, false);
113124
}
114125
}
115126

@@ -121,9 +132,11 @@ protected function executeSqlStatements() {
121132
if(is_file($sql_file)) {
122133
$ret = null;
123134
$retval = 0;
135+
$app->log('Loading ' . $sql_file . ' into db.', 0, false);
124136
exec($mysql_command . ' < ' . escapeshellarg($sql_file), $ret, $retval);
125137
if($retval != 0) {
126138
/* TODO: log error! */
139+
$app->log('Loading ' . $sql_file . ' into db failed.', 1, false);
127140
}
128141
}
129142
} else {
@@ -135,16 +148,19 @@ protected function executeSqlStatements() {
135148
} else {
136149
$ret = null;
137150
$retval = 0;
151+
$app->log('Loading ' . $sql_file . ' into db.', 0, false);
138152
exec($mysql_command . ' < ' . escapeshellarg($sql_file), $ret, $retval);
139153
if($retval != 0) {
140154
/* TODO: log error! */
155+
$app->log('Loading ' . $sql_file . ' into db failed.', 1, false);
141156
}
142157
}
143158

144159
$new_db_version++;
145160
}
146161

147162
$app->db->query('UPDATE `addons` SET `addon_version` = ?, `db_version` = ? WHERE `addon_ident` = ?', $this->addon_version, $new_db_version, $this->addon_ident);
163+
$app->log('Db version of addon is now ' . $new_db_version . '.', 0, false);
148164
}
149165

150166
return true;
@@ -153,21 +169,25 @@ protected function executeSqlStatements() {
153169
public function onBeforeInstall() { }
154170

155171
public function onInstall() {
172+
$app->log('Running onInstall()', 0, false);
156173
$this->copyAddonFiles();
157174
$this->copyInterfaceFiles();
158175
$this->copyServerFiles();
159176
$this->executeSqlStatements();
177+
$app->log('Finished onInstall()', 0, false);
160178
}
161179

162180
public function onAfterInstall() { }
163181

164182
public function onBeforeUpdate() { }
165183

166184
public function onUpdate() {
185+
$app->log('Running onUpdate()', 0, false);
167186
$this->copyAddonFiles();
168187
$this->copyInterfaceFiles();
169188
$this->copyServerFiles();
170189
$this->executeSqlStatements();
190+
$app->log('Finished onUpdate()', 0, false);
171191
}
172192

173193
public function onAfterUpdate() { }

0 commit comments

Comments
 (0)