Ejemplo n.º 1
0
 public function errorAction()
 {
     $errors = $this->_getParam('error_handler');
     if (!$errors) {
         $this->view->message = 'You have reached the error page';
         return;
     }
     switch ($errors->type) {
         case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
         case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
         case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
             // 404 error -- controller or action not found
             $this->getResponse()->setHttpResponseCode(404);
             $this->view->message = _r("Could not find the page you were looking for...");
             break;
         default:
             // application error
             $this->getResponse()->setHttpResponseCode(500);
             $this->view->message = 'Application error';
             if ($errors->exception instanceof GD_Exception) {
                 $this->view->extended_information = $errors->exception->getMessage();
             }
             break;
     }
     // Log exception
     GD_Debug::Log("Exception: " . $this->view->message, GD_Debug::DEBUG_BASIC);
     // conditionally display exceptions
     if ($this->getInvokeArg('displayExceptions') == true) {
         $this->view->exception = $errors->exception;
     }
     $this->view->request = $errors->request;
 }
Ejemplo n.º 2
0
 public function indexAction()
 {
     $this->view->headLink()->appendStylesheet("/css/template/form.css");
     $this->view->headLink()->appendStylesheet("/css/template/table.css");
     $this->view->headLink()->appendStylesheet("/css/pages/project_settings.css");
     $projects = new GD_Model_ProjectsMapper();
     $project_slug = $this->_getParam("project");
     if ($project_slug != "new") {
         $this->view->headTitle('Edit Project');
         $project = $projects->getProjectBySlug($project_slug);
         $new_project = false;
     } else {
         $this->view->headTitle('Add Project');
         $project = new GD_Model_Project();
         $project->setName("New Project");
         $project->setDeploymentBranch('master');
         // Usually default for git
         // Load the ssh key
         $ssh_keys_map = new GD_Model_SSHKeysMapper();
         $ssh_keys = new GD_Model_SSHKey();
         $ssh_keys_map->find(1, $ssh_keys);
         $project->setSSHKey($ssh_keys);
         $new_project = true;
     }
     $this->view->project = $project;
     $this->view->new_project = $new_project;
     // Populate list of servers for this project
     if ($project->getId() > 0) {
         $servers = new GD_Model_ServersMapper();
         $this->view->servers = $servers->getServersByProject($project->getId());
     }
     $form = new GDApp_Form_ProjectSettings($project, null, $new_project);
     $this->view->form = $form;
     if ($this->getRequest()->isPost()) {
         if ($form->isValid($this->getRequest()->getParams())) {
             $result = $this->saveProject($projects, $project, $new_project, $this->_getParam("errored") == "true");
             if ($result !== true) {
                 $form->repositoryUrl->addError("Could not clone the git repository [" . $result . "]. Please check the URL is correct and try again.");
             } else {
                 $this->_redirect($this->getFrontController()->getBaseUrl() . "project/" . $project->getSlug() . '/settings');
             }
         }
     } else {
         if (!$new_project) {
             $git = new GD_Git($project);
             try {
                 $git->checkValidRepository();
                 $this->view->valid_repository = true;
             } catch (GD_Exception $ex) {
                 $this->view->valid_repository = false;
                 $form->repositoryUrl->addError("Check this git repository URL - is it correct?");
                 GD_Debug::Log("Reclone required for {$project->getName()} (project settings)", GD_Debug::DEBUG_BASIC);
             }
         }
         $data = array('name' => $project->getName(), 'repositoryUrl' => $project->getRepositoryUrl(), 'deploymentBranch' => $project->getDeploymentBranch(), 'publicKey' => $project->getSSHKey()->getPublicKey());
         $form->populate($data);
     }
 }
Ejemplo n.º 3
0
 public function isValid($value)
 {
     $this->_setValue($value);
     $git = GD_Git::FromProject($this->_project);
     try {
         if ($git->checkValidRepository()) {
             if ($git->gitCheckout($value)) {
                 return true;
             }
         }
         $this->_error(self::INVALID);
         return false;
     } catch (GD_Exception $ex) {
         GD_Debug::Log("Repository for {$this->_project->getName()} was not valid [{$ex->getStringCode()}]", GD_Debug::DEBUG_BASIC);
         $this->_error(self::NO_REPO);
         return false;
     }
 }
