Beispiel #1
0
 /**
  * 
  * @return SS_HTTPResponse
  */
 protected function createFetch()
 {
     $fetch = new DNGitFetch();
     $fetch->ProjectID = $this->record->ID;
     $fetch->write();
     $fetch->start();
     $location = Director::absoluteBaseURL() . $this->Link() . '/fetch/' . $fetch->ID;
     $output = array('message' => 'Ping queued as job ' . $fetch->ResqueToken, 'href' => $location);
     $response = $this->getAPIResponse($output);
     $response->setStatusCode(201);
     $response->addHeader('Location', $location);
     return $response;
 }
Beispiel #2
0
 /**
  * @param SS_HTTPRequest $request
  *
  * @return SS_HTTPResponse|string
  */
 public function gitRevisions(SS_HTTPRequest $request)
 {
     // Performs canView permission check by limiting visible projects
     $project = $this->getCurrentProject();
     if (!$project) {
         return $this->project404Response();
     }
     // Performs canView permission check by limiting visible projects
     $env = $this->getCurrentEnvironment($project);
     if (!$env) {
         return $this->environment404Response();
     }
     // For now only permit advanced options on one environment type, because we hacked the "full-deploy"
     // checkbox in. Other environments such as the fast or capistrano one wouldn't know what to do with it.
     if (get_class($env) === 'RainforestEnvironment') {
         $advanced = Permission::check('DEPLOYNAUT_ADVANCED_DEPLOY_OPTIONS') ? 'true' : 'false';
     } else {
         $advanced = 'false';
     }
     $tabs = array();
     $id = 0;
     $data = array('id' => ++$id, 'name' => 'Deploy the latest version of a branch', 'field_type' => 'dropdown', 'field_label' => 'Choose a branch', 'field_id' => 'branch', 'field_data' => array(), 'advanced_opts' => $advanced);
     foreach ($project->DNBranchList() as $branch) {
         $sha = $branch->SHA();
         $name = $branch->Name();
         $branchValue = sprintf("%s (%s, %s old)", $name, substr($sha, 0, 8), $branch->LastUpdated()->TimeDiff());
         $data['field_data'][] = array('id' => $sha, 'text' => $branchValue);
     }
     $tabs[] = $data;
     $data = array('id' => ++$id, 'name' => 'Deploy a tagged release', 'field_type' => 'dropdown', 'field_label' => 'Choose a tag', 'field_id' => 'tag', 'field_data' => array(), 'advanced_opts' => $advanced);
     foreach ($project->DNTagList()->setLimit(null) as $tag) {
         $name = $tag->Name();
         $data['field_data'][] = array('id' => $tag->SHA(), 'text' => sprintf("%s", $name));
     }
     // show newest tags first.
     $data['field_data'] = array_reverse($data['field_data']);
     $tabs[] = $data;
     // Past deployments
     $data = array('id' => ++$id, 'name' => 'Redeploy a release that was previously deployed (to any environment)', 'field_type' => 'dropdown', 'field_label' => 'Choose a previously deployed release', 'field_id' => 'release', 'field_data' => array(), 'advanced_opts' => $advanced);
     // We are aiming at the format:
     // [{text: 'optgroup text', children: [{id: '<sha>', text: '<inner text>'}]}]
     $redeploy = array();
     foreach ($project->DNEnvironmentList() as $dnEnvironment) {
         $envName = $dnEnvironment->Name;
         $perEnvDeploys = array();
         foreach ($dnEnvironment->DeployHistory() as $deploy) {
             $sha = $deploy->SHA;
             // Check if exists to make sure the newest deployment date is used.
             if (!isset($perEnvDeploys[$sha])) {
                 $pastValue = sprintf("%s (deployed %s)", substr($sha, 0, 8), $deploy->obj('LastEdited')->Ago());
                 $perEnvDeploys[$sha] = array('id' => $sha, 'text' => $pastValue);
             }
         }
         if (!empty($perEnvDeploys)) {
             $redeploy[$envName] = array_values($perEnvDeploys);
         }
     }
     // Convert the array to the frontend format (i.e. keyed to regular array)
     foreach ($redeploy as $env => $descr) {
         $data['field_data'][] = array('text' => $env, 'children' => $descr);
     }
     $tabs[] = $data;
     $data = array('id' => ++$id, 'name' => 'Deploy a specific SHA', 'field_type' => 'textfield', 'field_label' => 'Choose a SHA', 'field_id' => 'SHA', 'field_data' => array(), 'advanced_opts' => $advanced);
     $tabs[] = $data;
     // get the last time git fetch was run
     $lastFetched = 'never';
     $fetch = DNGitFetch::get()->filter('ProjectID', $project->ID)->sort('LastEdited', 'DESC')->first();
     if ($fetch) {
         $lastFetched = $fetch->dbObject('LastEdited')->Ago();
     }
     $data = array('Tabs' => $tabs, 'last_fetched' => $lastFetched);
     return json_encode($data, JSON_PRETTY_PRINT);
 }
 /**
  * Setup a job to clone a git repository.
  * @return string resque token
  */
 public function cloneRepo()
 {
     // Avoid this being called multiple times in the same request
     if (!isset(self::$has_cloned_cache[$this->ID])) {
         $fetch = DNGitFetch::create();
         $fetch->ProjectID = $this->ID;
         $fetch->write();
         // passing true here tells DNGitFetch to force a git clone, otherwise
         // it will just update the repo if it already exists. We want to ensure
         // we're always cloning a new repo in this case, as the git URL may have changed.
         $fetch->start(true);
         self::$has_cloned_cache[$this->ID] = true;
     }
 }
