Exemplo n.º 1
0
 public function isValid($value)
 {
     $this->_setValue($value);
     $git = new GD_Git($this->_project);
     try {
         if ($git->checkValidRepository()) {
             if ($git->gitCheckout($value)) {
                 return true;
             }
         }
         $this->_error(self::INVALID);
         return false;
     } catch (GD_Exception $ex) {
         $this->_error(self::NO_REPO);
         return false;
     }
 }
Exemplo n.º 2
0
 public function executeDeploymentStartAction()
 {
     // session_start blocks other requests, so close the session for the
     // status AJAX request to work
     Zend_Session::writeClose();
     ob_start();
     GD_Debug::StartDeploymentLog($this->_getParam("id"));
     GD_Debug::Log("Setting time limit... ", GD_Debug::DEBUG_BASIC, false);
     set_time_limit(0);
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     // Project information
     GD_Debug::Log("Loading project... ", GD_Debug::DEBUG_BASIC, false);
     $projects = new GD_Model_ProjectsMapper();
     $project_slug = $this->_getParam("project");
     if ($project_slug != "") {
         $project = $projects->getProjectBySlug($project_slug);
     }
     if (is_null($project)) {
         throw new GD_Exception("Project '{$project_slug}' was not set up.");
     }
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     // Deployment information
     GD_Debug::Log("Loading deployment information... ", GD_Debug::DEBUG_BASIC, false);
     $deployments = new GD_Model_DeploymentsMapper();
     $deployment = new GD_Model_Deployment();
     $deployments->find($this->_getParam('id'), $deployment);
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     // Server information
     GD_Debug::Log("Loading server information... ", GD_Debug::DEBUG_BASIC, false);
     $servers = new GD_Model_ServersMapper();
     $server = new GD_Model_Server();
     $servers->find($deployment->getServersId(), $server);
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     // Update the deployment status to show we're now running
     GD_Debug::Log("Updating deployment status to running... ", GD_Debug::DEBUG_BASIC, false);
     $deployment->setDeploymentStatusesId(2);
     // Running
     $deployments->save($deployment);
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     // Perform a git pull to check we're up to date
     $git = new GD_Git($project);
     $git->gitPull();
     // File list to action
     GD_Debug::Log("Getting file list... ", GD_Debug::DEBUG_BASIC, false);
     $deployment_files = new GD_Model_DeploymentFilesMapper();
     $deployment_files_statuses = new GD_Model_DeploymentFileStatusesMapper();
     $file_list = $deployment_files->getDeploymentFilesByDeployment($deployment->getId());
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     // Check out the revision we want to upload from
     GD_Debug::Log("Checking out revision {$deployment->getToRevision()}... ", GD_Debug::DEBUG_BASIC, false);
     $previous_ref = $git->getCurrentBranch(true);
     $res = $git->gitCheckout($deployment->getToRevision());
     if (!$res) {
         GD_Debug::Log("FAILED.", GD_Debug::DEBUG_BASIC, true, false);
     } else {
         GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     }
     $errors = false;
     // Do the upload
     GD_Debug::Log("Actioning files now.", GD_Debug::DEBUG_BASIC);
     $ftp = new GD_Ftp($server);
     try {
         $ftp->connect();
     } catch (GD_Exception $ex) {
         GD_Debug::Log("FTP Connect failed: {$ex->getMessage()}", GD_Debug::DEBUG_BASIC);
     }
     foreach ($file_list as $file) {
         GD_Debug::Log("Actioning '{$file->getDetails()}'... ", GD_Debug::DEBUG_BASIC, false);
         $file->setDeploymentFileStatusesId($deployment_files_statuses->getDeploymentFileStatusByCode('IN_PROGRESS')->getId());
         $deployment_files->save($file);
         try {
             switch ($file->getDeploymentFileAction()->getGitStatus()) {
                 case 'A':
                 case 'M':
                     $ftp->upload($git->getGitDir() . $file->getDetails(), $file->getDetails());
                     break;
                 case 'D':
                     $ftp->delete($file->getDetails());
                     break;
                 default:
                     throw GD_Exception("Warning, unhandled action: '" . $file->getDeploymentFileAction()->getGitStatus() . "' ({$file->getDetails()}");
                     break;
             }
             $file->setDeploymentFileStatusesId($deployment_files_statuses->getDeploymentFileStatusByCode('COMPLETE')->getId());
             GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
         } catch (GD_Exception $ex) {
             $errors = true;
             $file->setDeploymentFileStatusesId($deployment_files_statuses->getDeploymentFileStatusByCode('FAILED')->getId());
             GD_Debug::Log("FAILED [" . $ex->getMessage() . "].", GD_Debug::DEBUG_BASIC, true, false);
         }
         $deployment_files->save($file);
     }
     // Revert to previous revision
     GD_Debug::Log("Checking out revision {$previous_ref}... ", GD_Debug::DEBUG_BASIC, false);
     $res = $git->gitCheckout($previous_ref);
     if (!$res) {
         GD_Debug::Log("FAILED.", GD_Debug::DEBUG_BASIC, true, false);
     } else {
         GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     }
     GD_Debug::Log("Setting deployment status " . ($errors ? "[errors]" : "[success]") . "... ", GD_Debug::DEBUG_BASIC, false);
     if ($errors) {
         $deployment->setDeploymentStatusesId(4);
         // Failed
         $deployments->save($deployment);
     } else {
         $deployment->setDeploymentStatusesId(3);
         // Complete
         $deployments->save($deployment);
     }
     GD_Debug::Log("done.", GD_Debug::DEBUG_BASIC, true, false);
     GD_Debug::Log("All finished.", GD_Debug::DEBUG_BASIC);
     $buf = ob_get_contents();
     if ($buf) {
         GD_Debug::Log("Extra content:\n\n{$buf}", GD_Debug::DEBUG_BASIC);
     }
     GD_Debug::EndDeploymentLog($this->_getParam("id"));
     ob_end_clean();
     flush();
     die;
 }
Exemplo n.º 3
0
 public function saveProject(GD_Model_ProjectsMapper $projects, GD_Model_Project $project, $new_project = false, $errored = false)
 {
     $repo_before = $project->getRepositoryUrl();
     $branch_before = $project->getDeploymentBranch();
     $project->setName($this->_request->getParam('name', false));
     $project->setRepositoryUrl($this->_request->getParam('repositoryUrl', false));
     $project->setDeploymentBranch($this->_request->getParam('deploymentBranch', false));
     $project->setRepositoryTypesId(1);
     $project->setSSHKeysId(1);
     $repo_after = $project->getRepositoryUrl();
     $branch_after = $project->getDeploymentBranch();
     // Save the project
     $projects->save($project);
     $git = new GD_Git($project);
     $branch_changed = false;
     // If repo URL changed, delete and re-clone
     if ($repo_before != $repo_after || $new_project || $errored) {
         // Delete any existing repo
         $git->deleteRepository();
         // Clone repository from source
         $result = $git->gitClone();
         if ($result !== true) {
             return $result;
         }
         // Checkout the appropriate branch
         if (!$git->gitCheckout($branch_after)) {
             return "Branch '{$branch_after}' does not exist.";
         }
         $branch_changed = true;
     }
     if ($branch_before != $branch_after && !$branch_changed) {
         // If not already checked out, or branch has changed, do a checkout
         $git->gitCheckout($branch_after);
     }
     return true;
 }