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

WIP Options to force install or upgrade #774

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ function plugin_flyvemdm_install() {
$migration = new Migration($version['version']);
require_once(PLUGIN_FLYVEMDM_ROOT . "/install/install.class.php");
spl_autoload_register([PluginFlyvemdmInstall::class, 'autoload']);
$forceInstall = false;
$forceUpgrade = false;
if (isCommandLine()) {
if (array_search('--force-install', $_SERVER['argv'])) {
$forceInstall = true;
}
if (array_search('--force-upgrade', $_SERVER['argv'])) {
$forceUpgrade = true;
}
}
$install = new PluginFlyvemdmInstall();
if (!$install->isPluginInstalled()) {
if ($forceInstall || !$install->isPluginInstalled() && !$forceUpgrade) {
return $install->install($migration);
}
return $install->upgrade($migration);
Expand Down
42 changes: 27 additions & 15 deletions install/install.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ class PluginFlyvemdmInstall {

protected $migration;

/**
* array of upgrade steps key => value
* key is the version to upgrade from
* value is the version to upgrade to
*
* Exemple: an entry '2.0' => '2.1' tells that versions 2.0
* are upgradable to 2.1
*
* @var array
*/
private $upgradeSteps = [
'0.0' => '2.0',
// '2.0' => '2.1',
// '2.1 => '3.0',
];

/**
* Autoloader for installation
* @param string $classname
Expand Down Expand Up @@ -472,30 +488,26 @@ public function createNotificationTargetInvitation() {
*
* @param string version to upgrade from
*/
public function upgrade(Migration $migration) {
public function upgrade(Migration $migration, $forceUpgrade = false) {
spl_autoload_register([__CLASS__, 'autoload']);

$this->migration = $migration;
$fromSchemaVersion = $this->getSchemaVersion();
if ($forceUpgrade) {
// Might return false
$fromSchemaVersion = array_search(PLUGIN_FLYVEMDM_SCHEMA_VERSION, $this->upgradeSteps);
} else {
$fromSchemaVersion = $this->getSchemaVersion();
}

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

switch ($fromSchemaVersion) {
case '0.0':
// Upgrade to 2.0
$this->upgradeOneStep('2.0');

case '2.0':
// Example : upgrade to version 2.1
// $this->upgradeOneStep('2.1');

case '3.0':
// Example : upgrade to version 3.0
// $this->upgradeOneStep('3.0');

while ($fromSchemaVersion && isset($this->upgradeSteps[$fromSchemaVersion])) {
$this->upgradeOneStep($this->upgradeSteps[$fromSchemaVersion]);
$fromSchemaVersion = $this->upgradeSteps[$fromSchemaVersion];
}

if (!PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE) {
$this->upgradeOneStep('develop');
}
Expand Down
24 changes: 13 additions & 11 deletions tools/cli_install.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
cli_install.php

Usage:
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]
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]

Options:
--as-user USER Do install/upgrade as specified USER. If not provided, 'glpi' user will be used
Expand All @@ -52,6 +52,8 @@
--mqtt-internal-address MQTTINTERNALADDRESS Sets the Internal address for Mosquitto MQTTINTERNALADDRESS. This parameter can be [ IP Address/Hostname ]
--mqtt-port MQTTPORT Sets the Listen Port for Mosquitto MQTTPORT
--mqtt-port-tls MQTTPORTTLS Sets the Listen Port TLS for Mosquitto MQTTPORTTLS
--force-install Force a fresh install
--force-upgrade Force upgrade from the latest previous version

DOC;

Expand All @@ -74,6 +76,16 @@

include (__DIR__ . "/../../../inc/includes.php");

$DB = new DB();
if (!$DB->connected) {
die("No DB connection\n");
}

if (!$DB->tableExists("glpi_configs")) {
echo "GLPI not installed\n";
exit(1);
}

if (isset($args['--enable-api']) && $args['--enable-api'] !== false) {
$config = [
'enable_api' => '1',
Expand Down Expand Up @@ -125,11 +137,6 @@
error_reporting(E_ALL | E_STRICT);
//set_error_handler('userErrorHandlerDebug');

$DB = new DB();
if (!$DB->connected) {
die("No DB connection\n");
}

$user = new User();
if (!$user->getFromDBbyName($asUser)) {
die("User $asUser not found in DB\n");
Expand All @@ -144,11 +151,6 @@

/*---------------------------------------------------------------------*/

if (!$DB->tableExists("glpi_configs")) {
echo "GLPI not installed\n";
exit(1);
}

$plugin = new Plugin();

// Install the plugin
Expand Down