File: uninstall.go

package info (click to toggle)
podman 5.7.0%2Bds2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 23,824 kB
  • sloc: sh: 4,700; python: 2,798; perl: 1,885; ansic: 1,484; makefile: 977; ruby: 42; csh: 8
file content (91 lines) | stat: -rw-r--r-- 2,776 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//go:build darwin

package main

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/spf13/cobra"
	"go.podman.io/storage/pkg/fileutils"
)

var uninstallCmd = &cobra.Command{
	Use:    "uninstall",
	Short:  "Uninstall the podman helper agent",
	Long:   "Uninstall the podman helper agent, which manages the /var/run/docker.sock link",
	PreRun: silentUsage,
	RunE:   uninstall,
}

func init() {
	addPrefixFlag(uninstallCmd)
	rootCmd.AddCommand(uninstallCmd)
}

func uninstall(_ *cobra.Command, _ []string) error {
	userName, _, homeDir, err := getUser()
	if err != nil {
		return err
	}

	labelName := fmt.Sprintf("com.github.containers.podman.helper-%s", userName)
	fileName := filepath.Join("/Library", "LaunchDaemons", labelName+".plist")

	if err = runDetectErr("launchctl", "unload", fileName); err != nil {
		// Try removing the service by label in case the service is half uninstalled
		if rerr := runDetectErr("launchctl", "remove", labelName); rerr != nil {
			// Exit code 3 = no service to remove
			if exitErr, ok := rerr.(*exec.ExitError); !ok || exitErr.ExitCode() != 3 {
				fmt.Fprintf(os.Stderr, "Warning: service unloading failed: %s\n", err.Error())
				fmt.Fprintf(os.Stderr, "Warning: remove also failed: %s\n", rerr.Error())
			}
		}
	}

	if err := os.Remove(fileName); err != nil {
		if !os.IsNotExist(err) {
			return fmt.Errorf("could not remove plist file: %s", fileName)
		}
	}

	helperPath := filepath.Join(installPrefix, "podman", "helper", userName)
	if err := os.RemoveAll(helperPath); err != nil {
		return fmt.Errorf("could not remove helper binary path: %s", helperPath)
	}

	// Get the file information of dockerSock
	if err := fileutils.Lexists(dockerSock); err != nil {
		// If the error is due to the file not existing, return nil
		if errors.Is(err, fs.ErrNotExist) {
			return nil
		}
		// Return an error if unable to get the file information
		return fmt.Errorf("could not stat dockerSock: %v", err)
	}
	if target, err := os.Readlink(dockerSock); err != nil {
		// Return an error if unable to read the symlink
		return fmt.Errorf("could not read dockerSock symlink: %v", err)
	} else {
		// Check if the target of the symlink matches the expected target
		expectedTarget := filepath.Join(homeDir, ".local", "share", "containers", "podman", "machine", "podman.sock")
		if target != expectedTarget {
			// If the targets do not match, print the information and return with nothing left to do
			fmt.Printf("dockerSock does not point to the expected target: %v\n", target)
			return nil
		}

		// Attempt to remove dockerSock
		if err := os.Remove(dockerSock); err != nil {
			if !errors.Is(err, fs.ErrNotExist) {
				return fmt.Errorf("could not remove dockerSock file: %s", err)
			}
		}
	}

	return nil
}