Пример #1
0
 /**
  * Forks specified script into new script
  *
  * @param string             $name  New script name
  * @param Scalr_Account_User $user  User performs a fork
  * @param int                $envId Environment of the new script
  *
  * @return Script Forked script
  * @throws \Exception
  */
 public function fork($name, Scalr_Account_User $user, $envId = null)
 {
     $script = new static();
     $script->name = $name;
     $script->description = $this->description;
     $script->os = $this->os;
     $script->isSync = $this->isSync;
     $script->allowScriptParameters = $this->allowScriptParameters;
     $script->timeout = $this->timeout;
     $script->accountId = $user->getAccountId() ?: NULL;
     $script->envId = $envId ? $envId : $this->envId;
     $script->createdById = $user->getId();
     $script->createdByEmail = $user->getEmail();
     $script->save();
     $version = new ScriptVersion();
     $version->scriptId = $script->id;
     $version->changedById = $user->getId();
     $version->changedByEmail = $user->getEmail();
     $version->content = $this->getLatestVersion()->content;
     $version->version = 1;
     $version->save();
     return $script;
 }
Пример #2
0
 /**
  * @param $name
  * @param \Scalr_Account_User $user
  * @return Script
  */
 public function fork($name, \Scalr_Account_User $user)
 {
     $script = new self();
     $script->name = $name;
     $script->description = $this->description;
     $script->os = $this->os;
     $script->isSync = $this->isSync;
     $script->timeout = $this->timeout;
     $script->accountId = $user->getAccountId() ? $user->getAccountId() : NULL;
     $script->envId = $this->envId;
     $script->createdById = $user->getId();
     $script->createdByEmail = $user->getEmail();
     $script->save();
     $version = new ScriptVersion();
     $version->scriptId = $script->id;
     $version->changedById = $user->getId();
     $version->changedByEmail = $user->getEmail();
     $version->content = $this->getLatestVersion()->content;
     $version->version = 1;
     $version->save();
     return $script;
 }
