| 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 |
|
|---|
| 22 |
* @see sfTask |
|---|
| 23 |
*/ |
|---|
| 24 |
protected function configure() |
|---|
| 25 |
{ |
|---|
| 26 |
$this->addArguments(array( |
|---|
| 27 |
new sfCommandArgument('server', sfCommandArgument::REQUIRED, 'The server name'), |
|---|
| 28 |
)); |
|---|
| 29 |
|
|---|
| 30 |
$this->addOptions(array( |
|---|
| 31 |
new sfCommandOption('go', null, sfCommandOption::PARAMETER_NONE, 'Do the deployment'), |
|---|
| 32 |
new sfCommandOption('rsync-dir', null, sfCommandOption::PARAMETER_REQUIRED, 'The directory where to look for rsync*.txt files', 'config'), |
|---|
| 33 |
new sfCommandOption('rsync-options', null, sfCommandOption::PARAMETER_OPTIONAL, 'To options to pass to the rsync executable', '-azC --force --delete'), |
|---|
| 34 |
)); |
|---|
| 35 |
|
|---|
| 36 |
$this->aliases = array('sync'); |
|---|
| 37 |
$this->namespace = 'project'; |
|---|
| 38 |
$this->name = 'deploy'; |
|---|
| 39 |
$this->briefDescription = 'Deploys a project to another server'; |
|---|
| 40 |
|
|---|
| 41 |
$this->detailedDescription = <<<EOF |
|---|
| 42 |
The [project:deploy|INFO] task deploys a project on a server: |
|---|
| 43 |
|
|---|
| 44 |
[./symfony project:deploy production|INFO] |
|---|
| 45 |
|
|---|
| 46 |
The server must be configured in [config/properties.ini|COMMENT]: |
|---|
| 47 |
|
|---|
| 48 |
[[production] |
|---|
| 49 |
host=www.example.com |
|---|
| 50 |
port=22 |
|---|
| 51 |
user=fabien |
|---|
| 52 |
dir=/var/www/sfblog/ |
|---|
| 53 |
type=rsync|INFO] |
|---|
| 54 |
|
|---|
| 55 |
To automate the deployment, the task uses rsync over SSH. |
|---|
| 56 |
You must configure SSH access with a key or configure the password |
|---|
| 57 |
in [config/properties.ini|COMMENT]. |
|---|
| 58 |
|
|---|
| 59 |
By default, the task is in dry-mode. To do a real deployment, you |
|---|
| 60 |
must pass the [--go|COMMENT] option: |
|---|
| 61 |
|
|---|
| 62 |
[./symfony project:deploy --go production|INFO] |
|---|
| 63 |
|
|---|
| 64 |
Files and directories configured in [config/rsync_exclude.txt|COMMENT] are |
|---|
| 65 |
not deployed: |
|---|
| 66 |
|
|---|
| 67 |
[.svn |
|---|
| 68 |
/web/uploads/* |
|---|
| 69 |
/cache/* |
|---|
| 70 |
/log/*|INFO] |
|---|
| 71 |
|
|---|
| 72 |
You can also create a [rsync.txt|COMMENT] and [rsync_include.txt|COMMENT] files. |
|---|
| 73 |
|
|---|
| 74 |
If you need to customize the [rsync*.txt|COMMENT] files based on the server, |
|---|
| 75 |
you can pass a [rsync-dir|COMMENT] option: |
|---|
| 76 |
|
|---|
| 77 |
[./symfony project:deploy --go --rsync-dir=config/production production|INFO] |
|---|
| 78 |
|
|---|
| 79 |
Last, you can specify the options passed to the rsync executable, using the |
|---|
| 80 |
[rsync-options|INFO] option (defaults are [-azC --force --delete --progress|INFO]): |
|---|
| 81 |
|
|---|
| 82 |
[./symfony project:deploy --go --rsync-options=-avz|INFO] |
|---|
| 83 |
EOF; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
* @see sfTask |
|---|
| 88 |
*/ |
|---|
| 89 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 90 |
{ |
|---|
| 91 |
$env = $arguments['server']; |
|---|
| 92 |
|
|---|
| 93 |
$ini = sfConfig::get('sf_config_dir').'/properties.ini'; |
|---|
| 94 |
if (!file_exists($ini)) |
|---|
| 95 |
{ |
|---|
| 96 |
throw new sfCommandException('You must create a config/properties.ini file'); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
$properties = parse_ini_file($ini, true); |
|---|
| 100 |
|
|---|
| 101 |
if (!isset($properties[$env])) |
|---|
| 102 |
{ |
|---|
| 103 |
throw new sfCommandException(sprintf('You must define the configuration for server "%s" in config/properties.ini', $env)); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
$properties = $properties[$env]; |
|---|
| 107 |
|
|---|
| 108 |
if (!isset($properties['host'])) |
|---|
| 109 |
{ |
|---|
| 110 |
throw new sfCommandException('You must define a "host" entry.'); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
if (!isset($properties['dir'])) |
|---|
| 114 |
{ |
|---|
| 115 |
throw new sfCommandException('You must define a "dir" entry.'); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
$host = $properties['host']; |
|---|
| 119 |
$dir = $properties['dir']; |
|---|
| 120 |
$user = isset($properties['user']) ? $properties['user'].'@' : ''; |
|---|
| 121 |
|
|---|
| 122 |
if (substr($dir, -1) != '/') |
|---|
| 123 |
{ |
|---|
| 124 |
$dir .= '/'; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
$ssh = 'ssh'; |
|---|
| 128 |
|
|---|
| 129 |
if (isset($properties['port'])) |
|---|
| 130 |
{ |
|---|
| 131 |
$port = $properties['port']; |
|---|
| 132 |
$ssh = '"ssh -p'.$port.'"'; |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
if (isset($properties['parameters'])) |
|---|
| 136 |
{ |
|---|
| 137 |
$parameters = $properties['parameters']; |
|---|
| 138 |
} |
|---|
| 139 |
else |
|---|
| 140 |
{ |
|---|
| 141 |
$parameters = $options['rsync-options']; |
|---|
| 142 |
if (file_exists($options['rsync-dir'].'/rsync_exclude.txt')) |
|---|
| 143 |
{ |
|---|
| 144 |
$parameters .= sprintf(' --exclude-from=%s/rsync_exclude.txt', $options['rsync-dir']); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
if (file_exists($options['rsync-dir'].'/rsync_include.txt')) |
|---|
| 148 |
{ |
|---|
| 149 |
$parameters .= sprintf(' --include-from=%s/rsync_include.txt', $options['rsync-dir']); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
if (file_exists($options['rsync-dir'].'/rsync.txt')) |
|---|
| 153 |
{ |
|---|
| 154 |
$parameters .= sprintf(' --files-from=%s/rsync.txt', $options['rsync-dir']); |
|---|
| 155 |
} |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
$dryRun = $options['go'] ? '' : '--dry-run'; |
|---|
| 159 |
|
|---|
| 160 |
$this->log($this->getFilesystem()->sh("rsync --progress $dryRun $parameters -e $ssh ./ $user$host:$dir")); |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|