Пример #1
0
 /**
  * @param int $servId
  * @param string $chefEnv
  */
 private function getChefClient($servId)
 {
     $criteria[] = ['id' => $servId];
     if ($this->user->isAdmin()) {
         $criteria[] = ['accountId' => null];
         $criteria[] = ['envId' => null];
         $criteria[] = ['level' => ChefServer::LEVEL_SCALR];
     } else {
         $criteria[] = ['$or' => [['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => $this->getEnvironmentId(true)], ['level' => ChefServer::LEVEL_ENVIRONMENT]]], ['$and' => [['accountId' => $this->user->getAccountId()], ['envId' => null], ['level' => ChefServer::LEVEL_ACCOUNT]]], ['$and' => [['accountId' => null], ['envId' => null], ['level' => ChefServer::LEVEL_SCALR]]]]];
     }
     $server = ChefServer::findOne($criteria);
     if (!$server) {
         throw new Scalr_Exception_InsufficientPermissions();
     }
     return Scalr_Service_Chef_Client::getChef($server->url, $server->username, $this->getCrypto()->decrypt($server->authKey));
 }
Пример #2
0
 /**
  * @param int $id
  * @param string $url
  * @param string $username
  * @param string $authKey
  * @param string $vUsername
  * @param string $vAuthKey
  * @throws Exception
  */
 public function xSaveAction($id, $url, $username, $authKey, $vUsername, $vAuthKey)
 {
     if (!$this->canManageServers()) {
         throw new Scalr_Exception_InsufficientPermissions();
     }
     if (!$id) {
         $server = new ChefServer();
         $server->setScope($this->request->getScope(), $this->user->getAccountId(), $this->getEnvironmentId(true));
     } else {
         $server = ChefServer::findPk($id);
         if (!$this->canEditServer($server)) {
             throw new Scalr_Exception_Core('Insufficient permissions to edit chef server at this scope');
         }
     }
     $validator = new Validator();
     $validator->validate($url, 'url', Validator::NOEMPTY);
     //check url unique within current scope
     $criteria = [];
     $criteria[] = ['url' => $url];
     if ($server->id) {
         $criteria[] = ['id' => ['$ne' => $server->id]];
     }
     switch ($this->request->getScope()) {
         case ChefServer::SCOPE_ENVIRONMENT:
             $criteria[] = ['level' => ChefServer::LEVEL_ENVIRONMENT];
             $criteria[] = ['envId' => $server->envId];
             $criteria[] = ['accountId' => $server->accountId];
             break;
         case ChefServer::SCOPE_ACCOUNT:
             $criteria[] = ['level' => ChefServer::LEVEL_ACCOUNT];
             $criteria[] = ['envId' => null];
             $criteria[] = ['accountId' => $server->accountId];
             break;
         case ChefServer::SCOPE_SCALR:
             $criteria[] = ['level' => ChefServer::LEVEL_SCALR];
             $criteria[] = ['envId' => null];
             $criteria[] = ['accountId' => null];
             break;
     }
     if (ChefServer::findOne($criteria)) {
         $validator->addError('url', 'Url must be unique within current scope');
     }
     if (!$validator->isValid($this->response)) {
         return;
     }
     $authKey = str_replace("\r\n", "\n", $authKey);
     $vAuthKey = str_replace("\r\n", "\n", $vAuthKey);
     $server->url = $url;
     $server->username = $username;
     $server->vUsername = $vUsername;
     $server->authKey = $this->getCrypto()->encrypt($authKey);
     $server->vAuthKey = $this->getCrypto()->encrypt($vAuthKey);
     $chef = Scalr_Service_Chef_Client::getChef($server->url, $server->username, $authKey);
     $response = $chef->listCookbooks();
     $chef2 = Scalr_Service_Chef_Client::getChef($server->url, $server->vUsername, $vAuthKey);
     $clientName = 'scalr-temp-client-' . rand(10000, 99999);
     $response = $chef2->createClient($clientName);
     $response2 = $chef->removeClient($clientName);
     $server->save();
     $this->response->data(array('server' => $this->getServerData($server)));
     $this->response->success('Chef server successfully saved');
 }