Development

/branches/1.1/lib/task/project/sfProjectDeployTask.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/task/project/sfProjectDeployTask.class.php

Revision 10501, 4.1 kB (checked in by nicolas, 6 years ago)

Reverted r10466: new features are only applied to unstable branch

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Deploys a project to another server.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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     ));
34
35     $this->aliases = array('sync');
36     $this->namespace = 'project';
37     $this->name = 'deploy';
38     $this->briefDescription = 'Deploys a project to another server';
39
40     $this->detailedDescription = <<<EOF
41 The [project:deploy|INFO] task deploys a project on a server:
42
43   [./symfony project:deploy production|INFO]
44
45 The server must be configured in [config/properties.ini|COMMENT]:
46
47   [[production]
48     host=www.example.com
49     port=22
50     user=fabien
51     dir=/var/www/sfblog/
52     type=rsync|INFO]
53
54 To automate the deployment, the task uses rsync over SSH.
55 You must configure SSH access with a key or configure the password
56 in [config/properties.ini|COMMENT].
57
58 By default, the task is in dry-mode. To do a real deployment, you
59 must pass the [--go|COMMENT] option:
60
61   [./symfony project:deploy --go production|INFO]
62
63 Files and directories configured in [config/rsync_exclude.txt|COMMENT] are
64 not deployed:
65
66   [.svn
67   /web/uploads/*
68   /cache/*
69   /log/*|INFO]
70
71 You can also create a [rsync.txt|COMMENT] and [rsync_include.txt|COMMENT] files.
72
73 If you need to customize the [rsync*.txt|COMMENT] files based on the server,
74 you can pass a [rsync-dir|COMMENT] option:
75
76   [./symfony project:deploy --go --rsync-dir=config/production production|INFO]
77 EOF;
78   }
79
80   /**
81    * @see sfTask
82    */
83   protected function execute($arguments = array(), $options = array())
84   {
85     $env = $arguments['server'];
86
87     $ini = sfConfig::get('sf_config_dir').'/properties.ini';
88     if (!file_exists($ini))
89     {
90       throw new sfCommandException('You must create a config/properties.ini file');
91     }
92
93     $properties = parse_ini_file($ini, true);
94
95     if (!isset($properties[$env]))
96     {
97       throw new sfCommandException(sprintf('You must define the configuration for server "%s" in config/properties.ini', $env));
98     }
99
100     $properties = $properties[$env];
101
102     if (!isset($properties['host']))
103     {
104       throw new sfCommandException('You must define a "host" entry.');
105     }
106
107     if (!isset($properties['dir']))
108     {
109       throw new sfCommandException('You must define a "dir" entry.');
110     }
111
112     $host = $properties['host'];
113     $dir  = $properties['dir'];
114     $user = isset($properties['user']) ? $properties['user'].'@' : '';
115
116     if (substr($dir, -1) != '/')
117     {
118       $dir .= '/';
119     }
120
121     $ssh = 'ssh';
122
123     if (isset($properties['port']))
124     {
125       $port = $properties['port'];
126       $ssh = '"ssh -p'.$port.'"';
127     }
128
129     if (isset($properties['parameters']))
130     {
131       $parameters = $properties['parameters'];
132     }
133     else
134     {
135       $parameters = '-azC --force --delete';
136       if (file_exists($options['rsync-dir'].'/rsync_exclude.txt'))
137       {
138         $parameters .= sprintf(' --exclude-from=%s/rsync_exclude.txt', $options['rsync-dir']);
139       }
140
141       if (file_exists($options['rsync-dir'].'/rsync_include.txt'))
142       {
143         $parameters .= sprintf(' --include-from=%s/rsync_include.txt', $options['rsync-dir']);
144       }
145
146       if (file_exists($options['rsync-dir'].'/rsync.txt'))
147       {
148         $parameters .= sprintf(' --files-from=%s/rsync.txt', $options['rsync-dir']);
149       }
150     }
151
152     $dryRun = $options['go'] ? '' : '--dry-run';
153
154     $this->log($this->getFilesystem()->sh("rsync --progress $dryRun $parameters -e $ssh ./ $user$host:$dir"));
155   }
156 }
157
Note: See TracBrowser for help on using the browser.