From b6a0db351c32d544085e4f36437580e289fdb9a1 Mon Sep 17 00:00:00 2001 From: eleonore12345 Date: Tue, 6 Aug 2024 18:05:33 +0200 Subject: [PATCH] dockermodule avance --- src/DockerModule.cpp | 36 +++++++++++++++++++++++------------- src/DockerModule.h | 1 + src/Module.h | 1 + src/Modules.h | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/DockerModule.cpp b/src/DockerModule.cpp index d26e2e8..95be0b7 100644 --- a/src/DockerModule.cpp +++ b/src/DockerModule.cpp @@ -28,18 +28,28 @@ int DockerModule::deploy (Service service) return 0; } -int DockerModule::clean () + +int removeContainersCreatedByDockerCompose() { - BashManager::execute("docker-compose down --rmi all --remove-orphans"); - /* - if ! "$deploy" ; then - section "Remove stray containers" - while read container ; do - [ -z "$container" ] && continue || true - echo "Removing $container" - run docker rm "$container" - done <<< "$(docker ps | grep "$docker_service" | cut -d ' ' -f 1)" -fi -*/ + return BashManager::execute("docker-compose down --rmi all --remove-orphans"); +} + +int removeContainersWithServiceName(string serviceName) +{ + string dockerService=serviceName.replace(serviceName.find('.'),1,"_"); + string cmd="$(docker ps | grep " + dockerService +" | cut -d ' ' -f 1)"; + return BashManager::execute(cmd); +} + +int DockerModule::remove (Service service) +{ + //remove unwanted containers + removeContainersCreatedByDockerCompose(); + removeContainersWithServiceName(service.getUsername()); return 0; -} \ No newline at end of file +} + +int DockerModule::clean() +{ + return 0; +} \ No newline at end of file diff --git a/src/DockerModule.h b/src/DockerModule.h index dea6ee3..b8d3492 100644 --- a/src/DockerModule.h +++ b/src/DockerModule.h @@ -14,6 +14,7 @@ class DockerModule : public Module ~DockerModule(){} //inline int prepare (); int deploy (Service service); + int remove(Service service); int clean (); }; diff --git a/src/Module.h b/src/Module.h index 756a25b..553c73f 100644 --- a/src/Module.h +++ b/src/Module.h @@ -16,6 +16,7 @@ class Module virtual ~Module()=0; //make protected to ensure it is not used externally? virtual int prepare ()=0; virtual int deploy (Service service)=0; + virtual int remove(Service service)=0; virtual int clean ()=0; }; #endif \ No newline at end of file diff --git a/src/Modules.h b/src/Modules.h index 673c2e4..ba4f9e4 100644 --- a/src/Modules.h +++ b/src/Modules.h @@ -3,4 +3,4 @@ using namespace std; -vector < string > modules = {"DockerModule"}; \ No newline at end of file +vector < string > modules = {"DockerModule"}; \ No newline at end of file