Beispiel #4
0
 /**
  * @param string $status
  * @global array $databaseConfig
  */
 protected function updateStatus($status)
 {
     global $databaseConfig;
     DB::connect($databaseConfig);
     if (!empty($this->args['fetchID'])) {
         $fetch = DNGitFetch::get()->byID($this->args['fetchID']);
         if ($fetch && $fetch->exists()) {
             $fetch->Status = $status;
             $fetch->write();
         }
     }
 }
 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));
 }
 /**
  * @return SS_HTTPResponse
  */
 protected function createUpdate()
 {
     /** @var DNGitFetch $fetch */
     $fetch = DNGitFetch::create();
     $fetch->ProjectID = $this->project->ID;
     $fetch->write();
     $fetch->start();
     $location = Director::absoluteBaseURL() . $this->Link() . '/update/' . $fetch->ID;
     $output = ['message' => 'git fetch has been queued', 'id' => $fetch->ID, 'location' => $location];
     $response = $this->getAPIResponse($output, 201);
     $response->addHeader('Location', $location);
     return $response;
 }
Beispiel #7
0
 /**
  * @deprecated 2.0.0 - moved to GitDispatcher
  *
  * @param \SS_HTTPRequest $request
  *
  * @return SS_HTTPResponse|string
  */
 public function gitRevisions(\SS_HTTPRequest $request)
 {
     // Performs canView permission check by limiting visible projects
     $project = $this->getCurrentProject();
     if (!$project) {
         return $this->project404Response();
     }
     // Performs canView permission check by limiting visible projects
     $env = $this->getCurrentEnvironment($project);
     if (!$env) {
         return $this->environment404Response();
     }
     $options = [];
     foreach ($env->getSupportedOptions() as $option) {
         $options[] = ['name' => $option->getName(), 'title' => $option->getTitle(), 'defaultValue' => $option->getDefaultValue()];
     }
     $tabs = [];
     $id = 0;
     $data = ['id' => ++$id, 'name' => 'Deploy the latest version of a branch', 'field_type' => 'dropdown', 'field_label' => 'Choose a branch', 'field_id' => 'branch', 'field_data' => [], 'options' => $options];
     foreach ($project->DNBranchList() as $branch) {
         $sha = $branch->SHA();
         $name = $branch->Name();
         $branchValue = sprintf("%s (%s, %s old)", $name, substr($sha, 0, 8), $branch->LastUpdated()->TimeDiff());
         $data['field_data'][] = ['id' => $sha, 'text' => $branchValue, 'branch_name' => $name];
     }
     $tabs[] = $data;
     $data = ['id' => ++$id, 'name' => 'Deploy a tagged release', 'field_type' => 'dropdown', 'field_label' => 'Choose a tag', 'field_id' => 'tag', 'field_data' => [], 'options' => $options];
     foreach ($project->DNTagList()->setLimit(null) as $tag) {
         $name = $tag->Name();
         $data['field_data'][] = ['id' => $tag->SHA(), 'text' => sprintf("%s", $name)];
     }
     // show newest tags first.
     $data['field_data'] = array_reverse($data['field_data']);
     $tabs[] = $data;
     // Past deployments
     $data = ['id' => ++$id, 'name' => 'Redeploy a release that was previously deployed (to any environment)', 'field_type' => 'dropdown', 'field_label' => 'Choose a previously deployed release', 'field_id' => 'release', 'field_data' => [], 'options' => $options];
     // We are aiming at the format:
     // [{text: 'optgroup text', children: [{id: '<sha>', text: '<inner text>'}]}]
     $redeploy = [];
     foreach ($project->DNEnvironmentList() as $dnEnvironment) {
         $envName = $dnEnvironment->Name;
         $perEnvDeploys = [];
         foreach ($dnEnvironment->DeployHistory()->filter('State', \DNDeployment::STATE_COMPLETED) as $deploy) {
             $sha = $deploy->SHA;
             // Check if exists to make sure the newest deployment date is used.
             if (!isset($perEnvDeploys[$sha])) {
                 $pastValue = sprintf("%s (deployed %s)", substr($sha, 0, 8), $deploy->obj('LastEdited')->Ago());
                 $perEnvDeploys[$sha] = ['id' => $sha, 'text' => $pastValue];
             }
         }
         if (!empty($perEnvDeploys)) {
             $redeploy[$envName] = array_values($perEnvDeploys);
         }
     }
     // Convert the array to the frontend format (i.e. keyed to regular array)
     foreach ($redeploy as $name => $descr) {
         $data['field_data'][] = ['text' => $name, 'children' => $descr];
     }
     $tabs[] = $data;
     $data = ['id' => ++$id, 'name' => 'Deploy a specific SHA', 'field_type' => 'textfield', 'field_label' => 'Choose a SHA', 'field_id' => 'SHA', 'field_data' => [], 'options' => $options];
     $tabs[] = $data;
     // get the last time git fetch was run
     $lastFetched = 'never';
     $fetch = DNGitFetch::get()->filter(['ProjectID' => $project->ID, 'Status' => 'Finished'])->sort('LastEdited', 'DESC')->first();
     if ($fetch) {
         $lastFetched = $fetch->dbObject('LastEdited')->Ago();
     }
     $data = ['Tabs' => $tabs, 'last_fetched' => $lastFetched];
     $this->applyRedeploy($request, $data);
     return json_encode($data, JSON_PRETTY_PRINT);
 }