Example #1
0
$app->get('/sapi/configs/:deployment/diff(/:subdeployment)', function ($deployment, $subdeployment = false) use($app) {
    check_deployment_exists($app, $deployment);
    $islocked = NagTester::getDeploymentDiffLock($deployment, $subdeployment);
    if ($islocked === false) {
        $diffResults = NagTester::getDeploymentDiffInfo($deployment, $subdeployment);
        if (empty($diffResults)) {
            $apiResponse = new APIViewData(1, $deployment, "Unable to detect diff results, change request type to POST to initiate creation");
            $app->halt(404, $apiResponse->returnJson());
        }
        $apiResponse = new APIViewData(0, $deployment, false);
        $configdata = json_decode($diffResults['configs'], true);
        $confdiff = $results = array();
        $confdiff['nagios'] = NagDiff::diff($configdata['nagiosconfs']['from'], $configdata['nagiosconfs']['to']);
        $confdiff['nagiosplugins'] = NagDiff::diff($configdata['plugins']['nagios']['from'], $configdata['plugins']['nagios']['to']);
        $confdiff['nrpe'] = NagDiff::diff($configdata['plugins']['nrpe']['core']['from'], $configdata['plugins']['nrpe']['core']['to']);
        $confdiff['supnrpe'] = NagDiff::diff($configdata['plugins']['nrpe']['sup']['from'], $configdata['plugins']['nrpe']['sup']['to']);
        foreach (array('nagios', 'nagiosplugins', 'nrpe', 'supnrpe') as $mkey) {
            foreach ($confdiff[$mkey] as $dKey => $dObj) {
                $val = $dObj->getGroupedOpcodes();
                if (empty($val)) {
                    continue;
                }
                $renderer = new Diff_Renderer_Text_Unified();
                $results[$mkey][$dKey] = base64_encode(htmlspecialchars($dObj->render($renderer)));
            }
        }
        $apiResponse->setExtraResponseData('diff', $results);
        unset($diffResults['configs']);
        // no need to pass raw data back to client
        $apiResponse->setExtraResponseData('meta', $diffResults, true);
    } else {
Example #2
0
 /**
  * buildDiffRevisions - build the revisions of the deployment and store them for diffing
  * 
  * @param mixed $deployment    deployment we are building revisions for
  * @param mixed $subdeployment deployment we are building revisions for
  * @param mixed $fromrev       from revision we are building
  * @param mixed $torev         to revision we are building
  * @param mixed $shardposition shard position we may be using
  *
  * @static
  * @access public
  * @return void
  */
 public static function buildDiffRevisions($deployment, $subdeployment, $fromrev, $torev, $shardposition)
 {
     self::$_results = array();
     self::$_output = "";
     NagRedis::init(true);
     $lockReturn = NagTester::setDeploymentBuildLock($deployment, $subdeployment, $fromrev);
     if ($lockReturn === false) {
         self::$_output = "Unable to acquire build lock for Deployment/Revision: {$deployment}/{$fromrev}";
         return false;
     }
     NagCreate::resetLocalCache();
     /* Get Current Nagios Configs */
     NagCreate::buildDeployment($deployment, $fromrev, true, true, $shardposition);
     $fromconfs = NagCreate::returnDeploymentConfigs($deployment);
     $fromconfs['nrpe.cfg'] = self::_getNRPECfg($deployment, $fromrev);
     $fromconfs['supplemental-nrpe.cfg'] = self::_getSupNRPECfg($deployment, $fromrev);
     NagTester::deleteDeploymentBuildLock($deployment, $subdeployment, $fromrev);
     $lockReturn = NagTester::setDeploymentBuildLock($deployment, $subdeployment, $torev);
     if ($lockReturn === false) {
         self::$_output = "Unable to acquire build lock for Deployment/Revision: {$deployment}/{$torev}";
         return false;
     }
     /* Get Future Revision Configs */
     NagCreate::buildDeployment($deployment, $torev, true, true, $shardposition);
     $toconfs = NagCreate::returnDeploymentConfigs($deployment);
     $toconfs['nrpe.cfg'] = self::_getNRPECfg($deployment, $torev);
     $toconfs['supplemental-nrpe.cfg'] = self::_getSupNRPECfg($deployment, $torev);
     NagTester::deleteDeploymentBuildLock($deployment, $subdeployment, $torev);
     /* Get Plugin Information */
     $fnagplugins = RevDeploy::getDeploymentNagiosPlugins($deployment, $fromrev);
     $tnagplugins = RevDeploy::getDeploymentNagiosPlugins($deployment, $torev);
     $nagplugins = array_merge($fnagplugins, $tnagplugins);
     $fromnagiosplugins = self::_getPlugins('nagios', $deployment, $fromrev, $nagplugins);
     $tonagiosplugins = self::_getPlugins('nagios', $deployment, $torev, $nagplugins);
     $fplugins = RevDeploy::getDeploymentNRPEPlugins($deployment, $fromrev);
     $tplugins = RevDeploy::getDeploymentNRPEPlugins($deployment, $torev);
     $plugins = array_merge($fplugins, $tplugins);
     $fromnrpecoreplugins = self::_getPlugins('nrpe-core', $deployment, $fromrev, $plugins);
     $tonrpecoreplugins = self::_getPlugins('nrpe-core', $deployment, $torev, $plugins);
     $fsplugins = RevDeploy::getDeploymentSupNRPEPlugins($deployment, $fromrev);
     $tsplugins = RevDeploy::getDeploymentSupNRPEPlugins($deployment, $torev);
     $supplugins = array_merge($fsplugins, $tsplugins);
     $fromsupnrpeplugins = self::_getPlugins('nrpe-sup', $deployment, $fromrev, $supplugins);
     $tosupnrpeplugins = self::_getPlugins('nrpe-sup', $deployment, $torev, $supplugins);
     /* Ok lets diff the results and send it out */
     $results = array();
     $results['nagiosconfs']['from'] = $fromconfs;
     $results['nagiosconfs']['to'] = $toconfs;
     $results['plugins']['nagios']['from'] = $fromnagiosplugins;
     $results['plugins']['nagios']['to'] = $tonagiosplugins;
     $results['plugins']['nrpe']['core']['from'] = $fromnrpecoreplugins;
     $results['plugins']['nrpe']['core']['to'] = $tonrpecoreplugins;
     $results['plugins']['nrpe']['sup']['from'] = $fromsupnrpeplugins;
     $results['plugins']['nrpe']['sup']['to'] = $tosupnrpeplugins;
     self::$_results = $results;
     unset($fromconfs, $toconfs, $results);
     unset($fnagplugins, $tnagplugins, $nagplugins, $fromnagiosplugins, $tonagiosplugins);
     unset($fplugins, $tplugins, $plugins, $fromnrpecoreplugins, $tonrpecoreplugins);
     unset($fsplugins, $tsplugins, $supplugins, $fromsupnrpeplugins, $tosupnrpeplugins);
     return true;
 }
 /**
  * diff_configs - diff configurations view / routine 
  * 
  * @access public
  * @return void
  */
 public function diff_configs()
 {
     $viewData = new ViewData();
     $deployment = $this->getDeployment('deployment_error');
     $this->checkGroupAuth($deployment);
     $fromrev = $this->getParam('fromrev');
     $torev = $this->getParam('torev');
     if ($fromrev === false) {
         $viewData->header = $this->getErrorHeader('deployment_error');
         $viewData->error = 'Unable to detect revision to diff from...';
         $this->sendError('generic_error', $viewData);
     } elseif ($torev === false) {
         $viewData->header = $this->getErrorHeader('deployment_error');
         $viewData->error = 'Unable to detect revision to diff too...';
         $this->sendError('generic_error', $viewData);
     } elseif ($fromrev == $torev) {
         $viewData->header = $this->getErrorHeader('deployment_error');
         $viewData->error = 'Unable to diff the revisions when they are the same revision...';
         $this->sendError('generic_error', $viewData);
     }
     $subdeployment = $this->getParam('subdeployment');
     $shardposition = $this->getParam('shard');
     $islocked = NagTester::getDeploymentBuildLock($deployment, $subdeployment, $fromrev);
     if ($islocked === false) {
         $islocked = NagTester::getDeploymentBuildLock($deployment, $subdeployment, $torev);
         if ($islocked === false) {
             $viewData->running = false;
         } else {
             $viewData->running = true;
         }
     } else {
         $viewData->running = true;
     }
     $deploymentResults = NagTester::getDeploymentDiffInfo($deployment, $subdeployment);
     if (empty($deploymentResults) && $islocked !== false) {
         $viewData->jobadded = false;
         $deploymentResults['timestamp'] = '0000000000';
         $viewData->output = base64_encode('Job is currently processing, this page will reload automatically...');
     } elseif (empty($deploymentResults) && $islocked === false) {
         NagPhean::init(BEANSTALKD_SERVER, BEANSTALKD_TUBE, true);
         NagPhean::addJob(BEANSTALKD_TUBE, json_encode(array('deployment' => $deployment, 'type' => 'diff', 'fromrev' => $fromrev, 'torev' => $torev, 'subdeployment' => $subdeployment, 'shard' => $shardposition)), 1024, 0, 900);
         $viewData->jobadded = true;
         $deploymentResults['timestamp'] = '0000000000';
         $viewData->output = base64_encode('Build Process has been initiated, this page will reload automatically...');
     } elseif (isset($deploymentResults['timestamp']) && $deploymentResults['timestamp'] < time() - 60 && $islocked === false) {
         NagPhean::init(BEANSTALKD_SERVER, BEANSTALKD_TUBE, true);
         NagPhean::addJob(BEANSTALKD_TUBE, json_encode(array('deployment' => $deployment, 'type' => 'diff', 'fromrev' => $fromrev, 'torev' => $torev, 'subdeployment' => $subdeployment, 'shard' => $shardposition)), 1024, 0, 900);
         $viewData->jobadded = true;
         $viewData->output = base64_encode('Build Process has been initiated, this page will reload automatically...');
     } elseif (isset($deploymentResults['subdeployment']) && $deploymentResults['subdeployment'] != $subdeployment && $islocked === false) {
         NagPhean::init(BEANSTALKD_SERVER, BEANSTALKD_TUBE, true);
         NagPhean::addJob(BEANSTALKD_TUBE, json_encode(array('deployment' => $deployment, 'type' => 'diff', 'fromrev' => $fromrev, 'torev' => $torev, 'subdeployment' => $subdeployment, 'shard' => $shardposition)), 1024, 0, 900);
         $viewData->jobadded = true;
         $viewData->output = base64_encode('Build Process has been initiated, this page will reload automatically...');
     } elseif (isset($deploymentResults['fromrev']) && $deploymentResults['fromrev'] != $fromrev && $islocked === false) {
         NagPhean::init(BEANSTALKD_SERVER, BEANSTALKD_TUBE, true);
         NagPhean::addJob(BEANSTALKD_TUBE, json_encode(array('deployment' => $deployment, 'type' => 'diff', 'fromrev' => $fromrev, 'torev' => $torev, 'subdeployment' => $subdeployment, 'shard' => $shardposition)), 1024, 0, 900);
         $viewData->jobadded = true;
         $viewData->output = base64_encode('Build Process has been initiated, this page will reload automatically...');
     } elseif (isset($deploymentResults['torev']) && $deploymentResults['torev'] != $torev && $islocked === false) {
         NagPhean::init(BEANSTALKD_SERVER, BEANSTALKD_TUBE, true);
         NagPhean::addJob(BEANSTALKD_TUBE, json_encode(array('deployment' => $deployment, 'type' => 'diff', 'fromrev' => $fromrev, 'torev' => $torev, 'subdeployment' => $subdeployment, 'shard' => $shardposition)), 1024, 0, 900);
         $viewData->jobadded = true;
         $viewData->output = base64_encode('Build Process has been initiated, this page will reload automatically...');
     } elseif (!empty($deploymentResults) && $islocked !== false) {
         $viewData->output = base64_encode('Job is currently processing, this page will reload automatically...');
         $viewData->jobadded = false;
     } else {
         $viewData->jobadded = false;
         $configdata = json_decode($deploymentResults['configs'], true);
         $deploymentData = $deploymentResults;
         unset($deploymentData['configs']);
         unset($deploymentData['output']);
         $viewData->meta = $deploymentData;
         $viewData->diff = NagDiff::diff($configdata['nagiosconfs']['from'], $configdata['nagiosconfs']['to']);
         $viewData->nagplugins = NagDiff::diff($configdata['plugins']['nagios']['from'], $configdata['plugins']['nagios']['to']);
         $viewData->cplugins = NagDiff::diff($configdata['plugins']['nrpe']['core']['from'], $configdata['plugins']['nrpe']['core']['to']);
         $viewData->splugins = NagDiff::diff($configdata['plugins']['nrpe']['sup']['from'], $configdata['plugins']['nrpe']['sup']['to']);
     }
     if (isset($deploymentResults['starttime']) && $deploymentResults['timestamp'] != '0000000000') {
         $viewData->meta['totaltime'] = $deploymentResults['timestamp'] - $deploymentResults['starttime'];
     }
     if ($viewData->jobadded === true || $viewData->running === true) {
         $viewData->refresh = 15;
     }
     $viewData->deployment = $deployment;
     $viewData->subdeployment = $subdeployment;
     $viewData->action = 'diff_configs';
     $viewData->controller = 'deployment';
     $viewData->fromrev = $fromrev;
     $viewData->torev = $torev;
     $this->sendResponse('deployment_diff_configs', $viewData);
 }