diff options
author | Andreas Åkre Solberg <andreas.solberg@uninett.no> | 2010-03-15 13:55:48 +0000 |
---|---|---|
committer | Andreas Åkre Solberg <andreas.solberg@uninett.no> | 2010-03-15 13:55:48 +0000 |
commit | eb65cc858be72e8d2fee59d139e7674681d24251 (patch) | |
tree | d2510abd042760e3ca7eb39381de1c93f69f9c57 /modules/core/lib/ModuleInstaller.php | |
parent | 629acc4907bfbdca672b953b3688a9eb6da297fd (diff) | |
download | simplesamlphp-eb65cc858be72e8d2fee59d139e7674681d24251.zip simplesamlphp-eb65cc858be72e8d2fee59d139e7674681d24251.tar.gz simplesamlphp-eb65cc858be72e8d2fee59d139e7674681d24251.tar.bz2 |
add module.php CLI to install modules
git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@2219 44740490-163a-0410-bde0-09ae8108e29a
Diffstat (limited to 'modules/core/lib/ModuleInstaller.php')
-rw-r--r-- | modules/core/lib/ModuleInstaller.php | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/modules/core/lib/ModuleInstaller.php b/modules/core/lib/ModuleInstaller.php new file mode 100644 index 0000000..d18f59b --- /dev/null +++ b/modules/core/lib/ModuleInstaller.php @@ -0,0 +1,188 @@ +<?php + +class sspmod_core_ModuleInstaller { + + public $module; + + public function __construct(sspmod_core_ModuleDefinition $module) { + $this->module = $module; + + } + + public function remove($branch = NULL) { + $access = $this->module->getAccess($branch); + + switch($access['type']) { + // case 'svn' : + // $this->requireInstalled(); + // $this->remove($access); + // break; + + default: + $this->requireInstalled(); + $this->removeModuleDir($access); + break; + + } + } + + public function install($branch = NULL) { + + $access = $this->module->getAccess($branch); + + switch($access['type']) { + case 'svn' : + $this->requireNotInstalled(); + $this->svnCheckout($access); + $this->enable(); + $this->prepareConfig(); + break; + + case 'zip' : + $this->requireNotInstalled(); + $this->zipLoad($access); + $this->enable(); + $this->prepareConfig(); + break; + + + default: + throw new Exception('Unknown access method type. Not one of [zip,tgz,svn]'); + + } + + } + + public static function exec($cmd) { + echo ' $ ' . $cmd . "\n"; + $output = shell_exec(escapeshellcmd($cmd)); + + if (empty($output)) return; + + $oa = explode("\n", $output); + + foreach($oa AS $ol) { + echo ' > ' . $ol . "\n"; + } + + } + + public function upgrade($branch = NULL) { + + $access = $this->module->getAccess($branch); + + switch($access['type']) { + case 'svn' : + $this->requireInstalled(); + $this->svnUpdate($access); + $this->enable(); + $this->prepareConfig(); + break; + + case 'zip' : + $this->requireInstalled(); + $this->zipLoad($access); + $this->enable(); + $this->prepareConfig(); + break; + + default: + throw new Exception('Unknown access method type. Not one of [zip,tgz,svn]'); + + } + + } + + public function dirExists() { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + + $dir = $basedir . 'modules/' . $this->module->def['id']; + + return (file_exists($dir) && is_dir($dir)); + } + + public function requireValidURL($url) { + if (!preg_match('|http(s)?://[a-zA-Z0-9_-/.]|', $url)) + throw new Exception('Invalid URL [' . $url . ']'); + } + + public function requireNotInstalled() { + if ($this->dirExists()) + throw new Exception('The module [' . $this->module->def['id'] . '] is already installed.'); + } + + public function requireInstalled() { + if (!$this->dirExists()) + throw new Exception('The module [' . $this->module->def['id'] . '] is not installed.'); + } + + public function svnCheckout($access) { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + $cmd = "svn co " . escapeshellarg($access['url']) . " " . $basedir . "modules/" . $this->module->def['id']; + self::exec($cmd); + } + + public function svnUpdate($access) { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + $cmd = "svn up " . $basedir . "modules/" . $this->module->def['id']; + self::exec($cmd); + } + + public function removeModuleDir($access) { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + $cmd = "rm -rf " . $basedir . "modules/" . $this->module->def['id']; + self::exec($cmd); + } + + public function enable() { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + + $this->requireInstalled(); + + $cmd = "touch " . $basedir . "modules/" . $this->module->def['id'] . '/enable'; + self::exec($cmd); + } + + public function prepareConfig() { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + + $this->requireInstalled(); + + $dir = $basedir . "modules/" . $this->module->def['id'] . '/config-templates'; + if (!file_exists($dir)) return; + + $files = scandir($dir); + foreach($files AS $file) { + if(!preg_match('|^.*\.php|', $file)) continue; + + if (file_exists($basedir . 'config/' . $file)) { + echo "Configuration file [" . $file . "] already exists. Will not overwrite existing file.\n"; + continue; + } + + $cmd = 'cp ' . $dir . '/' . $file . ' ' . $basedir . 'config/'; + self::exec($cmd); + } + } + + public function zipLoad($access) { + $config = SimpleSAML_Configuration::getConfig('config.php'); + $basedir = $config->getBaseDir(); + + $zipfile = $access['url']; + $localfile = tempnam(sys_get_temp_dir(), 'ssp-module-'); + $filecontents = file_get_contents($zipfile); + file_put_contents($localfile, $filecontents); + + $cmd = "unzip -qo " . escapeshellarg($localfile) . " -d " . $basedir . "modules/"; + self::exec($cmd); + + } + +}
\ No newline at end of file |