1 <?php
2 3 4 5 6 7 8 9
10
11 12 13 14 15 16 17 18
19 class P3RsyncCommand extends CConsoleCommand
20 {
21 22 23 24
25 public $servers;
26 27 28 29
30 public $aliases;
31 32 33 34
35 public $params;
36
37 public function getHelp() {
38 echo <<<EOS
39 Usage: yiic rsync <server:src> <server:dest> <alias>
40
41 Specify the shorthands in config/console.php, make sure the
42 URLs point to the yii webapp directory (usually 'protected').
43
44 'commandMap' => array(
45 'rsync'=>array(
46 'class' => 'ext.phundament.p3extensions.commands.P3RsyncCommand',
47 'servers' => array(
48 'dev' => realpath(dirname(__FILE__).'/..'), // local development path
49 'prod' => 'user@example.com:/path/to/webapp/protected', // remote url
50 ),
51 'aliases' => array(
52 'data' => 'application.data' // alias to be synced
53 ),
54 ),
55 ),
56
57 Note: One server location has to be a local file path!
58
59 EOS;
60 }
61
62 63 64 65
66 public function run($args) {
67 if (!isset($this->servers)) {
68 echo "No server specified in config!";
69 exit;
70 }
71
72 if (!isset($this->aliases)) {
73 echo "No alias defined in config!";
74 exit;
75 }
76
77 if (!isset($args[2])) {
78 $this->getHelp();
79 exit;
80 }
81 $src = $args[0];
82 $dest = $args[1];
83 $alias = $args[2];
84
85 $path = Yii::getPathOfAlias($this->aliases[$alias]);
86 $relativePath = str_replace(Yii::app()->basePath,"",$path);
87
88 $srcUrl = $this->servers[$src].$relativePath."/";
89 $destUrl = $this->servers[$dest].$relativePath."/";
90
91 echo "Start rsync of '".$alias."' (".$relativePath.") from '".$src."' to '".$dest."'? [Yes|No] ";
92 if(!strncasecmp(trim(fgets(STDIN)),'y',1)) {
93 $cmd = "rsync {$this->params} -av ".$srcUrl." ".$destUrl;
94 echo "\n".$cmd."\n";
95 system($cmd, $output);
96 } else {
97 echo "Skipped.\n";
98 }
99 }
100
101 }
102
103 ?>