public static function buildDeployment($deployment, $revision = false, $diff = false, $forcebuild = false, $shardposition = false) { self::checkPerms($deployment); self::$deployment = $deployment; self::$m_logger = new NagLogger(); self::deleteDeployment(); if ($revision === false) { /* No revision means we are dealing with a request from a consumer so we will fetch Deployment information from Nagios Configurator API */ $response = NagRequest::fetchDeploymentData($deployment, self::getSubDeployment()); $responseObj = json_decode($response); $helper = new NagHelpers(); $helper->setAliasTemplate($responseObj->miscsettings->aliastemplate); $helper->setGlobalNegate($responseObj->miscsettings->deploynegate); if (SHARDING === true && $responseObj->miscsettings->ensharding == 'on') { $helper->enableSharding($responseObj->miscsettings->shardkey, $responseObj->miscsettings->shardcount, SHARDING_POSITION); } } else { /* A revision means we are dealing with a request from the UI so we will fetch Deployment information from Redis store directly */ $response = RevDeploy::getDeploymentData($deployment, $revision, true); $responseObj = json_decode($response); $helper = new NagHelpers(true); $helper->setAliasTemplate($responseObj->miscsettings->aliastemplate); $helper->setGlobalNegate($responseObj->miscsettings->deploynegate); if ($shardposition !== false) { $deploymentInfo = RevDeploy::getDeploymentInfo($deployment); $helper->enableSharding($deploymentInfo['shardkey'], $deploymentInfo['shardcount'], $shardposition); } } /* Create Initial Files for Nagios */ if (!empty($responseObj->timeperiods)) { self::createTimeperiodFile($responseObj->timeperiods); } if (!empty($responseObj->commands)) { self::createCommandFile($responseObj->commands); } if (!empty($responseObj->contacttemplates)) { self::createContactTemplateFile($responseObj->contacttemplates); } if (!empty($responseObj->contacts)) { self::createContactFile($responseObj->contacts); } if (!empty($responseObj->contactgroups)) { self::createContactGroupFile($responseObj->contactgroups); } if (!empty($responseObj->hosttemplates)) { self::createHostTemplateFile($responseObj->hosttemplates); } if (!empty($responseObj->hostgroups)) { self::createHostGroupFile($responseObj->hostgroups); } if (!empty($responseObj->servicetemplates)) { self::createServiceTemplateFile($responseObj->servicetemplates); } if (!empty($responseObj->servicegroups)) { self::createServiceGroupFile($responseObj->servicegroups); } if (!empty($responseObj->resourcecfg)) { self::createResourceConfigFile($responseObj->resourcecfg); } if (!empty($responseObj->cgicfg)) { self::createCgiConfigFile($responseObj->cgicfg); } if (!empty($responseObj->modgearmancfg)) { self::createModgearmanConfigFile($responseObj->modgearmancfg); } if (!empty($responseObj->nagioscfg)) { self::createNagiosConfigFile($responseObj->nagioscfg); } /* Initialize Helper object for storing information for hosts and services */ if (!empty($responseObj->hostsearches)) { if ($diff === true && !empty(self::$m_localcache)) { foreach (self::$m_localcache as $module => $items) { foreach ($items as $host => $hostData) { $helper->importHost($host, $hostData); } } } else { foreach ($responseObj->hostsearches as $md5Key => $hsObj) { if (($subdeployment = self::getSubDeployment()) !== false) { if (isset($hsObj->subdeployment) && $hsObj->subdeployment != $subdeployment) { continue; } } $module = $hsObj->location; if (preg_match("/^RS-(\\w+)-(\\w+)\$/", $module)) { $modObj = new RightScale(); } elseif (preg_match("/^AWSEC2-(\\w+)-(\\w+)\$/", $module)) { $modObj = new AWSEC2(); } else { $modObj = new $module(); } $hostInfo = $modObj->getSearchResults($hsObj); if (empty($hostInfo)) { self::$m_logger->addToLog("Empty Results Detected for {$hsObj->location} : {$hsObj->srchparam}"); continue; } foreach ($hostInfo as $host => $hostData) { $helper->importHost($host, $hostData); if ($diff === true) { if (!isset(self::$m_localcache[$module]) || !is_array(self::$m_localcache[$module])) { self::$m_localcache[$module] = array(); } self::$m_localcache[$module][$host] = $hostData; } } } } } if (!empty($responseObj->statichosts)) { foreach ($responseObj->statichosts as $encIP => $tmpObj) { if (($subdeployment = self::getSubDeployment()) !== false) { if (isset($tmpObj->subdeployment) && $tmpObj->subdeployment != $subdeployment) { continue; } } $helper->importStaticHost(NagMisc::recursive_object_to_array($tmpObj)); } } /* Import Service and Node => Service Pointer Data */ if ($forcebuild === false) { if (empty($responseObj->services)) { return "Initial Service Data was Empty"; } elseif (empty($responseObj->nodetemplates)) { return "Initial NodeTemplate Data was Empty"; } } $helper->scrubHosts(); if (!empty($responseObj->services)) { $helper->importServices($responseObj->services); } if (!empty($responseObj->nodetemplates)) { $helper->importNodeTemplate($responseObj->nodetemplates, self::getSubDeployment()); } if (!empty($responseObj->servicedependencies)) { $helper->importServiceDependencies($responseObj->servicedependencies); } if (!empty($responseObj->serviceescalations)) { $helper->importServiceEscalations($responseObj->serviceescalations); } $hostCache = $helper->returnHosts(); $serviceCache = $helper->returnServices($hostCache); $serviceDependencyCache = $helper->returnServiceDependencies($hostCache); $serviceEscalationsCache = $helper->returnServiceEscalations($hostCache); /* Create core host / service nagios config files */ if ($forcebuild === false) { if (empty($hostCache)) { return "Host Cache is Empty, unable to create Host File"; } elseif (empty($serviceCache)) { return "Service Cache is Empty, unable to create Services File"; } } if (!empty($hostCache)) { self::createHostFile($hostCache); } if (!empty($serviceCache)) { self::createServiceFile($serviceCache); } if (!empty($serviceDependencyCache)) { self::createServiceDependencyFile($serviceDependencyCache); } if (!empty($serviceEscalationsCache)) { self::createServiceEscalationsFile($serviceEscalationsCache); } return true; }
*/ $app->map('/api/getMGCfg/:deployment', function ($deployment) use($app) { check_deployment_exists($app, $deployment); $deployrev = RevDeploy::getDeploymentRev($deployment); if (RevDeploy::existsDeploymentModgearmanCfg($deployment, $deployrev) === true) { $results = RevDeploy::getDeploymentModgearmanCfg($deployment, $deployrev); echo json_encode($results); return; } $apiResponse = new APIViewData(1, $deployment, "Unable to detect modgearman config file for deployment: {$deployment}"); $app->halt(403, $apiResponse->returnJson()); })->via('GET', 'POST'); $app->get('/api/getNagiosCfg/:deployment(/:subdeployment)', function ($deployment, $subdeployment = false) use($app) { check_deployment_exists($app, $deployment); $deployrev = RevDeploy::getDeploymentRev($deployment); $viewData = RevDeploy::getDeploymentData($deployment, $deployrev); if ($subdeployment !== false) { $hostsearchresults = array(); foreach ($viewData['hostsearches'] as $key => $hostsearch) { if ($hostsearch['subdeployment'] == $subdeployment) { unset($hostsearch['subdeployment']); $hostsearchresults[] = $hostsearch; } } $viewData['hostsearches'] = $hostsearchresults; $nodetemplateresults = array(); foreach ($viewData['nodetemplates'] as $key => $templateinfo) { if ($templateinfo['subdeployment'] == $subdeployment) { unset($templateinfo['subdeployment']); $nodetemplateresults[$key] = $templateinfo; }
// https://github.com/zynga/saigon // Author: Matt West (https://github.com/mhwest13) // License: BSD 2-Clause // /* * Saigon API - Consumer Routes * * Updated the v1 api consumer routes to be more v2 compatible. */ /* * Saigon Meta Info Call */ $app->get('/sapi/consumer/saigoninfo/:deployment(/:subdeployment)', function ($deployment, $subdeployment = false) use($app) { check_deployment_exists($app, $deployment); $deployRev = RevDeploy::getDeploymentRev($deployment); $deploymentData = RevDeploy::getDeploymentData($deployment, $deployRev); $apiResponse = new APIViewData(0, $deployment, false); if ($subdeployment !== false) { $apiResponse->setExtraResponseData('subdeployment', $subdeployment); $hostsearchresults = array(); foreach ($deploymentData['hostsearches'] as $key => $hostsearch) { if ($hostsearch['subdeployment'] == $subdeployment) { unset($hostsearch['subdeployment']); $hostsearchresults[] = $hostsearch; } } $deploymentData['hostsearches'] = $hostsearchresults; $nodetemplateresults = array(); foreach ($deploymentData['nodetemplates'] as $key => $templateinfo) { if ($templateinfo['subdeployment'] == $subdeployment) { unset($templateinfo['subdeployment']);