-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDeploymentCommand.php
More file actions
175 lines (147 loc) · 6.48 KB
/
DeploymentCommand.php
File metadata and controls
175 lines (147 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace BeSimple\DeploymentBundle\Command;
use Symfony\Component\EventDispatcher\EventInterface;
use BeSimple\DeploymentBundle\Deployer\Deployer;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand as BaseCommand;
use BeSimple\DeploymentBundle\Events;
use BeSimple\DeploymentBundle\Event\CommandEvent;
use BeSimple\DeploymentBundle\Event\DeployerEvent;
use BeSimple\DeploymentBundle\Event\FeedbackEvent;
abstract class DeploymentCommand extends BaseCommand
{
/**
* @return void
*/
protected function configure()
{
parent::configure();
$this
->setDefinition(array(
new InputArgument('server', InputArgument::OPTIONAL, 'The target server name', null),
new InputOption('tag', 't', InputOption::VALUE_NONE, 'Tag with git if real deployment')
))
;
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$deployer = $this->getContainer()->get('be_simple_deployment.deployer');
$eventDispatcher = $this->getContainer()->get('event_dispatcher');
$this->output = $output;
$this->output->setDecorated(true);
$self = $this;
if ($output->getVerbosity() > Output::VERBOSITY_QUIET) {
// Start event
$eventDispatcher->addListener(Events::onDeploymentStart, function (DeployerEvent $event) use ($self) {
$self->write(sprintf(
'Starting %s deployment on server "%s"',
$event->isTest() ? 'test' : 'real',
$event->getServer()
));
});
$eventDispatcher->addListener(Events::onDeploymentSshStart, function (CommandEvent $event) use ($self) {
$self->write(sprintf(
'[Try SSH Command] %s',
$event->getCommand()
));
});
// Feedback events
if ($output->getVerbosity() > Output::VERBOSITY_NORMAL) {
$eventDispatcher->addListener(Events::onDeploymentRsyncFeedback, function (FeedbackEvent $event) use ($self) {
$self->write(sprintf(
'[Rsync] %s',
$event->getFeedback()
), $event->isError() ? 'error' : 'comment');
});
$eventDispatcher->addListener(Events::onDeploymentSshFeedback, function (FeedbackEvent $event) use ($self) {
$self->write(sprintf(
'[SSH] %s',
$event->getFeedback()
), $event->isError() ? 'error' : 'comment');
});
}
// Success events
$eventDispatcher->addListener(Events::onDeploymentSuccess, function (DeployerEvent $event) use ($self) {
$self->write(sprintf(
'%s deployment on server "%s" succeded',
$event->isTest() ? 'Test' : 'Real',
$event->getServer()
), 'info');
});
if($input->getOption('tag')){
$eventDispatcher->addListener(Events::onDeploymentSuccess, function (DeployerEvent $event) use ($self) {
if(!$event->isTest()){
$path = realpath($this->getApplication()->getKernel()->getRootDir().'/..');
if(is_dir($path)){
$tag = 'deploy-'. strtolower($event->getServer());
$self->write(sprintf(
'Trying to set GIT tag %s in %s',
$tag,
$path
), 'comment');
$gitCmd = sprintf(
'git --git-dir=%s --work-tree=%s',
$path.'/.git',
$path
);
$status = shell_exec($gitCmd.' status -s');
if($status !== null){
$self->write(sprintf('There are uncommitted changes - wont set the tag: %s', $status), 'error');
return;
}
$commit = shell_exec($gitCmd.' rev-parse HEAD');
if(!$commit){
$self->write('Last commit-hash not found for HEAD', 'error');
return;
}
$tagResult = shell_exec($gitCmd.' tag -f '. $tag .' '. $commit);
if(!$tagResult){
$self->write('Tagging seems ok. Try "git push --tags"', 'info');
return;
}
$self->write($tagResult, 'info');
}
}
});
}
$eventDispatcher->addListener(Events::onDeploymentRsyncSuccess, function (CommandEvent $event) use ($self) {
$self->write(sprintf(
'[Rsync success] %s',
$event->getCommand()
));
}, 'info');
$eventDispatcher->addListener(Events::onDeploymentSshSuccess, function (CommandEvent $event) use ($self) {
$self->write(sprintf(
'[SSH success] %s',
$event->getCommand()
));
}, 'info');
}
$this->executeDeployment($deployer, $input->getArgument('server'));
}
/**
* @param string $message
* @param string $style
* @return void
*/
public function write($message, $style = 'comment')
{
$this->output->writeln(sprintf('<%s>%s</%s>', $style, $message, $style));
}
/**
* @abstract
* @param \BeSimple\DeploymentBundle\Deployer\Deployer $deployer
* @param string $server
* @return void
*/
abstract protected function executeDeployment(Deployer $deployer, $server);
}