Skip to content

Commit fbe9397

Browse files
author
Frederic Dewinne
committed
add deploy logger
1 parent d57df5f commit fbe9397

File tree

8 files changed

+91
-5
lines changed

8 files changed

+91
-5
lines changed

features/bootstrap/CliContext.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,25 @@ private function getOutput()
130130

131131
return trim(preg_replace("/ +$/m", '', $output));
132132
}
133+
134+
/**
135+
* Checks whether specified file exists and contains specified string.
136+
*
137+
* @Then /^"([^"]*)" file should contain:$/
138+
*
139+
* @param string $path file path
140+
* @param PyStringNode $text file content
141+
*/
142+
public function fileShouldContain($path, PyStringNode $text)
143+
{
144+
\PHPUnit_Framework_Assert::assertFileExists($path);
145+
146+
$fileContent = trim(file_get_contents($path));
147+
// Normalize the line endings in the output
148+
if ("\n" !== PHP_EOL) {
149+
$fileContent = str_replace(PHP_EOL, "\n", $fileContent);
150+
}
151+
152+
\PHPUnit_Framework_Assert::assertContains($this->getExpectedOutput($text), $fileContent);
153+
}
133154
}

features/bootstrap/DeployAgentContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public function prepareFileSystem()
106106
if (!file_exists('/tmp/test')) {
107107
mkdir('/tmp/test', 0777, true);
108108
}
109+
touch('data/logs/deploy.log');
110+
$mask = umask(0);
111+
chmod('data/logs/deploy.log', 0775);
112+
umask($mask);
109113
}
110114

