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: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class SwitchSymlink extends Link
27: {
28: 29: 30: 31: 32:
33: private static $iNbInstances = 0;
34:
35: 36: 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: 52: 53:
54: public static function getTagName ()
55: {
56: return 'switchsymlink';
57: }
58:
59: 60: 61: 62: 63: 64:
65: public static function getNbInstances ()
66: {
67: return self::$iNbInstances;
68: }
69:
70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 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: 107: 108: 109: 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: 133: 134: 135: 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: