|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onigoetz\Deployer\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Net_SFTP; |
|
6
|
|
|
use Onigoetz\Deployer\Configuration\Environment; |
|
7
|
|
|
use Onigoetz\Deployer\Configuration\Sources\Cloned; |
|
8
|
|
|
use Onigoetz\Deployer\Configuration\Sources\Upload; |
|
9
|
|
|
use Onigoetz\Deployer\RemoteActionRunner; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
class DeployCommand extends BaseCommand |
|
15
|
|
|
{ |
|
16
|
|
|
protected function configure() |
|
17
|
|
|
{ |
|
18
|
|
|
$this |
|
19
|
|
|
->setName('server:deploy') |
|
20
|
|
|
->setDescription('Deploy the latest release') |
|
21
|
|
|
->addArgument('to', InputArgument::REQUIRED, 'The environment to deploy to'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!defined('VERBOSE')) { |
|
27
|
|
|
define('VERBOSE', $input->getOption('verbose')); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
$environment = $this->getEnvironment($input->getArgument('to'), $output); |
|
31
|
|
|
|
|
32
|
|
|
//Prepares the binary name |
|
33
|
|
|
$dest = $environment->getDirectories()->getNewBinaryName(); |
|
34
|
|
|
|
|
35
|
|
|
$output->writeln('This binary\'s name will be : ' . $dest); |
|
36
|
|
|
|
|
37
|
|
|
if ($environment->getSource() instanceof Upload) { |
|
38
|
|
|
//TODO :: prepare build |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->allServers( |
|
42
|
|
|
$environment, |
|
43
|
|
|
$input, |
|
44
|
|
|
$output, |
|
45
|
|
|
function ($ssh) use ($environment, $input, $output, $dest) { |
|
46
|
|
|
$this->deploy($input, $output, $ssh, $environment, $dest); |
|
47
|
|
|
} |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function deploy( |
|
52
|
|
|
InputInterface $input, |
|
53
|
|
|
OutputInterface $output, |
|
54
|
|
|
Net_SFTP $ssh, |
|
55
|
|
|
Environment $environment, |
|
56
|
|
|
$destination |
|
57
|
|
|
) { |
|
58
|
|
|
$destinationDir = dirname($destination); |
|
59
|
|
|
|
|
60
|
|
|
$runner = new RemoteActionRunner($output, $ssh); |
|
61
|
|
|
|
|
62
|
|
|
$this->runAction( |
|
63
|
|
|
'Create folders on server', |
|
64
|
|
|
$output, |
|
65
|
|
|
function () use ($runner, $destinationDir) { |
|
66
|
|
|
$runner->setupServer($destinationDir); |
|
67
|
|
|
} |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
if ($environment->getSource() instanceof Cloned) { |
|
71
|
|
|
$class = 'Onigoetz\\Deployer\\SCM\\' . ucfirst($environment->getSource()->getType()); |
|
72
|
|
|
if (!class_exists($class)) { |
|
73
|
|
|
throw new \Exception("Cannot find SCM '$class'"); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var \Onigoetz\Deployer\SCM\SCM |
|
78
|
|
|
*/ |
|
79
|
|
|
$scm = new $class($environment); |
|
80
|
|
|
|
|
81
|
|
|
$final = $environment->getSource()->getFinalUrl($this->getHelper('question'), $input, $output); |
|
82
|
|
|
|
|
83
|
|
|
$this->runAction( |
|
84
|
|
|
'Clone the latest version', |
|
85
|
|
|
$output, |
|
86
|
|
|
function () use ($scm, $ssh, $runner, $final, $destination) { |
|
87
|
|
|
$git = $scm->getCommand($ssh); |
|
88
|
|
|
|
|
89
|
|
|
return $runner->exec($scm->cloneCommand($git, $final, $destination)); |
|
90
|
|
|
} |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$substitutions = $environment->getSubstitutions($destination); |
|
95
|
|
|
|
|
96
|
|
|
$output->writeln('<fg=blue;options=bold>Before deployment actions</fg=blue;options=bold>'); |
|
97
|
|
|
$this->runActions($runner, $environment->getTasks('before'), $output, $substitutions); |
|
98
|
|
|
|
|
99
|
|
|
$output->writeln('<fg=blue;options=bold>Deployment</fg=blue;options=bold>'); |
|
100
|
|
|
$this->runAction( |
|
101
|
|
|
'Store the current deployment for eventual rollback', |
|
102
|
|
|
$output, |
|
103
|
|
|
function () use ($runner, $environment, $ssh) { |
|
104
|
|
|
$previous = $runner->getSymlinkDestination($environment->getDirectories()->getDeploy()); |
|
105
|
|
|
$ssh->put($environment->getDirectories()->getRoot() . '/previous', $previous); |
|
106
|
|
|
|
|
107
|
|
|
return "Previous snapshot : $previous"; |
|
108
|
|
|
} |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$this->runAction( |
|
112
|
|
|
'Symlink the new deployment', |
|
113
|
|
|
$output, |
|
114
|
|
|
function () use ($environment, $runner, $destination) { |
|
115
|
|
|
$deploy = $environment->getDirectories()->getDeploy(); |
|
116
|
|
|
|
|
117
|
|
|
$runner->rmfile($deploy); |
|
118
|
|
|
$runner->symlink($destination, $deploy); |
|
119
|
|
|
} |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
$output->writeln(''); |
|
123
|
|
|
$output->writeln('<fg=blue;options=bold>After deployment actions</fg=blue;options=bold>'); |
|
124
|
|
|
$this->runActions($runner, $environment->getTasks('after'), $output, $substitutions); |
|
125
|
|
|
|
|
126
|
|
|
$output->writeln('Done'); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|