enforce valid ipv4 configuration
This commit is contained in:
		
							parent
							
								
									ea6403258b
								
							
						
					
					
						commit
						884c9d8428
					
				| 
						 | 
				
			
			@ -3,6 +3,7 @@ from lib import logging as logging_lib
 | 
			
		|||
from lib import docker_interface
 | 
			
		||||
from lib import get_specs
 | 
			
		||||
import docker
 | 
			
		||||
from docker.types import EndpointConfig, NetworkingConfig
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
client = docker_interface.client
 | 
			
		||||
| 
						 | 
				
			
			@ -76,11 +77,8 @@ def deploy(validated_containers):
 | 
			
		|||
            if "network" in validated_container:
 | 
			
		||||
                container_options["network_mode"]=validated_container["network"]
 | 
			
		||||
            if "ip" in validated_container:
 | 
			
		||||
                container_options["networking_config"]=client.api.create_networking_config({
 | 
			
		||||
                    'clore-br0': client.api.create_endpoint_config(
 | 
			
		||||
                        ipv4_address=validated_container["ip"]
 | 
			
		||||
                    )
 | 
			
		||||
                })
 | 
			
		||||
                del container_options["network_mode"]
 | 
			
		||||
 | 
			
		||||
            if "gpus" in validated_container and type(validated_container["gpus"])==bool:
 | 
			
		||||
                container_options["runtime"]="nvidia"
 | 
			
		||||
                container_options["device_requests"].append(docker.types.DeviceRequest(count=-1, capabilities=[['gpu']]))
 | 
			
		||||
| 
						 | 
				
			
			@ -121,6 +119,9 @@ def deploy(validated_containers):
 | 
			
		|||
 | 
			
		||||
            if not validated_container["name"] in created_container_names and image_ready:
 | 
			
		||||
                container = client.containers.create(**container_options)
 | 
			
		||||
                if "ip" in validated_container:
 | 
			
		||||
                    client.networks.get(validated_container["network"] if "network" in validated_container else "clore-br0").connect(container, ipv4_address=validated_container["ip"])
 | 
			
		||||
                    client.networks.get("bridge").disconnect(container)
 | 
			
		||||
                if not "paused" in validated_container:
 | 
			
		||||
                    container.start()
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
| 
						 | 
				
			
			@ -138,7 +139,12 @@ def deploy(validated_containers):
 | 
			
		|||
                all_stopped_container_names.append(container.name)
 | 
			
		||||
            if container.name in needed_running_names and container.status != 'running':
 | 
			
		||||
                try:
 | 
			
		||||
                    container.start()
 | 
			
		||||
                    attached_networks = container.attrs['NetworkSettings']['Networks']
 | 
			
		||||
                    if "bridge" in attached_networks.keys() or len(attached_networks.keys())==0: # Ip was not attached, remove container
 | 
			
		||||
                        container.stop()
 | 
			
		||||
                        container.remove()
 | 
			
		||||
                    else:
 | 
			
		||||
                        container.start()
 | 
			
		||||
                except Exception as e:
 | 
			
		||||
                    pass
 | 
			
		||||
            elif container.name in paused_names and container.status == 'running':
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue