This repository was archived by the owner on Oct 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathDockerClient.php
94 lines (80 loc) · 2.77 KB
/
DockerClient.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
<?php
namespace Docker;
use Http\Client\Common\Plugin\ContentLengthPlugin;
use Http\Client\Common\Plugin\DecoderPlugin;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\PluginClient;
use Http\Client\HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use Http\Client\Socket\Client as SocketHttpClient;
use Psr\Http\Message\RequestInterface;
class DockerClient implements HttpClient
{
/**
* @var HttpClient
*/
private $httpClient;
public function __construct($socketClientOptions = [])
{
$messageFactory = new GuzzleMessageFactory();
$socketClient = new SocketHttpClient($messageFactory, $socketClientOptions);
$lengthPlugin = new ContentLengthPlugin();
$decodingPlugin = new DecoderPlugin();
$errorPlugin = new ErrorPlugin();
$this->httpClient = new PluginClient($socketClient, [
$errorPlugin,
$lengthPlugin,
$decodingPlugin
]);
}
/**
* (@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
{
return $this->httpClient->sendRequest($request);
}
/**
* @return DockerClient
*/
public static function create()
{
return new self([
'remote_socket' => 'unix:///var/run/docker.sock'
]);
}
/**
* Create a docker client from environment variables
*
* @return DockerClient
*
* @throws \RuntimeException Throw exception when invalid environment variables are given
*/
public static function createFromEnv()
{
$options = [
'remote_socket' => getenv('DOCKER_HOST') ? getenv('DOCKER_HOST') : 'unix:///var/run/docker.sock'
];
if (getenv('DOCKER_TLS_VERIFY') && getenv('DOCKER_TLS_VERIFY') == 1) {
if (!getenv('DOCKER_CERT_PATH')) {
throw new \RuntimeException('Connection to docker has been set to use TLS, but no PATH is defined for certificate in DOCKER_CERT_PATH docker environnement variable');
}
$cafile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'ca.pem';
$certfile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'cert.pem';
$keyfile = getenv('DOCKER_CERT_PATH').DIRECTORY_SEPARATOR.'key.pem';
$stream_context = [
'cafile' => $cafile,
'local_cert' => $certfile,
'local_pk' => $keyfile,
];
if (getenv('DOCKER_PEER_NAME')) {
$stream_context['peer_name'] = getenv('DOCKER_PEER_NAME');
}
$options['ssl'] = true;
$options['stream_context_options'] = [
'ssl' => $stream_context
];
}
return new self($options);
}
}