|
| 1 | +package destroy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/loft-sh/log" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // PlatformContainerName is the name of the docker container for vCluster platform |
| 15 | + PlatformContainerName = "vcluster-platform" |
| 16 | + // PlatformVolumeName is the name of the docker volume for vCluster platform |
| 17 | + PlatformVolumeName = "vcluster-platform" |
| 18 | +) |
| 19 | + |
| 20 | +// ErrDockerPlatformNotFound is returned when no docker platform installation is found |
| 21 | +var ErrDockerPlatformNotFound = fmt.Errorf("no vCluster platform docker installation found (no container or volume)") |
| 22 | + |
| 23 | +// DestroyDocker stops and removes the vCluster platform docker container and volume. |
| 24 | +// If ignoreNotFound is false and no container or volume is found, it returns ErrDockerPlatformNotFound. |
| 25 | +func DestroyDocker(ctx context.Context, ignoreNotFound bool, log log.Logger) error { |
| 26 | + // check if docker is available |
| 27 | + _, err := exec.LookPath("docker") |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("docker is not installed or not available in PATH: %w", err) |
| 30 | + } |
| 31 | + |
| 32 | + // check if docker daemon is running |
| 33 | + output, err := exec.CommandContext(ctx, "docker", "ps").CombinedOutput() |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("docker daemon is not running or not accessible: %s", string(output)) |
| 36 | + } |
| 37 | + |
| 38 | + // track if we found anything to destroy |
| 39 | + foundContainer := false |
| 40 | + foundVolume := false |
| 41 | + |
| 42 | + // check if container exists |
| 43 | + containerID, err := findContainer(ctx, PlatformContainerName) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("failed to find container: %w", err) |
| 46 | + } |
| 47 | + |
| 48 | + if containerID != "" { |
| 49 | + foundContainer = true |
| 50 | + // stop the container |
| 51 | + log.Infof("Stopping vCluster platform container %s...", PlatformContainerName) |
| 52 | + out, err := exec.CommandContext(ctx, "docker", "stop", containerID).CombinedOutput() |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to stop container: %w: %s", err, string(out)) |
| 55 | + } |
| 56 | + |
| 57 | + // remove the container |
| 58 | + log.Infof("Removing vCluster platform container %s...", PlatformContainerName) |
| 59 | + out, err = exec.CommandContext(ctx, "docker", "rm", containerID).CombinedOutput() |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("failed to remove container: %w: %s", err, string(out)) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // check if volume exists and remove it |
| 66 | + hasVolume, err := volumeExists(ctx, PlatformVolumeName) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to check if volume exists: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + if hasVolume { |
| 72 | + foundVolume = true |
| 73 | + log.Infof("Removing vCluster platform volume %s...", PlatformVolumeName) |
| 74 | + out, err := exec.CommandContext(ctx, "docker", "volume", "rm", PlatformVolumeName).CombinedOutput() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to remove volume: %w: %s", err, string(out)) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // check if anything was found |
| 81 | + if !foundContainer && !foundVolume { |
| 82 | + if ignoreNotFound { |
| 83 | + log.Info("No vCluster platform docker installation found") |
| 84 | + return nil |
| 85 | + } |
| 86 | + return ErrDockerPlatformNotFound |
| 87 | + } |
| 88 | + |
| 89 | + log.Info("Successfully destroyed vCluster platform docker installation") |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// findContainer finds a container by name and returns its ID |
| 94 | +func findContainer(ctx context.Context, name string) (string, error) { |
| 95 | + out, err := exec.CommandContext(ctx, "docker", "ps", "-q", "-a", "-f", "name=^"+name+"$").CombinedOutput() |
| 96 | + if err != nil { |
| 97 | + return "", wrapCommandError(out, err) |
| 98 | + } |
| 99 | + |
| 100 | + containerID := strings.TrimSpace(string(out)) |
| 101 | + return containerID, nil |
| 102 | +} |
| 103 | + |
| 104 | +// volumeExists checks if a docker volume exists |
| 105 | +func volumeExists(ctx context.Context, name string) (bool, error) { |
| 106 | + out, err := exec.CommandContext(ctx, "docker", "volume", "ls", "-q", "-f", "name=^"+name+"$").CombinedOutput() |
| 107 | + if err != nil { |
| 108 | + return false, wrapCommandError(out, err) |
| 109 | + } |
| 110 | + |
| 111 | + return strings.TrimSpace(string(out)) != "", nil |
| 112 | +} |
| 113 | + |
| 114 | +func wrapCommandError(stdout []byte, err error) error { |
| 115 | + if err == nil { |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + message := "" |
| 120 | + if len(stdout) > 0 { |
| 121 | + message += string(stdout) + "\n" |
| 122 | + } |
| 123 | + |
| 124 | + var exitError *exec.ExitError |
| 125 | + if errors.As(err, &exitError) && exitError != nil && len(exitError.Stderr) > 0 { |
| 126 | + message += string(exitError.Stderr) + "\n" |
| 127 | + } |
| 128 | + |
| 129 | + return fmt.Errorf("%s%w", message, err) |
| 130 | +} |
0 commit comments