mirror of
				https://github.com/hauke68/Magallanes.git
				synced 2025-11-04 00:50:18 +01:00 
			
		
		
		
	Merge (partial) PR #56
This commit is contained in:
		
						commit
						25de6b0a8f
					
				
							
								
								
									
										137
									
								
								Mage/Task/BuiltIn/Deployment/Strategy/GitRemoteCacheTask.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								Mage/Task/BuiltIn/Deployment/Strategy/GitRemoteCacheTask.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,137 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					namespace Mage\Task\BuiltIn\Deployment\Strategy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Exception;
 | 
				
			||||||
 | 
					use Mage\Task\AbstractTask;
 | 
				
			||||||
 | 
					use Mage\Task\ErrorWithMessageException;
 | 
				
			||||||
 | 
					use Mage\Task\Releases\IsReleaseAware;
 | 
				
			||||||
 | 
					use Mage\Task\SkipException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * The git remote cache deployment task.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * This tasks uses a remote checkout on the server to provide the release.
 | 
				
			||||||
 | 
					 * In our use case this remote cache resides in $to/$shared/git-remote-cache,
 | 
				
			||||||
 | 
					 * $shared is substituted with "shared" by default. At this time, the remote cache
 | 
				
			||||||
 | 
					 * is not build automatically, you need to provide a clean checkout before you can
 | 
				
			||||||
 | 
					 * start using it.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @package Mage\Task\BuiltIn\Deployment\Strategy
 | 
				
			||||||
 | 
					 * @author Mario Mueller <mueller@freshcells.de>
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					class GitRemoteCacheTask extends AbstractTask implements IsReleaseAware
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Returns the Title of the Task
 | 
				
			||||||
 | 
					     * @return string
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function getName()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return 'Deploy via remote cached git repository [built-in]';
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Runs the task
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @return boolean
 | 
				
			||||||
 | 
					     * @throws Exception
 | 
				
			||||||
 | 
					     * @throws ErrorWithMessageException
 | 
				
			||||||
 | 
					     * @throws SkipException
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public function run()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $overrideRelease = $this->getParameter('overrideRelease', false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if ($overrideRelease == true) {
 | 
				
			||||||
 | 
					            $releaseToOverride = false;
 | 
				
			||||||
 | 
					            $resultFetch = $this->runCommandRemote('ls -ld current | cut -d"/" -f2', $releaseToOverride);
 | 
				
			||||||
 | 
					            if ($resultFetch && is_numeric($releaseToOverride)) {
 | 
				
			||||||
 | 
					                $this->getConfig()->setReleaseId($releaseToOverride);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $excludes = array(
 | 
				
			||||||
 | 
					            '.git',
 | 
				
			||||||
 | 
					            '.svn',
 | 
				
			||||||
 | 
					            '.mage',
 | 
				
			||||||
 | 
					            '.gitignore',
 | 
				
			||||||
 | 
					            '.gitkeep',
 | 
				
			||||||
 | 
					            'nohup.out'
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Look for User Excludes
 | 
				
			||||||
 | 
					        $userExcludes = $this->getConfig()->deployment('excludes', array());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $deployToDirectory = $this->getConfig()->deployment('to');
 | 
				
			||||||
 | 
					        if ($this->getConfig()->release('enabled', false) == true) {
 | 
				
			||||||
 | 
					            $releasesDirectory = $this->getConfig()->release('directory', 'releases');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $deployToDirectory = rtrim($this->getConfig()->deployment('to'), '/')
 | 
				
			||||||
 | 
					                . '/' . $releasesDirectory
 | 
				
			||||||
 | 
					                . '/' . $this->getConfig()->getReleaseId();
 | 
				
			||||||
 | 
					            $this->runCommandRemote('mkdir -p ' . $releasesDirectory . '/' . $this->getConfig()->getReleaseId());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $branch = $this->getParameter('branch');
 | 
				
			||||||
 | 
					        $remote = $this->getParameter('remote', 'origin');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $remoteCacheParam = $this->getParameter('remote_cache', 'shared/git-remote-cache');
 | 
				
			||||||
 | 
					        $remoteCacheFolder = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $remoteCacheParam;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Don't use -C as git 1.7 does not support it
 | 
				
			||||||
 | 
					        $command = 'cd ' . $remoteCacheFolder . ' && /usr/bin/env git fetch ' . $remote;
 | 
				
			||||||
 | 
					        $result = $this->runCommandRemote($command);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $command = 'cd ' . $remoteCacheFolder . ' && /usr/bin/env git checkout ' . $branch;
 | 
				
			||||||
 | 
					        $result = $this->runCommandRemote($command) && $result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $command = 'cd ' . $remoteCacheFolder . ' && /usr/bin/env git pull --rebase ' . $branch;
 | 
				
			||||||
 | 
					        $result = $this->runCommandRemote($command) && $result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $excludes = array_merge($excludes, $userExcludes);
 | 
				
			||||||
 | 
					        $excludeCmd = '';
 | 
				
			||||||
 | 
					        foreach ($excludes as $excludeFile) {
 | 
				
			||||||
 | 
					            $excludeCmd .= ' --exclude=' . $excludeFile;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $command = 'cd ' . $remoteCacheFolder . ' && /usr/bin/env git archive ' . $branch . ' | tar -x -C ' . $deployToDirectory . ' ' . $excludeCmd;
 | 
				
			||||||
 | 
					        $result = $this->runCommandRemote($command) && $result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Count Releases
 | 
				
			||||||
 | 
					        if ($this->getConfig()->release('enabled', false) == true) {
 | 
				
			||||||
 | 
					            $releasesDirectory = $this->getConfig()->release('directory', 'releases');
 | 
				
			||||||
 | 
					            $symlink = $this->getConfig()->release('symlink', 'current');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (substr($symlink, 0, 1) == '/') {
 | 
				
			||||||
 | 
					                $releasesDirectory = rtrim($this->getConfig()->deployment('to'), '/') . '/' . $releasesDirectory;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $maxReleases = $this->getConfig()->release('max', false);
 | 
				
			||||||
 | 
					            if (($maxReleases !== false) && ($maxReleases > 0)) {
 | 
				
			||||||
 | 
					                $releasesList = '';
 | 
				
			||||||
 | 
					                $countReleasesFetch = $this->runCommandRemote('ls -1 ' . $releasesDirectory, $releasesList);
 | 
				
			||||||
 | 
					                $releasesList = trim($releasesList);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if ($countReleasesFetch && $releasesList != '') {
 | 
				
			||||||
 | 
					                    $releasesList = explode(PHP_EOL, $releasesList);
 | 
				
			||||||
 | 
					                    if (count($releasesList) > $maxReleases) {
 | 
				
			||||||
 | 
					                        $releasesToDelete = array_diff($releasesList, array($this->getConfig()->getReleaseId()));
 | 
				
			||||||
 | 
					                        sort($releasesToDelete);
 | 
				
			||||||
 | 
					                        $releasesToDeleteCount = count($releasesToDelete) - $maxReleases;
 | 
				
			||||||
 | 
					                        $releasesToDelete = array_slice($releasesToDelete, 0, $releasesToDeleteCount + 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                        foreach ($releasesToDelete as $releaseIdToDelete) {
 | 
				
			||||||
 | 
					                            $directoryToDelete = $releasesDirectory . '/' . $releaseIdToDelete;
 | 
				
			||||||
 | 
					                            if ($directoryToDelete != '/') {
 | 
				
			||||||
 | 
					                                $command = 'rm -rf ' . $directoryToDelete;
 | 
				
			||||||
 | 
					                                $result = $result && $this->runCommandRemote($command);
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $result;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user