saltyorg / sb

0 stars 9 forks source link

Add list ports command(s) #18

Open saltydk opened 7 months ago

saltydk commented 7 months ago

Add one or several commands related to identifying what hostname and port to use for a given role/install.

Some boilerplate Python:

import docker

client = docker.from_env()

def get_traefik_service_info(container):
    labels = container.labels
    services_info = []

    # Filter Traefik service labels
    for key, value in labels.items():
        if 'traefik.http.services' in key and '.loadbalancer.server.port' in key and '-http' not in key:
            # Adjusted to correctly parse the service name
            service_name = key.split('.')[3]
            services_info.append((service_name, value))

    return services_info

def get_port_bindings(container):
    port_bindings = container.attrs['HostConfig']['PortBindings'] or {}
    binding_info = ', '.join([f"{k} -> {', '.join(vv['HostPort'] for vv in v)}" for k, v in port_bindings.items()]) if port_bindings else "No port bindings"
    return binding_info

def print_service_info(container):
    services_info = get_traefik_service_info(container)
    binding_info = get_port_bindings(container)

    # Print only if there are Traefik services or port bindings
    if services_info or binding_info != "No port bindings":
        print(f"Container:       {container.name}")
        if services_info:
            for service_name, port in services_info:
                print(f"Traefik Service: {service_name} - Port: {port}")
        else:
            print("Traefik Service: None")
        print(f"Port Bindings:   {binding_info}\n")

def main():
    for container in client.containers.list():
        print_service_info(container)

if __name__ == "__main__":
    main()

Not the best format as far as the output so could use some formatting suggestions as to what we want this to output both as a single container lookup and as a look at everything installed output.

keldian commented 7 months ago

How about a table? Something like:

+-------------------+-------------------+-------+------------------------+
|     Container     |  Traefik Service  | Port  |      Port Bindings     |
+-------------------+-------------------+-------+------------------------+
| authelia          | authelia          | 9091  | No port bindings       |
| lldap             | lldap             | 17170 | No port bindings       |
| plex              | plex-webtools     | 33400 | No port bindings       |
|                   | plex              | 32400 |                        |
| radarr            | radarr            | 7878  | No port bindings       |
| rutorrent         | rutorrent         | 80    | 51413/tcp -> 51413,    |
|                   |                   |       | 51413/udp -> 51413,    |
|                   |                   |       | 6881/udp -> 6881       |
| traefik           | None              | None  | 443/tcp -> 443,        |
|                   |                   |       | 443/udp -> 443,        |
|                   |                   |       | 80/tcp -> 80           |
+-------------------+-------------------+-------+------------------------+
saltydk commented 7 months ago
+-------------------+-------------------+-------------------+-------------------+
|     Container     |  Traefik Service  |        Port       |   Port Bindings   |
+-------------------+-------------------+-------------------+-------------------+
| radarrjapanese    | radarrjapanese    | 7878              |                   |
+-------------------+-------------------+-------------------+-------------------+
| radarrkorean      | radarrkorean      | 7878              |                   |
+-------------------+-------------------+-------------------+-------------------+
| radarranime       | radarranime       | 7878              |                   |
+-------------------+-------------------+-------------------+-------------------+
| radarrdanish      | radarrdanish      | 7878              |                   |
+-------------------+-------------------+-------------------+-------------------+
| radarr4k          | radarr4k          | 7878              |                   |
+-------------------+-------------------+-------------------+-------------------+
| radarr            | radarr            | 7878              |                   |
+-------------------+-------------------+-------------------+-------------------+
| sonarrjapanese    | sonarrjapanese    | 8989              |                   |
+-------------------+-------------------+-------------------+-------------------+
| sonarrkorean      | sonarrkorean      | 8989              |                   |
+-------------------+-------------------+-------------------+-------------------+
| sonarrdanish      | sonarrdanish      | 8989              |                   |
+-------------------+-------------------+-------------------+-------------------+
| sonarranime       | sonarranime       | 8989              |                   |
+-------------------+-------------------+-------------------+-------------------+
| sonarr            | sonarr            | 8989              |                   |
+-------------------+-------------------+-------------------+-------------------+
| prowlarr          | prowlarr          | 9696              |                   |
+-------------------+-------------------+-------------------+-------------------+
| jackett           | jackett           | 9117              |                   |
+-------------------+-------------------+-------------------+-------------------+
| autobrr           | autobrr           | 7474              |                   |
+-------------------+-------------------+-------------------+-------------------+
| netdata           | netdata           | 19999             |                   |
+-------------------+-------------------+-------------------+-------------------+
| your-spotify      | your-spotify      | 80                |                   |
+-------------------+-------------------+-------------------+-------------------+
| omegabrr          | omegabrr          | 7441              |                   |
+-------------------+-------------------+-------------------+-------------------+
| pgadmin           | pgadmin           | 80                |                   |
+-------------------+-------------------+-------------------+-------------------+
| traefik           |                   |                   | 443/tcp           |
|                   |                   |                   | 443/udp           |
|                   |                   |                   | 80/tcp            |
+-------------------+-------------------+-------------------+-------------------+
| dozzle            | dozzle            | 8080              |                   |
+-------------------+-------------------+-------------------+-------------------+
| portainer         | portainer         | 9000              |                   |
+-------------------+-------------------+-------------------+-------------------+

