File: preflight_checks_common.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (186 lines) | stat: -rw-r--r-- 5,599 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package preflight

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/crc-org/crc/v2/pkg/crc/adminhelper"
	"github.com/crc-org/crc/v2/pkg/crc/cluster"
	"github.com/crc-org/crc/v2/pkg/crc/constants"
	"github.com/crc-org/crc/v2/pkg/crc/logging"
	"github.com/crc-org/crc/v2/pkg/crc/machine/bundle"
	crcpreset "github.com/crc-org/crc/v2/pkg/crc/preset"
	"github.com/crc-org/crc/v2/pkg/crc/ssh"
	"github.com/crc-org/crc/v2/pkg/crc/validation"
	crcos "github.com/crc-org/crc/v2/pkg/os"
	"github.com/docker/go-units"
	"github.com/pkg/errors"
)

func bundleCheck(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) Check {
	return Check{
		configKeySuffix:  "check-bundle-extracted",
		checkDescription: "Checking if CRC bundle is extracted in '$HOME/.crc'",
		check:            checkBundleExtracted(bundlePath),
		fixDescription:   "Getting bundle for the CRC executable",
		fix:              fixBundleExtracted(bundlePath, preset, enableBundleQuayFallback),
		flags:            SetupOnly,

		labels: None,
	}
}

func memoryCheck(preset crcpreset.Preset) Check {
	return Check{
		configKeySuffix:  "check-ram",
		checkDescription: "Checking minimum RAM requirements",
		check: func() error {
			return validation.ValidateEnoughMemory(constants.GetDefaultMemory(preset))
		},
		fixDescription: fmt.Sprintf("crc requires at least %s to run", units.HumanSize(float64(constants.GetDefaultMemory(preset)*1024*1024))),
		flags:          NoFix,

		labels: None,
	}
}

var genericCleanupChecks = []Check{
	{
		cleanupDescription: "Removing CRC Machine Instance directory",
		cleanup:            removeCRCMachinesDir,
		flags:              CleanUpOnly,

		labels: None,
	},
	{
		cleanupDescription: "Removing older logs",
		cleanup:            removeAllLogs,
		flags:              CleanUpOnly,

		labels: None,
	},
	{
		cleanupDescription: "Removing pull secret from the keyring",
		cleanup:            cluster.ForgetPullSecret,
		flags:              CleanUpOnly,

		labels: None,
	},
	{
		cleanupDescription: "Removing hosts file records added by CRC",
		cleanup:            removeHostsFileEntry,
		flags:              CleanUpOnly,

		labels: None,
	},
	{
		cleanupDescription: "Removing CRC Specific entries from user's known_hosts file",
		cleanup:            removeCRCHostEntriesFromKnownHosts,
		flags:              CleanUpOnly,

		labels: None,
	},
}

func checkBundleExtracted(bundlePath string) func() error {
	return func() error {
		logging.Infof("Checking if %s exists", bundlePath)
		bundleName := bundle.GetBundleNameFromURI(bundlePath)
		if _, err := bundle.Get(bundleName); err != nil {
			logging.Debugf("error getting bundle info for %s: %v", bundleName, err)
			return err
		}
		logging.Debugf("%s exists", bundlePath)
		return nil
	}
}

func fixBundleExtracted(bundlePath string, preset crcpreset.Preset, enableBundleQuayFallback bool) func() error {
	// Should be removed after 1.19 release
	// This check will ensure correct mode for `~/.crc/cache` directory
	// in case it exists.
	if err := os.Chmod(constants.MachineCacheDir, 0775); err != nil {
		logging.Debugf("Error changing %s permissions to 0775", constants.MachineCacheDir)
	}

	return func() error {
		bundleDir := filepath.Dir(constants.GetDefaultBundlePath(preset))
		logging.Debugf("Ensuring directory %s exists", bundleDir)
		if err := os.MkdirAll(bundleDir, 0775); err != nil {
			return fmt.Errorf("Cannot create directory %s: %v", bundleDir, err)
		}
		var err error
		logging.Infof("Downloading bundle: %s...", bundlePath)
		if bundlePath, err = bundle.Download(preset, bundlePath, enableBundleQuayFallback); err != nil {
			return err
		}

		logging.Infof("Uncompressing %s", bundlePath)
		if _, err := bundle.Extract(bundlePath); err != nil {
			if errors.Is(err, os.ErrNotExist) {
				return errors.Wrap(err, "Use `crc setup -b <bundle-path>`")
			}
			return err
		}
		return nil
	}
}

func removeHostsFileEntry() error {
	err := adminhelper.CleanHostsFile()
	if errors.Is(err, os.ErrNotExist) {
		return nil
	}
	return err
}

func removeCRCMachinesDir() error {
	logging.Debug("Deleting machines directory")
	if err := os.RemoveAll(constants.MachineInstanceDir); err != nil {
		return fmt.Errorf("Failed to delete crc machines directory: %w", err)
	}
	return nil
}

func removeAllLogs() error {
	// remove all log files, need close the logfile before deletion
	logging.CloseLogging()
	if err := crcos.RemoveFileGlob(filepath.Join(constants.CrcBaseDir, "*.log")); err != nil {
		return fmt.Errorf("Failed to remove old log files: %w", err)
	}
	return nil
}

func removeCRCHostEntriesFromKnownHosts() error {
	return ssh.RemoveCRCHostEntriesFromKnownHosts()
}

func checkPodmanInOcBinDir() error {
	podmanBinPath := filepath.Join(constants.CrcOcBinDir, constants.PodmanRemoteExecutableName)
	if crcos.FileExists(podmanBinPath) {
		return fmt.Errorf("Found podman executable: %s", podmanBinPath)
	}
	return nil
}

func fixPodmanInOcBinDir() error {
	podmanBinPath := filepath.Join(constants.CrcOcBinDir, constants.PodmanRemoteExecutableName)
	if crcos.FileExists(podmanBinPath) {
		logging.Debugf("Removing podman binary at: %s", podmanBinPath)
		return os.Remove(podmanBinPath)
	}
	return nil
}

func removePodmanFromOcBinDirCheck() Check {
	return Check{
		configKeySuffix:  "check-podman-in-ocbindir",
		checkDescription: fmt.Sprintf("Check if Podman binary exists in: %s", constants.CrcOcBinDir),
		check:            checkPodmanInOcBinDir,
		fixDescription:   fmt.Sprintf("Removing Podman binary from: %s", constants.CrcOcBinDir),
		fix:              fixPodmanInOcBinDir,

		labels: None,
	}
}