111115
/**
@@ -115,6 +119,7 @@ public function cleanFileSystem()
115119
{
116120
exec('rm -Rf /tmp/test');
117121
exec('rm -Rf data/packages/*');
122+
unlink('data/logs/deploy.log');
118123
}
119124

120125
/**

features/deploy-continuousphp-application.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@ Feature: Deploy a specific build of a configured application provided by continu
1717
And file "./data/packages/deploy-agent-staging/3a4c7c3d-27db-4221-aaf5-401de8aa09c3.tar.gz" should exist
1818
And file "/tmp/test/application/3a4c7c3d-27db-4221-aaf5-401de8aa09c3/README.md" should exist
1919
And file "/tmp/test/application/current/continuousphp.package" should match "/tmp/test/application/3a4c7c3d-27db-4221-aaf5-401de8aa09c3/continuousphp.package"
20+
And the output should contain:
21+
"""
22+
Downloading package...
23+
Extracting package...
24+
Starting deploy-agent-staging (3a4c7c3d-27db-4221-aaf5-401de8aa09c3)
25+
deploy-agent-staging (3a4c7c3d-27db-4221-aaf5-401de8aa09c3) has successfully started
26+
"""
27+
And "./data/logs/deploy.log" file should contain:
28+
"""
29+
deploy-agent-staging deployment launched from CLI for build 3a4c7c3d-27db-4221-aaf5-401de8aa09c3
30+
"""
31+
And "./data/logs/deploy.log" file should contain:
32+
"""
33+
Downloading package...
34+
"""
35+
And "./data/logs/deploy.log" file should contain:
36+
"""
37+
Starting deploy-agent-staging (3a4c7c3d-27db-4221-aaf5-401de8aa09c3)
38+
"""
39+
And "./data/logs/deploy.log" file should contain:
40+
"""
41+
deploy-agent-staging (3a4c7c3d-27db-4221-aaf5-401de8aa09c3) has successfully started
42+
"""
2043

2144
@filesystem
2245
Scenario: Deploy a build with hooks using CLI

module/DeployAgent/config/module.config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
'provider/continuousphp' => false,
3434
'application/application' => false,
3535
],
36+
'abstract_factories' =>
37+
[
38+
'Zend\Log\LoggerAbstractServiceFactory'
39+
],
3640
'initializers' =>
3741
[
3842
'Continuous\\DeployAgent\\EntityManagerInitializer'
@@ -168,7 +172,7 @@
168172
],
169173
'log' =>
170174
[
171-
'DeployLog' =>
175+
'logger/deploy' =>
172176
[
173177
'writers' =>
174178
[

module/DeployAgent/src/Controller/ApplicationController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ public function deployAction()
9999

100100
$application->getEventManager()->attachAggregate($taskManager);
101101

102-
$application->getEventManager()->trigger(Application::EVENT_INSTALL, $application, ['build' => $build]);
102+
$application->getEventManager()->trigger(
103+
Application::EVENT_INSTALL,
104+
$application,
105+
[
106+
'build' => $build,
107+
'source' => 'CLI'
108+
]
109+
);
103110

104111
return false;
105112
}

module/DeployAgent/src/Controller/HookController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,14 @@ public function deployAction()
4747

4848
$application->getEventManager()->attachAggregate($taskManager);
4949

50-
$application->getEventManager()->trigger(Application::EVENT_INSTALL, $application, ['build' => $build]);
50+
$application->getEventManager()->trigger(
51+
Application::EVENT_INSTALL,
52+
$application,
53+
[
54+
'build' => $build,
55+
'source' => 'webhook'
56+
]
57+
);
5158

5259
return false;
5360
}

module/DeployAgent/src/Task/TaskManager.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
use Zend\EventManager\EventManagerInterface;
2525
use Zend\EventManager\ListenerAggregateInterface;
2626
use Zend\Console\Adapter\AdapterInterface as ConsoleAdapter;
27+
use Zend\Log\LoggerAwareInterface;
28+
use Zend\Log\LoggerAwareTrait;
2729
use Zend\Stdlib\Hydrator\ClassMethods;
2830

2931
/**
@@ -33,8 +35,9 @@
3335
* @subpackage Task
3436
* @license http://opensource.org/licenses/Apache-2.0 Apache License, Version 2.0
3537
*/
36-
class TaskManager implements ListenerAggregateInterface
38+
class TaskManager implements ListenerAggregateInterface, LoggerAwareInterface
3739
{
40+
use LoggerAwareTrait;
3841

3942
/**
4043
* @var ConsoleAdapter
@@ -234,11 +237,16 @@ public function install(EventInterface $event)
234237
$this->setApplication($application);
235238
$this->setBuild($build);
236239

240+
$message = $application->getName() . ' deployment launched from ' . $event->getParam('source')
241+
. ' for build ' . $build;
242+
$this->getLogger()->debug($message);
243+
237244
/** @var Continuousphp $provider */
238245
$provider = $application->getProvider();
239246
$origin = $provider->getSource($build);
240247
$origin->setFilename($build . '.tar.gz');
241-
248+
249+
$this->getLogger()->info('Downloading package...');
242250
if ($this->getConsole()) {
243251
$origin->getEventManager()->attach(
244252
DeployEvent::EVENT_FETCH_PRE,
@@ -258,6 +266,7 @@ function (DeployEvent $event) {
258266

259267
$resource = new Archive($resourcePath);
260268

269+
$this->getLogger()->info('Extracting package...');
261270
if ($this->getConsole()) {
262271
$resource->getEventManager()->attach(
263272
DeployEvent::EVENT_FETCH_PRE,
@@ -305,6 +314,8 @@ public function activate(EventInterface $event)
305314

306315
$application->getEventManager()->trigger($application::EVENT_BEFORE_ACTIVATE);
307316

317+
$message = 'Starting ' . $application->getName() . ' (' . $build . ')';
318+
$this->getLogger()->info($message);
308319
if ($this->getConsole()) {
309320
$this->getConsole()
310321
->writeLine(
@@ -320,6 +331,8 @@ public function activate(EventInterface $event)
320331

321332
$application->getEventManager()->trigger($application::EVENT_AFTER_ACTIVATE);
322333

334+
$message = $application->getName() . ' (' . $build . ') has successfully started';
335+
$this->getLogger()->info($message);
323336
if ($this->getConsole()) {
324337
$this->getConsole()
325338
->writeLine(
@@ -331,6 +344,7 @@ public function activate(EventInterface $event)
331344

332345
public function loadConfig($path)
333346
{
347+
$this->getLogger()->info('Applying config from ' . $path);
334348
$this->getConsole()->writeLine('Applying config from ' . $path, ColorInterface::WHITE);
335349
$configReader = new \Zend\Config\Reader\Yaml(['Symfony\Component\Yaml\Yaml', 'parse']);
336350
$config = $configReader->fromFile($path);

module/DeployAgent/src/Task/TaskManagerFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Continuous\DeployAgent\Task;
1212

1313
use League\Flysystem\Filesystem;
14+
use Zend\Log\Logger;
1415
use Zend\ServiceManager\FactoryInterface;
1516
use Zend\ServiceManager\ServiceLocatorInterface;
1617
use Zend\Console\Adapter\AdapterInterface as ConsoleAdapter;
@@ -40,6 +41,10 @@ public function createService(ServiceLocatorInterface $serviceLocator)
4041
}
4142
}
4243

44+
/** @var Logger $logger */
45+
$logger = $serviceLocator->get('logger/deploy');
46+
$taskManager->setLogger($logger);
47+
4348
$taskManager->setPackageStoragePath($serviceLocator->get('Config')['agent']['package_storage_path'])
4449
->setPackageFileSystem(
4550
new Filesystem($serviceLocator->get('BsbFlysystemAdapterManager')->get('packages'))

0 commit comments

Comments
 (0)