podman

Форк
0
91 строка · 2.7 Кб
1
//go:build darwin
2

3
package main
4

5
import (
6
	"errors"
7
	"fmt"
8
	"io/fs"
9
	"os"
10
	"os/exec"
11
	"path/filepath"
12

13
	"github.com/containers/storage/pkg/fileutils"
14
	"github.com/spf13/cobra"
15
)
16

17
var uninstallCmd = &cobra.Command{
18
	Use:    "uninstall",
19
	Short:  "Uninstall the podman helper agent",
20
	Long:   "Uninstall the podman helper agent, which manages the /var/run/docker.sock link",
21
	PreRun: silentUsage,
22
	RunE:   uninstall,
23
}
24

25
func init() {
26
	addPrefixFlag(uninstallCmd)
27
	rootCmd.AddCommand(uninstallCmd)
28
}
29

30
func uninstall(cmd *cobra.Command, args []string) error {
31
	userName, _, homeDir, err := getUser()
32
	if err != nil {
33
		return err
34
	}
35

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

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

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

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

61
	// Get the file information of dockerSock
62
	if err := fileutils.Lexists(dockerSock); err != nil {
63
		// If the error is due to the file not existing, return nil
64
		if errors.Is(err, fs.ErrNotExist) {
65
			return nil
66
		}
67
		// Return an error if unable to get the file information
68
		return fmt.Errorf("could not stat dockerSock: %v", err)
69
	}
70
	if target, err := os.Readlink(dockerSock); err != nil {
71
		// Return an error if unable to read the symlink
72
		return fmt.Errorf("could not read dockerSock symlink: %v", err)
73
	} else {
74
		// Check if the target of the symlink matches the expected target
75
		expectedTarget := filepath.Join(homeDir, ".local", "share", "containers", "podman", "machine", "podman.sock")
76
		if target != expectedTarget {
77
			// If the targets do not match, print the information and return with nothing left to do
78
			fmt.Printf("dockerSock does not point to the expected target: %v\n", target)
79
			return nil
80
		}
81

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

90
	return nil
91
}
92

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.