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 GAubry\Shell\PathStatus;
  6: use Himedia\Padocc\AttributeProperties;
  7: use Himedia\Padocc\Task\Base\Environment;
  8: use Himedia\Padocc\Task\Base\Link;
  9: 
 10: /**
 11:  * Permute les liens symboliques de la dernière release vers la nouvelle à la fin du déploiement.
 12:  * Tâche ajoutée par défaut en tant que dernière tâche de l'environnement, si et seulement si aucune
 13:  * tâche SwitchSymlink ou fille (comme B2CSwitchSymlink) n'est spécifiée dans le XML,
 14:  * et si l'attribut withsymlinks de la tâche env vaut true. À inclure en toute fin de tâche env ou target.
 15:  *
 16:  * Attributs :
 17:  * - 'src' : laisser à vide à moins d'être bien conscient des conséquences
 18:  * - 'target' : laisser à vide à moins d'être bien conscient des conséquences
 19:  * - 'server' : laisser à vide à moins d'être bien conscient des conséquences
 20:  *
 21:  * Exemple : <switchsymlink />
 22:  *
 23:  * @category TwengaDeploy
 24:  * @author Geoffroy AUBRY <gaubry@hi-media.com>
 25:  */
 26: class SwitchSymlink extends Link
 27: {
 28:     /**
 29:      * Compteur d'instances de la classe.
 30:      * @var int
 31:      * @see getNbInstances()
 32:      */
 33:     private static $iNbInstances = 0;
 34: 
 35:     /**
 36:      * {@inheritdoc}
 37:      */
 38:     protected function init()
 39:     {
 40:         parent::init();
 41: 
 42:         $this->aAttrProperties = array(
 43:             'src' => AttributeProperties::FILE | AttributeProperties::DIR | AttributeProperties::ALLOW_PARAMETER,
 44:             'target' => AttributeProperties::FILE | AttributeProperties::DIR | AttributeProperties::ALLOW_PARAMETER,
 45:             'server' => AttributeProperties::ALLOW_PARAMETER
 46:         );
 47:         self::$iNbInstances++;
 48:     }
 49: 
 50:     /**
 51:      * {@inheritdoc}
 52:      * @codeCoverageIgnore
 53:      */
 54:     public static function getTagName ()
 55:     {
 56:         return 'switchsymlink';
 57:     }
 58: 
 59:     /**
 60:      * Accesseur au compteur d'instances de la classe.
 61:      *
 62:      * @return int nombre d'instances de la classe.
 63:      * @see $iNbInstances
 64:      */
 65:     public static function getNbInstances ()
 66:     {
 67:         return self::$iNbInstances;
 68:     }
 69: 
 70:     /**
 71:      * Vérifie au moyen de tests basiques que la tâche peut être exécutée.
 72:      * Lance une exception si tel n'est pas le cas.
 73:      *
 74:      * Comme toute les tâches sont vérifiées avant que la première ne soit exécutée,
 75:      * doit permettre de remonter au plus tôt tout dysfonctionnement.
 76:      * Appelé avant la méthode execute().
 77:      *
 78:      * @throws \UnexpectedValueException en cas d'attribut ou fichier manquant
 79:      * @throws \DomainException en cas de valeur non permise
 80:      */
 81:     public function check ()
 82:     {
 83:         if (! isset($this->aAttValues['src'])
 84:             && ! isset($this->aAttValues['target'])
 85:             && ! isset($this->aAttValues['server'])
 86:         ) {
 87:             $sBaseSymLink = $this->oProperties->getProperty('basedir');
 88:             $sRollbackID = $this->oProperties->getProperty('rollback_id');
 89:             if ($sRollbackID !== '') {
 90:                 $this->getLogger()->info("Rollback to '$sRollbackID' requested.");
 91:                 $sID = $sRollbackID;
 92:             } else {
 93:                 $sID = $this->oProperties->getProperty('execution_id');
 94:             }
 95:             $sReleaseSymLink = $sBaseSymLink . $this->aConfig['symlink_releases_dir_suffix'] . '/' . $sID;
 96: 
 97:             $this->aAttValues['src'] = $sBaseSymLink;
 98:             $this->aAttValues['target'] = $sReleaseSymLink;
 99:             $this->aAttValues['server'] = '${' . Environment::SERVERS_CONCERNED_WITH_BASE_DIR . '}';
100:         }
101: 
102:         parent::check();
103:     }
104: 
105:     /**
106:      * Phase de traitements centraux de l'exécution de la tâche.
107:      * Elle devrait systématiquement commencer par "parent::centralExecute();".
108:      * Appelé par execute().
109:      * @see execute()
110:      */
111:     protected function centralExecute ()
112:     {
113:         $this->getLogger()->info('+++');
114:         if ($this->oProperties->getProperty('with_symlinks') === 'true') {
115:             if ($this->oProperties->getProperty(Environment::SERVERS_CONCERNED_WITH_BASE_DIR) == '') {
116:                 $this->getLogger()->info('No release found.');
117:             } else {
118:                 $this->oProperties->setProperty('with_symlinks', 'false');
119:                 $this->checkTargets();
120:                 $this->getLogger()->info('---');
121:                 parent::centralExecute();
122:                 $this->getLogger()->info('+++');
123:                 $this->oProperties->setProperty('with_symlinks', 'true');
124:             }
125:         } else {
126:             $this->getLogger()->info("Mode 'withsymlinks' is off: nothing to do.");
127:         }
128:         $this->getLogger()->info('---');
129:     }
130: 
131:     /**
132:      * Vérifie que chaque répertoire cible des liens existe.
133:      * Notamment nécessaire en cas de rollback.
134:      *
135:      * @throws \RuntimeException si l'un des répertoires cible des liens n'existe pas
136:      */
137:     protected function checkTargets ()
138:     {
139:         $this->getLogger()->info('Check that all symlinks targets exists.+++');
140: 
141:         $aValidStatus = array(
142:             PathStatus::STATUS_DIR,
143:             PathStatus::STATUS_SYMLINKED_DIR
144:         );
145: 
146:         $sPath = $this->aAttValues['target'];
147:         $aServers = $this->expandPath($this->aAttValues['server']);
148:         $aPathStatusResult = $this->oShell->getParallelSSHPathStatus($sPath, $aServers);
149:         foreach ($aServers as $sServer) {
150:             $sExpandedPath = $sServer . ':' . $sPath;
151:             if (! in_array($aPathStatusResult[$sServer], $aValidStatus)) {
152:                 $sMsg = "Target attribute must be a directory or a symlink to a directory: '" . $sExpandedPath . "'";
153:                 throw new \RuntimeException($sMsg);
154:             }
155:         }
156: 
157:         $this->getLogger()->info('---');
158:     }
159: }
160: 
Platform for Automatized Deployments with pOwerful Concise Configuration API documentation generated by ApiGen 2.8.0