Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit 09bc8d3

Browse files
committed
feat(install): options to force install or upgrade
Signed-off-by: Thierry Bugier <[email protected]>
1 parent a79ea1a commit 09bc8d3

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed

Diff for: hook.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,18 @@ function plugin_flyvemdm_install() {
3737
$migration = new Migration($version['version']);
3838
require_once(PLUGIN_FLYVEMDM_ROOT . "/install/install.class.php");
3939
spl_autoload_register([PluginFlyvemdmInstall::class, 'autoload']);
40+
$forceInstall = false;
41+
$forceUpgrade = false;
42+
if (isCommandLine()) {
43+
if (array_search('--force-install', $_SERVER['argv'])) {
44+
$forceInstall = true;
45+
}
46+
if (array_search('--force-upgrade', $_SERVER['argv'])) {
47+
$forceUpgrade = true;
48+
}
49+
}
4050
$install = new PluginFlyvemdmInstall();
41-
if (!$install->isPluginInstalled()) {
51+
if ($forceInstall || !$install->isPluginInstalled() && !$forceUpgrade) {
4252
return $install->install($migration);
4353
}
4454
return $install->upgrade($migration);

Diff for: install/install.class.php

+27-15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ class PluginFlyvemdmInstall {
4949

5050
protected $migration;
5151

52+
/**
53+
* array of upgrade steps key => value
54+
* key is the version to upgrade from
55+
* value is the version to upgrade to
56+
*
57+
* Exemple: an entry '2.0' => '2.1' tells that versions 2.0
58+
* are upgradable to 2.1
59+
*
60+
* @var array
61+
*/
62+
private $upgradeSteps = [
63+
'0.0' => '2.0',
64+
// '2.0' => '2.1',
65+
// '2.1 => '3.0',
66+
];
67+
5268
/**
5369
* Autoloader for installation
5470
* @param string $classname
@@ -472,30 +488,26 @@ public function createNotificationTargetInvitation() {
472488
*
473489
* @param string version to upgrade from
474490
*/
475-
public function upgrade(Migration $migration) {
491+
public function upgrade(Migration $migration, $forceUpgrade = false) {
476492
spl_autoload_register([__CLASS__, 'autoload']);
477493

478494
$this->migration = $migration;
479-
$fromSchemaVersion = $this->getSchemaVersion();
495+
if ($forceUpgrade) {
496+
// Might return false
497+
$fromSchemaVersion = array_search(PLUGIN_FLYVEMDM_SCHEMA_VERSION, $this->upgradeSteps);
498+
} else {
499+
$fromSchemaVersion = $this->getSchemaVersion();
500+
}
480501

481502
// Prevent problem of execution time
482503
ini_set("max_execution_time", "0");
483504
ini_set("memory_limit", "-1");
484505

485-
switch ($fromSchemaVersion) {
486-
case '0.0':
487-
// Upgrade to 2.0
488-
$this->upgradeOneStep('2.0');
489-
490-
case '2.0':
491-
// Example : upgrade to version 2.1
492-
// $this->upgradeOneStep('2.1');
493-
494-
case '3.0':
495-
// Example : upgrade to version 3.0
496-
// $this->upgradeOneStep('3.0');
497-
506+
while ($fromSchemaVersion && isset($this->upgradeSteps[$fromSchemaVersion])) {
507+
$this->upgradeOneStep($this->upgradeSteps[$fromSchemaVersion]);
508+
$fromSchemaVersion = $this->upgradeSteps[$fromSchemaVersion];
498509
}
510+
499511
if (!PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE) {
500512
$this->upgradeOneStep('develop');
501513
}

Diff for: tools/cli_install.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
cli_install.php
4040
4141
Usage:
42-
cli_install.php [--as-user USER] [--api-user-token APITOKEN] [--enable-api ] [--enable-email ] [ --tests ] [--dev] [--mqtt-address MQTTADDRESS] [--mqtt-internal-address MQTTINTERNALADDRESS] [--mqtt-port MQTTPORT] [--mqtt-port-tls MQTTPORTTLS]
42+
cli_install.php [--as-user USER] [--api-user-token APITOKEN] [--enable-api ] [--enable-email ] [ --tests ] [--dev] [--mqtt-address MQTTADDRESS] [--mqtt-internal-address MQTTINTERNALADDRESS] [--mqtt-port MQTTPORT] [--mqtt-port-tls MQTTPORTTLS] [--force-install | --force-upgrade]
4343
4444
Options:
4545
--as-user USER Do install/upgrade as specified USER. If not provided, 'glpi' user will be used
@@ -52,6 +52,8 @@
5252
--mqtt-internal-address MQTTINTERNALADDRESS Sets the Internal address for Mosquitto MQTTINTERNALADDRESS. This parameter can be [ IP Address/Hostname ]
5353
--mqtt-port MQTTPORT Sets the Listen Port for Mosquitto MQTTPORT
5454
--mqtt-port-tls MQTTPORTTLS Sets the Listen Port TLS for Mosquitto MQTTPORTTLS
55+
--force-install Force a fresh install
56+
--force-upgrade Force upgrade from the latest previous version
5557
5658
DOC;
5759

0 commit comments

Comments
 (0)