This commit is contained in:
		
							
								
								
									
										58
									
								
								scripts/script.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								scripts/script.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,58 @@
 | 
			
		||||
import yaml
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
# Define the paths to your Docker Compose and Portainer app template files
 | 
			
		||||
docker_compose_file = "docker-compose.yml"
 | 
			
		||||
portainer_template_file = "portainer-template.json"
 | 
			
		||||
 | 
			
		||||
# Load the Docker Compose file
 | 
			
		||||
with open(docker_compose_file, 'r') as compose_file:
 | 
			
		||||
    compose_data = yaml.safe_load(compose_file)
 | 
			
		||||
 | 
			
		||||
# Initialize an empty list to store new template entries
 | 
			
		||||
new_template_entries = []
 | 
			
		||||
 | 
			
		||||
# Iterate through services in the Docker Compose file
 | 
			
		||||
for service_name, service_config in compose_data.get("services", {}).items():
 | 
			
		||||
    # Get the container name from the service configuration
 | 
			
		||||
    container_name = service_config.get("container_name", service_name)
 | 
			
		||||
 | 
			
		||||
    # Create a new template entry based on the service configuration
 | 
			
		||||
    new_template_entry = {
 | 
			
		||||
        "type": 1,
 | 
			
		||||
        "title": container_name,
 | 
			
		||||
        "description": "Description of the service",  # Customize as needed
 | 
			
		||||
        "categories": ["SthopeDev"],  # Customize as needed
 | 
			
		||||
        "platform": "linux",  # Customize as needed
 | 
			
		||||
        "logo": "https://yourdomain.url/path/to/logo.png",  # Replace with logo URL
 | 
			
		||||
        "image": service_config.get("image", ""),
 | 
			
		||||
        "ports": [f"{port}/tcp" for port in service_config.get("ports", [])],
 | 
			
		||||
        "volumes": [{"container": volume.split(":")[0], "bind": volume.split(":")[1]} for volume in service_config.get("volumes", [])],
 | 
			
		||||
        "environment": [{"name": env_var.split("=")[0], "label": env_var.split("=")[0], "default": env_var.split("=")[1]} for env_var in service_config.get("environment", [])],
 | 
			
		||||
        "restart": service_config.get("restart", "no"),
 | 
			
		||||
        "labels": service_config.get("labels", {}),  # Add labels
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    # Check if "network_mode" is present and add it as "network"
 | 
			
		||||
    if "network_mode" in service_config:
 | 
			
		||||
        new_template_entry["network"] = service_config["network_mode"]
 | 
			
		||||
 | 
			
		||||
    # Check if "user" is present and add it
 | 
			
		||||
    if "user" in service_config:
 | 
			
		||||
        new_template_entry["user"] = service_config["user"]
 | 
			
		||||
 | 
			
		||||
    # Add the new template entry to the list
 | 
			
		||||
    new_template_entries.append(new_template_entry)
 | 
			
		||||
 | 
			
		||||
# Load the existing Portainer app template file
 | 
			
		||||
with open(portainer_template_file, 'r') as template_file:
 | 
			
		||||
    template_data = json.load(template_file)
 | 
			
		||||
 | 
			
		||||
# Add the new template entries to the existing Portainer app template
 | 
			
		||||
template_data["templates"].extend(new_template_entries)
 | 
			
		||||
 | 
			
		||||
# Save the updated Portainer app template
 | 
			
		||||
with open(portainer_template_file, 'w') as template_file:
 | 
			
		||||
    json.dump(template_data, template_file, indent=2)
 | 
			
		||||
 | 
			
		||||
print("Services from the Docker Compose file have been added to the Portainer app template.")
 | 
			
		||||
		Reference in New Issue
	
	Block a user