mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	Abort Deployment when pre-deploy task fails. Addresses issue #29
This commit is contained in:
		
							parent
							
								
									d8961674d3
								
							
						
					
					
						commit
						03b158c0a5
					
				@ -70,8 +70,18 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    protected $hostsCount = 0;
 | 
					    protected $hostsCount = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Current Status of the Deployment (in progress, succeded, failed)
 | 
				
			||||||
 | 
					     * @var string
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    protected static $deployStatus = 'in_progress';
 | 
					    protected static $deployStatus = 'in_progress';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Total of Failed tasks
 | 
				
			||||||
 | 
					     * @var integer
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected static $failedTasks = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Returns the Status of the Deployment
 | 
					     * Returns the Status of the Deployment
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
@ -105,7 +115,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // Release ID
 | 
					        // Release ID
 | 
				
			||||||
        $this->getConfig()->setReleaseId(date('YmdHis'));
 | 
					        $this->getConfig()->setReleaseId(date('YmdHis'));
 | 
				
			||||||
        $failedTasks = 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Deploy Summary
 | 
					        // Deploy Summary
 | 
				
			||||||
        Console::output('<dark_gray>Deploy summary</dark_gray>', 1, 1);
 | 
					        Console::output('<dark_gray>Deploy summary</dark_gray>', 1, 1);
 | 
				
			||||||
