Skip to content

Instantly share code, notes, and snippets.

@sukum
Forked from jmertic/gist:6294266
Last active December 25, 2015 14:29
Show Gist options
  • Save sukum/6991293 to your computer and use it in GitHub Desktop.
Save sukum/6991293 to your computer and use it in GitHub Desktop.
Silent install a module loadable package from the command line
#!/usr/bin/env php
<?php
function usage()
{
print("usage: -i /path/to/instance -p /path/to/expanded/module -z /path/to/zipfile\n");
exit(1);
}
$opts = getopt('i:p:z:');
if (!$opts) usage();
$sugar_directory = array_key_exists('i', $opts) ? $opts['i'] : false;
$package_dir = array_key_exists('p', $opts) ? $opts['p'] : false;
$zip_file = array_key_exists('z', $opts) ? $opts['z'] : false;
if (!$sugar_directory || !$package_dir || !$zip_file) {
usage();
}
if ( function_exists('posix_getpwuid') ) {
$euinfo = posix_getpwuid(posix_geteuid());
$euser = $euinfo['name'];
$auser = exec("echo $(ps axho user,comm|grep -E \"httpd|apache\"|uniq|grep -v \"root\"|awk 'END {if ($1) print $1}')");
if ($euser != $auser ) {
error_log("Must be run as $auser! [not: $euser]");
usage();
}
}
if (!is_dir($package_dir)) {
error_log("Package directory isn't a directory");
exit(1);
}
if (!is_readable($zip_file)) {
error_log("Can't read zip file at $zip_file");
exit(1);
}
if (!chdir($sugar_directory)) {
error_log("Failed to chdir to $sugar_directory");
exit(1);
}
if (!is_readable($package_dir . '/manifest.php')) {
error_log("Package dir does not contain a readable manifest.php\n");
exit(1);
}
require_once($package_dir . '/manifest.php');
if (!is_array($manifest)) {
error_log("Sourced manifest but \$manifest was not set!\n");
exit(1);
}
if (!array_key_exists('name', $manifest) || $manifest['name'] == '') {
error_log("manifest doesn't specify a name!\n");
exit(1);
}
//initialize
if(!defined('sugarEntry')) define('sugarEntry', true);
require($sugar_directory.'/config.php');
require_once($sugar_directory.'/include/entryPoint.php');
require_once($sugar_directory.'/ModuleInstall/ModuleInstaller.php');
$current_user = new User();
$current_user->is_admin = '1';
//initialize the module installer
$modInstaller = new ModuleInstaller();
$modInstaller->silent = true; //shuts up the javscript progress bar
// disable packageScanner
$sugar_config['moduleInstaller']['packageScan'] = false;
// Squelch some warnings
$GLOBALS[ 'app_list_strings' ][ 'moduleList' ] = Array();
// Check for already installed
$new_upgrade = new UpgradeHistory();
$new_upgrade->name = $manifest['name'];
if ($new_upgrade->checkForExisting($new_upgrade) !== null) {
error_log("Already installed.\n");
exit(0);
}
//start installation
echo "Patching $sugar_directory\n";
$modInstaller->install($package_dir);
$to_serialize = array(
'manifest' => $manifest,
'installdefs' => isset($installdefs) ? $installdefs : Array(),
'upgrade_manifest' => isset($upgrade_manifest) ? $upgrade_manifest : Array(),
);
echo "Adding UpgradeHistory object\n";
$new_upgrade->name = $manifest['name'];
$new_upgrade->filename = $package_dir;
$new_upgrade->md5sum = md5_file($zip_file);
$new_upgrade->type = array_key_exists('type', $manifest) ? $manifest['type'] : 'module';
$new_upgrade->version = array_key_exists('version', $manifest) ? $manifest['version'] : '';
$new_upgrade->status = "installed";
$new_upgrade->author = array_key_exists('author', $manifest) ? $manifest['author'] : '';
$new_upgrade->description = array_key_exists('description', $manifest) ? $manifest['description'] : '';
$new_upgrade->id_name = array_key_exists('id', $installdefs) ? $installdefs['id'] : '';
$new_upgrade->manifest = base64_encode(serialize($to_serialize));
$new_upgrade->save();
echo "Success!\n";
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment