/**
  * @param SS_HTTPRequest $request
  *
  * @return string
  */
 public function show(\SS_HTTPRequest $request)
 {
     $targetEnvironment = null;
     $targetEnvironmentId = $request->getVar('environmentId');
     if (!empty($targetEnvironmentId)) {
         $targetEnvironment = DNEnvironment::get()->byId((int) $targetEnvironmentId);
     }
     $refs = [];
     $prevDeploys = [];
     $uatEnvironment = $this->project->DNEnvironmentList()->filter('Usage', DNEnvironment::UAT)->first();
     $uatBuild = $uatEnvironment ? $uatEnvironment->CurrentBuild() : null;
     if ($uatBuild && $uatBuild->exists() && $targetEnvironment && $targetEnvironment->Usage === DNEnvironment::PRODUCTION) {
         $refs[self::REF_TYPE_FROM_UAT] = ['id' => self::REF_TYPE_FROM_UAT, 'label' => 'Promote the version currently on UAT', 'description' => 'Promote the version currently on UAT', 'promote_build' => $this->formatter->getDeploymentData($uatBuild)];
     }
     $refs[self::REF_TYPE_BRANCH] = ['id' => self::REF_TYPE_BRANCH, 'label' => 'Branch version', 'description' => 'Deploy the latest version of a branch', 'list' => $this->getGitBranches($this->project)];
     $refs[self::REF_TYPE_TAG] = ['id' => self::REF_TYPE_TAG, 'label' => 'Tag version', 'description' => 'Deploy a tagged release', 'list' => $this->getGitTags($this->project)];
     // @todo: the original was a tree that was keyed by environment, the
     // front-end dropdown needs to be changed to support that. brrrr.
     foreach ($this->getGitPrevDeploys($this->project) as $env) {
         foreach ($env as $deploy) {
             $prevDeploys[] = $deploy;
         }
     }
     $refs[self::REF_TYPE_PREVIOUS] = ['id' => self::REF_TYPE_PREVIOUS, 'label' => 'Redeploy a release that was previously deployed (to any environment)', 'description' => 'Deploy a previous release', 'list' => $prevDeploys];
     $refs[self::REF_TYPE_SHA] = ['id' => self::REF_TYPE_SHA, 'label' => 'Deploy a specific SHA', 'description' => 'Deploy a specific SHA'];
     $options = [];
     if ($targetEnvironment) {
         foreach ($targetEnvironment->getSupportedOptions() as $option) {
             $options[] = ['name' => $option->getName(), 'title' => $option->getTitle(), 'defaultValue' => $option->getDefaultValue()];
         }
     }
     // get the last time git fetch was run
     $lastFetchedDate = 'never';
     $lastFetchedAgo = null;
     $fetch = DNGitFetch::get()->filter(['ProjectID' => $this->project->ID, 'Status' => 'Finished'])->sort('LastEdited', 'DESC')->first();
     if ($fetch) {
         $lastFetchedDate = $fetch->obj('LastEdited')->Date();
         $lastFetchedAgo = $fetch->obj('LastEdited')->Ago();
     }
     return $this->getAPIResponse(['refs' => $refs, 'options' => $options, 'last_fetched_date' => $lastFetchedDate, 'last_fetched_ago' => $lastFetchedAgo], 200);
 }
Example #2
0
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     if ($this->Usage == 'Production' || $this->Usage == 'UAT') {
         $conflicting = DNEnvironment::get()->filter('ProjectID', $this->ProjectID)->filter('Usage', $this->Usage)->exclude('ID', $this->ID);
         foreach ($conflicting as $otherEnvironment) {
             $otherEnvironment->Usage = 'Unspecified';
             $otherEnvironment->write();
         }
     }
 }
Example #3
0
 /**
  * Provides a DNEnvironmentList of environments found in this project.
  */
 public function DNEnvironmentList()
 {
     return DNEnvironment::get()->filter('ProjectID', $this->ID)->setProjectID($this->ID);
 }
 /**
  * Validate a specific alert configuration from configuration YAML is correct.
  *
  * @param string $name
  * @param array $config
  * @param DNProject $project
  * @param DeploynautLogFile $log
  * @return boolean
  */
 public function validateAlert($name, $config, $project, $log)
 {
     // validate we have an environment set for the alert
     if (!isset($config['environment'])) {
         $log->write(sprintf('WARNING: Failed to configure alert "%s". Missing "environment" key in .alerts.yml. Skipped.', $name));
         return false;
     }
     // validate we have an environmentcheck suite name to check
     if (!isset($config['check_url'])) {
         $log->write(sprintf('WARNING: Failed to configure alert "%s". Missing "check_url" key in .alerts.yml. Skipped.', $name));
         return false;
     }
     // validate we have contacts for the alert
     if (!isset($config['contacts'])) {
         $log->write(sprintf('WARNING: Failed to configure alert "%s". Missing "contacts" key in .alerts.yml. Skipped.', $name));
         return false;
     }
     // validate that each value in the config is valid, build up a list of contacts we'll use later
     foreach ($config['contacts'] as $contactEmail) {
         // special case for ops
         if ($contactEmail == 'ops') {
             continue;
         }
         $contact = $project->AlertContacts()->filter('Email', $contactEmail)->first();
         if (!($contact && $contact->exists())) {
             $log->write(sprintf('WARNING: Failed to configure alert "%s". No such contact "%s". Skipped.', $name, $contactEmail));
             return false;
         }
     }
     // validate the environment specified in the alert actually exists
     if (!DNEnvironment::get()->filter('Name', $config['environment'])->first()) {
         $log->write(sprintf('WARNING: Failed to configure alert "%s". Invalid environment "%s" in .alerts.yml. Skipped.', $name, $config['environment']));
         return false;
     }
     return true;
 }
 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));
 }
Example #6
0
 /**
  * Return a dependent {@link DNEnvironment} based on this pipeline's dependent environment configuration.
  * @return DNEnvironment
  */
 public function getDependentEnvironment()
 {
     // dependent environment not available
     $projectName = $this->getConfigSetting('PipelineConfig', 'DependsOnProject');
     $environmentName = $this->getConfigSetting('PipelineConfig', 'DependsOnEnvironment');
     if (empty($projectName) || empty($environmentName)) {
         return null;
     }
     $project = DNProject::get()->filter('Name', $projectName)->first();
     if (!($project && $project->exists())) {
         throw new Exception(sprintf('Could not find dependent project "%s"', $projectName));
     }
     $environment = DNEnvironment::get()->filter(array('ProjectID' => $project->ID, 'Name' => $environmentName))->first();
     if (!($environment && $environment->exists())) {
         throw new Exception(sprintf('Could not find dependent environment "%s" in project "%s"', $environmentName, $projectName));
     }
     return $environment;
 }
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     if ($this->Usage === self::PRODUCTION || $this->Usage === self::UAT) {
         $conflicting = DNEnvironment::get()->filter('ProjectID', $this->ProjectID)->filter('Usage', $this->Usage)->exclude('ID', $this->ID);
         foreach ($conflicting as $otherEnvironment) {
             $otherEnvironment->Usage = self::UNSPECIFIED;
             $otherEnvironment->write();
         }
     }
 }