Ejemplo n.º 1
0
 /**
  * @param string $status
  * @global array $databaseConfig
  */
 protected function updateStatus($status)
 {
     global $databaseConfig;
     DB::connect($databaseConfig);
     $dnDeployment = DNDeployment::get()->byID($this->args['deploymentID']);
     $dnDeployment->Status = $status;
     $dnDeployment->write();
 }
Ejemplo n.º 2
0
 /**
  * Do the actual job by calling the appropiate backend
  */
 public function perform()
 {
     echo "[-] PingJob starting" . PHP_EOL;
     $log = new DeploynautLogFile($this->args['logfile']);
     $ping = DNDeployment::get()->byID($this->args['pingID']);
     $environment = $ping->Environment();
     $project = $environment->Project();
     $environment->Backend()->ping($environment, $log, $project);
 }
Ejemplo n.º 3
0
 /**
  *
  * @param string $environmentName
  * @return boolean True if this release has ever been deployed to the given environment
  */
 public function EverDeployedTo($environmentName)
 {
     $environments = $this->project->Environments()->filter('Name', $environmentName);
     if (!$environments->count()) {
         return false;
     }
     $environment = $environments->first();
     $deployments = DNDeployment::get()->filter('Status', 'Finished')->filter('EnvironmentID', $environment->ID);
     if ($deployments->count()) {
         return true;
     }
     return false;
 }
Ejemplo n.º 4
0
 /**
  * Get the current deployed build for this environment
  *
  * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be
  * a lot faster. I presume you came here because of the Project display template, which only needs a SHA.
  *
  * @return false|DNDeployment
  */
 public function CurrentBuild()
 {
     // The DeployHistory function is far too slow to use for this
     /** @var DNDeployment $deploy */
     $deploy = DNDeployment::get()->filter(array('EnvironmentID' => $this->ID, 'Status' => 'Finished'))->sort('LastEdited DESC')->first();
     if (!$deploy || !$deploy->SHA) {
         return false;
     }
     $repo = $this->Project()->getRepository();
     if (!$repo) {
         return $deploy;
     }
     try {
         $commit = $repo->getCommit($deploy->SHA);
         if ($commit) {
             $deploy->Message = Convert::raw2xml($commit->getMessage());
             $deploy->Committer = Convert::raw2xml($commit->getCommitterName());
             $deploy->CommitDate = $commit->getCommitterDate()->Format('d/m/Y g:ia');
             $deploy->Author = Convert::raw2xml($commit->getAuthorName());
             $deploy->AuthorDate = $commit->getAuthorDate()->Format('d/m/Y g:ia');
         }
         // We can't find this SHA, so we ignore adding a commit message to the deployment
     } catch (Exception $ex) {
     }
     return $deploy;
 }
Ejemplo n.º 5
0
 /**
  * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments
  * if an old deployment is stuck.
  *
  * @return DataList
  */
 public function runningDeployments()
 {
     return DNDeployment::get()->filter(['EnvironmentID' => $this->ID, 'Status' => ['Queued', 'Started'], 'Created:GreaterThan' => strtotime('-1 hour')]);
 }
Ejemplo n.º 6
0
 /**
  *
  * @param int $id
  * @return SS_HTTPResponse
  */
 protected function getDeploy($id)
 {
     $deploy = DNDeployment::get()->byID($id);
     if (!$deploy) {
         return $this->message('Deploy not found', 404);
     }
     $output = array('status' => $deploy->ResqueStatus(), 'message' => $deploy->LogContent());
     return $this->getAPIResponse($output);
 }
Ejemplo n.º 7
0
 /**
  * @param int $deploymentID
  * @return \DNDeployment
  */
 public function updateDeployment($deploymentID)
 {
     $deployment = \DNDeployment::get()->byId($deploymentID);
     $deployment->EnvironmentID = $this->environment->ID;
     $deployment->SHA = $this->getOption('sha');
     $deployment->RefType = $this->getOption('ref_type');
     $deployment->RefName = $this->getOption('ref_name');
     $deployment->Summary = $this->getOption('summary');
     $deployment->Title = $this->getOption('title');
     $deployment->Strategy = $this->toJSON();
     $deployment->DeployerID = \Member::currentUserID();
     $deployment->write();
     // re-get and return the deployment so we have the correct state
     return \DNDeployment::get()->byId($deployment->ID);
 }
Ejemplo n.º 8
0
 /**
  * Action - Get the latest deploy log
  *
  * @return string
  */
 public function deploylog(SS_HTTPRequest $request)
 {
     $params = $request->params();
     $deployment = DNDeployment::get()->byId($params['Identifier']);
     if (!$deployment || !$deployment->ID) {
         throw new SS_HTTPResponse_Exception('Deployment not found', 404);
     }
     if (!$deployment->canView()) {
         return Security::permissionFailure();
     }
     $environment = $deployment->Environment();
     $project = $environment->Project();
     if ($environment->Name != $params['Environment']) {
         throw new LogicException("Environment in URL doesn't match this deploy");
     }
     if ($project->Name != $params['Project']) {
         throw new LogicException("Project in URL doesn't match this deploy");
     }
     $log = $deployment->log();
     if ($log->exists()) {
         $content = $log->content();
     } else {
         $content = 'Waiting for action to start';
     }
     $sendJSON = strpos($request->getHeader('Accept'), 'application/json') !== false || $request->getExtension() == 'json';
     $content = preg_replace('/(?:(?:\\r\\n|\\r|\\n)\\s*){2}/s', "\n", $content);
     if ($sendJSON) {
         $this->response->addHeader("Content-type", "application/json");
         return json_encode(array('status' => $deployment->ResqueStatus(), 'content' => $content));
     } else {
         $this->response->addHeader("Content-type", "text/plain");
         return $content;
     }
 }
Ejemplo n.º 9
0
 /**
  * Get the current deployed build for this environment
  *
  * Dear people of the future: If you are looking to optimize this, simply create a CurrentBuildSHA(), which can be a lot faster.
  * I presume you came here because of the Project display template, which only needs a SHA.
  *
  * @return string
  */
 public function CurrentBuild()
 {
     // The DeployHistory function is far too slow to use for this
     $deploy = DNDeployment::get()->filter(array('EnvironmentID' => $this->ID, 'Status' => 'Finished'))->sort('LastEdited DESC')->first();
     if (!$deploy || !$deploy->SHA) {
         return false;
     }
     $repo = $this->Project()->getRepository();
     if (!$repo) {
         return $deploy;
     }
     try {
         $commit = $repo->getCommit($deploy->SHA);
         if ($commit) {
             $deploy->Message = Convert::raw2xml($commit->getMessage());
         }
         // We can't find this SHA, so we ignore adding a commit message to the deployment
     } catch (Gitonomy\Git\Exception\ReferenceNotFoundException $ex) {
     }
     return $deploy;
 }
Ejemplo n.º 10
0
 /**
  * @param string $status Transition
  * @global array $databaseConfig
  */
 protected function updateStatus($status)
 {
     global $databaseConfig;
     DB::connect($databaseConfig);
     $deployment = DNDeployment::get()->byID($this->args['deploymentID']);
     $deployment->getMachine()->apply($status);
 }
Ejemplo n.º 11
0
 /**
  * @param \SS_HTTPRequest $request
  * @return \SS_HTTPResponse
  */
 public function abort(\SS_HTTPRequest $request)
 {
     if ($request->httpMethod() !== 'POST') {
         return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
     }
     $this->checkSecurityToken();
     if (!self::can_abort_deployment($this->environment)) {
         return $this->getAPIResponse(['message' => 'You are not authorised to perform this action'], 403);
     }
     $deployment = \DNDeployment::get()->byId($request->postVar('id'));
     $errorResponse = $this->validateDeployment($deployment);
     if ($errorResponse instanceof \SS_HTTPResponse) {
         return $errorResponse;
     }
     try {
         $deployment->getMachine()->apply(\DNDeployment::TR_ABORT);
     } catch (\Exception $e) {
         return $this->getAPIResponse(['message' => $e->getMessage()], 400);
     }
     return $this->sendResponse(['message' => 'Deployment abort request successfully received', 'deployment' => $this->formatter->getDeploymentData($deployment)], 200);
 }
Ejemplo n.º 12
0
 /**
  * @deprecated 2.0.0 - moved to DeployDispatcher
  *
  * @param \SS_HTTPRequest $request
  *
  * @return string
  * @throws SS_HTTPResponse_Exception
  */
 public function abortDeploy(\SS_HTTPRequest $request)
 {
     $params = $request->params();
     $deployment = DNDeployment::get()->byId($params['Identifier']);
     if (!$deployment || !$deployment->ID) {
         throw new SS_HTTPResponse_Exception('Deployment not found', 404);
     }
     if (!$deployment->canView()) {
         return Security::permissionFailure();
     }
     // For now restrict to ADMINs only.
     if (!Permission::check('ADMIN')) {
         return Security::permissionFailure();
     }
     $environment = $deployment->Environment();
     $project = $environment->Project();
     if ($environment->Name != $params['Environment']) {
         throw new LogicException("Environment in URL doesn't match this deploy");
     }
     if ($project->Name != $params['Project']) {
         throw new LogicException("Project in URL doesn't match this deploy");
     }
     if (!in_array($deployment->Status, ['Queued', 'Deploying', 'Aborting'])) {
         throw new LogicException(sprintf("Cannot abort from %s state.", $deployment->Status));
     }
     $deployment->getMachine()->apply(DNDeployment::TR_ABORT);
     return $this->sendResponse($deployment->ResqueStatus(), []);
 }
 public function DeployHistory()
 {
     return DNDeployment::get()->filter('EnvironmentID', $this->ID)->sort('LastEdited DESC');
 }
Ejemplo n.º 14
0
 /**
  * A history of all builds deployed to this environment
  *
  * @return ArrayList
  */
 public function DeployHistory()
 {
     $history = DNDeployment::get()->filter('EnvironmentID', $this->ID)->sort('LastEdited DESC');
     $repo = $this->Project()->getRepository();
     if (!$repo) {
         return $history;
     }
     $ammendedHistory = new ArrayList();
     foreach ($history as $deploy) {
         if (!$deploy->SHA) {
             continue;
         }
         try {
             $commit = $repo->getCommit($deploy->SHA);
             if ($commit) {
                 $deploy->Message = Convert::raw2xml($commit->getMessage());
             }
             // We can't find this SHA, so we ignore adding a commit message to the deployment
         } catch (Gitonomy\Git\Exception\ReferenceNotFoundException $ex) {
         }
         $ammendedHistory->push($deploy);
     }
     return $ammendedHistory;
 }
Ejemplo n.º 15
0
 /**
  * @param \SS_HTTPRequest $request
  * @return \SS_HTTPResponse
  */
 public function reject(\SS_HTTPRequest $request)
 {
     if ($request->httpMethod() !== 'POST') {
         return $this->getAPIResponse(['message' => 'Method not allowed, requires POST'], 405);
     }
     $this->checkSecurityToken();
     $deployment = \DNDeployment::get()->byId($request->postVar('id'));
     $errorResponse = $this->validateDeployment($deployment);
     if ($errorResponse instanceof \SS_HTTPResponse) {
         return $errorResponse;
     }
     // reject permissions are the same as can approve
     if (!self::can_approve($this->environment)) {
         return $this->getAPIResponse(['message' => 'You are not authorised to reject this deployment'], 403);
     }
     // if the current user is not the person who was selected for approval on submit, but they got
     // here because they still have permission, then change the approver to the current user
     if (\Member::currentUserID() !== $deployment->ApproverID) {
         $deployment->ApproverID = \Member::currentUserID();
     }
     if ($request->postVar('rejected_reason')) {
         $deployment->RejectedReason = $request->postVar('rejected_reason');
     }
     try {
         $deployment->getMachine()->apply(\DNDeployment::TR_REJECT);
     } catch (\Exception $e) {
         return $this->getAPIResponse(['message' => $e->getMessage()], 400);
     }
     return $this->getAPIResponse(['message' => 'Deployment request has been rejected', 'deployment' => $this->formatter->getDeploymentData($deployment)], 200);
 }
 public function run($request)
 {
     $args = $request->getVar('args');
     $dryRun = $args && in_array('--dry-run', $args);
     $log = function ($message) {
         $message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
         echo $message . PHP_EOL;
     };
     if (!Director::is_cli()) {
         $log('This task must be run under the command line');
         return;
     }
     if ($dryRun) {
         $log('Running in dry-run mode. No data will be deleted');
     }
     $count = 0;
     foreach (DNEnvironment::get() as $environment) {
         $project = $environment->Project();
         if (!$project || !$project->exists()) {
             $log(sprintf('Environment (ID %s, Name: %s, Created: %s) is linked to a non-existent project. Deleting', $environment->ID, $environment->Name, $environment->Created));
             if (!$dryRun) {
                 $environment->delete();
                 $environment->destroy();
             }
             $count++;
         }
     }
     foreach (DNDeployment::get() as $deployment) {
         $environment = $deployment->Environment();
         if (!$environment || !$environment->exists()) {
             $log(sprintf('Deployment (ID %s, Created: %s) is linked to a non-existent environment. Deleting', $deployment->ID, $deployment->Created));
             if (!$dryRun) {
                 $deployment->delete();
                 $deployment->destroy();
             }
             $count++;
         }
     }
     foreach (DNDataTransfer::get() as $transfer) {
         $environment = $transfer->Environment();
         if (!$environment || !$environment->exists()) {
             $log(sprintf('Data transfer (ID %s, Created: %s) is linked to a non-existent environment. Deleting', $transfer->ID, $transfer->Created));
             if (!$dryRun) {
                 $transfer->delete();
                 $transfer->destroy();
             }
             $count++;
         }
     }
     foreach (DNDataArchive::get() as $archive) {
         $environment = $archive->Environment();
         if (!$environment || !$environment->exists()) {
             $log(sprintf('Archive (ID %s, Created: %s) is linked to a non-existent environment. Deleting', $archive->ID, $archive->Created));
             if (!$dryRun) {
                 $archive->delete();
                 $archive->destroy();
             }
             $count++;
         }
     }
     foreach (DNGitFetch::get() as $fetch) {
         $project = $fetch->Project();
         if (!$project || !$project->exists()) {
             $log(sprintf('Git fetch (ID %s, Created: %s) is linked to a non-existent project. Deleting', $fetch->ID, $fetch->Created));
             if (!$dryRun) {
                 $fetch->delete();
                 $fetch->destroy();
             }
             $count++;
         }
     }
     foreach (DNPing::get() as $ping) {
         $environment = $ping->Environment();
         if (!$environment || !$environment->exists()) {
             $log(sprintf('Ping (ID %s, Created: %s) is linked to a non-existent environment. Deleting', $ping->ID, $ping->Created));
             if (!$dryRun) {
                 $ping->delete();
                 $ping->destroy();
             }
             $count++;
         }
     }
     $log(sprintf('Finished. Processed %s records', $count));
 }
Ejemplo n.º 17
0
 /**
  * Action - Get the latest deploy log
  *
  * @param SS_HTTPRequest $request
  *
  * @return string
  * @throws SS_HTTPResponse_Exception
  */
 public function deploylog(SS_HTTPRequest $request)
 {
     $params = $request->params();
     $deployment = DNDeployment::get()->byId($params['Identifier']);
     if (!$deployment || !$deployment->ID) {
         throw new SS_HTTPResponse_Exception('Deployment not found', 404);
     }
     if (!$deployment->canView()) {
         return Security::permissionFailure();
     }
     $environment = $deployment->Environment();
     $project = $environment->Project();
     if ($environment->Name != $params['Environment']) {
         throw new LogicException("Environment in URL doesn't match this deploy");
     }
     if ($project->Name != $params['Project']) {
         throw new LogicException("Project in URL doesn't match this deploy");
     }
     $log = $deployment->log();
     if ($log->exists()) {
         $content = $log->content();
     } else {
         $content = 'Waiting for action to start';
     }
     return $this->sendResponse($deployment->ResqueStatus(), $content);
 }
Ejemplo n.º 18
0
 /**
  * Fetchs all deployments in progress. Limits to 1 hour to prevent deployments
  * if an old deployment is stuck.
  *
  * @return DataList
  */
 public function runningDeployments()
 {
     return DNDeployment::get()->filter(['EnvironmentID' => $this->ID, 'State' => [DNDeployment::STATE_QUEUED, DNDeployment::STATE_DEPLOYING, DNDeployment::STATE_ABORTING], 'Created:GreaterThan' => strtotime('-1 hour')]);
 }