fix get random color

This commit is contained in:
Simon Rieger 2024-03-19 20:57:48 +01:00
parent 84387e105d
commit 9b0c4efe56
2 changed files with 19 additions and 9 deletions

View file

@ -3,7 +3,7 @@
Visualize the relationship between Docker networks and containers Visualize the relationship between Docker networks and containers
as a neat graphviz graph. as a neat graphviz graph.
This repository fork [e-dant/docker-network-graph](https://github.com/e-dant/docker-network-graph) This repository fork [MuratovAS/docker-network-graph](https://github.com/MuratovAS/docker-network-graph)
Changes: Changes:
- Improved design - Improved design
- Added the ability to generate url - Added the ability to generate url
@ -30,7 +30,18 @@ If you want to generate a graph for a remote system you can also easily
run this script inside a pre-built docker container: run this script inside a pre-built docker container:
```bash ```bash
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock ghcr.io/muratovas/docker-network-graph:latest -u build container
docker build . -t simono41/docker-network-graph
and create a PNG
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock simono41/docker-network-graph | dot -Tpng -o out.png
or as SVG
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock simono41/docker-network-graph | dot -Tsvg -o out.svg
``` ```
For more advanced use cases you can append arguments to the `docker run` For more advanced use cases you can append arguments to the `docker run`
@ -40,7 +51,7 @@ command as if you were running it in a local shell.
In most cases what you want to run are the following couple commands: In most cases what you want to run are the following couple commands:
```bash ```bash
git clone https://github.com/muratovas/docker-network-graph.git git clone https://github.com/simono41/docker-network-graph.git
cd docker-network-graph cd docker-network-graph
pipenv install pipenv install
pipenv run python docker-network-graph.py -o output.svg pipenv run python docker-network-graph.py -o output.svg

View file

@ -77,11 +77,10 @@ def get_unique_color() -> str:
i += 1 i += 1
else: else:
# Generate random color if we've already used the 12 preset ones # Generate random color if we've already used the 12 preset ones
c = '#'.join([f"{random.randint(0, 255):02x}" for _ in range(3)]) c = "#{:06x}".format(random.randint(0, 0xFFFFFF))
return c return c
def get_networks( def get_networks(
client: docker.DockerClient, verbose: bool client: docker.DockerClient, verbose: bool
) -> typing.Dict[str, Network]: ) -> typing.Dict[str, Network]:
@ -90,7 +89,7 @@ def get_networks(
for net in sorted(client.networks.list(), key=lambda k: k.name): for net in sorted(client.networks.list(), key=lambda k: k.name):
try: try:
gateway = net.attrs["IPAM"]["Config"][0]["Subnet"] gateway = net.attrs["IPAM"]["Config"][0]["Subnet"]
except (KeyError, IndexError): except (KeyError, IndexError, TypeError):
# This network doesn't seem to be used, skip it # This network doesn't seem to be used, skip it
continue continue
@ -234,13 +233,13 @@ def generate_graph(verbose: bool, file: str, url: str):
comment="Docker Network Graph", comment="Docker Network Graph",
engine="sfdp", engine="sfdp",
format=ext[1:], format=ext[1:],
graph_attr=dict(splines="true"), graph_attr={'splines': 'true', 'rankdir': 'LR'},
) )
else: else:
g = Graph( g = Graph(
comment="Docker Network Graph", comment="Docker Network Graph",
engine="sfdp", engine="sfdp",
graph_attr=dict(splines="true"), graph_attr={'splines': 'true', 'rankdir': 'LR'},
) )
for _, network in networks.items(): for _, network in networks.items():
@ -286,4 +285,4 @@ if __name__ == "__main__":
parser.add_argument("-u", "--url", help="generate link for GraphvizOnline", action="store_true") parser.add_argument("-u", "--url", help="generate link for GraphvizOnline", action="store_true")
args = parser.parse_args() args = parser.parse_args()
generate_graph(args.verbose, args.out, args.url) generate_graph(args.verbose, args.out, args.url)