Skip to content

Commit 4849ce2

Browse files
alexxedgreg-1-anderson
authored andcommitted
Allow passing the file to docker-compose.yml and any other docker-compose arguments (#39)
1 parent 2c1fd13 commit 4849ce2

File tree

4 files changed

+126
-1
lines changed

4 files changed

+126
-1
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
### 2.0.2 -
4+
* When the transport is Docker, allow setting any docker-compose flags in the alias file
5+
36
### 2.0.1 - 2019/Apr/2
47

58
* Do not format output in RealTimeOutput

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,49 @@ This is equivalent to:
3232
```
3333
$process = $processManager->siteProcess($site_alias, ['git', '--untracked-files=no', 'status']);
3434
```
35+
### Transports
36+
#### SSH
37+
Wraps a command so that it runs on a remote system via the ssh cli.
38+
39+
Example:
40+
```yaml
41+
local:
42+
host: localhost
43+
uri: http://localhost
44+
ssh:
45+
options: -o PasswordAuthentication=no -i $HOME/.ssh/id_rsa
46+
47+
```
48+
#### Docker Compose
49+
Wraps a command so that it runs on a remote system via docker-compose.
50+
51+
Example:
52+
```yaml
53+
local:
54+
host: localhost
55+
uri: http://localhost
56+
docker:
57+
service: drupal
58+
compose:
59+
options: --project dockerComposeProjectName --file docker-compose.yml --project-directory dockerComposeWorkDir
60+
exec:
61+
options: --user www-data
62+
63+
```
64+
65+
The above would execute commands prefixed with:
66+
```
67+
docker-compose --project dockerComposeProjectName --file docker-compose.yml --project-directory dockerComposeWorkDir exec --user www-data -T drupal
68+
```
69+
70+
`docker.project` and `compose.options --project` do the same thing, docker.project existed before options.
71+
72+
`docker.service` is the exact name of the service as it appears in docker-compos.yml
73+
74+
Check the [docker-compose](https://docs.docker.com/compose/reference/overview/) manual for all available options.
75+
76+
#### Local
77+
Runs the command on the local system.
3578

3679
## Symfony 4
3780

src/Transport/DockerComposeTransport.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,14 @@ public function addChdir($cd, $args)
6262
protected function getTransport()
6363
{
6464
$transport = ['docker-compose'];
65-
if ($project = $this->siteAlias->get('docker.project', '')) {
65+
$project = $this->siteAlias->get('docker.project', '');
66+
$options = $this->siteAlias->get('docker.compose.options', '');
67+
if ($project && (strpos($options, '-p') === false || strpos($options, '--project') === false)) {
6668
$transport = array_merge($transport, ['-p', $project]);
6769
}
70+
if ($options) {
71+
$transport[] = Shell::preEscaped($options);
72+
}
6873
return array_merge($transport, ['exec']);
6974
}
7075

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Consolidation\SiteProcess;
4+
5+
use Consolidation\SiteProcess\Transport\DockerComposeTransport;
6+
use PHPUnit\Framework\TestCase;
7+
use Consolidation\SiteAlias\SiteAlias;
8+
9+
class DockerComposeTransportTest extends TestCase
10+
{
11+
/**
12+
* Data provider for testWrap.
13+
*/
14+
public function wrapTestValues()
15+
{
16+
return [
17+
[
18+
'docker-compose --project project --project-directory projectDir --file myCompose.yml exec -T --user root drupal ls',
19+
[
20+
'docker' => [
21+
'service' => 'drupal',
22+
'compose' => [
23+
'options' => '--project project --project-directory projectDir --file myCompose.yml'
24+
],
25+
'exec' => ['options' => '--user root']
26+
]
27+
],
28+
],
29+
[
30+
'docker-compose exec -T drupal ls',
31+
[
32+
'docker' => [
33+
'service' => 'drupal',
34+
]
35+
],
36+
],
37+
[
38+
'docker-compose --project project2 --file myCompose.yml exec -T drupal ls',
39+
[
40+
'docker' => [
41+
'service' => 'drupal',
42+
'project' => 'project1',
43+
'compose' => [
44+
'options' => '--project project2 --file myCompose.yml'
45+
]
46+
]
47+
],
48+
],
49+
[
50+
'docker-compose -p project1 --file myCompose.yml exec -T drupal ls',
51+
[
52+
'docker' => [
53+
'service' => 'drupal',
54+
'project' => 'project1',
55+
'compose' => [
56+
'options' => '--file myCompose.yml'
57+
]
58+
]
59+
],
60+
],
61+
];
62+
}
63+
64+
/**
65+
* @dataProvider wrapTestValues
66+
*/
67+
public function testWrap($expected, $siteAliasData)
68+
{
69+
$siteAlias = new SiteAlias($siteAliasData, '@alias.dev');
70+
$dockerTransport = new DockerComposeTransport($siteAlias);
71+
$actual = $dockerTransport->wrap(['ls']);
72+
$this->assertEquals($expected, implode(' ', $actual));
73+
}
74+
}

0 commit comments

Comments
 (0)