Add image name extraction
This commit is contained in:
		
							parent
							
								
									3718276f1c
								
							
						
					
					
						commit
						c70ae3eb78
					
				@ -13,13 +13,29 @@
 | 
				
			|||||||
# You should have received a copy of the GNU Affero General Public License
 | 
					# You should have received a copy of the GNU Affero General Public License
 | 
				
			||||||
# along with this program.  If not, see <http:#www.gnu.org/licenses/>.
 | 
					# along with this program.  If not, see <http:#www.gnu.org/licenses/>.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
from typing import List
 | 
					from typing import List
 | 
				
			||||||
from dataclasses import dataclass
 | 
					from dataclasses import dataclass
 | 
				
			||||||
from app.deploy_types import DeployCommandContext, VolumeMapping
 | 
					from app.deploy_types import DeployCommandContext, VolumeMapping
 | 
				
			||||||
 | 
					from app.util import get_parsed_stack_config, get_yaml, get_compose_file_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _container_image_from_service(service: str):
 | 
					def _container_image_from_service(stack:str, service: str):
 | 
				
			||||||
    return "cerc/test-container:local"
 | 
					    # Parse the compose files looking for the image name of the specified service
 | 
				
			||||||
 | 
					    image_name = None
 | 
				
			||||||
 | 
					    parsed_stack = get_parsed_stack_config(stack)
 | 
				
			||||||
 | 
					    pods = parsed_stack["pods"]
 | 
				
			||||||
 | 
					    yaml = get_yaml()
 | 
				
			||||||
 | 
					    for pod in pods:
 | 
				
			||||||
 | 
					        pod_file_path = os.path.join(get_compose_file_dir(), f"docker-compose-{pod}.yml")
 | 
				
			||||||
 | 
					        parsed_pod_file = yaml.load(open(pod_file_path, "r"))
 | 
				
			||||||
 | 
					        if "services" in parsed_pod_file:
 | 
				
			||||||
 | 
					            services = parsed_pod_file["services"]
 | 
				
			||||||
 | 
					            if service in services:
 | 
				
			||||||
 | 
					                service_definition = services[service]
 | 
				
			||||||
 | 
					                if "image" in service_definition:
 | 
				
			||||||
 | 
					                    image_name = service_definition["image"]
 | 
				
			||||||
 | 
					    return image_name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _volumes_to_docker(mounts: List[VolumeMapping]):
 | 
					def _volumes_to_docker(mounts: List[VolumeMapping]):
 | 
				
			||||||
@ -33,7 +49,7 @@ def _volumes_to_docker(mounts: List[VolumeMapping]):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def run_container_command(ctx: DeployCommandContext, service: str, command: str, mounts: List[VolumeMapping]):
 | 
					def run_container_command(ctx: DeployCommandContext, service: str, command: str, mounts: List[VolumeMapping]):
 | 
				
			||||||
    docker = ctx.docker
 | 
					    docker = ctx.docker
 | 
				
			||||||
    container_image = _container_image_from_service(service)
 | 
					    container_image = _container_image_from_service(ctx.stack, service)
 | 
				
			||||||
    docker_volumes = _volumes_to_docker(mounts)
 | 
					    docker_volumes = _volumes_to_docker(mounts)
 | 
				
			||||||
    docker_output = docker.run(container_image, ["-c", command], entrypoint="bash", volumes=docker_volumes)
 | 
					    docker_output = docker.run(container_image, ["-c", command], entrypoint="bash", volumes=docker_volumes)
 | 
				
			||||||
    # There doesn't seem to be a way to get an exit code from docker.run()
 | 
					    # There doesn't seem to be a way to get an exit code from docker.run()
 | 
				
			||||||
 | 
				
			|||||||
@ -20,7 +20,7 @@ import os
 | 
				
			|||||||
from pathlib import Path
 | 
					from pathlib import Path
 | 
				
			||||||
from shutil import copyfile, copytree
 | 
					from shutil import copyfile, copytree
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
from app.util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options, get_yaml
 | 
					from app.util import get_stack_file_path, get_parsed_deployment_spec, get_parsed_stack_config, global_options, get_yaml, get_compose_file_dir
 | 
				
			||||||
from app.deploy_types import DeploymentContext, DeployCommandContext
 | 
					from app.deploy_types import DeploymentContext, DeployCommandContext
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -28,14 +28,6 @@ def _make_default_deployment_dir():
 | 
				
			|||||||
    return "deployment-001"
 | 
					    return "deployment-001"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def _get_compose_file_dir():
 | 
					 | 
				
			||||||
    # TODO: refactor to use common code with deploy command
 | 
					 | 
				
			||||||
    # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
 | 
					 | 
				
			||||||
    data_dir = Path(__file__).absolute().parent.joinpath("data")
 | 
					 | 
				
			||||||
    source_compose_dir = data_dir.joinpath("compose")
 | 
					 | 
				
			||||||
    return source_compose_dir
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def _get_named_volumes(stack):
 | 
					def _get_named_volumes(stack):
 | 
				
			||||||
    # Parse the compose files looking for named volumes
 | 
					    # Parse the compose files looking for named volumes
 | 
				
			||||||
    named_volumes = []
 | 
					    named_volumes = []
 | 
				
			||||||
@ -43,7 +35,7 @@ def _get_named_volumes(stack):
 | 
				
			|||||||
    pods = parsed_stack["pods"]
 | 
					    pods = parsed_stack["pods"]
 | 
				
			||||||
    yaml = get_yaml()
 | 
					    yaml = get_yaml()
 | 
				
			||||||
    for pod in pods:
 | 
					    for pod in pods:
 | 
				
			||||||
        pod_file_path = os.path.join(_get_compose_file_dir(), f"docker-compose-{pod}.yml")
 | 
					        pod_file_path = os.path.join(get_compose_file_dir(), f"docker-compose-{pod}.yml")
 | 
				
			||||||
        parsed_pod_file = yaml.load(open(pod_file_path, "r"))
 | 
					        parsed_pod_file = yaml.load(open(pod_file_path, "r"))
 | 
				
			||||||
        if "volumes" in parsed_pod_file:
 | 
					        if "volumes" in parsed_pod_file:
 | 
				
			||||||
            volumes = parsed_pod_file["volumes"]
 | 
					            volumes = parsed_pod_file["volumes"]
 | 
				
			||||||
 | 
				
			|||||||
@ -56,6 +56,14 @@ def get_parsed_stack_config(stack):
 | 
				
			|||||||
        sys.exit(1)
 | 
					        sys.exit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_compose_file_dir():
 | 
				
			||||||
 | 
					    # TODO: refactor to use common code with deploy command
 | 
				
			||||||
 | 
					    # See: https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
 | 
				
			||||||
 | 
					    data_dir = Path(__file__).absolute().parent.joinpath("data")
 | 
				
			||||||
 | 
					    source_compose_dir = data_dir.joinpath("compose")
 | 
				
			||||||
 | 
					    return source_compose_dir
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_parsed_deployment_spec(spec_file):
 | 
					def get_parsed_deployment_spec(spec_file):
 | 
				
			||||||
    spec_file_path = Path(spec_file)
 | 
					    spec_file_path = Path(spec_file)
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user