Skip to content

Commit

Permalink
IMP guess url of the services with docker inspect (#132)
Browse files Browse the repository at this point in the history
* IMP guess url of the services with docker inspect

* IMP guessing url with docker py lib instead of docker inspect
  • Loading branch information
bealdav authored Mar 23, 2020
1 parent 79ef5a0 commit 75300ad
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docky/common/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @author Sébastien BEAU <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import docker
from compose.project import OneOffFilter
from compose.cli import command
from compose.config.errors import ComposeFileNotFound
Expand Down Expand Up @@ -42,16 +43,43 @@ def get_containers(self, service=None):
return self.project.containers(**kwargs)

def display_service_tooltip(self):
infos = self._get_services_info()
for service in self.project.services:
labels = service.options.get('labels', {})
if labels.get('docky.access.help'):
# TODO remove after some versions
logger.warning(
"'docky.access.help' is replaced by 'docky.help'. "
"Please update this key in your docker files.")
if infos.get(service.name):
# some applications provide extra parameters to access resource
infos[service.name] += labels.get("docky.url_suffix", "")
logger.info(infos[service.name])
if labels.get('docky.help'):
logger.info(labels.get('docky.help'))

def _get_services_info(self):
""" Search IP and Port for each services
"""
client = docker.from_env()
services = (x for x in client.containers.list()
if self.project.name in x.attrs["Name"])
infos = {}
for serv in services:
local = "%s_local" % self.project.name
ip = serv.attrs["NetworkSettings"]["Networks"][local].get("IPAddress", "")
info = {
"name": serv.attrs["Config"]["Labels"].get(
"com.docker.compose.service", ""),
"ip": ip,
"port": [x for x in serv.attrs["NetworkSettings"].get("Ports", "")]
}
info["port"] = info["port"] and info["port"][0].replace("/tcp", "") or ""
if info["name"] != "db":
# There is no web app to access 'db' service: try adminer for that
infos[info["name"]] = "%(name)s http://%(ip)s:%(port)s" % (info)
return infos

def create_volume(self):
"""Mkdir volumes if they don't exist yet.
Expand Down

0 comments on commit 75300ad

Please sign in to comment.