/**
  * Performs upgrade literally for the stage ONE.
  *
  * Implementation of this method performs update steps needs to be taken
  * to accomplish upgrade successfully.
  *
  * If there are any error during an execution of this scenario it must
  * throw an exception.
  *
  * @param   int  $stage  optional The stage number
  * @throws  \Exception
  */
 protected function run1($stage)
 {
     $observers = $this->db->Execute("SELECT * FROM farm_event_observers WHERE event_observer_name = 'MailEventObserver'");
     while ($observer = $observers->FetchRow()) {
         $dbFarm = \DBFarm::LoadByID($observer['farmid']);
         // Create endpoint
         $endpointId = $this->db->GetOne("SELECT endpoint_id FROM webhook_endpoints WHERE env_id = ? AND url = ?", array($dbFarm->EnvID, 'SCALR_MAIL_SERVICE'));
         if ($endpointId) {
             $endpoint = WebhookEndpoint::findPk(bin2hex($endpointId));
         } else {
             $endpoint = new WebhookEndpoint();
             $endpoint->level = WebhookEndpoint::LEVEL_ENVIRONMENT;
             $endpoint->accountId = $dbFarm->ClientID;
             $endpoint->envId = $dbFarm->EnvID;
             $endpoint->securityKey = \Scalr::GenerateRandomKey(64);
             $endpoint->isValid = true;
             $endpoint->url = "SCALR_MAIL_SERVICE";
             $endpoint->save();
         }
         //Create webhook configuration
         $webhook = new WebhookConfig();
         $webhook->level = WebhookConfig::LEVEL_ENVIRONMENT;
         $webhook->accountId = $dbFarm->ClientID;
         $webhook->envId = $dbFarm->EnvID;
         $webhook->name = "MailEventObserver(FarmID: {$dbFarm->ID})";
         $webhook->postData = $this->db->GetOne("SELECT value FROM farm_event_observers_config WHERE `key` = ? AND observerid = ?", array('EventMailTo', $observer['id']));
         $webhook->save();
         //save endpoints
         $configEndpoint = new WebhookConfigEndpoint();
         $configEndpoint->webhookId = $webhook->webhookId;
         $configEndpoint->setEndpoint($endpoint);
         $configEndpoint->save();
         //save events
         $dbEvents = $this->db->Execute("SELECT * FROM farm_event_observers_config WHERE `key` LIKE '%Notify' AND observerid = ?", array($observer['id']));
         while ($info = $dbEvents->FetchRow()) {
             preg_match('/On([A-Za-z0-9]+)Notify/si', $info['key'], $matches);
             $configEvent = new WebhookConfigEvent();
             $configEvent->webhookId = $webhook->webhookId;
             $configEvent->eventType = $matches[1];
             $configEvent->save();
         }
         //save farms
         $configFarm = new WebhookConfigFarm();
         $configFarm->webhookId = $webhook->webhookId;
         $configFarm->farmId = $dbFarm->ID;
         $configFarm->save();
     }
 }