Ejemplo n.º 4
0
 /**
  * Set the current debug level or default. If debugging enabled then open
  * the file handle and allow logging.
  */
 private static function initialise()
 {
     try {
         self::$_current_debug_level = GD_Config::get("debug_level");
     } catch (Zend_Db_Adapter_Exception $ex) {
         self::$_current_debug_level = self::DEBUG_NONE;
     }
     if (!self::$_current_debug_level || self::$_current_debug_level == "0") {
         return false;
     }
     if (!self::$_fh) {
         $logfile = sys_get_temp_dir() . "/godeploy_log";
         self::$_fh = fopen($logfile, "a");
         //chmod($logfile, 0755);
         if (!self::$_fh) {
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 5
0
 /**
  * Set the current debug level or default. If debugging enabled then open
  * the file handle and allow logging.
  */
 private static function initialise()
 {
     try {
         self::$_current_debug_level = GD_Config::get("debug_level");
     } catch (Exception $ex) {
         error_log("Failed to get debug_level setting, error was: {$ex->getMessage()}", 0);
         self::$_current_debug_level = self::DEBUG_NONE;
     }
     if (!self::$_current_debug_level || self::$_current_debug_level == "0") {
         return false;
     }
     if (!self::$_fh) {
         $logfile = GD_Config::get('logfile');
         self::$_fh = @fopen($logfile, "a");
         if (!self::$_fh) {
             return false;
         }
     }
     return true;
 }
Ejemplo n.º 6
0
 /**
  * Authentication function to attempt to login the user using the credentials supplied in the constructor.
  *
  * @throws Zend_Auth_Adapter_Exception If authentication cannot be performed
  * @return Zend_Auth_Result
  */
 public function authenticate()
 {
     // Get user details for the email we're trying to get
     $users = new GD_Model_UsersMapper();
     $user = $users->getUserByName($this->_username, true);
     if (is_null($user)) {
         GD_Debug::Log("Authentication failure - user '{$this->_username}' not found.", GD_Debug::DEBUG_BASIC);
         return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, $this->_username);
     }
     // The password is stored with the salt so we need to extract the salt
     // to re-encrypt the user's password
     $stored_fullhash = $user->getPassword();
     $salt_len = strrpos($stored_fullhash, "\$") + 1;
     $salt = substr($stored_fullhash, 0, $salt_len);
     $compare_hash = crypt($this->_password, $salt);
     // If we passed the tests, then we successfully authenticated
     if ($this->_username == $user->getName() && $compare_hash == $user->getPassword()) {
         return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $this->_username);
     } else {
         GD_Debug::Log("Authentication failure - incorrect password for '{$this->_username}'.", GD_Debug::DEBUG_BASIC);
         return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, $this->_username);
     }
 }
Ejemplo n.º 7
0
 public function Exec($cmd, $noisy = false)
 {
     parent::Exec($cmd, $noisy);
     GD_Debug::Log("GD_Shell cmd: {$cmd}", GD_Debug::DEBUG_FULL);
 }
Ejemplo n.º 8
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 = GD_Git::FromProject($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 = GD_Ftp::FromServer($server);
     try {
         $ftp->connect();
     } catch (GD_Exception $ex) {
         GD_Debug::Log("FTP Connect failed: {$ex->getMessage()}", GD_Debug::DEBUG_BASIC);
     }
     $config_map = new GD_Model_ConfigsMapper();
     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);
         $matches = array();
         $is_config_file = preg_match('/^!CFG!(\\d+)!(.*)$/', $file->getDetails(), $matches);
         try {
             if ($is_config_file == 1) {
                 // Configuration file - store in temp dir from DB then upload
                 $tmpfile = tempnam(sys_get_temp_dir(), 'gdcfg');
                 $config = new GD_Model_Config();
                 $config_map->find($matches[1], $config);
                 file_put_contents($tmpfile, $config->getContent());
                 GD_Debug::Log(" >> to '{$matches[2]}'", GD_Debug::DEBUG_BASIC, false, false);
                 $ftp->upload($tmpfile, $matches[2]);
             } else {
                 // Regular file - upload as normal
                 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) {
             // Only fail the whole deployment if we're not a delete action
             if ($file->getDeploymentFileAction()->getGitStatus() != 'D') {
                 $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();
     ob_end_clean();
     flush();
     die;
 }