From 8cb3ebebc342e81123a97c3e5809ffdbfbcd5ca4 Mon Sep 17 00:00:00 2001 From: James Nesbitt Date: Thu, 18 Jan 2024 12:32:05 +0200 Subject: [PATCH] PRODENG-2480 remove docker lib/run folders - new docker info function to detect folders - new linux function for removing lib/run folders (default or found) - run new linux remove function in various linux configurer uninstall MCR functions Signed-off-by: James Nesbitt --- Makefile | 18 +++++------ pkg/configurer/enterpriselinux/el.go | 5 +++ pkg/configurer/linux.go | 46 ++++++++++++++++++++++++++-- pkg/configurer/sles/sles.go | 2 ++ pkg/configurer/ubuntu/ubuntu.go | 3 ++ pkg/docker/info.go | 31 +++++++++++++++++++ 6 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 pkg/docker/info.go diff --git a/Makefile b/Makefile index bdd6c65e..3490cbf7 100644 --- a/Makefile +++ b/Makefile @@ -15,13 +15,13 @@ else endif GOOS ?= ${uname_s} BUILDER_IMAGE = launchpad-builder -GO = docker run --rm -v "$(CURDIR)":/go/src/github.com/Mirantis/mcc \ +GO ?= docker run --rm -v "$(CURDIR)":/go/src/github.com/Mirantis/mcc \ -w "/go/src/github.com/Mirantis/mcc" \ -e GOPATH\ -e GOOS \ -e GOARCH \ -e GOEXE \ - $(BUILDER_IMAGE) + $(BUILDER_IMAGE) go gosrc = $(wildcard *.go */*.go */*/*.go */*/*/*.go) VOLUME_MOUNTS=-v "$(CURDIR):/v" @@ -42,16 +42,16 @@ unit-test: builder $(TARGET): $(gosrc) docker build -t $(BUILDER_IMAGE) -f Dockerfile.builder . - GOOS=${GOOS} $(GO) go build $(BUILD_FLAGS) -o $(TARGET) main.go + GOOS=${GOOS} $(GO) build $(BUILD_FLAGS) -o $(TARGET) main.go build: $(TARGET) -build-all: builder - GOOS=linux GOARCH=amd64 $(GO) go build $(BUILD_FLAGS) -o bin/launchpad-linux-x64 main.go - GOOS=linux GOARCH=arm64 $(GO) go build $(BUILD_FLAGS) -o bin/launchpad-linux-arm64 main.go - GOOS=windows GOARCH=amd64 $(GO) go build $(BUILD_FLAGS) -o bin/launchpad-win-x64.exe main.go - GOOS=darwin GOARCH=amd64 $(GO) go build $(BUILD_FLAGS) -o bin/launchpad-darwin-x64 main.go - GOOS=darwin GOARCH=arm64 $(GO) go build $(BUILD_FLAGS) -o bin/launchpad-darwin-arm64 main.go +build-all: + GOOS=linux GOARCH=amd64 $(GO) build $(BUILD_FLAGS) -o bin/launchpad-linux-x64 main.go + GOOS=linux GOARCH=arm64 $(GO) build $(BUILD_FLAGS) -o bin/launchpad-linux-arm64 main.go + GOOS=windows GOARCH=amd64 $(GO) build $(BUILD_FLAGS) -o bin/launchpad-win-x64.exe main.go + GOOS=darwin GOARCH=amd64 $(GO) build $(BUILD_FLAGS) -o bin/launchpad-darwin-x64 main.go + GOOS=darwin GOARCH=arm64 $(GO) build $(BUILD_FLAGS) -o bin/launchpad-darwin-arm64 main.go release: build-all sign-win ./release.sh diff --git a/pkg/configurer/enterpriselinux/el.go b/pkg/configurer/enterpriselinux/el.go index bb771b66..8132cc99 100644 --- a/pkg/configurer/enterpriselinux/el.go +++ b/pkg/configurer/enterpriselinux/el.go @@ -26,12 +26,17 @@ func (c Configurer) UninstallMCR(h os.Host, scriptPath string, engineConfig comm if err != nil { return err } + if err := c.StopService(h, "docker"); err != nil { return err } + if err := c.StopService(h, "containerd"); err != nil { return err } + + defer c.RemoveDockerFolders(h) + return h.Exec("sudo yum remove -y docker-ee docker-ee-cli") } diff --git a/pkg/configurer/linux.go b/pkg/configurer/linux.go index c4b3d495..81b49d00 100644 --- a/pkg/configurer/linux.go +++ b/pkg/configurer/linux.go @@ -7,14 +7,20 @@ import ( "strconv" "strings" - "github.com/Mirantis/mcc/pkg/util" + escape "github.com/alessio/shellescape" + log "github.com/sirupsen/logrus" + "github.com/k0sproject/rig/exec" "github.com/k0sproject/rig/os" + "github.com/Mirantis/mcc/pkg/docker" common "github.com/Mirantis/mcc/pkg/product/common/api" - log "github.com/sirupsen/logrus" + "github.com/Mirantis/mcc/pkg/util" +) - escape "github.com/alessio/shellescape" +const ( + LINUX_DOCKER_PATH_LIB_DEFAULT = "/var/lib/docker" + LINUX_DOCKER_PATH_RUN_DEFAULT = "/var/run/docker" ) // LinuxConfigurer is a generic linux host configurer. @@ -57,6 +63,40 @@ func (c LinuxConfigurer) InstallMCR(h os.Host, scriptPath string, engineConfig c return c.riglinux.StartService(h, "docker") } +// RemoveDockerFolders Remove the docker run and lib folders IF THEY CAN BE FOUND +// - if docker is still running, then `docker info` is checked for run/lib paths +// - default docker run/lib paths are checked +func (c LinuxConfigurer) RemoveDockerFolders(h os.Host) error { + rmps := []string{} + + if di, err := docker.Info(h); err != nil { + log.Warnf("%h: RM Docker paths: could not discover docker paths from info, docker daemon is likely not running. Falling back to default path [%s]: %s", h, LINUX_DOCKER_PATH_LIB_DEFAULT, err.Error()) + rmps = append(rmps, LINUX_DOCKER_PATH_LIB_DEFAULT) + } else if di.DockerRootDir != "" { + log.Warnf("%s: RM Docker Paths: no lib path in docker info, falling back to default path [%s]", h, LINUX_DOCKER_PATH_LIB_DEFAULT) + rmps = append(rmps, LINUX_DOCKER_PATH_LIB_DEFAULT) + } else { + rmps = append(rmps, di.DockerRootDir) + } + + // command to a delete a path + rmc := "rm -rf %s" + // Can we sudo the rm command + if sc, err := h.Sudo(rmc); err == nil { + rmc = sc + } + + for _, p := range rmps { + if err := h.Execf(rmc, p); err != nil { + log.Warnf("%s: RM Docker Paths: Failed to delete path [%s]: %s", h, p, err.Error()) + } else { + log.Infof("%s: RM Docker Paths: deleted path [%s]", h, p) + } + } + + return nil +} + // RestartMCR restarts Docker EE engine. func (c LinuxConfigurer) RestartMCR(h os.Host) error { return c.riglinux.RestartService(h, "docker") diff --git a/pkg/configurer/sles/sles.go b/pkg/configurer/sles/sles.go index d4825610..096f3960 100644 --- a/pkg/configurer/sles/sles.go +++ b/pkg/configurer/sles/sles.go @@ -38,6 +38,8 @@ func (c Configurer) UninstallMCR(h os.Host, scriptPath string, engineConfig comm return err } + defer c.RemoveDockerFolders(h) + return h.Exec("sudo zypper -n remove -y --clean-deps docker-ee docker-ee-cli") } diff --git a/pkg/configurer/ubuntu/ubuntu.go b/pkg/configurer/ubuntu/ubuntu.go index 516dd212..fb5d8734 100644 --- a/pkg/configurer/ubuntu/ubuntu.go +++ b/pkg/configurer/ubuntu/ubuntu.go @@ -31,5 +31,8 @@ func (c Configurer) UninstallMCR(h os.Host, scriptPath string, engineConfig comm if err := c.StopService(h, "containerd"); err != nil { return err } + + defer c.RemoveDockerFolders(h) + return h.Exec("sudo apt-get remove -y docker-ee docker-ee-cli && sudo apt autoremove -y") } diff --git a/pkg/docker/info.go b/pkg/docker/info.go new file mode 100644 index 00000000..9030b657 --- /dev/null +++ b/pkg/docker/info.go @@ -0,0 +1,31 @@ +package docker + +import ( + "encoding/json" + "fmt" + + log "github.com/sirupsen/logrus" + + "github.com/k0sproject/rig/os" +) + +// DockerInfo structural data for information on a hosts docker implementation +type DockerInfo struct { + DockerRootDir string `json:"DockerRootDir"` +} + +// Info retrieve docker info data for a host +// +// @NOTE this function uses rig.os.Host instead of product.mke.api.Host +func Info(h os.Host) (DockerInfo, error) { + di := DockerInfo{} + + log.Infof("%s: Retrieving docker info", h) + + if output, err := h.ExecOutput("docker info --format=json"); err != nil { + return di, fmt.Errorf("%s: failed to retrieve docker info: %s", h, err.Error()) + } else if err := json.Unmarshal([]byte(output), &di); err != nil { + return di, fmt.Errorf("%s: failed to unmarshall docker info: %s", h, err.Error()) + } + return di, nil +}