/** * @throws \Exception */ public function run() { if (!$this->command instanceof ProvisionCommand) { throw new \Exception("The Php task can only be run by the Provision command."); } $phps = Config::get('php::builds'); if (empty($phps)) { return; } $phpsOld = Config::getOld('php::builds'); $installedPhps = Util::getInstalledPhps(); $default_php = null; foreach ($phps as $build => $meta) { $meta['_build'] = $build; $meta['_path'] = "/opt/phpbrew/php/{$build}"; $meta['installed'] = !isset($meta['installed']) || $meta['installed']; $meta['_is_installed'] = in_array($build, $installedPhps); $meta['_old'] = isset($phpsOld[$build]) ? $phpsOld[$build] : []; // ignore if php to be removed and is not installed if (!$meta['installed'] && !$meta['_is_installed']) { continue; } $this->managePhp($meta); $this->manageExtensions($meta); $this->manageFpm($meta); if (!empty($meta['default'])) { $default_php = $build; } } $this->setDefaultPhp($default_php); }
public static function runPhpCode($code, $build = 'system') { if ($build == 'system') { $fpmPort = '9001'; } else { $phps = Config::get('php::builds'); $phpsInstalled = Util::getInstalledPhps(); if (empty($phps[$build]) || !in_array($build, $phpsInstalled)) { throw new \Exception("Unable to find php {$build}"); } if (empty($phps[$build]['fpm']['port'])) { throw new \Exception("Unable to find fpm port for php {$build}"); } $fpmPort = $phps[$build]['fpm']['port']; } $filename = uniqid('coderunner_', true) . '.php'; $filepath = '/tmp/' . $filename; $fs = self::getFilesystem(); $fs->touch($filepath); $fs->chmod($filepath, 0777); $fs->write($filepath, $code); $scname = '/' . $filename; $scfname = $filepath; $scroot = dirname($scfname); $cmd = 'SCRIPT_NAME=%s \\ SCRIPT_FILENAME=%s \\ DOCUMENT_ROOT=%s \\ REQUEST_METHOD=GET \\ cgi-fcgi -bind -connect 127.0.0.1:%s'; $cmd = sprintf($cmd, $scname, $scfname, $scroot, $fpmPort); self::exec($cmd, false, $output); $fs->remove($filepath); return $output; }
protected function __getInstalledPhps() { $phps = Config::get('php::builds'); if (empty($phps)) { return []; } $phpsInstalled = Util::getInstalledPhps(); $default_build = null; foreach ($phps as $build => $meta) { if (!in_array($build, $phpsInstalled)) { unset($phps[$build]); continue; } if (isset($meta['extensions'])) { foreach ($meta['extensions'] as $extname => $extmeta) { if (isset($extmeta['installed']) && !$extmeta['installed']) { unset($meta['extensions'][$extname]); } } } $pidfile = '/opt/phpbrew/php/' . $build . '/var/run/php-fpm.pid'; $phps[$build]['running'] = false; if (file_exists($pidfile) && trim(file_get_contents($pidfile)) != "") { $phps[$build]['running'] = true; } if (!empty($phps[$build]['default'])) { $default_build = $build; } $phps[$build]['default'] = false; } if ($default_build) { $phps[$default_build]['default'] = true; } return $phps; }
/** * @throws \Exception */ public function run() { if (!$this->command instanceof ProvisionCommand) { throw new \Exception("The End task can only be run by the Provision command."); } // Write current config file to be used in the next provisioning process $this->output->writeDebug("Writing a copy of the current environment.yaml file"); Config::writeOld(); }
/** * Sets the php-fpm config file that shall be included in the vhost config file based on the defined * php version in project::vhost::php * * @param string $id * @param array $project * @param array $vhost * @return array */ protected function __setVhostFpmInclude($id, $project, $vhost) { static $default_php_build; if (empty($project['php']['build'])) { return $vhost; } $php_build = $project['php']['build']; if ($php_build == 'system') { $vhost['includes'] = ['/etc/apache2/php/php-system-fpm.conf']; return $vhost; } $phps_config = Config::get('php::builds'); // set default php version if not set if (!isset($default_php_build)) { foreach ($phps_config as $build => $meta) { if (!empty($meta['default'])) { $default_php_build = $build; } } if (!isset($default_php_build)) { $default_php_build = 0; } } if ($php_build == 'default' && 0 === $default_php_build) { $this->output->writeError("Unable to use default php build for project '{$id}' because no default php build found"); return $vhost; } if ($php_build == 'default') { $php_build = $default_php_build; } $phps_installed = Util::getInstalledPhps(); if (!in_array($php_build, $phps_installed) || !isset($phps_config[$php_build])) { $this->output->writeError("Unable to use php build '{$php_build}' for project '{$id}', build isn't installed"); return $vhost; } if (empty($phps_config[$php_build]['fpm']['port'])) { $this->output->writeError("Unable to use php build '{$php_build}' for project '{$id}', php fpm port isn't configured"); return $vhost; } $php_version_fpm_conf = '/etc/apache2/php/php-' . $php_build . '-fpm.conf'; if (!file_exists($php_version_fpm_conf)) { $this->output->writeError("Unable to use php build '{$php_build}' for project '{$id}', apache php-fpm config file '{$php_version_fpm_conf}' doesn't exist"); return $vhost; } $vhost['includes'] = [$php_version_fpm_conf]; return $vhost; }
/** * Tells whether a project needs to be checked due to other relevant changes * * @param $id * @param $project * @return bool */ protected function __projectNeedsCheck($id, $project) { // Re-check if php::builds config has been changed if (!empty($project['vhost']) && !empty($project['php'])) { return Config::hasChanges('php::builds'); } return false; }
/** * @throws \Exception */ public function run() { if (!$this->command instanceof ProvisionCommand) { throw new \Exception("The Init task can only be run by the Provision command."); } // Check box version if (!file_exists('/etc/dashbrew/.version')) { throw new \Exception("Unable to find base box version file."); } $box_version = file_get_contents('/etc/dashbrew/.version'); if (empty($box_version)) { throw new \Exception("Invalid base box version file."); } if (false === strpos($box_version, DASHBREW_BASEBOX_VERSION, 0)) { throw new \Exception("Incompatible base box version ({$box_version}). Dashbrew requires a base box version " . DASHBREW_BASEBOX_VERSION . ".x"); } $this->output->writeDebug("Checking for available " . $box_version . " patches"); $box_patch_file = '/etc/dashbrew/.patch'; $box_patch_md5 = null; if (file_exists($box_patch_file)) { $box_patch_md5 = file_get_contents($box_patch_file); } $available_patch = file_get_contents('https://raw.githubusercontent.com/mdkholy/dashbrew-basebox/master/patches/' . $box_version . '.sh'); $apply_patch = false; if (!empty($available_patch)) { $available_patch_md5 = md5($available_patch); if ($box_patch_md5 != $available_patch_md5) { $apply_patch = true; } } if ($apply_patch) { $this->output->writeInfo("An update patch is available for your box. Updating..."); $fs = Util::getFilesystem(); $exec_patch_file = '/tmp/dashbrew-basebox-patch.sh'; $fs->write($exec_patch_file, $available_patch, 'root'); $fs->chmod($exec_patch_file, 0755); $proc = Util::Process($this->output, "sudo bash {$exec_patch_file}", ['timeout' => null]); if ($proc->isSuccessful()) { $this->output->writeInfo("Update patch has been applied successfully"); } else { $this->output->writeError("Error occured while applying update patch"); } $fs->remove($exec_patch_file); $fs->write($box_patch_file, $available_patch_md5); $fs->chmod($box_patch_file, 0644); } // Parse & initialize config.yaml file if (file_exists(Config::CONFIG_FILE)) { $this->output->writeInfo("Initializing environment.yaml"); } else { $this->output->writeInfo("Provisioning without environment.yaml config file"); } Config::init(); if (Config::get('debug')) { $this->output->setVerbosity(Output::VERBOSITY_DEBUG); } // Initialize config files and directories $this->output->writeDebug("Initializing config files"); $config_files = [['path' => '/etc/monit/monitrc', 'source' => '/vagrant/config/monit/monitrc', 'default' => '/etc/monit/monitrc', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/apache2/apache2.conf', 'source' => '/vagrant/config/apache/apache.conf', 'default' => '/etc/apache2/apache2.conf', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/php5/fpm/php-fpm.conf', 'source' => '/vagrant/config/php/fpm/php-fpm.conf', 'default' => '/etc/php5/fpm/php-fpm.conf', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/php5/fpm/php.ini', 'source' => '/vagrant/config/php/fpm/php.ini', 'default' => '/etc/php5/fpm/php.ini', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/mysql/my.cnf', 'source' => '/vagrant/config/mysql/my.cnf', 'default' => '/etc/mysql/my.cnf', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/php5/cli/php.ini', 'source' => '/vagrant/config/php/cli/php.ini', 'default' => '/etc/php5/cli/php.ini', 'owner' => 'root', 'group' => 'root'], ['path' => '/opt/phpbrew/config.yaml', 'source' => '/vagrant/config/phpbrew/config.yaml', 'default' => '/opt/phpbrew/config.yaml', 'owner' => 'vagrant', 'group' => 'vagrant'], ['path' => '/usr/share/phpmyadmin/config.inc.php', 'source' => '/vagrant/config/phpmyadmin/config.inc.php', 'default' => '/usr/share/phpmyadmin/config.inc.php', 'owner' => 'vagrant', 'group' => 'www-data']]; $config_dirs = [['path' => '/home/vagrant', 'source' => '/vagrant/config/home', 'default' => '/home/vagrant', 'owner' => 'vagrant', 'group' => 'vagrant'], ['path' => '/etc/monit/conf.d', 'source' => '/vagrant/config/monit/conf.d', 'default' => '/etc/monit/conf.d', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/php5/cli/conf.d', 'source' => '/vagrant/config/php/cli/conf.d', 'default' => '/etc/php5/cli/conf.d', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/php5/fpm/conf.d', 'source' => '/vagrant/config/php/fpm/conf.d', 'default' => '/etc/php5/fpm/conf.d', 'owner' => 'root', 'group' => 'root'], ['path' => '/etc/php5/fpm/pool.d', 'source' => '/vagrant/config/php/fpm/pool.d', 'default' => '/etc/php5/fpm/pool.d', 'owner' => 'root', 'group' => 'root']]; $installed_phps = Util::getInstalledPhps(); $phps = Config::get('php::builds'); foreach ($phps as $build => $meta) { if (!in_array($build, $installed_phps)) { continue; } $config_dirs[] = ['path' => "/opt/phpbrew/php/{$build}/var/db", 'source' => "/vagrant/config/phpbrew/{$build}/conf.d", 'default' => "/opt/phpbrew/php/{$build}/var/db", 'owner' => 'vagrant', 'group' => 'vagrant']; $config_files[] = ['path' => "/opt/phpbrew/php/{$build}/etc/php.ini", 'source' => "/vagrant/config/phpbrew/{$build}/php.ini", 'default' => "/opt/phpbrew/php/{$build}/etc/php.ini", 'owner' => 'vagrant', 'group' => 'vagrant']; $config_files[] = ['path' => "/opt/phpbrew/php/{$build}/etc/php-fpm.conf", 'source' => "/vagrant/config/phpbrew/{$build}/php-fpm.conf", 'default' => "/opt/phpbrew/php/{$build}/etc/php-fpm.conf", 'owner' => 'vagrant', 'group' => 'vagrant']; } foreach ($config_files as $config_file) { SyncManager::addRule(SyncManager::SYNC_FILE, $config_file); } foreach ($config_dirs as $config_dir) { SyncManager::addRule(SyncManager::SYNC_DIR, $config_dir); } $services = ['apache', 'mysql', 'php-system-fpm', 'mailcatcher']; foreach ($services as $service) { ServiceManager::addService($service); } }
/** * Manages nodjs modules via npm */ protected function manageNpmPackages() { $packages_config = Config::get('npm::packages'); if (empty($packages_config)) { return; } $this->output->writeInfo("Checking nodejs modules"); $packages = ['install' => [], 'remove' => []]; foreach ($packages_config as $package => $installed) { $is_installed = false; $proc = Util::process($this->output, "npm -j ls -g {$package}", ['stderr' => false]); if ($proc->isSuccessful()) { $is_installed = false !== stripos($proc->getOutput(), '"' . $package . '": {'); } if ($installed && $is_installed || !$installed && !$is_installed) { continue; } if (!$installed) { $packages['remove'][] = $package; continue; } $packages['install'][] = $package; } foreach ($packages['remove'] as $package) { $this->output->writeInfo("Uninstalling npm package '{$package}'"); $proc = Util::process($this->output, "npm uninstall -g {$package}"); if (!$proc->isSuccessful()) { $this->output->writeError("Error occured uninstalling npm package '{$package}'"); continue; } $this->output->writeInfo("Successfully uninstalled npm package '{$package}'"); } foreach ($packages['install'] as $package) { $this->output->writeInfo("Installing npm package '{$package}'"); $proc = Util::process($this->output, "npm install -g {$package}", ['timeout' => null]); if (!$proc->isSuccessful()) { $this->output->writeError("Error occured while installing npm package '{$package}'"); continue; } $this->output->writeInfo("Successfully installed npm package '{$package}'"); } }