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

Commit

Permalink
prepare mergeability
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Aug 22, 2018
1 parent 22afc23 commit bb33099
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 686 deletions.
25 changes: 10 additions & 15 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,23 @@
* Entry point for installation process
*/
function plugin_flyvemdm_install() {
$version = plugin_version_flyvemdm();
$migration = new Migration($version['version']);
require_once(PLUGIN_FLYVEMDM_ROOT . "/install/install.class.php");
spl_autoload_register([PluginFlyvemdmInstall::class, 'autoload']);
$install = new PluginFlyvemdmInstall();
if (!$install->isPluginInstalled()) {
return $install->install($migration);
}
return $install->upgrade($migration);
global $DB;

require_once(PLUGIN_FLYVEMDM_ROOT . "/install/installer.class.php");
$installer = new PluginFlyvemdmInstaller();

return $installer->install();
}

/**
* Uninstalls the plugin
* @return boolean True if success
*/
function plugin_flyvemdm_uninstall() {
require_once(PLUGIN_FLYVEMDM_ROOT . "/install/install.class.php");
$install = new PluginFlyvemdmInstall();
require_once(PLUGIN_FLYVEMDM_ROOT . "/install/installer.class.php");
$installer = new PluginFlyvemdmInstaller();

return $install->uninstall();
return $installer->uninstall();
}

/**
Expand Down Expand Up @@ -158,11 +155,9 @@ function plugin_Flyvemdm_addDefaultWhere($itemtype) {
case PluginFlyvemdmAgent::class:
return PluginFlyvemdmAgent::addDefaultWhere();

case PluginFlyvemdmFDroidApplication::class: {
case PluginFlyvemdmFDroidApplication::class:
return PluginFlyvemdmFDroidApplication::addDefaultWhere();
}
}

}

/**
Expand Down
213 changes: 95 additions & 118 deletions install/install.class.php → install/installer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,36 @@
* @since 0.1.0
*
*/
class PluginFlyvemdmInstall {
class PluginFlyvemdmInstaller {

const DEFAULT_CIPHERS_LIST = 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:AES128:AES256:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK';

const BACKEND_MQTT_USER = 'flyvemdm-backend';

// Order of this array is mandatory due tu dependancies on install and uninstall
protected static $itemtypesToInstall = [
'mqttuser',
// Must be before config because config creates a mqtt user for the plugin
'mqttacl',
// Must be before config because config creates a mqtt ACL for the plugin
'config',
'entityconfig',
'mqttlog',
'agent',
'package',
'file',
'fleet',
'profile',
'notificationtargetinvitation',
'geolocation',
'policy',
'policycategory',
'fleet_policy',
'wellknownpath',
'invitation',
'invitationlog',
];

protected static $currentVersion = null;

protected $migration;
Expand All @@ -54,7 +78,7 @@ class PluginFlyvemdmInstall {
* @param string $classname
* @return bool
*/
public static function autoload($classname) {
public function autoload($classname) {
// useful only for installer GLPI autoloader already handles inc/ folder
$filename = dirname(__DIR__) . '/inc/' . strtolower(str_replace('PluginFlyvemdm', '',
$classname)) . '.class.php';
Expand All @@ -71,34 +95,52 @@ public static function autoload($classname) {
* @return boolean true (assume success, needs enhancement)
*
*/
public function install(Migration $migration) {
public function install() {
global $DB;

$this->migration = $migration;
spl_autoload_register([__CLASS__, 'autoload']);

$this->installSchema();
$this->createInitialConfig();
$this->migration->executeMigration();
$this->installUpgradeCommonTasks();

return true;
}

protected function installSchema() {
global $DB;

$this->migration->displayMessage("create database schema");
$this->migration = new Migration(PLUGIN_FLYVEMDM_VERSION);
$this->migration->setVersion(PLUGIN_FLYVEMDM_VERSION);

// adding DB model from sql file
// TODO : migrate in-code DB model setup here
if (self::getCurrentVersion() == '') {
// Setup DB model
$dbFile = PLUGIN_FLYVEMDM_ROOT . "/install/mysql/plugin_flyvemdm_empty.sql";
if (!$DB->runFile($dbFile)) {
$this->migration->displayWarning("Error creating tables : " . $DB->error(), true);
return false;
}

$dbFile = __DIR__ . '/mysql/plugin_flyvemdm_empty.sql';
if (!$DB->runFile($dbFile)) {
$this->migration->displayWarning("Error creating tables : " . $DB->error(), true);
return false;
$this->createInitialConfig();
} else {
if (PluginFlyvemdmCommon::endsWith(PLUGIN_FLYVEMDM_VERSION,
"-dev") || (version_compare(self::getCurrentVersion(),
PLUGIN_FLYVEMDM_VERSION) != 0)) {
// TODO : Upgrade (or downgrade)
$this->upgrade(self::getCurrentVersion());
}
}

$this->migration->executeMigration();

if (version_compare(GLPI_VERSION, '9.3.0') >= 0) {
$this->migrateToInnodb();
}
$this->createDirectories();
$this->createFirstAccess();
$this->createGuestProfileAccess();
$this->createAgentProfileAccess();
$this->createDefaultFleet();
$this->createPolicies();
$this->createNotificationTargetInvitation();
$this->createJobs();
$this->createRootEntityConfig();
$this->createDisplayPreferences();

Config::setConfigurationValues('flyvemdm', ['version' => PLUGIN_FLYVEMDM_VERSION]);

return true;
}

Expand Down Expand Up @@ -156,45 +198,25 @@ public function createDirectories() {
}

// Create cache directory for the template engine
if (!file_exists(FLYVEMDM_TEMPLATE_CACHE_PATH)) {
if (!mkdir(FLYVEMDM_TEMPLATE_CACHE_PATH, 0770, true)) {
$this->migration->displayWarning("Cannot create " . FLYVEMDM_TEMPLATE_CACHE_PATH . " directory");
}
PluginFlyvemdmCommon::recursiveRmdir(FLYVEMDM_TEMPLATE_CACHE_PATH);
if (!mkdir(FLYVEMDM_TEMPLATE_CACHE_PATH, 0770, true)) {
$this->migration->displayWarning("Cannot create " . FLYVEMDM_TEMPLATE_CACHE_PATH . " directory");
}
}

/**
* @return null|string
*/
public function getSchemaVersion() {
if ($this->isPluginInstalled()) {
$config = Config::getConfigurationValues('flyvemdm', ['schema_version']);
if (!isset($config['schema_version'])) {
return '0.0';
}
return $config['schema_version'];
}

return null;
}

/**
* is the plugin already installed ?
*
* @return boolean
*/
public function isPluginInstalled() {
global $DB;

// Check tables of the plugin between 1.1 and 2.0 releases
$result = $DB->query("SHOW TABLES LIKE 'glpi_plugin_flyvemdm_%'");
if ($result) {
if ($DB->numrows($result) > 0) {
return true;
public static function getCurrentVersion() {
if (self::$currentVersion === null) {
$config = \Config::getConfigurationValues('flyvemdm', ['version']);
if (!isset($config['version'])) {
self::$currentVersion = '';
} else {
self::$currentVersion = $config['version'];
}
}

return false;
return self::$currentVersion;
}

protected function createRootEntityConfig() {
Expand Down Expand Up @@ -235,8 +257,6 @@ protected function createFirstAccess() {
PluginFlyvemdmInvitation::$rightname => ALLSTANDARDRIGHT,
PluginFlyvemdmInvitationLog::$rightname => READ,
PluginFlyvemdmTaskstatus::$rightname => READ,
PluginFlyvemdmFDroidApplication::$rightname => READ | UPDATE | READNOTE | UPDATENOTE,
PluginFlyvemdmFDroidMarket::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE,
];

$profileRight->updateProfileRights($_SESSION['glpiactiveprofile']['id'], $newRights);
Expand Down Expand Up @@ -475,53 +495,22 @@ public function createNotificationTargetInvitation() {
/**
* Upgrade the plugin to the current code version
*
* @param string version to upgrade from
* @param string $fromVersion
*/
public function upgrade(Migration $migration) {
spl_autoload_register([__CLASS__, 'autoload']);

$this->migration = $migration;
$fromSchemaVersion = $this->getSchemaVersion();

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');

protected function upgrade($fromVersion) {
switch ($fromVersion) {
case '2.0.0':
// Example : upgrade to version 3.0.0
// $this->upgradeOneStep('3.0.0');
case '3.0.0':
// Example : upgrade to version 4.0.0
// $this->upgradeOneStep('4.0.0');

default:
}
if (!PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE) {
$this->upgradeOneStep('develop');
if (PluginFlyvemdmCommon::endsWith(PLUGIN_FLYVEMDM_VERSION, "-dev")) {
$this->upgradeOneStep('dev');
}
$this->installUpgradeCommonTasks();
return true;
}

private function installUpgradeCommonTasks() {
$this->createDirectories();
$this->createFirstAccess();
$this->createGuestProfileAccess();
$this->createAgentProfileAccess();
$this->createDefaultFleet();
$this->createPolicies();
$this->createNotificationTargetInvitation();
$this->createJobs();
$this->createRootEntityConfig();
$this->createDisplayPreferences();

Config::setConfigurationValues(
'flyvemdm', [
'version' => PLUGIN_FLYVEMDM_VERSION,
'schema_version' => PLUGIN_FLYVEMDM_SCHEMA_VERSION,
]
);
}

/**
Expand All @@ -530,19 +519,21 @@ private function installUpgradeCommonTasks() {
* @param string $toVersion
*/
protected function upgradeOneStep($toVersion) {

ini_set("max_execution_time", "0");
ini_set("memory_limit", "-1");

$suffix = str_replace('.', '_', $toVersion);
$includeFile = __DIR__ . "/update_to_$suffix.php";
$includeFile = __DIR__ . "/upgrade/update_to_$suffix.php";
if (is_readable($includeFile) && is_file($includeFile)) {
include_once $includeFile;
$updateClass = "PluginFlyvemdmUpgradeTo$suffix";
$this->migration->addNewMessageArea("Upgrade to $toVersion");
$upgradeStep = new $updateClass();
$upgradeStep->upgrade($this->migration);
$this->migration->executeMigration();
$this->migration->displayMessage('Done');
$updateFunction = "plugin_flyvemdm_update_to_$suffix";
if (function_exists($updateFunction)) {
$this->migration->addNewMessageArea("Upgrade to $toVersion");
$updateFunction($this->migration);
$this->migration->executeMigration();
$this->migration->displayMessage('Done');
}
}
}

Expand All @@ -552,18 +543,6 @@ protected function createJobs() {
'comment' => __('Parse uploaded applications to collect metadata', 'flyvemdm'),
'mode' => CronTask::MODE_EXTERNAL,
]);

CronTask::Register(PluginFlyvemdmFDroidMarket::class, 'UpdateRepositories', DAY_TIMESTAMP,
[
'comment' => __('Update the list of applications available from F-Droid like repositories', 'flyvemdm'),
'mode' => CronTask::MODE_EXTERNAL
]);

CronTask::Register(PluginFlyvemdmFDroidApplication::class, 'DownloadApplications', DAY_TIMESTAMP,
[
'comment' => __('Imports applications for deployment', 'flyvemdm'),
'mode' => CronTask::MODE_EXTERNAL
]);
}

/**
Expand Down Expand Up @@ -833,8 +812,6 @@ protected function deleteTables() {
PluginFlyvemdmPolicyCategory::getTable(),
PluginFlyvemdmWellknownpath::getTable(),
PluginFlyvemdmTaskstatus::getTable(),
PluginFlyvemdmFDroidApplication::getTable(),
PluginFlyvemdmFDroidMarket::getTable(),
];

foreach ($tables as $table) {
Expand Down
File renamed without changes.
Loading

0 comments on commit bb33099

Please sign in to comment.