Compare commits
34 commits
Author | SHA1 | Date | |
---|---|---|---|
1a31d9790e | |||
|
e1dfdc65f2 | ||
|
3ff9b0ccff | ||
|
60b3599c63 | ||
|
d1ef03be39 | ||
|
edaad5164c | ||
|
8cbae63adf | ||
|
03ab873e22 | ||
|
e1ad47ee9e | ||
|
ead07e8fa9 | ||
|
bf8d4b200f | ||
|
6ab4ce942b | ||
|
c025ca9266 | ||
|
1b9e7782e0 | ||
|
78c83037ee | ||
|
a72bc1e45f | ||
|
aa9cff3705 | ||
|
f6f67be46c | ||
|
27758c0f0a | ||
|
96ee45edc9 | ||
|
deac235afe | ||
|
2a744599c0 | ||
|
29e2e60ef8 | ||
|
fb1d7655f1 | ||
|
add1fa32c0 | ||
|
9a87507a83 | ||
|
1eb3843a45 | ||
|
155f0e1fad | ||
|
acc508b1d8 | ||
|
ec09768b7e | ||
|
66399cf913 | ||
|
cb072b3711 | ||
|
e90b9c6c96 | ||
|
40d4feb59f |
19 changed files with 1521 additions and 420 deletions
30
.github/workflows/Deploy.yml
vendored
Normal file
30
.github/workflows/Deploy.yml
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
name: Deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Cache Composer packages
|
||||
id: composer-cache
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: vendor
|
||||
key: ${{ runner.os }}-node-${{ hashFiles('**/composer.lock') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-node-
|
||||
|
||||
- name: Build and push Docker images
|
||||
uses: docker/build-push-action@v1
|
||||
with:
|
||||
username: ${{ secrets.DOCKER_USERNAME }}
|
||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||
repository: pmsipilot/docker-compose-viz
|
||||
tag_with_ref: true
|
43
.github/workflows/Test.yml
vendored
Normal file
43
.github/workflows/Test.yml
vendored
Normal file
|
@ -0,0 +1,43 @@
|
|||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Validate composer.json
|
||||
run: composer validate --ansi --strict
|
||||
|
||||
- name: Cache Composer packages
|
||||
id: composer-cache
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: vendor
|
||||
key: ${{ runner.os }}-node-${{ hashFiles('**/composer.lock') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-node-
|
||||
|
||||
- name: Install dependencies
|
||||
if: steps.composer-cache.outputs.cache-hit != 'true'
|
||||
run: composer install --prefer-dist --no-progress --no-suggest
|
||||
|
||||
- name: Unit tests
|
||||
run: composer run ut
|
||||
|
||||
- name: Coding style
|
||||
run: composer run cst
|
||||
|
||||
- name: Build and push Docker images
|
||||
uses: docker/build-push-action@v1
|
||||
with:
|
||||
push: false
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -2,3 +2,6 @@ vendor/
|
|||
docker.lock
|
||||
bin/
|
||||
!bin/dcv
|
||||
!bin/entrypoint.sh
|
||||
.cache/
|
||||
.php_cs.cache
|
||||
|
|
16
.php_cs
16
.php_cs
|
@ -1,12 +1,18 @@
|
|||
<?php
|
||||
|
||||
$finder = Symfony\CS\Finder\DefaultFinder::create()
|
||||
->exclude('somedir')
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->in(__DIR__.DIRECTORY_SEPARATOR.'src')
|
||||
->in(__DIR__.DIRECTORY_SEPARATOR.'spec')
|
||||
;
|
||||
|
||||
return Symfony\CS\Config\Config::create()
|
||||
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)
|
||||
->finder($finder)
|
||||
return (new PhpCsFixer\Config())
|
||||
->setRules([
|
||||
'@PSR2' => true,
|
||||
'@Symfony' => true,
|
||||
'array_syntax' => ['syntax' => 'short'],
|
||||
'no_useless_else' => true,
|
||||
'no_useless_return' => true,
|
||||
'ordered_class_elements' => true,
|
||||
])
|
||||
->setFinder($finder)
|
||||
;
|
||||
|
|
50
.travis.yml
50
.travis.yml
|
@ -1,50 +0,0 @@
|
|||
language: php
|
||||
|
||||
sudo: required
|
||||
|
||||
services:
|
||||
- docker
|
||||
|
||||
php:
|
||||
- 7.0
|
||||
- 7.1
|
||||
- nightly
|
||||
|
||||
env:
|
||||
matrix:
|
||||
- COMPOSERFLAGS=
|
||||
- COMPOSERFLAGS=--prefer-lowest
|
||||
|
||||
global:
|
||||
# DOCKER_EMAIL
|
||||
- secure: JAbpFtQJovMT1IqHJmSlVI3xQpLUoqDlbYXUgExK99dH7+GH78Rd6CgjCo5fDAw5KW4zBoTJut7BWv+qdF1j9qpUEXlxB6dEwKdLHCCM8M9p+BddkaZUWF9kG+4pC1h6AeDeWi7Q77TzrOX7VAP1UubFOPItRNayF99zCJH6ioh8qnHNi2XHedIagMW4PGLp6HmTuyhzdF6RVEXcCos2fpkx5wzcjALb/ffN8A0dqtfELRvs6bjaQZ1ktmNjd3fSnQOtGO14VOei8E58e4roZNdDHMoIxzeqaM+bphsTZvHne4DAZEn3wU2iguzXLZMCAgF336Inu+t2Rjv4UDLcPeppMrYA+02Ww0kbCXcIQx4Og0Un54SY5qdIHZtnTXA+SAFr+5r+T80p29Tlp9cUfxaHWFynDEEDWx7minAATweSQU50ipPnAkge1WY/ADSN1EARqM2GYVQYLvddIaTSIbnq37MnlncWL+jkcQ0cL83xidYd7JnQ7/gZJf9MzeZzMVXrY+2VYL1WOrv1uwmVi30bpQss1D7IVVtN5iBlB2ciph+iHdvkhv6f8ji9xZmWkotfDzsRoZg7csDAVgliz55QkJYKeWDoiW7kb1i+pIsgmrdEonPMg7NHjSqIIkrwI89016Tsx4LH1t2sRRYW8Z7wuVhW3VcJD+CnkhAWvLc=
|
||||
# DOCKER_USER
|
||||
- secure: "bdD/Sg7IvpY/qLBK8KiaMBihGx00sj+K3+qHnHoqzqqzF6NjZFgfeJfhvVRLz84JPxTqDOcISwAfCvrV/cmtQkFVI7UP5Xg2GniTH8VGJxhBQxCmqLGd1tOLR9PPGtEh4HcjybGcOcIE3zn0xHhTwgS6jLxrrwPeapDAc+BYJcbbyrT1tjy5mv+wR3HmB2p5oiRhUL3P6u1+ND/j2oDZDoSllNRs+5jTiSXToIlXjICr6f8bey5DfOpGEAnopMXqSQmcf92C9dixfWprk0KE2MvELbE4hWtuu44FPLJcXbrMunKpFOiN1IfIxU2CXjlYpG9vcroJe3hZWpXG9ZY9nn5KDv+6H48zu+CQgZx0uobBaNmVFOXxiqDElb/kjjF6+Xx55JxGuRpcsGNdsbqU6qwTJmgeBv1UPLGste3BHY5dQkf10QbxXJmHgF1GQihS5mjpf1cobNMra0JxIFDUfpedB3cnFoM+masxKY6fexu9hXb3d5Yi7DKsHA2JUGayVCaGjcJEYYBzNLlNs9NbMcQHqfQ6a90HM1yH11DKMPWEGkvp7+M1ixjOUpTYng5yrOhgl624LDsNu/NwadtKTrk3wO9liyOJgojTUvbpVlB9potFf8vtX8JhKqGyOcMgaOx8VZt+gIL1W25hHtmEYglsfkEze3fOwWW8o4CBI2E="
|
||||
# DOCKER_PASS
|
||||
- secure: "BqpmNofWU38cV3eUT9Hm9wxiiRlp1LKl1JQBxvsYng/dk4L7ONiBVsrG6T7nYhkTktWc/II4ZAoQ7AGHN42W1quzSmc6d0fszQ7uTwNobfFQu2JzFNkhyP9D6b0v8uXhT8n6TPqOGgoJUcguStvasG3TuZwn7+PhwjyVfnzf+DhyaXjCljMatg7ekL0JDPGdAz/SQhuBiwk2xlZtxtEokNy7IVr9VcMi2O0nG3LMhCl1sQjo3JKBxPsalQi78dShDUHcazAE68T7M1FjAZCJYia902FMDWiIuujLamq+NpDgEKB3aLCLwF/o3j8z3ekPrk2v9Zokz+t36cQ2BmPpwqFfhvPdUv9tj9bi7Qv2R4NKreX8TWB8KB5afSVWiKfufWV5hp5KfwEmcLBc/hQdjRIwzDqVPK/fyy/GJ5fT4X5kz+YYLQEFxeWPtxL+OpQUXx2P5iDhx5qz173lO4h1WX4vEQ3p4aFbfnNREUDPGYsMJo6flm5Azq8F0qh065sxPldKunr9H4fAXrFzqMJnTepReEGPNJRn35TLl08RI7GTp0hKxlaycsu+c2Qz0/GcKbODWf5w24d/pxrOMM9KmJpDZTBm9bWiZlRbbZm1OnK0PiaRi9ft44Em5NYTFVvuWL2M2tIyGObI3kquKTkANvrSuPTofJ0JawXg2YBKOH8="
|
||||
|
||||
matrix:
|
||||
exclude:
|
||||
- php: 7.0
|
||||
env: COMPOSERFLAGS=
|
||||
- php: 7.1
|
||||
env: COMPOSERFLAGS=--prefer-lowest
|
||||
- php: nightly
|
||||
env: COMPOSERFLAGS=--prefer-lowest
|
||||
allow_failures:
|
||||
- php: nightly
|
||||
|
||||
script:
|
||||
- make test
|
||||
|
||||
deploy:
|
||||
- provider: script
|
||||
script: ci/deploy.sh
|
||||
on:
|
||||
php: 7.1
|
||||
all_branches: master
|
||||
- provider: script
|
||||
script: ci/deploy.sh
|
||||
on:
|
||||
php: 7.1
|
||||
tags: true
|
15
CHANGELOG.md
15
CHANGELOG.md
|
@ -1,4 +1,17 @@
|
|||
# `1.0.0` (unreleased)
|
||||
# `1.2.0` (unreleased)
|
||||
|
||||
* Add a logger and enable `-v` and `-vv` options
|
||||
* Add the `--no-networks` and `--no-ports` options to avoid rendering networks and ports
|
||||
* Add the `--background` option to set the graph's background color
|
||||
* Versions correctly merged and checked
|
||||
|
||||
# `1.1.0`
|
||||
|
||||
* Display `depends_on` conditions
|
||||
* Handle conditions in `depends_on`
|
||||
* Automatically load override file if it exists or ignore it using `--ignore-override`
|
||||
|
||||
# `1.0.0`
|
||||
|
||||
* Avoid duplicating edges when there is multiple extended services
|
||||
* Display extended services as components with inverted arrows
|
||||
|
|
21
Dockerfile
21
Dockerfile
|
@ -1,4 +1,16 @@
|
|||
FROM php:7.1-alpine
|
||||
FROM php:7.4-alpine as builder
|
||||
|
||||
COPY composer.json /dcv/composer.json
|
||||
COPY composer.lock /dcv/composer.lock
|
||||
|
||||
WORKDIR /dcv
|
||||
|
||||
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
|
||||
php composer-setup.php && \
|
||||
php -r "unlink('composer-setup.php');" && \
|
||||
php composer.phar install --prefer-dist
|
||||
|
||||
FROM php:7.4-alpine
|
||||
|
||||
RUN apk update && \
|
||||
apk add graphviz ttf-dejavu && \
|
||||
|
@ -8,16 +20,17 @@ RUN apk update && \
|
|||
|
||||
COPY bin/ /dcv/bin
|
||||
COPY src/ /dcv/src
|
||||
COPY vendor/ /dcv/vendor
|
||||
COPY --from=builder /dcv/vendor /dcv/vendor
|
||||
|
||||
RUN chmod +x /dcv/bin/dcv
|
||||
|
||||
RUN addgroup dcv && \
|
||||
adduser -D -G dcv -s /bin/bash -g "docker-compose-viz" -h /input dcv
|
||||
|
||||
USER dcv
|
||||
#USER dcv
|
||||
USER root
|
||||
VOLUME /input
|
||||
WORKDIR /input
|
||||
|
||||
ENTRYPOINT ["/dcv/bin/dcv"]
|
||||
ENTRYPOINT ["/dcv/bin/entrypoint.sh"]
|
||||
CMD ["render", "-m", "image", "-f"]
|
||||
|
|
2
LICENSE
2
LICENSE
|
@ -1,5 +1,5 @@
|
|||
The MIT License (MIT)
|
||||
Copyright (c) 2016 PMSIpilot
|
||||
Copyright (c) 2020 PMSIpilot
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
||||
|
|
14
Makefile
14
Makefile
|
@ -12,29 +12,23 @@ docker: docker.lock
|
|||
test: vendor unit cs
|
||||
|
||||
unit: vendor
|
||||
$(PHP) bin/kahlan --pattern='*.php' --reporter=verbose --persistent=false --cc=true
|
||||
$(COMPOSER) run ut
|
||||
|
||||
cs:
|
||||
$(PHP) bin/php-cs-fixer fix --dry-run
|
||||
$(COMPOSER) run cst
|
||||
|
||||
fix-cs:
|
||||
$(PHP) bin/php-cs-fixer fix
|
||||
$(COMPOSER) run cs
|
||||
|
||||
clean:
|
||||
rm -rf vendor/
|
||||
|
||||
docker.lock: Dockerfile vendor
|
||||
$(COMPOSER) dump-autoload --classmap-authoritative
|
||||
docker.lock: Dockerfile bin/entrypoint.sh vendor src/application.php src/functions.php
|
||||
$(DOCKER) build -t $(DCV_IMAGE_NAME) .
|
||||
touch docker.lock
|
||||
|
||||
ifndef COMPOSERFLAGS
|
||||
vendor: composer.lock
|
||||
$(COMPOSER) install --prefer-dist
|
||||
else
|
||||
vendor: composer.lock
|
||||
$(COMPOSER) update $(COMPOSERFLAGS)
|
||||
endif
|
||||
|
||||
composer.lock: composer.json
|
||||
$(COMPOSER) update $(COMPOSERFLAGS)
|
||||
|
|
25
README.md
25
README.md
|
@ -1,7 +1,5 @@
|
|||
# `docker-compose-viz`
|
||||
|
||||
[](https://travis-ci.org/pmsipilot/docker-compose-viz)
|
||||
[](https://styleci.io/repos/65026022)
|
||||
[](http://isitmaintained.com/project/pmsipilot/docker-compose-viz "Average time to resolve an issue")
|
||||
[](http://isitmaintained.com/project/pmsipilot/docker-compose-viz "Percentage of issues still open")
|
||||
[](https://hub.docker.com/r/pmsipilot/docker-compose-viz/)
|
||||
|
@ -13,8 +11,13 @@
|
|||
|
||||
Considering the current working directory is where your `docker-compose.yml` file is located:
|
||||
|
||||
```
|
||||
```bash
|
||||
docker build . -t pmsipilot/docker-compose-viz
|
||||
|
||||
docker run --rm -it --name dcv -v $(pwd):/input pmsipilot/docker-compose-viz render -m image docker-compose.yml
|
||||
|
||||
# PowerShell
|
||||
docker run --rm -it --name dcv -v ${pwd}:/input pmsipilot/docker-compose-viz render -m image docker-compose.yml
|
||||
```
|
||||
|
||||
This will generate the `docker-compose.png` file in the current working directory.
|
||||
|
@ -24,7 +27,7 @@ This will generate the `docker-compose.png` file in the current working director
|
|||
Before you start, make sure you have:
|
||||
|
||||
* [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx) installed,
|
||||
* [PHP 7](http://php.net/downloads.php#v7.0.9) installed,
|
||||
* [PHP 7.2](http://php.net/downloads.php#v7.2.32) (at least) installed,
|
||||
* GraphViz installed (see below for a guide on how to install it)
|
||||
|
||||
```
|
||||
|
@ -45,18 +48,21 @@ bin/dcv
|
|||
## Usage
|
||||
|
||||
```
|
||||
render [options] [--] [<input-file>]
|
||||
Usage:
|
||||
render [options] [--] [<input-file>]
|
||||
|
||||
Arguments:
|
||||
input-file Path to a docker compose file [default: "./docker-compose.yml"]
|
||||
|
||||
Options:
|
||||
--override=OVERRIDE Tag of the override file to use [default: "override"]
|
||||
-o, --output-file=OUTPUT-FILE Path to a output file (Only for "dot" and "image" output format) [default: "./docker-compose.dot" or "./docker-compose.png"]
|
||||
-m, --output-format=OUTPUT-FORMAT Output format (one of: "dot", "image", "display") [default: "display"]
|
||||
--only=ONLY Display a graph only for a given services (multiple values allowed)
|
||||
-f, --force Overwrites output file if it already exists
|
||||
--no-volumes Do not display volumes
|
||||
-r, --horizontal Display a horizontal graph
|
||||
--ignore-override Ignore override file
|
||||
```
|
||||
|
||||
## How to read the graph
|
||||
|
@ -172,7 +178,14 @@ digraph G {
|
|||
|
||||

|
||||
|
||||
### Troubleshooting
|
||||
|
||||
#### Getting "failed to open stream: Permission denied"?
|
||||
|
||||
Make sure the target directory is writeable by the user in the Docker container.
|
||||
Or create a writeable directory first. See [workaround #41](https://github.com/pmsipilot/docker-compose-viz/issues/41#issuecomment-483384999)
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT)
|
||||
Copyright (c) 2016 PMSIpilot
|
||||
Copyright ® 2020 PMSIpilot
|
||||
|
|
12
bin/entrypoint.sh
Executable file
12
bin/entrypoint.sh
Executable file
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/env sh
|
||||
|
||||
set -e
|
||||
|
||||
CURRENT_DIR=$(dirname $0)
|
||||
|
||||
if [ "$1" = "render" ]
|
||||
then
|
||||
$CURRENT_DIR/dcv "$@"
|
||||
else
|
||||
exec "$@"
|
||||
fi
|
|
@ -4,8 +4,9 @@ export REPO=pmsipilot/docker-compose-viz
|
|||
export TAG=`if [ "$TRAVIS_BRANCH" == "master" ]; then echo "latest"; else echo $TRAVIS_BRANCH ; fi`
|
||||
|
||||
git reset --hard
|
||||
git clean -dfx
|
||||
composer install --no-dev --prefer-dist --classmap-authoritative
|
||||
|
||||
docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
|
||||
docker login -u $DOCKER_USER -p $DOCKER_PASS
|
||||
docker build -f Dockerfile -t $REPO:$TAG .
|
||||
docker push $REPO
|
||||
|
|
|
@ -2,14 +2,15 @@
|
|||
"name": "pmsipilot/docker-compose-viz",
|
||||
"description": "Docker compose graph visualization",
|
||||
"require": {
|
||||
"symfony/yaml": "^3.1 || ^4@dev",
|
||||
"php": "^7.2",
|
||||
"symfony/yaml": "^3.1 || ^4",
|
||||
"symfony/console": "^3.1",
|
||||
"clue/graph": "^0.9",
|
||||
"graphp/graphviz": "^0.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"crysalead/kahlan": "^2.5.4",
|
||||
"friendsofphp/php-cs-fixer": "^1.11"
|
||||
"friendsofphp/php-cs-fixer": "^2",
|
||||
"kahlan/kahlan": "^4.7"
|
||||
},
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
|
@ -24,7 +25,9 @@
|
|||
"PMSIpilot\\DockerComposeViz\\": "src/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"bin-dir": "bin/"
|
||||
"scripts": {
|
||||
"cs": "php-cs-fixer fix",
|
||||
"cst": "php-cs-fixer fix --dry-run",
|
||||
"ut": "kahlan --grep='*.php' --reporter=verbose --persistent=false"
|
||||
}
|
||||
}
|
||||
|
|
1334
composer.lock
generated
1334
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,8 +0,0 @@
|
|||
{
|
||||
"version": 2,
|
||||
"services": {
|
||||
"foo": {
|
||||
"image": "bar"
|
||||
}
|
||||
}
|
||||
}
|
4
spec/fixtures/read-configuration/invalid.yml
vendored
Normal file
4
spec/fixtures/read-configuration/invalid.yml
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
version
|
||||
services:
|
||||
foo:
|
||||
image: bar
|
|
@ -19,7 +19,7 @@ describe('Reading configuration', function () {
|
|||
|
||||
it('should report if YAML is invalid', function () {
|
||||
expect(function () {
|
||||
readConfiguration(__DIR__.'/fixtures/read-configuration/invalid.json');
|
||||
readConfiguration(__DIR__.'/fixtures/read-configuration/invalid.yml');
|
||||
})
|
||||
->toThrow(new InvalidArgumentException());
|
||||
});
|
||||
|
|
|
@ -4,52 +4,90 @@ namespace PMSIpilot\DockerComposeViz;
|
|||
|
||||
use Graphp\GraphViz\GraphViz;
|
||||
use Symfony\Component\Console;
|
||||
use function PMSIpilot\DockerComposeViz\applyGraphvizStyle;
|
||||
use function PMSIpilot\DockerComposeViz\createGraph;
|
||||
use function PMSIpilot\DockerComposeViz\fetchNetworks;
|
||||
use function PMSIpilot\DockerComposeViz\fetchServices;
|
||||
use function PMSIpilot\DockerComposeViz\fetchVolumes;
|
||||
use function PMSIpilot\DockerComposeViz\readConfiguration;
|
||||
|
||||
$application = new Console\Application();
|
||||
|
||||
$application->register('render')
|
||||
->addArgument('input-file', Console\Input\InputArgument::OPTIONAL, 'Path to a docker compose file', getcwd().DIRECTORY_SEPARATOR.'docker-compose.yml')
|
||||
|
||||
->addOption('override', null, Console\Input\InputOption::VALUE_REQUIRED, 'Tag of the override file to use', 'override')
|
||||
->addOption('output-file', 'o', Console\Input\InputOption::VALUE_REQUIRED, 'Path to a output file (Only for "dot" and "image" output format)')
|
||||
->addOption('output-format', 'm', Console\Input\InputOption::VALUE_REQUIRED, 'Output format (one of: "dot", "image", "display")', 'display')
|
||||
->addOption('output-format', 'm', Console\Input\InputOption::VALUE_REQUIRED, 'Output format (one of: "dot", "image", "display", "graphviz")', 'display')
|
||||
->addOption('graphviz-output-format', null, Console\Input\InputOption::VALUE_REQUIRED, 'GraphViz Output format (see `man dot` for details)', 'svg')
|
||||
->addOption('only', null, Console\Input\InputOption::VALUE_IS_ARRAY | Console\Input\InputOption::VALUE_REQUIRED, 'Display a graph only for a given services')
|
||||
|
||||
->addOption('force', 'f', Console\Input\InputOption::VALUE_NONE, 'Overwrites output file if it already exists')
|
||||
->addOption('no-volumes', null, Console\Input\InputOption::VALUE_NONE, 'Do not display volumes')
|
||||
->addOption('no-networks', null, Console\Input\InputOption::VALUE_NONE, 'Do not display networks')
|
||||
->addOption('no-ports', null, Console\Input\InputOption::VALUE_NONE, 'Do not display ports')
|
||||
->addOption('horizontal', 'r', Console\Input\InputOption::VALUE_NONE, 'Display a horizontal graph')
|
||||
->addOption('ignore-override', null, Console\Input\InputOption::VALUE_NONE, 'Ignore override file')
|
||||
->addOption('background', null, Console\Input\InputOption::VALUE_REQUIRED, 'Set the graph background color', '#ffffff')
|
||||
|
||||
->setCode(function (Console\Input\InputInterface $input, Console\Output\OutputInterface $output) {
|
||||
$inputFile = $input->getArgument('input-file');
|
||||
$outputFormat = $input->getOption('output-format');
|
||||
$outputFile = $input->getOption('output-file') ?: getcwd().DIRECTORY_SEPARATOR.'docker-compose.'.($outputFormat === 'dot' ? $outputFormat : 'png');
|
||||
$onlyServices = $input->getOption('only');
|
||||
$backgroundColor = $input->getOption('background');
|
||||
|
||||
if (in_array($outputFormat, ['dot', 'image', 'display']) === false) {
|
||||
throw new Console\Exception\InvalidArgumentException(sprintf('Invalid output format "%s". It must be one of "dot", "png" or "display".', $outputFormat));
|
||||
if (0 === preg_match('/^#[a-fA-F0-9]{6}|transparent$/', $backgroundColor)) {
|
||||
throw new Console\Exception\InvalidArgumentException(sprintf('Invalid background color "%s". It must be a valid hex color or "transparent".', $backgroundColor));
|
||||
}
|
||||
|
||||
if ($outputFormat === 'display') {
|
||||
$logger = logger($output);
|
||||
$inputFile = $input->getArgument('input-file');
|
||||
$inputFileExtension = pathinfo($inputFile, PATHINFO_EXTENSION);
|
||||
$overrideFile = dirname($inputFile).DIRECTORY_SEPARATOR.basename($inputFile, '.'.$inputFileExtension).'.'.$input->getOption('override').'.'.$inputFileExtension;
|
||||
|
||||
$outputFormat = $input->getOption('output-format');
|
||||
$outputFile = $input->getOption('output-file') ?: getcwd().DIRECTORY_SEPARATOR.'docker-compose.'.('dot' === $outputFormat ? $outputFormat : 'png');
|
||||
$onlyServices = $input->getOption('only');
|
||||
|
||||
if (false === in_array($outputFormat, ['dot', 'image', 'display', 'graphviz'])) {
|
||||
throw new Console\Exception\InvalidArgumentException(sprintf('Invalid output format "%s". It must be one of "dot", "image" or "display".', $outputFormat));
|
||||
}
|
||||
|
||||
if ('display' === $outputFormat) {
|
||||
if ($input->getOption('force') || $input->getOption('output-file')) {
|
||||
$output->writeln('<comment>The following options are ignored with the "display" output format: "--force", "--output-file"</comment>');
|
||||
}
|
||||
} else {
|
||||
if (file_exists($outputFile) === true && $input->getOption('force') === false) {
|
||||
if (true === file_exists($outputFile) && false === $input->getOption('force')) {
|
||||
throw new Console\Exception\InvalidArgumentException(sprintf('File "%s" already exists. Use the "--force" option to overwrite it.', $outputFile));
|
||||
}
|
||||
}
|
||||
|
||||
$logger(sprintf('Reading <comment>configuration</comment> from <info>"%s"</info>', $inputFile));
|
||||
$configuration = readConfiguration($inputFile);
|
||||
$configurationVersion = (string) ($configuration['version'] ?? 1);
|
||||
|
||||
if (!$input->getOption('ignore-override') && file_exists($overrideFile)) {
|
||||
$logger(sprintf('Reading <comment>override</comment> from <info>"%s"</info>', $overrideFile));
|
||||
$override = readConfiguration($overrideFile);
|
||||
$overrideVersion = (string) ($override['version'] ?? 1);
|
||||
|
||||
if ($configurationVersion !== $overrideVersion) {
|
||||
throw new Console\Exception\LogicException(sprintf('Version mismatch: file "%s" specifies version "%s" but file "%s" uses version "%s"', $inputFile, $configurationVersion, $overrideFile, $overrideVersion));
|
||||
}
|
||||
|
||||
$configuration = array_merge_recursive($configuration, $override);
|
||||
|
||||
$logger(sprintf('Configuration <comment>version</comment> is <info>"%s"</info>', $configurationVersion), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
|
||||
$configuration['version'] = $configurationVersion;
|
||||
}
|
||||
|
||||
$logger('Fetching <comment>services</comment>');
|
||||
$services = fetchServices($configuration);
|
||||
$logger(sprintf('Found <info>%d</info> <comment>services</comment>', count($services)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
|
||||
|
||||
$logger('Fetching <comment>volumes</comment>');
|
||||
$volumes = fetchVolumes($configuration);
|
||||
$logger(sprintf('Found <info>%d</info> <comment>volumes</comment>', count($volumes)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
|
||||
|
||||
$logger('Fetching <comment>networks</comment>');
|
||||
$networks = fetchNetworks($configuration);
|
||||
$logger(sprintf('Found <info>%d</info> <comment>networks</comment>', count($networks)), Console\Output\OutputInterface::VERBOSITY_VERY_VERBOSE);
|
||||
|
||||
if ([] !== $onlyServices) {
|
||||
$logger(sprintf('Only <info>%s</info> <comment>services</comment> will be displayed', implode(', ', $onlyServices)));
|
||||
|
||||
$intersect = array_intersect($onlyServices, array_keys($services));
|
||||
|
||||
if ($intersect !== $onlyServices) {
|
||||
|
@ -65,9 +103,30 @@ $application->register('render')
|
|||
);
|
||||
}
|
||||
|
||||
$flags = 0;
|
||||
if (true === $input->getOption('no-volumes')) {
|
||||
$logger('<comment>Volumes</comment> will not be displayed');
|
||||
|
||||
$flags |= WITHOUT_VOLUMES;
|
||||
}
|
||||
|
||||
if (true === $input->getOption('no-networks')) {
|
||||
$logger('<comment>Networks</comment> will not be displayed');
|
||||
|
||||
$flags |= WITHOUT_NETWORKS;
|
||||
}
|
||||
|
||||
if (true === $input->getOption('no-ports')) {
|
||||
$logger('<comment>Ports</comment> will not be displayed');
|
||||
|
||||
$flags |= WITHOUT_PORTS;
|
||||
}
|
||||
|
||||
$logger('Rendering <comment>graph</comment>');
|
||||
$graph = applyGraphvizStyle(
|
||||
createGraph($services, $volumes, $networks, $input->getOption('no-volumes') === false, $inputFile),
|
||||
$input->getOption('horizontal')
|
||||
createGraph($services, $volumes, $networks, $inputFile, $flags),
|
||||
$input->getOption('horizontal'),
|
||||
$input->getOption('background')
|
||||
);
|
||||
|
||||
switch ($outputFormat) {
|
||||
|
@ -83,6 +142,13 @@ $application->register('render')
|
|||
$renderer = new GraphViz();
|
||||
$renderer->display($graph);
|
||||
break;
|
||||
|
||||
case 'graphviz':
|
||||
$renderer = new GraphViz();
|
||||
$format = $input->getOption('graphviz-output-format');
|
||||
|
||||
file_put_contents($outputFile, $renderer->setFormat($format)->createImageData($graph));
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -5,26 +5,40 @@ namespace PMSIpilot\DockerComposeViz;
|
|||
use Fhaculty\Graph\Edge;
|
||||
use Fhaculty\Graph\Graph;
|
||||
use Fhaculty\Graph\Vertex;
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
const WITHOUT_VOLUMES = 1;
|
||||
const WITHOUT_NETWORKS = 2;
|
||||
const WITHOUT_PORTS = 4;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
function logger(OutputInterface $output): callable
|
||||
{
|
||||
return function (string $message, int $verbosity = null) use ($output) {
|
||||
$output->writeln(sprintf('[%s] %s', date(DATE_ISO8601), $message), $verbosity ?: OutputInterface::VERBOSITY_VERBOSE);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*
|
||||
* @param string $path Path to a YAML file
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function readConfiguration(string $path) : array
|
||||
function readConfiguration(string $path): array
|
||||
{
|
||||
if (file_exists($path) === false) {
|
||||
throw new \InvalidArgumentException(sprintf('File "%s" does not exist', $path));
|
||||
if (false === file_exists($path)) {
|
||||
throw new InvalidArgumentException(sprintf('File "%s" does not exist', $path));
|
||||
}
|
||||
|
||||
try {
|
||||
return Yaml::parse(file_get_contents($path));
|
||||
} catch (ParseException $exception) {
|
||||
throw new \InvalidArgumentException(sprintf('File "%s" does not contain valid YAML', $path));
|
||||
throw new InvalidArgumentException(sprintf('File "%s" does not contain valid YAML', $path), $exception->getCode(), $exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,9 +49,9 @@ function readConfiguration(string $path) : array
|
|||
*
|
||||
* @return array List of service definitions exctracted from the configuration
|
||||
*/
|
||||
function fetchServices(array $configuration) : array
|
||||
function fetchServices(array $configuration): array
|
||||
{
|
||||
if (isset($configuration['version']) === false || (int) $configuration['version'] === 1) {
|
||||
if (false === isset($configuration['version']) || 1 === (int) $configuration['version']) {
|
||||
return $configuration;
|
||||
}
|
||||
|
||||
|
@ -51,9 +65,9 @@ function fetchServices(array $configuration) : array
|
|||
*
|
||||
* @return array List of service definitions exctracted from the configuration
|
||||
*/
|
||||
function fetchVolumes(array $configuration) : array
|
||||
function fetchVolumes(array $configuration): array
|
||||
{
|
||||
if (isset($configuration['version']) === false || (int) $configuration['version'] === 1) {
|
||||
if (false === isset($configuration['version']) || 1 === (int) $configuration['version']) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@ -67,9 +81,9 @@ function fetchVolumes(array $configuration) : array
|
|||
*
|
||||
* @return array List of service definitions exctracted from the configuration
|
||||
*/
|
||||
function fetchNetworks(array $configuration) : array
|
||||
function fetchNetworks(array $configuration): array
|
||||
{
|
||||
if (isset($configuration['version']) === false || (int) $configuration['version'] === 1) {
|
||||
if (false === isset($configuration['version']) || 1 === (int) $configuration['version']) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
@ -87,26 +101,28 @@ function fetchNetworks(array $configuration) : array
|
|||
*
|
||||
* @return Graph The complete graph for the given list of services
|
||||
*/
|
||||
function createGraph(array $services, array $volumes, array $networks, bool $withVolumes, string $path) : Graph
|
||||
function createGraph(array $services, array $volumes, array $networks, string $path, int $flags): Graph
|
||||
{
|
||||
return makeVerticesAndEdges(new Graph(), $services, $volumes, $networks, $withVolumes, $path);
|
||||
return makeVerticesAndEdges(new Graph(), $services, $volumes, $networks, $path, $flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*
|
||||
* @param Graph $graph Input graph
|
||||
* @param bool $horizontal Display a horizontal graph
|
||||
* @param Graph $graph Input graph
|
||||
* @param bool $horizontal Display a horizontal graph
|
||||
* @param string $horizontal Background color (any hex color or 'transparent')
|
||||
*
|
||||
* @return Graph A copy of the input graph with style attributes
|
||||
*/
|
||||
function applyGraphvizStyle(Graph $graph, bool $horizontal) : Graph
|
||||
function applyGraphvizStyle(Graph $graph, bool $horizontal, string $background): Graph
|
||||
{
|
||||
$graph = $graph->createGraphClone();
|
||||
$graph->setAttribute('graphviz.graph.bgcolor', $background);
|
||||
$graph->setAttribute('graphviz.graph.pad', '0.5');
|
||||
$graph->setAttribute('graphviz.graph.ratio', 'fill');
|
||||
|
||||
if ($horizontal === true) {
|
||||
if (true === $horizontal) {
|
||||
$graph->setAttribute('graphviz.graph.rankdir', 'LR');
|
||||
}
|
||||
|
||||
|
@ -137,7 +153,7 @@ function applyGraphvizStyle(Graph $graph, bool $horizontal) : Graph
|
|||
case 'port':
|
||||
$vertex->setAttribute('graphviz.shape', 'circle');
|
||||
|
||||
if (($proto = $vertex->getAttribute('docker_compose.proto')) === 'udp') {
|
||||
if ('udp' === ($proto = $vertex->getAttribute('docker_compose.proto'))) {
|
||||
$vertex->setAttribute('graphviz.style', 'dashed');
|
||||
}
|
||||
break;
|
||||
|
@ -172,8 +188,12 @@ function applyGraphvizStyle(Graph $graph, bool $horizontal) : Graph
|
|||
break;
|
||||
}
|
||||
|
||||
if (($alias = $edge->getAttribute('docker_compose.alias')) !== null) {
|
||||
if (null !== ($alias = $edge->getAttribute('docker_compose.alias'))) {
|
||||
$edge->setAttribute('graphviz.label', $alias);
|
||||
|
||||
if (null !== $edge->getAttribute('docker_compose.condition')) {
|
||||
$edge->setAttribute('graphviz.fontsize', '10');
|
||||
}
|
||||
}
|
||||
|
||||
if ($edge->getAttribute('docker_compose.bidir')) {
|
||||
|
@ -195,42 +215,53 @@ function applyGraphvizStyle(Graph $graph, bool $horizontal) : Graph
|
|||
*
|
||||
* @return Graph A copy of the input graph with vertices and edges for services
|
||||
*/
|
||||
function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, array $networks, bool $withVolumes, $path) : Graph
|
||||
function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, array $networks, string $path, int $flags): Graph
|
||||
{
|
||||
if ($withVolumes === true) {
|
||||
if (false === ((bool) ($flags & WITHOUT_VOLUMES))) {
|
||||
foreach (array_keys($volumes) as $volume) {
|
||||
addVolume($graph, 'named: '.$volume);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($networks as $network => $definition) {
|
||||
addNetwork(
|
||||
$graph, 'net: '.$network,
|
||||
isset($definition['external']) && $definition['external'] === true ? 'external_network' : 'network'
|
||||
);
|
||||
if (false === ((bool) ($flags & WITHOUT_NETWORKS))) {
|
||||
foreach ($networks as $network => $definition) {
|
||||
addNetwork(
|
||||
$graph,
|
||||
'net: '.$network,
|
||||
isset($definition['external']) && true === $definition['external'] ? 'external_network' : 'network'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($services as $service => $definition) {
|
||||
addService($graph, $service);
|
||||
|
||||
if (isset($definition['extends'])) {
|
||||
$configuration = readConfiguration(dirname($path).DIRECTORY_SEPARATOR.$definition['extends']['file']);
|
||||
$extendedServices = fetchServices($configuration);
|
||||
$extendedVolumes = fetchVolumes($configuration);
|
||||
$extendedNetworks = fetchVolumes($configuration);
|
||||
if (isset($definition['extends']['file'])) {
|
||||
$configuration = readConfiguration(dirname($path).DIRECTORY_SEPARATOR.$definition['extends']['file']);
|
||||
$extendedServices = fetchServices($configuration);
|
||||
$extendedVolumes = fetchVolumes($configuration);
|
||||
$extendedNetworks = fetchNetworks($configuration);
|
||||
|
||||
$graph = makeVerticesAndEdges($graph, $extendedServices, $extendedVolumes, $extendedNetworks, $withVolumes, dirname($path).DIRECTORY_SEPARATOR.$definition['extends']['file']);
|
||||
$graph = makeVerticesAndEdges($graph, $extendedServices, $extendedVolumes, $extendedNetworks, dirname($path).DIRECTORY_SEPARATOR.$definition['extends']['file'], $flags);
|
||||
}
|
||||
|
||||
addRelation(
|
||||
addService($graph, $definition['extends']['service']),
|
||||
addService($graph, $definition['extends']['service']),
|
||||
$graph->getVertex($service),
|
||||
'extends'
|
||||
);
|
||||
}
|
||||
|
||||
$serviceLinks = [];
|
||||
|
||||
foreach ($definition['links'] ?? [] as $link) {
|
||||
list($target, $alias) = explodeMapping($link);
|
||||
|
||||
$serviceLinks[$alias] = $target;
|
||||
}
|
||||
|
||||
foreach ($serviceLinks as $alias => $target) {
|
||||
addRelation(
|
||||
addService($graph, $target),
|
||||
$graph->getVertex($service),
|
||||
|
@ -250,11 +281,14 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
|
|||
);
|
||||
}
|
||||
|
||||
foreach ($definition['depends_on'] ?? [] as $dependency) {
|
||||
foreach ($definition['depends_on'] ?? [] as $key => $dependency) {
|
||||
addRelation(
|
||||
$graph->getVertex($service),
|
||||
addService($graph, $dependency),
|
||||
'depends_on'
|
||||
addService($graph, is_array($dependency) ? $key : $dependency),
|
||||
'depends_on',
|
||||
is_array($dependency) && isset($dependency['condition']) ? $dependency['condition'] : null,
|
||||
false,
|
||||
is_array($dependency) && isset($dependency['condition'])
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -266,11 +300,25 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
|
|||
);
|
||||
}
|
||||
|
||||
if ($withVolumes === true) {
|
||||
foreach ($definition['volumes'] ?? [] as $volume) {
|
||||
list($host, $container, $attr) = explodeMapping($volume);
|
||||
if (false === ((bool) ($flags & WITHOUT_VOLUMES))) {
|
||||
$serviceVolumes = [];
|
||||
|
||||
if ($host[0] !== '.' && $host[0] !== DIRECTORY_SEPARATOR) {
|
||||
foreach ($definition['volumes'] ?? [] as $volume) {
|
||||
if (is_array($volume)) {
|
||||
$host = $volume['source'];
|
||||
$container = $volume['target'];
|
||||
$attr = !empty($volume['read-only']) ? 'ro' : '';
|
||||
} else {
|
||||
list($host, $container, $attr) = explodeVolumeMapping($volume);
|
||||
}
|
||||
|
||||
$serviceVolumes[$container] = [$host, $attr];
|
||||
}
|
||||
|
||||
foreach ($serviceVolumes as $container => $volume) {
|
||||
list($host, $attr) = $volume;
|
||||
|
||||
if ('.' !== $host[0] && DIRECTORY_SEPARATOR !== $host[0]) {
|
||||
$host = 'named: '.$host;
|
||||
}
|
||||
|
||||
|
@ -279,33 +327,37 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
|
|||
$graph->getVertex($service),
|
||||
'volumes',
|
||||
$host !== $container ? $container : null,
|
||||
$attr !== 'ro'
|
||||
'ro' !== $attr
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($definition['ports'] ?? [] as $port) {
|
||||
list($host, $container, $proto) = explodeMapping($port);
|
||||
if (false === ((bool) ($flags & WITHOUT_PORTS))) {
|
||||
foreach ($definition['ports'] ?? [] as $port) {
|
||||
list($target, $host, $container, $proto) = explodePortMapping($port);
|
||||
|
||||
addRelation(
|
||||
addPort($graph, (int) $host, $proto),
|
||||
$graph->getVertex($service),
|
||||
'ports',
|
||||
$host !== $container ? $container : null
|
||||
);
|
||||
addRelation(
|
||||
addPort($graph, (int) $host, $proto, $target),
|
||||
$graph->getVertex($service),
|
||||
'ports',
|
||||
$host !== $container ? $container : null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($definition['networks'] ?? [] as $network => $config) {
|
||||
$network = is_int($network) ? $config : $network;
|
||||
$config = is_int($network) ? [] : $config;
|
||||
$aliases = $config['aliases'] ?? [];
|
||||
if (false === ((bool) ($flags & WITHOUT_NETWORKS))) {
|
||||
foreach ($definition['networks'] ?? [] as $network => $config) {
|
||||
$network = is_int($network) ? $config : $network;
|
||||
$config = is_int($network) ? [] : $config;
|
||||
$aliases = $config['aliases'] ?? [];
|
||||
|
||||
addRelation(
|
||||
$graph->getVertex($service),
|
||||
addNetwork($graph, 'net: '.$network),
|
||||
'networks',
|
||||
count($aliases) > 0 ? implode(', ', $aliases) : null
|
||||
);
|
||||
addRelation(
|
||||
$graph->getVertex($service),
|
||||
addNetwork($graph, 'net: '.$network),
|
||||
'networks',
|
||||
count($aliases) > 0 ? implode(', ', $aliases) : null
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -323,7 +375,7 @@ function makeVerticesAndEdges(Graph $graph, array $services, array $volumes, arr
|
|||
*/
|
||||
function addService(Graph $graph, string $service, string $type = null)
|
||||
{
|
||||
if ($graph->hasVertex($service) === true) {
|
||||
if (true === $graph->hasVertex($service)) {
|
||||
return $graph->getVertex($service);
|
||||
}
|
||||
|
||||
|
@ -342,13 +394,15 @@ function addService(Graph $graph, string $service, string $type = null)
|
|||
*
|
||||
* @return Vertex
|
||||
*/
|
||||
function addPort(Graph $graph, int $port, string $proto = null)
|
||||
function addPort(Graph $graph, int $port, string $proto = null, string $target = null)
|
||||
{
|
||||
if ($graph->hasVertex($port) === true) {
|
||||
return $graph->getVertex($port);
|
||||
$target = $target ? $target.':' : null;
|
||||
|
||||
if (true === $graph->hasVertex($target.$port)) {
|
||||
return $graph->getVertex($target.$port);
|
||||
}
|
||||
|
||||
$vertex = $graph->createVertex($port);
|
||||
$vertex = $graph->createVertex($target.$port);
|
||||
$vertex->setAttribute('docker_compose.type', 'port');
|
||||
$vertex->setAttribute('docker_compose.proto', $proto ?: 'tcp');
|
||||
|
||||
|
@ -365,7 +419,7 @@ function addPort(Graph $graph, int $port, string $proto = null)
|
|||
*/
|
||||
function addVolume(Graph $graph, string $path)
|
||||
{
|
||||
if ($graph->hasVertex($path) === true) {
|
||||
if (true === $graph->hasVertex($path)) {
|
||||
return $graph->getVertex($path);
|
||||
}
|
||||
|
||||
|
@ -386,7 +440,7 @@ function addVolume(Graph $graph, string $path)
|
|||
*/
|
||||
function addNetwork(Graph $graph, string $name, string $type = null)
|
||||
{
|
||||
if ($graph->hasVertex($name) === true) {
|
||||
if (true === $graph->hasVertex($name)) {
|
||||
return $graph->getVertex($name);
|
||||
}
|
||||
|
||||
|
@ -404,10 +458,9 @@ function addNetwork(Graph $graph, string $name, string $type = null)
|
|||
* @param string $type Type of the relation (one of "links", "volumes_from", "depends_on", "ports");
|
||||
* @param string|null $alias Alias associated to the linked element
|
||||
* @param bool|null $bidirectional Biderectional or not
|
||||
*
|
||||
* @return Edge\Directed
|
||||
* @param bool|null $condition Wether the alias represents a condition or not
|
||||
*/
|
||||
function addRelation(Vertex $from, Vertex $to, string $type, string $alias = null, bool $bidirectional = false) : Edge\Directed
|
||||
function addRelation(Vertex $from, Vertex $to, string $type, string $alias = null, bool $bidirectional = false, bool $condition = false): Edge\Directed
|
||||
{
|
||||
$edge = null;
|
||||
|
||||
|
@ -427,10 +480,14 @@ function addRelation(Vertex $from, Vertex $to, string $type, string $alias = nul
|
|||
|
||||
$edge->setAttribute('docker_compose.type', $type);
|
||||
|
||||
if ($alias !== null) {
|
||||
if (null !== $alias) {
|
||||
$edge->setAttribute('docker_compose.alias', $alias);
|
||||
}
|
||||
|
||||
if (true === $condition) {
|
||||
$edge->setAttribute('docker_compose.condition', true);
|
||||
}
|
||||
|
||||
$edge->setAttribute('docker_compose.bidir', $bidirectional);
|
||||
|
||||
return $edge;
|
||||
|
@ -441,19 +498,56 @@ function addRelation(Vertex $from, Vertex $to, string $type, string $alias = nul
|
|||
*
|
||||
* @param string $mapping A docker mapping (<from>[:<to>])
|
||||
*
|
||||
* @return array An 2 items array containing the parts of the mapping.
|
||||
* @return array An 2 or 3 items array containing the parts of the mapping.
|
||||
* If the mapping does not specify a second part, the first one will be repeated
|
||||
*/
|
||||
function explodeMapping($mapping) : array
|
||||
function explodeMapping($mapping): array
|
||||
{
|
||||
$parts = explode(':', $mapping);
|
||||
$parts[1] = $parts[1] ?? $parts[0];
|
||||
|
||||
$subparts = array_values(array_filter(explode('/', $parts[1])));
|
||||
return [$parts[0], $parts[1]];
|
||||
}
|
||||
|
||||
if (count($subparts) > 2) {
|
||||
$subparts = [$parts[1], $parts[2] ?? null];
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param string $mapping A docker mapping (<from>[:<to>])
|
||||
*
|
||||
* @return array An 2 or 3 items array containing the parts of the mapping.
|
||||
* If the mapping does not specify a second part, the first one will be repeated
|
||||
*/
|
||||
function explodeVolumeMapping($mapping): array
|
||||
{
|
||||
$parts = explode(':', $mapping);
|
||||
$parts[1] = $parts[1] ?? $parts[0];
|
||||
|
||||
return [$parts[0], $parts[1], $parts[2] ?? null];
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @param string $mapping A docker mapping (<from>[:<to>])
|
||||
*
|
||||
* @return array An 2 or 3 items array containing the parts of the mapping.
|
||||
* If the mapping does not specify a second part, the first one will be repeated
|
||||
*/
|
||||
function explodePortMapping($mapping): array
|
||||
{
|
||||
$parts = explode(':', $mapping);
|
||||
|
||||
if (count($parts) < 3) {
|
||||
$target = null;
|
||||
$host = $parts[0];
|
||||
$container = $parts[1] ?? $parts[0];
|
||||
} else {
|
||||
$target = $parts[0];
|
||||
$host = $parts[1];
|
||||
$container = $parts[2];
|
||||
}
|
||||
|
||||
return [$parts[0], $subparts[0], $subparts[1] ?? null];
|
||||
$subparts = array_values(array_filter(explode('/', $container)));
|
||||
|
||||
return [$target, $host, $subparts[0], $subparts[1] ?? null];
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue