initial commit
This commit is contained in:
89
legacy/fetch.py
Normal file
89
legacy/fetch.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
# Function to fetch JSON data using curl
|
||||
def fetch_json_data(curl_command):
|
||||
try:
|
||||
# Run the curl command and capture output
|
||||
result = subprocess.run(curl_command, shell=True, capture_output=True, text=True)
|
||||
if result.returncode != 0:
|
||||
raise Exception(f"curl command failed: {result.stderr}")
|
||||
# Parse the JSON output
|
||||
return json.loads(result.stdout)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error parsing JSON: {e}")
|
||||
return []
|
||||
except Exception as e:
|
||||
print(f"Error executing curl: {e}")
|
||||
return []
|
||||
|
||||
# Function to clean and simplify container names
|
||||
def clean_container_name(name):
|
||||
# Remove leading slash and any suffixes like '-1' or '-<service>-1'
|
||||
name = name.lstrip('/')
|
||||
# Map container names to simplified service names
|
||||
name_mapping = {
|
||||
'nextcloud-app-1': 'nextcloud',
|
||||
'pgadmin': 'pgadmin',
|
||||
'code-server': 'code',
|
||||
'glance': 'glance',
|
||||
'stirling-pdf-stirling-pdf-1': 'pdf',
|
||||
'technitium-dns': 'dns',
|
||||
'portainer-portainer-1': 'portainer',
|
||||
'browserfile-filebrowser-1': 'filebrowser',
|
||||
'calibre-web': 'calibre',
|
||||
'miniflux_db': 'miniflux-db', # Note: This is the DB container, may not need a link
|
||||
'miniflux': 'miniflux',
|
||||
'jupyterlab': 'jupyter'
|
||||
}
|
||||
return name_mapping.get(name, name)
|
||||
|
||||
# Function to generate links
|
||||
def generate_links(data):
|
||||
base_url = "zserver.zapto.org"
|
||||
links = []
|
||||
|
||||
for container in data:
|
||||
# Get the first name from the Names list
|
||||
if container.get("Names"):
|
||||
container_name = container["Names"][0]
|
||||
service_name = clean_container_name(container_name)
|
||||
# Skip internal services like miniflux_db that typically don't have public links
|
||||
if service_name.endswith('-db'):
|
||||
continue
|
||||
# Check if the container has exposed ports
|
||||
if container.get("Ports"):
|
||||
for port in container["Ports"]:
|
||||
# Only include containers with public ports (bound to 0.0.0.0 or ::)
|
||||
if port.get("IP") in ["0.0.0.0", "::"] and port.get("PublicPort"):
|
||||
link = f"{base_url}/{service_name}"
|
||||
links.append({"Service": service_name.capitalize(), "Link": link})
|
||||
break # Only need one public port per container
|
||||
|
||||
return links
|
||||
|
||||
# Main function
|
||||
def main():
|
||||
# Example curl command to fetch container data (adjust as needed)
|
||||
# For Docker API, you might use: curl -s --unix-socket /var/run/docker.sock http://localhost/containers/json
|
||||
# For Portainer, you might use: curl -s -H "Authorization: Bearer <token>" http://<portainer-host>/api/endpoints/<endpoint-id>/docker/containers/json
|
||||
curl_command = 'curl -X GET "https://zserver.zapto.org/portainer/api/endpoints/2/docker/containers/json?all=true" -H "X-API-Key: ptr_KFQqKse9K4Nc9M5jnpc61fpAvGzdTOXTzswz9CwOF74=" -u "admin:shakkal123"'
|
||||
|
||||
# Fetch JSON data
|
||||
data = fetch_json_data(curl_command)
|
||||
|
||||
if not data:
|
||||
print("No data fetched or invalid JSON.")
|
||||
return
|
||||
|
||||
# Generate links
|
||||
links = generate_links(data)
|
||||
|
||||
# Print links in a table format
|
||||
print("| Service | Link |")
|
||||
print("|---------|------|")
|
||||
for link in links:
|
||||
print(f"| {link['Service']} | {link['Link']} |")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user