Overview

Namespaces

  • GAubry
    • ErrorHandler
    • Helpers
    • Logger
    • Shell
  • Himedia
    • Padocc
      • DB
      • Minifier
      • Numbering
      • Properties
      • Task
        • Base
        • Extended
  • None
  • Psr
    • Log

Classes

  • B2CSwitchSymlink
  • BuildLanguage
  • CVSExport
  • GitExport
  • Minifier
  • SwitchSymlink
  • TwengaServers
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Himedia\Padocc\Task\Extended;
  4: 
  5: use Himedia\Padocc\AttributeProperties;
  6: use Himedia\Padocc\Task;
  7: use Himedia\Padocc\Task\Base\Sync;
  8: 
  9: /**
 10:  * Exporte tout ou partie du contenu d'un dépôt Git vers une ou plusieurs destinations.
 11:  * À inclure dans une tâche env ou target.
 12:  *
 13:  * Exploite le script '/src/inc/cvsexport.sh'.
 14:  * Réalise la synchronisation à l'aide d'une tâche sync avec la liste d'exclusion suivante
 15:  * (en plus des éventuels include et exclude spécifiés dans la tâche) : '.bzr/', '.cvsignore', '.git/',
 16:  * '.gitignore', '.svn/', 'cvslog.*', 'CVS', 'CVS.adm'.
 17:  *
 18:  * Attributs :
 19:  * - 'repository'
 20:  * - 'ref' : branche ou tag à déployer
 21:  * - 'localrepositorydir' : lieu temporaire d'extraction du contenu qui nous intéresse du dépôt avant de l'envoyer
 22:  * vers la destination ⇒ laisser à vide de manière générale,
 23:  * l'outil utilisera alors le répertoire $aConfig['dir']['repositories'].
 24:  * - 'srcsubdir' : sous-répertoire du dépôt qui nous intéresse
 25:  * - 'destdir'
 26:  * - 'include' : si l'on veut filtrer
 27:  * - 'exclude' : si l'on veut filtrer
 28:  *
 29:  * Exemple : <gitexport repository="git@git.twenga.com:rts/rts.git" ref="${REF}"
 30:  *     destdir="${SERVERS}:${BASEDIR}" exclude="config.* /Tests" />
 31:  *
 32:  * @author Geoffroy AUBRY <gaubry@hi-media.com>
 33:  */
 34: class GitExport extends Task
 35: {
 36:     /**
 37:      * Tâche de synchronisation sous-jacente.
 38:      * @var Sync
 39:      */
 40:     private $oSyncTask;
 41: 
 42:     /**
 43:      * {@inheritdoc}
 44:      */
 45:     protected function init()
 46:     {
 47:         parent::init();
 48: 
 49:         $this->aAttrProperties = array(
 50:             'repository' => AttributeProperties::FILE | AttributeProperties::REQUIRED,
 51:             'ref' => AttributeProperties::REQUIRED | AttributeProperties::ALLOW_PARAMETER,
 52:             'localrepositorydir' => AttributeProperties::DIR,
 53:             'srcsubdir' => AttributeProperties::DIR,
 54:             'destdir' => AttributeProperties::DIR | AttributeProperties::REQUIRED
 55:                 | AttributeProperties::ALLOW_PARAMETER,
 56:             // TODO AttributeProperties::DIRJOKER abusif ici, mais à cause du multivalué :
 57:             'include' => AttributeProperties::FILEJOKER | AttributeProperties::DIRJOKER,
 58:             'exclude' => AttributeProperties::FILEJOKER | AttributeProperties::DIRJOKER,
 59:         );
 60: 
 61:         // Valeur par défaut de l'attribut localrepositorydir :
 62:         if (empty($this->aAttValues['localrepositorydir'])) {
 63:             $this->aAttValues['localrepositorydir'] =
 64:                 $this->aConfig['dir']['repositories'] . '/git/'
 65:                 . $this->oProperties->getProperty('project_name') . '_'
 66:                 . $this->oProperties->getProperty('environment_name') . '_'
 67:                 . $this->sCounter;
 68:         } else {
 69:             $this->aAttValues['localrepositorydir'] =
 70:                 preg_replace('#/$#', '', $this->aAttValues['localrepositorydir']);
 71:         }
 72: 
 73:         // Création de la tâche de synchronisation sous-jacente :
 74:         $this->oNumbering->addCounterDivision();
 75:         if (empty($this->aAttValues['srcsubdir'])) {
 76:             $this->aAttValues['srcsubdir'] = '';
 77:         } else {
 78:             $this->aAttValues['srcsubdir'] = '/' . preg_replace('#^/|/$#', '', $this->aAttValues['srcsubdir']);
 79:         }
 80:         $aSyncAttributes = array(
 81:             'src' => $this->aAttValues['localrepositorydir'] . $this->aAttValues['srcsubdir'] . '/',
 82:             'destdir' => $this->aAttValues['destdir'],
 83:         );
 84:         if (! empty($this->aAttValues['include'])) {
 85:             $aSyncAttributes['include'] = $this->aAttValues['include'];
 86:         }
 87:         if (! empty($this->aAttValues['exclude'])) {
 88:             $aSyncAttributes['exclude'] = $this->aAttValues['exclude'];
 89:         }
 90:         $this->oSyncTask = Sync::getNewInstance($aSyncAttributes, $this->oProject, $this->oDIContainer);
 91:         $this->oNumbering->removeCounterDivision();
 92:     }
 93: 
 94:     /**
 95:      * {@inheritdoc}
 96:      * @codeCoverageIgnore
 97:      */
 98:     public static function getTagName ()
 99:     {
100:         return 'gitexport';
101:     }
102: 
103:     /**
104:      * Prépare la tâche avant exécution : vérifications basiques, analyse des serveurs concernés...
105:      */
106:     public function setUp ()
107:     {
108:         parent::setUp();
109:         $this->getLogger()->info('+++');
110: 
111:         // La tâche Sync vérifie que le 'srcsubdir' existe bien. Mais s'il s'agit du 1er déploiement
112:         // de l'env concerné, alors le mkdir au niveau GitExport ne sera pas encore réalisé au moment
113:         // du check Sync… d'où la levée d'une exception, que l'on mange !
114:         try {
115:             $this->oSyncTask->setUp();
116:         } catch (\UnexpectedValueException $oException) {
117:             if ($oException->getMessage() !== "File or directory '" . $this->aAttValues['localrepositorydir']
118:                 . $this->aAttValues['srcsubdir'] . '/' . "' not found!") {
119:                 throw $oException;
120:             }
121:         }
122: 
123:         $this->getLogger()->info('---');
124:     }
125: 
126:     /**
127:      * Phase de traitements centraux de l'exécution de la tâche.
128:      * Elle devrait systématiquement commencer par "parent::centralExecute();".
129:      * Appelé par execute().
130:      * @see execute()
131:      */
132:     protected function centralExecute ()
133:     {
134:         parent::centralExecute();
135:         $this->getLogger()->info('+++');
136: 
137:         $aRef = $this->processPath($this->aAttValues['ref']);
138:         $sRef = $aRef[0];
139: 
140:         $sMsg = "Export '$sRef' reference from '" . $this->aAttValues['repository'] . "' git repository+++";
141:         $this->getLogger()->info($sMsg);
142:         $aResult = $this->oShell->exec(
143:             $this->aConfig['bash_path'] . ' ' . $this->aConfig['dir']['inc'] . '/gitexport.sh'
144:             . ' "' . $this->aAttValues['repository'] . '"'
145:             . ' "' . $sRef . '"'
146:             . ' "' . $this->aAttValues['localrepositorydir'] . '"'
147:         );
148:         $this->getLogger()->info(implode("\n", $aResult) . '---');
149: 
150:         $this->oSyncTask->execute();
151:         $this->getLogger()->info('---');
152:     }
153: }
154: 
Platform for Automatized Deployments with pOwerful Concise Configuration API documentation generated by ApiGen 2.8.0