Something like that?

RaneyDazed commented 7 months ago

I like yours salty. I came up w this :p kinda ugly. I feel like i should leave this up to the adults. I can drop the "no port bindings" and the -> port but yours is pertier.

+-------------------+--------------------------+-------------------------+
|     Container     | Traefik Service and Port |      Port Bindings      |
+-------------------+--------------------------+-------------------------+
|     authelia      |      authelia: 9091      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|      autobrr      |      autobrr: 7474       |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|      immich       |       immich: 3001       |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|      lgsm_pw      |           None           |   27015/udp -> 27015,   |
|                   |                          |    8211/udp -> 8211     |
|    -----------    |       -----------        |       -----------       |
|       lldap       |       lldap: 17170       |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|     notifiarr     |     notifiarr: 5454      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|     omegabrr      |      omegabrr: 7441      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|     overseerr     |     overseerr: 5055      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|     prowlarr      |      prowlarr: 9696      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|    qbittorrent    |    qbittorrent: 8080     |   56881/tcp -> 56881,   |
|                   |                          |   56881/udp -> 56881    |
|    -----------    |       -----------        |       -----------       |
|      radarr       |       radarr: 7878       |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|     radarr4k      |      radarr4k: 7878      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|    radarranime    |    radarranime: 7878     |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|      sabnzbd      |      sabnzbd: 8080       |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|      sonarr       |       sonarr: 8989       |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|     sonarr4k      |      sonarr4k: 8989      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|    sonarranime    |    sonarranime: 8989     |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|      traefik      |           None           | 443/tcp -> 443, 443/udp |
|                   |                          |  -> 443, 80/tcp -> 80   |
|    -----------    |       -----------        |       -----------       |
| traefik-robotstxt |     robotstxt: 8080      |    No port bindings     |
|    -----------    |       -----------        |       -----------       |
|    vaultwarden    |    vaultwarden: 6423     |    No port bindings     |
+-------------------+--------------------------+-------------------------+
keldian commented 7 months ago

@https://github.com/saltyorg/sb/issues/18#issuecomment-1935192399

Yes, even better with row dividers. Could also cut down the headers a bit so they stand out more (or less?):

      Container        Traefik Service           Port           Port Bindings 
+-------------------+-------------------+-------------------+-------------------+
| omegabrr          | omegabrr          | 7441              |                   |
+-------------------+-------------------+-------------------+-------------------+
| pgadmin           | pgadmin           | 80                |                   |
+-------------------+-------------------+-------------------+-------------------+
| traefik           |                   |                   | 443/tcp           |
|                   |                   |                   | 443/udp           |
|                   |                   |                   | 80/tcp            |
+-------------------+-------------------+-------------------+-------------------+
| dozzle            | dozzle            | 8080              |                   |
+-------------------+-------------------+-------------------+-------------------+
| portainer         | portainer         | 9000              |                   |
+-------------------+-------------------+-------------------+-------------------+

Unicode version:

      Container        Traefik Service           Port           Port Bindings 
┌───────────────────┼───────────────────┼───────────────────┼───────────────────┐
│ omegabrr          │ omegabrr          │ 7441              │                   │
├───────────────────┼───────────────────┼───────────────────┼───────────────────┤
│ pgadmin           │ pgadmin           │ 80                │                   │
├───────────────────┼───────────────────┼───────────────────┼───────────────────┤
│ traefik           │                   │                   │ 443/tcp           │
│                   │                   │                   │ 443/udp           │
│                   │                   │                   │ 80/tcp            │
├───────────────────┼───────────────────┼───────────────────┼───────────────────┤
│ dozzle            │ dozzle            │ 8080              │                   │
├───────────────────┼───────────────────┼───────────────────┼───────────────────┤
│ portainer         │ portainer         │ 9000              │                   │
└───────────────────┴───────────────────┴───────────────────┴───────────────────┘
RaneyDazed commented 7 months ago

I like them sorted :p

keldian commented 7 months ago

Sorting is a given. The question is by which attribute first, container? port?

There will be a single container lookup argument anyway, so we won't need sorting to find a specific thing, at least.

RaneyDazed commented 7 months ago

isn't the idea to have either a single container lookup or lookup all? in which case alphabetical sort might be nice. like sb ports or sb ports sabnzbd?

I tried with and without alphabetical sort by container name and I think readability is cleaner with alphabetical. I don't think there's much use for sorting by port, but I could be wrong.

keldian commented 7 months ago

"Cleaner" I don't see how, and again, I can't think of a practical use for sorting the full list by `container` besides finding a specific row more easily—a use case the single container lookup already covers—but let me know if there is one.

On the other hand, it might be interesting to have a quick notion of which containers use the same listening port, for that limited use case where a user might want to publish them and avoid conflicts at least. Idk.