This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Executor.php
143 lines (120 loc) · 3.8 KB
/
Executor.php
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
<?php
/*
* This file is part of JoliCi.
*
* (c) Joel Wurtz <jwurtz@jolicode.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Joli\JoliCi;
use Docker\Docker;
use Docker\Context\Context;
use Docker\Exception\ImageNotFoundException;
use Docker\Image;
use Docker\Container as DockerContainer;
use Monolog\Logger;
class Executor
{
/**
* Docker client
*
* @var Docker
*/
protected $docker;
/**
* Logger to log message when building
*
* @var LoggerCallback
*/
protected $logger;
/**
* @var boolean Use cache when building
*/
private $usecache = true;
/**
* @var boolean Use cache when building
*/
private $quietBuild = true;
/**
* @var integer Default timeout for run
*/
private $timeout = 600;
/**
* @var string Base directory where builds are located
*/
private $buildPath;
public function __construct(LoggerCallback $logger, Docker $docker, $buildPath, $usecache = true, $quietBuild = true, $timeout = 600)
{
$this->logger = $logger;
$this->docker = $docker;
$this->usecache = $usecache;
$this->quietBuild = $quietBuild;
$this->timeout = $timeout;
$this->buildPath = $buildPath;
}
/**
* Test a build
*
* @param Job $build
* @param array|string $command
*
* @return integer
*/
public function test(Job $build, $command = null)
{
$exitCode = 1;
// 2 Create image for build
if (false !== $this->create($build)) {
// 3 Run test if build has created an image
$exitCode = $this->run($build, $command);
}
return $exitCode;
}
/**
* Create a build
*
* @param Job $build Build used to create image
*
* @return Image|boolean Return the image created if sucessful or false otherwise
*/
public function create(Job $build)
{
$context = new Context($this->buildPath . DIRECTORY_SEPARATOR . $build->getDirectory());
$this->docker->build($context, $build->getName(), $this->logger->getBuildCallback(), $this->quietBuild, $this->usecache, true);
$this->logger->clearStatic();
try {
return $this->docker->getImageManager()->find($build->getRepository(), $build->getTag());
} catch (ImageNotFoundException $e) {
return false;
}
}
/**
* Run a build (it's suppose the image exist in docker
*
* @param Job $build Build to run
* @param string|array $command Command to use when run the build (null, by default, will use the command registered to the image)
*
* @return integer The exit code of the command run inside (0 = success, otherwise it has failed)
*/
public function run(Job $build, $command = null)
{
$image = $this->docker->getImageManager()->find($build->getRepository(), $build->getTag());
$config = array('HostConfig' => array( 'Links' => array()));
foreach ($build->getServices() as $service) {
if ($service->getContainer()) {
$config['HostConfig']['Links'][] = sprintf('%s:%s', $service->getContainer()->getRuntimeInformations()['Name'], $service->getName());
}
}
$container = new DockerContainer($config);
if (is_string($command)) {
$command = array('/bin/bash', '-c', $command);
}
if (is_array($command)) {
$container->setCmd($command);
}
$container->setImage($image);
$this->docker->getContainerManager()->run($container, $this->logger->getRunCallback(), array(), false, $this->timeout);
return $container->getExitCode();
}
}