| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfProjectDeployTask extends sfBaseTask |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$outputBuffer = '', |
|---|
| 23 |
$errorBuffer = ''; |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
* @see sfTask |
|---|
| 27 |
*/ |
|---|
| 28 |
protected function configure() |
|---|
| 29 |
{ |
|---|
| 30 |
$this->addArguments(array( |
|---|
| 31 |
new sfCommandArgument('server', sfCommandArgument::REQUIRED, 'The server name'), |
|---|
| 32 |
)); |
|---|
| 33 |
|
|---|
| 34 |
$this->addOptions(array( |
|---|
| 35 |
new sfCommandOption('go', null, sfCommandOption::PARAMETER_NONE, 'Do the deployment'), |
|---|
| 36 |
new sfCommandOption('rsync-dir', null, sfCommandOption::PARAMETER_REQUIRED, 'The directory where to look for rsync*.txt files', 'config'), |
|---|
| 37 |
new sfCommandOption('rsync-options', null, sfCommandOption::PARAMETER_OPTIONAL, 'To options to pass to the rsync executable', '-azC --force --delete --progress'), |
|---|
| 38 |
)); |
|---|
| 39 |
|
|---|
| 40 |
$this->aliases = array('sync'); |
|---|
| 41 |
$this->namespace = 'project'; |
|---|
| 42 |
$this->name = 'deploy'; |
|---|
| 43 |
$this->briefDescription = 'Deploys a project to another server'; |
|---|
| 44 |
|
|---|
| 45 |
$this->detailedDescription = <<<EOF |
|---|
| 46 |
The [project:deploy|INFO] task deploys a project on a server: |
|---|
| 47 |
|
|---|
| 48 |
[./symfony project:deploy production|INFO] |
|---|
| 49 |
|
|---|
| 50 |
The server must be configured in [config/properties.ini|COMMENT]: |
|---|
| 51 |
|
|---|
| 52 |
[[production] |
|---|
| 53 |
host=www.example.com |
|---|
| 54 |
port=22 |
|---|
| 55 |
user=fabien |
|---|
| 56 |
dir=/var/www/sfblog/ |
|---|
| 57 |
type=rsync|INFO] |
|---|
| 58 |
|
|---|
| 59 |
To automate the deployment, the task uses rsync over SSH. |
|---|
| 60 |
You must configure SSH access with a key or configure the password |
|---|
| 61 |
in [config/properties.ini|COMMENT]. |
|---|
| 62 |
|
|---|
| 63 |
By default, the task is in dry-mode. To do a real deployment, you |
|---|
| 64 |
must pass the [--go|COMMENT] option: |
|---|
| 65 |
|
|---|
| 66 |
[./symfony project:deploy --go production|INFO] |
|---|
| 67 |
|
|---|
| 68 |
Files and directories configured in [config/rsync_exclude.txt|COMMENT] are |
|---|
| 69 |
not deployed: |
|---|
| 70 |
|
|---|
| 71 |
[.svn |
|---|
| 72 |
/web/uploads/* |
|---|
| 73 |
/cache/* |
|---|
| 74 |
/log/*|INFO] |
|---|
| 75 |
|
|---|
| 76 |
You can also create a [rsync.txt|COMMENT] and [rsync_include.txt|COMMENT] files. |
|---|
| 77 |
|
|---|
| 78 |
If you need to customize the [rsync*.txt|COMMENT] files based on the server, |
|---|
| 79 |
you can pass a [rsync-dir|COMMENT] option: |
|---|
| 80 |
|
|---|
| 81 |
[./symfony project:deploy --go --rsync-dir=config/production production|INFO] |
|---|
| 82 |
|
|---|
| 83 |
Last, you can specify the options passed to the rsync executable, using the |
|---|
| 84 |
[rsync-options|INFO] option (defaults are [-azC --force --delete --progress|INFO]): |
|---|
| 85 |
|
|---|
| 86 |
[./symfony project:deploy --go --rsync-options=-avz|INFO] |
|---|
| 87 |
EOF; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
* @see sfTask |
|---|
| 92 |
*/ |
|---|
| 93 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 94 |
{ |
|---|
| 95 |
$env = $arguments['server']; |
|---|
| 96 |
|
|---|
| 97 |
$ini = sfConfig::get('sf_config_dir').'/properties.ini'; |
|---|
| 98 |
if (!file_exists($ini)) |
|---|
| 99 |
{ |
|---|
| 100 |
throw new sfCommandException('You must create a config/properties.ini file'); |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
$properties = parse_ini_file($ini, true); |
|---|
| 104 |
|
|---|
| 105 |
if (!isset($properties[$env])) |
|---|
| 106 |
{ |
|---|
| 107 |
throw new sfCommandException(sprintf('You must define the configuration for server "%s" in config/properties.ini', $env)); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
$properties = $properties[$env]; |
|---|
| 111 |
|
|---|
| 112 |
if (!isset($properties['host'])) |
|---|
| 113 |
{ |
|---|
| 114 |
throw new sfCommandException('You must define a "host" entry.'); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
if (!isset($properties['dir'])) |
|---|
| 118 |
{ |
|---|
| 119 |
throw new sfCommandException('You must define a "dir" entry.'); |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
$host = $properties['host']; |
|---|
| 123 |
$dir = $properties['dir']; |
|---|
| 124 |
$user = isset($properties['user']) ? $properties['user'].'@' : ''; |
|---|
| 125 |
|
|---|
| 126 |
if (substr($dir, -1) != '/') |
|---|
| 127 |
{ |
|---|
| 128 |
$dir .= '/'; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
$ssh = 'ssh'; |
|---|
| 132 |
|
|---|
| 133 |
if (isset($properties['port'])) |
|---|
| 134 |
{ |
|---|
| 135 |
$port = $properties['port']; |
|---|
| 136 |
$ssh = '"ssh -p'.$port.'"'; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
if (isset($properties['parameters'])) |
|---|
| 140 |
{ |
|---|
| 141 |
$parameters = $properties['parameters']; |
|---|
| 142 |
} |
|---|
| 143 |
else |
|---|
| 144 |
{ |
|---|
| 145 |
$parameters = $options['rsync-options']; |
|---|
| 146 |
if (file_exists($options['rsync-dir'].'/rsync_exclude.txt')) |
|---|
| 147 |
{ |
|---|
| 148 |
$parameters .= sprintf(' --exclude-from=%s/rsync_exclude.txt', $options['rsync-dir']); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
if (file_exists($options['rsync-dir'].'/rsync_include.txt')) |
|---|
| 152 |
{ |
|---|
| 153 |
$parameters .= sprintf(' --include-from=%s/rsync_include.txt', $options['rsync-dir']); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
if (file_exists($options['rsync-dir'].'/rsync.txt')) |
|---|
| 157 |
{ |
|---|
| 158 |
$parameters .= sprintf(' --files-from=%s/rsync.txt', $options['rsync-dir']); |
|---|
| 159 |
} |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
$dryRun = $options['go'] ? '' : '--dry-run'; |
|---|
| 163 |
$command = "rsync $dryRun $parameters -e $ssh ./ $user$host:$dir"; |
|---|
| 164 |
|
|---|
| 165 |
$this->getFilesystem()->execute($command, $options['trace'] ? array($this, 'logOutput') : null, array($this, 'logErrors')); |
|---|
| 166 |
|
|---|
| 167 |
$this->clearBuffers(); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
public function logOutput($output) |
|---|
| 171 |
{ |
|---|
| 172 |
if (false !== $pos = strpos($output, "\n")) |
|---|
| 173 |
{ |
|---|
| 174 |
$this->outputBuffer .= substr($output, 0, $pos); |
|---|
| 175 |
$this->log($this->outputBuffer); |
|---|
| 176 |
$this->outputBuffer = substr($output, $pos + 1); |
|---|
| 177 |
} |
|---|
| 178 |
else |
|---|
| 179 |
{ |
|---|
| 180 |
$this->outputBuffer .= $output; |
|---|
| 181 |
} |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
public function logErrors($output) |
|---|
| 185 |
{ |
|---|
| 186 |
if (false !== $pos = strpos($output, "\n")) |
|---|
| 187 |
{ |
|---|
| 188 |
$this->errorBuffer .= substr($output, 0, $pos); |
|---|
| 189 |
$this->log($this->formatter->format($this->errorBuffer, 'ERROR')); |
|---|
| 190 |
$this->errorBuffer = substr($output, $pos + 1); |
|---|
| 191 |
} |
|---|
| 192 |
else |
|---|
| 193 |
{ |
|---|
| 194 |
$this->errorBuffer .= $output; |
|---|
| 195 |
} |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
protected function clearBuffers() |
|---|
| 199 |
{ |
|---|
| 200 |
if ($this->outputBuffer) |
|---|
| 201 |
{ |
|---|
| 202 |
$this->log($this->outputBuffer); |
|---|
| 203 |
$this->outputBuffer = ''; |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
if ($this->errorBuffer) |
|---|
| 207 |
{ |
|---|
| 208 |
$this->log($this->formatter->format($this->errorBuffer, 'ERROR')); |
|---|
| 209 |
$this->errorBuffer = ''; |
|---|
| 210 |
} |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|