示例#2
0
文件: Configs.php 项目: scalr/scalr
 /**
  * @param string $webhookId
  * @param string $name
  * @param JsonData $endpoints
  * @param JsonData $events
  * @param JsonData $farms
  * @param int $timeout
  * @param int $attempts
  * @param boolean $skipPrivateGv
  * @param string $postData
  * @throws Exception
  */
 public function xSaveAction($webhookId, $name, JsonData $endpoints, JsonData $events, JsonData $farms, $timeout = 3, $attempts = 3, $skipPrivateGv = 0, $postData = '')
 {
     if (!$webhookId) {
         $webhook = new WebhookConfig();
         $webhook->setScope($this->request->getScope(), $this->user->getAccountId(), $this->getEnvironmentId(true));
     } else {
         $webhook = WebhookConfig::findPk($webhookId);
         if (!$this->canManageWebhook($webhook)) {
             throw new Scalr_Exception_Core('Insufficient permissions to edit webhook');
         }
     }
     $validator = new Validator();
     $validator->validate($name, 'name', Validator::NOEMPTY);
     if (!$validator->isValid($this->response)) {
         return;
     }
     $webhook->name = $name;
     $webhook->postData = $postData;
     $webhook->skipPrivateGv = $skipPrivateGv;
     $webhook->timeout = $timeout;
     $webhook->attempts = $attempts;
     $webhook->save();
     //save endpoints
     $endpoints = (array) $endpoints;
     foreach (WebhookConfigEndpoint::findByWebhookId($webhook->webhookId) as $webhookConfigEndpoint) {
         $index = array_search($webhookConfigEndpoint->endpointId, $endpoints);
         if ($index === false) {
             $webhookConfigEndpoint->delete();
         } else {
             unset($endpoints[$index]);
         }
     }
     if (!empty($endpoints)) {
         $criteria = [];
         $criteria[] = ['endpointId' => ['$in' => $endpoints]];
         switch ($this->request->getScope()) {
             case WebhookConfig::SCOPE_ENVIRONMENT:
                 $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => $this->getEnvironmentId()], ['level' => WebhookConfig::LEVEL_ENVIRONMENT]]], ['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => WebhookConfig::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => WebhookConfig::LEVEL_SCALR]]]]];
                 break;
             case WebhookConfig::SCOPE_ACCOUNT:
                 $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => WebhookConfig::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => WebhookConfig::LEVEL_SCALR]]]]];
                 break;
             case WebhookConfig::SCOPE_SCALR:
                 $criteria[] = ['level' => WebhookConfig::LEVEL_SCALR];
                 $criteria[] = ['envId' => null];
                 $criteria[] = ['accountId' => null];
                 break;
         }
         foreach (WebhookEndpoint::find($criteria) as $endpoint) {
             $configEndpoint = new WebhookConfigEndpoint();
             $configEndpoint->webhookId = $webhook->webhookId;
             $configEndpoint->setEndpoint($endpoint);
             $configEndpoint->save();
         }
     }
     //save events
     $allEvents = $this->getEventsList();
     $events = (array) $events;
     foreach (WebhookConfigEvent::findByWebhookId($webhook->webhookId) as $event) {
         $index = array_search($event->eventType, $events);
         if ($index === false) {
             if (isset($allEvents[$event->eventType])) {
                 //20486-rebundlecomplete-emails - we shouldn't remove some events(RebundleComplete...)
                 $event->delete();
             }
         } else {
             unset($events[$index]);
         }
     }
     foreach ($events as $event) {
         /*if (!isset(EVENT_TYPE::getScriptingEvents()[$event])) {
               continue;
           }*/
         $configEvent = new WebhookConfigEvent();
         $configEvent->webhookId = $webhook->webhookId;
         $configEvent->eventType = $event;
         $configEvent->save();
     }
     //save farms
     $farms = (array) $farms;
     if (empty($farms)) {
         $farms = [0];
     }
     foreach (WebhookConfigFarm::findByWebhookId($webhook->webhookId) as $farm) {
         $index = array_search($farm->farmId, $farms);
         if ($index === false) {
             $farm->delete();
         } else {
             unset($farms[$index]);
         }
     }
     foreach ($farms as $farmId) {
         $configFarm = new WebhookConfigFarm();
         $configFarm->webhookId = $webhook->webhookId;
         $configFarm->farmId = $farmId;
         $configFarm->save();
     }
     $endpoints = [];
     foreach ($webhook->getEndpoints() as $endpoint) {
         $endpoints[] = $endpoint->endpointId;
     }
     $events = [];
     foreach ($webhook->getEvents() as $event) {
         $events[] = $event->eventType;
     }
     $farms = [];
     foreach ($webhook->getFarms() as $farm) {
         if ($farm->farmId) {
             $farms[] = $farm->farmId;
         }
     }
     $this->response->success('Webhook successfully saved');
     $this->response->data(array('webhook' => array('webhookId' => $webhook->webhookId, 'name' => $webhook->name, 'postData' => $webhook->postData, 'timeout' => $webhook->timeout, 'attempts' => $webhook->attempts, 'skipPrivateGv' => $webhook->skipPrivateGv, 'endpoints' => $endpoints, 'events' => $events, 'farms' => $farms, 'scope' => $webhook->getScope())));
 }
示例#3
0
 public function xSaveAction()
 {
     $this->request->defineParams(array('endpoints' => array('type' => 'json'), 'action', 'events' => array('type' => 'json'), 'action', 'farms' => array('type' => 'json'), 'action'));
     if (!$this->request->getParam('webhookId')) {
         $webhook = new WebhookConfig();
         $webhook->level = WebhookConfig::LEVEL_ENVIRONMENT;
         $webhook->accountId = $this->getEnvironment()->clientId;
         $webhook->envId = $this->getEnvironmentId();
     } else {
         $webhook = WebhookConfig::findPk($this->request->getParam('webhookId'));
         if ($webhook->envId != $this->getEnvironmentId() || $webhook->accountId != $this->getEnvironment()->clientId) {
             throw new Scalr_Exception_Core('Insufficient permissions to edit webhook');
         }
     }
     $webhook->name = $this->request->getParam('name');
     $webhook->postData = $this->request->getParam('postData');
     $webhook->skipPrivateGv = $this->request->getParam('skipPrivateGv') == 'on' ? 1 : 0;
     $webhook->save();
     //save endpoints
     $endpoints = $this->getParam('endpoints');
     foreach (WebhookConfigEndpoint::findByWebhookId($webhook->webhookId) as $endpoint) {
         $index = array_search($endpoint->endpointId, $endpoints);
         if ($index === false) {
             $endpoint->delete();
         } else {
             unset($endpoints[$index]);
         }
     }
     if (!empty($endpoints)) {
         $endpoints = WebhookEndpoint::find(array(array('accountId' => $this->getEnvironment()->clientId), array('envId' => $this->getEnvironmentId()), array('endpointId' => array('$in' => $endpoints))));
         foreach ($endpoints as $endpoint) {
             $configEndpoint = new WebhookConfigEndpoint();
             $configEndpoint->webhookId = $webhook->webhookId;
             $configEndpoint->setEndpoint($endpoint);
             $configEndpoint->save();
         }
     }
     //save events
     $events = $this->getParam('events');
     $allEvents = $this->getEventsList();
     foreach (WebhookConfigEvent::findByWebhookId($webhook->webhookId) as $event) {
         $index = array_search($event->eventType, $events);
         if ($index === false) {
             if (isset($allEvents[$event->eventType])) {
                 //20486-rebundlecomplete-emails - we shouldn't remove some events(RebundleComplete...)
                 $event->delete();
             }
         } else {
             unset($events[$index]);
         }
     }
     foreach ($events as $event) {
         /*if (!isset(EVENT_TYPE::getScriptingEvents()[$event])) {
               continue;
           }*/
         $configEvent = new WebhookConfigEvent();
         $configEvent->webhookId = $webhook->webhookId;
         $configEvent->eventType = $event;
         $configEvent->save();
     }
     //save farms
     $farms = $this->getParam('farms');
     if (empty($farms)) {
         $farms = array(0);
     }
     foreach (WebhookConfigFarm::findByWebhookId($webhook->webhookId) as $farm) {
         $index = array_search($farm->farmId, $farms);
         if ($index === false) {
             $farm->delete();
         } else {
             unset($farms[$index]);
         }
     }
     foreach ($farms as $farmId) {
         $configFarm = new WebhookConfigFarm();
         $configFarm->webhookId = $webhook->webhookId;
         $configFarm->farmId = $farmId;
         $configFarm->save();
     }
     $endpoints = array();
     foreach ($webhook->getEndpoints() as $endpoint) {
         $endpoints[] = $endpoint->endpointId;
     }
     $events = array();
     foreach ($webhook->getEvents() as $event) {
         $events[] = $event->eventType;
     }
     $farms = array();
     foreach ($webhook->getFarms() as $farm) {
         if ($farm->farmId) {
             $farms[] = $farm->farmId;
         }
     }
     $this->response->data(array('webhook' => array('webhookId' => $webhook->webhookId, 'name' => $webhook->name, 'postData' => $webhook->postData, 'skipPrivateGv' => $webhook->skipPrivateGv, 'endpoints' => $endpoints, 'events' => $events, 'farms' => $farms)));
 }