Пример #3
0
 /**
  * @param int $id
  * @param string $name
  * @param string $description
  * @param int $isSync
  * @param bool $allowScriptParameters
  * @param int $envId optional
  * @param int $timeout optional
  * @param int $version
  * @param RawData $content
  * @param string $tags
  * @param string $uploadType optional
  * @param string $uploadUrl optional
  * @param FileUploadData $uploadFile optional
  * @param bool $checkScriptParameters optional
  * @throws Scalr_UI_Exception_NotFound
  * @throws Scalr_Exception_InsufficientPermissions
  * @throws Exception
  */
 public function xSaveAction($id, $name, $description, $isSync = 0, $allowScriptParameters = false, $envId = NULL, $timeout = NULL, $version, RawData $content, $tags, $uploadType = NULL, $uploadUrl = NULL, FileUploadData $uploadFile = NULL, $checkScriptParameters = false)
 {
     $this->request->restrictAccess('SCRIPTS', 'MANAGE');
     $validator = new Validator();
     $validator->validate($name, 'name', Validator::NOEMPTY);
     if ($uploadType && $uploadType == 'URL') {
         $validator->validate($uploadUrl, 'uploadUrl', Validator::URL);
         if (!$validator->isValid($this->response)) {
             return;
         }
     }
     if ($uploadType) {
         $content = false;
         if ($uploadType == 'URL') {
             $content = @file_get_contents($uploadUrl);
             $validator->validate($content, 'uploadUrl', Validator::NOEMPTY, [], 'Invalid source');
         } else {
             if ($uploadType == 'File') {
                 $content = $uploadFile;
                 $validator->validate($content, 'uploadFile', Validator::NOEMPTY, [], 'Invalid source');
             } else {
                 $validator->addError('uploadType', 'Invalid source for script');
             }
         }
     }
     $envId = $this->getEnvironmentId(true);
     $content = str_replace("\r\n", "\n", $content);
     $tagsResult = [];
     foreach (explode(',', $tags) as $t) {
         $t = trim($t);
         if ($t) {
             if (!preg_match('/^[a-zA-Z0-9-]{3,10}$/', $t)) {
                 $validator->addError('tags', sprintf('Invalid name for tag: %s', $t));
             }
             $tagsResult[] = $t;
         }
     }
     $tags = $tagsResult;
     $criteria = [];
     $criteria[] = ['name' => $name];
     if ($id) {
         $criteria[] = ['id' => ['$ne' => $id]];
     }
     switch ($this->request->getScope()) {
         case Script::SCOPE_ENVIRONMENT:
             $criteria[] = ['envId' => $envId];
             $criteria[] = ['accountId' => $this->user->getAccountId()];
             break;
         case Script::SCOPE_ACCOUNT:
             $criteria[] = ['envId' => null];
             $criteria[] = ['accountId' => $this->user->getAccountId()];
             break;
         case Script::SCOPE_SCALR:
             $criteria[] = ['envId' => null];
             $criteria[] = ['accountId' => null];
             break;
     }
     if (Script::findOne($criteria)) {
         $validator->addError('name', 'Script name must be unique within current scope');
     }
     if (!$validator->isValid($this->response)) {
         return;
     }
     /* @var $script Script */
     if ($id) {
         $script = Script::findPk($id);
         if (!$script) {
             throw new Scalr_UI_Exception_NotFound();
         }
         $script->checkPermission($this->user, $envId);
         if (!$script->accountId && $this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
             throw new Scalr_Exception_InsufficientPermissions();
         }
         if (!$script->envId && $this->request->getScope() == ScopeInterface::SCOPE_ENVIRONMENT) {
             throw new Scalr_Exception_InsufficientPermissions();
         }
     } else {
         $script = new Script();
         $script->accountId = $this->user->getAccountId() ?: NULL;
         $script->createdById = $this->user->getId();
         $script->createdByEmail = $this->user->getEmail();
         $script->envId = $envId;
         $version = 1;
     }
     //check variables in script content
     if (!$id && $checkScriptParameters && !$allowScriptParameters) {
         $scriptHasParameters = Script::hasVariables($content);
         if (!$scriptHasParameters) {
             /* @var $scriptVersion ScriptVersion */
             foreach ($script->getVersions() as $scriptVersion) {
                 if ($scriptVersion->version != $version) {
                     $scriptHasParameters = Script::hasVariables($scriptVersion->content);
                     if ($scriptHasParameters) {
                         break;
                     }
                 }
             }
         }
         if ($scriptHasParameters) {
             $this->response->data(['showScriptParametersConfirmation' => true]);
             $this->response->failure();
             return;
         }
     }
     $script->name = $name;
     $script->description = $description;
     $script->timeout = $timeout ? $timeout : NULL;
     $script->isSync = $isSync == 1 ? 1 : 0;
     $script->allowScriptParameters = $allowScriptParameters ? 1 : 0;
     $script->os = !strncmp($content, '#!cmd', strlen('#!cmd')) || !strncmp($content, '#!powershell', strlen('#!powershell')) ? Script::OS_WINDOWS : Script::OS_LINUX;
     $script->save();
     $scriptVersion = NULL;
     if ($version) {
         $scriptVersion = $script->getVersion($version);
     }
     if (!$scriptVersion && $script->getLatestVersion()->content !== $content) {
         $scriptVersion = new ScriptVersion();
         $scriptVersion->scriptId = $script->id;
         $scriptVersion->version = $script->getLatestVersion()->version + 1;
     }
     if ($scriptVersion) {
         $scriptVersion->changedById = $this->user->getId();
         $scriptVersion->changedByEmail = $this->user->getEmail();
         $scriptVersion->content = $content;
         $scriptVersion->save();
     }
     if ($this->user->getAccountId()) {
         Tag::setTags($tags, $this->user->getAccountId(), Tag::RESOURCE_SCRIPT, $script->id);
     }
     $this->response->success('Script successfully saved');
     $this->response->data(['script' => array_merge($this->getScript($script), $this->getScriptInfo($script))]);
 }
Пример #4
0
 /**
  * @param int $id
  * @param string $name
  * @param string $description
  * @param int $isSync
  * @param int $envId optional
  * @param int $timeout optional
  * @param int $version
  * @param RawData $content
  * @param string $tags
  * @param string $uploadType optional
  * @param string $uploadUrl optional
  * @param FileUploadData $uploadFile optional
  * @throws Scalr_UI_Exception_NotFound
  * @throws Scalr_Exception_InsufficientPermissions
  * @throws Exception
  */
 public function xSaveAction($id, $name, $description, $isSync = 0, $envId = 2, $timeout = NULL, $version, RawData $content, $tags, $uploadType = NULL, $uploadUrl = NULL, FileUploadData $uploadFile = NULL)
 {
     $this->request->restrictAccess(Acl::RESOURCE_ADMINISTRATION_SCRIPTS, Acl::PERM_ADMINISTRATION_SCRIPTS_MANAGE);
     $validator = new Validator();
     $validator->validate($name, 'name', Validator::NOEMPTY);
     if ($uploadType) {
         $content = false;
         if ($uploadType == 'URL') {
             $content = @file_get_contents($uploadUrl);
             $validator->validate($content, 'uploadUrl', Validator::NOEMPTY, [], 'Invalid source');
         } else {
             if ($uploadType == 'File') {
                 $content = $uploadFile;
                 $validator->validate($content, 'uploadFile', Validator::NOEMPTY, [], 'Invalid source');
             } else {
                 $validator->addError('uploadType', 'Invalid source for script');
             }
         }
     }
     $content = str_replace("\r\n", "\n", $content);
     $tagsResult = [];
     foreach (explode(',', $tags) as $t) {
         $t = trim($t);
         if ($t) {
             if (!preg_match('/^[a-zA-Z0-9-]{3,10}$/', $t)) {
                 $validator->addError('tags', sprintf('Invalid name for tag: %s', $t));
             }
             $tagsResult[] = $t;
         }
     }
     $tags = $tagsResult;
     if (!$validator->isValid($this->response)) {
         return;
     }
     /* @var Script $script */
     if ($id) {
         $script = Script::findPk($id);
         if (!$script) {
             throw new Scalr_UI_Exception_NotFound();
         }
         $script->checkPermission($this->user, $this->getEnvironmentId(true));
         if (!$script->accountId && $this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
             throw new Scalr_Exception_InsufficientPermissions();
         }
     } else {
         $script = new Script();
         $script->accountId = $this->user->getAccountId() ? $this->user->getAccountId() : NULL;
         $script->createdById = $this->user->getId();
         $script->createdByEmail = $this->user->getEmail();
         $version = 1;
     }
     if ($this->user->isScalrAdmin()) {
         $envId = NULL;
     } else {
         if (!in_array($envId, array_map(function ($e) {
             return $e['id'];
         }, $this->user->getEnvironments()))) {
             $envId = NULL;
         }
     }
     $script->name = $name;
     $script->description = $description;
     $script->timeout = $timeout ? $timeout : NULL;
     $script->isSync = $isSync == 1 ? 1 : 0;
     $script->envId = $envId;
     $script->os = !strncmp($content, '#!cmd', strlen('#!cmd')) || !strncmp($content, '#!powershell', strlen('#!powershell')) ? Script::OS_WINDOWS : Script::OS_LINUX;
     $script->save();
     $scriptVersion = NULL;
     if ($version) {
         $scriptVersion = $script->getVersion($version);
     }
     if (!$scriptVersion && $script->getLatestVersion()->content !== $content) {
         $scriptVersion = new ScriptVersion();
         $scriptVersion->scriptId = $script->id;
         $scriptVersion->version = $script->getLatestVersion()->version + 1;
     }
     if ($scriptVersion) {
         $scriptVersion->changedById = $this->user->getId();
         $scriptVersion->changedByEmail = $this->user->getEmail();
         $scriptVersion->content = $content;
         $scriptVersion->save();
     }
     if ($this->user->getAccountId()) {
         Tag::setTags($tags, $this->user->getAccountId(), Tag::RESOURCE_SCRIPT, $script->id);
     }
     $this->response->success('Script successfully saved');
 }