@ -134,9 +143,131 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
 | 
				
			|||||||
        // Run Pre-Deployment Tasks
 | 
					        // Run Pre-Deployment Tasks
 | 
				
			||||||
        $this->runNonDeploymentTasks('pre-deploy', $this->getConfig(), 'Pre-Deployment');
 | 
					        $this->runNonDeploymentTasks('pre-deploy', $this->getConfig(), 'Pre-Deployment');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Check Status
 | 
				
			||||||
 | 
					        if (self::$failedTasks > 0) {
 | 
				
			||||||
 | 
					        	self::$deployStatus = self::FAILED;
 | 
				
			||||||
 | 
					        	Console::output('A total of <dark_gray>' . self::$failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					        	// Run Deployment Tasks
 | 
				
			||||||
 | 
					        	$this->runDeploymentTasks();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        	// Check Status
 | 
				
			||||||
 | 
					        	if (self::$failedTasks > 0) {
 | 
				
			||||||
 | 
					        		self::$deployStatus = self::FAILED;
 | 
				
			||||||
 | 
					        		Console::output('A total of <dark_gray>' . self::$failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
 | 
				
			||||||
 | 
					        	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        	// Run Post-Deployment Tasks
 | 
				
			||||||
 | 
					        	$this->runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Time Information Hosts
 | 
				
			||||||
 | 
					        if ($this->hostsCount > 0) {
 | 
				
			||||||
 | 
					            $timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts);
 | 
				
			||||||
 | 
					            Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount));
 | 
				
			||||||
 | 
					            Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Time Information General
 | 
				
			||||||
 | 
					        $timeText = $this->transcurredTime(time() - $this->startTime);
 | 
				
			||||||
 | 
					        Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Send Notifications
 | 
				
			||||||
 | 
					        $this->sendNotification();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Unlock
 | 
				
			||||||
 | 
					        if (file_exists('.mage/~working.lock')) {
 | 
				
			||||||
 | 
					        	unlink('.mage/~working.lock');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Execute Pre and Post Deployment Tasks
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param string $stage
 | 
				
			||||||
 | 
					     * @param Config $config
 | 
				
			||||||
 | 
					     * @param string $title
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    protected function runNonDeploymentTasks($stage, Config $config, $title)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $tasksToRun = $config->getTasks($stage);
 | 
				
			||||||
 | 
					        self::$failedTasks = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // PreDeployment Hook
 | 
				
			||||||
 | 
					        if ($stage == 'pre-deploy') {
 | 
				
			||||||
 | 
					        	// Look for Remote Source
 | 
				
			||||||
 | 
					        	if (is_array($config->deployment('source', null))) {
 | 
				
			||||||
 | 
					        		array_unshift($tasksToRun, 'scm/clone');
 | 
				
			||||||
 | 
					        	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        	// Change Branch
 | 
				
			||||||
 | 
					        	if ($config->deployment('scm', false)) {
 | 
				
			||||||
 | 
					        		array_unshift($tasksToRun, 'scm/change-branch');
 | 
				
			||||||
 | 
					        	}
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // PostDeployment Hook
 | 
				
			||||||
 | 
					        if ($stage == 'post-deploy') {
 | 
				
			||||||
 | 
					        	// If Deploy failed, clear post deploy tasks
 | 
				
			||||||
 | 
					        	if (self::$deployStatus == self::FAILED) {
 | 
				
			||||||
 | 
					        		$tasksToRun = array();
 | 
				
			||||||
 | 
					        	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        	// Change Branch Back
 | 
				
			||||||
 | 
					        	if ($config->deployment('scm', false)) {
 | 
				
			||||||
 | 
					        		array_unshift($tasksToRun, 'scm/change-branch');
 | 
				
			||||||
 | 
					        		$config->addParameter('_changeBranchRevert');
 | 
				
			||||||
 | 
					        	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        	// Remove Remote Source
 | 
				
			||||||
 | 
					        	if (is_array($config->deployment('source', null))) {
 | 
				
			||||||
 | 
					        		 array_push($tasksToRun, 'scm/remove-clone');
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (count($tasksToRun) == 0) {
 | 
				
			||||||
 | 
					            Console::output('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					            Console::output('Starting <dark_gray>' . $title . '</dark_gray> tasks:');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $tasks = 0;
 | 
				
			||||||
 | 
					            $completedTasks = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            foreach ($tasksToRun as $taskData) {
 | 
				
			||||||
 | 
					                $tasks++;
 | 
				
			||||||
 | 
					                $task = Factory::get($taskData, $config, false, $stage);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($this->runTask($task)) {
 | 
				
			||||||
 | 
					                    $completedTasks++;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                	self::$failedTasks++;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if ($completedTasks == $tasks) {
 | 
				
			||||||
 | 
					                $tasksColor = 'green';
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                $tasksColor = 'red';
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected function runDeploymentTasks()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					    	if (self::$deployStatus == self::FAILED) {
 | 
				
			||||||
 | 
					    		return;
 | 
				
			||||||
 | 
					    	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    	// Run Tasks for Deployment
 | 
					    	// Run Tasks for Deployment
 | 
				
			||||||
    	$hosts = $this->getConfig()->getHosts();
 | 
					    	$hosts = $this->getConfig()->getHosts();
 | 
				
			||||||
    	$this->hostsCount = count($hosts);
 | 
					    	$this->hostsCount = count($hosts);
 | 
				
			||||||
 | 
					    	self::$failedTasks = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    	if ($this->hostsCount == 0) {
 | 
					    	if ($this->hostsCount == 0) {
 | 
				
			||||||
    		Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
 | 
					    		Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, skipping deployment tasks.</dark_gray>', 1, 3);
 | 
				
			||||||
@ -197,7 +328,7 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
 | 
				
			|||||||
    					if ($this->runTask($task)) {
 | 
					    					if ($this->runTask($task)) {
 | 
				
			||||||
    						$completedTasks++;
 | 
					    						$completedTasks++;
 | 
				
			||||||
    					} else {
 | 
					    					} else {
 | 
				
			||||||
                            $failedTasks++;
 | 
					    						self::$failedTasks++;
 | 
				
			||||||
    					}
 | 
					    					}
 | 
				
			||||||
    				}
 | 
					    				}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -215,9 +346,8 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
 | 
				
			|||||||
    		}
 | 
					    		}
 | 
				
			||||||
    		$this->endTimeHosts = time();
 | 
					    		$this->endTimeHosts = time();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if ($failedTasks > 0) {
 | 
					    		if (self::$failedTasks > 0) {
 | 
				
			||||||
    			self::$deployStatus = self::FAILED;
 | 
					    			self::$deployStatus = self::FAILED;
 | 
				
			||||||
                Console::output('A total of <dark_gray>' . $failedTasks . '</dark_gray> deployment tasks failed: <red>ABORTING</red>', 1, 2);
 | 
					 | 
				
			||||||
    		} else {
 | 
					    		} else {
 | 
				
			||||||
    			self::$deployStatus = self::SUCCEDED;
 | 
					    			self::$deployStatus = self::SUCCEDED;
 | 
				
			||||||
    		}
 | 
					    		}
 | 
				
			||||||
@ -292,101 +422,6 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
 | 
				
			|||||||
    			}
 | 
					    			}
 | 
				
			||||||
    		}
 | 
					    		}
 | 
				
			||||||
    	}
 | 
					    	}
 | 
				
			||||||
 | 
					 | 
				
			||||||
    	// Run Post-Deployment Tasks
 | 
					 | 
				
			||||||
    	$this->runNonDeploymentTasks('post-deploy', $this->getConfig(), 'Post-Deployment');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Time Information Hosts
 | 
					 | 
				
			||||||
        if ($this->hostsCount > 0) {
 | 
					 | 
				
			||||||
            $timeTextHost = $this->transcurredTime($this->endTimeHosts - $this->startTimeHosts);
 | 
					 | 
				
			||||||
            Console::output('Time for deployment: <dark_gray>' . $timeTextHost . '</dark_gray>.');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $timeTextPerHost = $this->transcurredTime(round(($this->endTimeHosts - $this->startTimeHosts) / $this->hostsCount));
 | 
					 | 
				
			||||||
            Console::output('Average time per host: <dark_gray>' . $timeTextPerHost . '</dark_gray>.');
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Time Information General
 | 
					 | 
				
			||||||
        $timeText = $this->transcurredTime(time() - $this->startTime);
 | 
					 | 
				
			||||||
        Console::output('Total time: <dark_gray>' . $timeText . '</dark_gray>.', 1, 2);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Send Notifications
 | 
					 | 
				
			||||||
        $this->sendNotification();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Unlock
 | 
					 | 
				
			||||||
        if (file_exists('.mage/~working.lock')) {
 | 
					 | 
				
			||||||
        	unlink('.mage/~working.lock');
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Execute Pre and Post Deployment Tasks
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param string $stage
 | 
					 | 
				
			||||||
     * @param Config $config
 | 
					 | 
				
			||||||
     * @param string $title
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    protected function runNonDeploymentTasks($stage, Config $config, $title)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        $tasksToRun = $config->getTasks($stage);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // PreDeployment Hook
 | 
					 | 
				
			||||||
        if ($stage == 'pre-deploy') {
 | 
					 | 
				
			||||||
        	// Look for Remote Source
 | 
					 | 
				
			||||||
        	if (is_array($config->deployment('source', null))) {
 | 
					 | 
				
			||||||
        		array_unshift($tasksToRun, 'scm/clone');
 | 
					 | 
				
			||||||
        	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        	// Change Branch
 | 
					 | 
				
			||||||
        	if ($config->deployment('scm', false)) {
 | 
					 | 
				
			||||||
        		array_unshift($tasksToRun, 'scm/change-branch');
 | 
					 | 
				
			||||||
        	}
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // PostDeployment Hook
 | 
					 | 
				
			||||||
        if ($stage == 'post-deploy') {
 | 
					 | 
				
			||||||
        	// If Deploy failed, clear post deploy tasks
 | 
					 | 
				
			||||||
        	if (self::$deployStatus == self::FAILED) {
 | 
					 | 
				
			||||||
        		$tasksToRun = array();
 | 
					 | 
				
			||||||
        	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        	// Change Branch Back
 | 
					 | 
				
			||||||
        	if ($config->deployment('scm', false)) {
 | 
					 | 
				
			||||||
        		array_unshift($tasksToRun, 'scm/change-branch');
 | 
					 | 
				
			||||||
        		$config->addParameter('_changeBranchRevert');
 | 
					 | 
				
			||||||
        	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        	// Remove Remote Source
 | 
					 | 
				
			||||||
        	if (is_array($config->deployment('source', null))) {
 | 
					 | 
				
			||||||
        		 array_push($tasksToRun, 'scm/remove-clone');
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (count($tasksToRun) == 0) {
 | 
					 | 
				
			||||||
            Console::output('<dark_gray>No </dark_gray><light_cyan>' . $title . '</light_cyan> <dark_gray>tasks defined.</dark_gray>', 1, 3);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            Console::output('Starting <dark_gray>' . $title . '</dark_gray> tasks:');
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $tasks = 0;
 | 
					 | 
				
			||||||
            $completedTasks = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            foreach ($tasksToRun as $taskData) {
 | 
					 | 
				
			||||||
                $tasks++;
 | 
					 | 
				
			||||||
                $task = Factory::get($taskData, $config, false, $stage);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if ($this->runTask($task)) {
 | 
					 | 
				
			||||||
                    $completedTasks++;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            if ($completedTasks == $tasks) {
 | 
					 | 
				
			||||||
                $tasksColor = 'green';
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                $tasksColor = 'red';
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            Console::output('Finished <dark_gray>' . $title . '</dark_gray> tasks: <' . $tasksColor . '>' . $completedTasks . '/' . $tasks . '</' . $tasksColor . '> tasks done.', 1, 3);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 | 
				
			|||||||
@ -4,18 +4,19 @@ deployment:
 | 
				
			|||||||
  from: ./
 | 
					  from: ./
 | 
				
			||||||
  to: /var/www/
 | 
					  to: /var/www/
 | 
				
			||||||
  scm:
 | 
					  scm:
 | 
				
			||||||
    branch: master2
 | 
					    branch: master
 | 
				
			||||||
releases:
 | 
					releases:
 | 
				
			||||||
  enabled: true
 | 
					  enabled: true
 | 
				
			||||||
  max: 5
 | 
					  max: 5
 | 
				
			||||||
  symlink: current
 | 
					  symlink: current
 | 
				
			||||||
  directory: releases
 | 
					  directory: releases
 | 
				
			||||||
hosts:
 | 
					hosts:
 | 
				
			||||||
#  - localhost
 | 
					  - localhost
 | 
				
			||||||
#  - dbserver
 | 
					#  - dbserver
 | 
				
			||||||
tasks:
 | 
					tasks:
 | 
				
			||||||
  pre-deploy:
 | 
					  pre-deploy:
 | 
				
			||||||
    - sampleTask
 | 
					    - sampleTask
 | 
				
			||||||
 | 
					#    - failTask
 | 
				
			||||||
    - scm/update
 | 
					    - scm/update
 | 
				
			||||||
  on-deploy:
 | 
					  on-deploy:
 | 
				
			||||||
    - privileges
 | 
					    - privileges
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										17
									
								
								docs/example-config/.mage/tasks/FailTask.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								docs/example-config/.mage/tasks/FailTask.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace Task;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Mage\Task\AbstractTask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class FailTask extends AbstractTask
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public function getName()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return 'A Failing Task';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function run()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user