Skip to content

Commit

Permalink
Issue #507 Implement cleanup feature for macos
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkumar authored and anjannath committed Apr 15, 2020
1 parent 16515f6 commit 598780d
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 34 deletions.
16 changes: 16 additions & 0 deletions pkg/crc/preflight/preflight_check_tray_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func fixDaemonPlistFileExists() error {
return fixPlistFileExists(daemonConfig)
}

func removeDaemonPlistFile() error {
return launchd.RemovePlist(daemonAgentLabel)
}

func checkIfTrayPlistFileExists() error {
if !launchd.PlistExists(trayAgentLabel) {
return fmt.Errorf("Tray plist file does not exist")
Expand All @@ -78,6 +82,10 @@ func fixTrayPlistFileExists() error {
return fixPlistFileExists(trayConfig)
}

func removeTrayPlistFile() error {
return launchd.RemovePlist(trayAgentLabel)
}

func checkIfDaemonAgentRunning() error {
if !launchd.AgentRunning(daemonAgentLabel) {
return fmt.Errorf("crc daemon is not running")
Expand All @@ -93,6 +101,10 @@ func fixDaemonAgentRunning() error {
return launchd.StartAgent(daemonAgentLabel)
}

func unLoadDaemonAgent() error {
return launchd.UnloadPlist(daemonAgentLabel)
}

func checkIfTrayAgentRunning() error {
if !launchd.AgentRunning(trayAgentLabel) {
return fmt.Errorf("Tray is not running")
Expand All @@ -108,6 +120,10 @@ func fixTrayAgentRunning() error {
return launchd.StartAgent(trayAgentLabel)
}

func unLoadTrayAgent() error {
return launchd.UnloadPlist(trayAgentLabel)
}

func checkTrayVersion() error {
v, err := getTrayVersion(constants.TrayAppBundlePath)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/crc/preflight/preflight_checks_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ func fixResolverFilePermissions() error {
return addFileWritePermissionToUser(resolverFile)
}

func removeResolverFile() error {
// Check if the resolver file exist or not
if _, err := os.Stat(resolverFile); !os.IsNotExist(err) {
logging.Debugf("Removing %s file", resolverFile)
_, stdErr, err := crcos.RunWithPrivilege(fmt.Sprintf("Remove file %s", resolverFile), "rm", "-f", resolverFile)
if err != nil {
return fmt.Errorf("Unable to delete the resolver File: %s %v: %s", resolverFile, err, stdErr)
}
}
return nil
}

func checkHostsFilePermissions() error {
return isUserHaveFileWritePermission(hostFile)
}
Expand All @@ -194,6 +206,14 @@ func fixHostsFilePermissions() error {
return addFileWritePermissionToUser(hostFile)
}

func removeUserPermissionForHostsFile() error {
_, stdErr, err := crcos.RunWithPrivilege(fmt.Sprintf("change ownership of %s", hostFile), "chown", "root", hostFile)
if err != nil {
return fmt.Errorf("Unable to change ownership of the filename: %s %v: %s", hostFile, err, stdErr)
}
return nil
}

func isUserHaveFileWritePermission(filename string) error {
err := unix.Access(filename, unix.R_OK|unix.W_OK)
if err != nil {
Expand Down
84 changes: 50 additions & 34 deletions pkg/crc/preflight/preflight_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package preflight

import (
"fmt"

"github.com/code-ready/crc/pkg/crc/logging"
)

// SetupHost performs the prerequisite checks and setups the host to run the cluster
Expand All @@ -26,18 +24,22 @@ var hyperkitPreflightChecks = [...]PreflightCheck{

var dnsPreflightChecks = [...]PreflightCheck{
{
configKeySuffix: "check-resolver-file-permissions",
checkDescription: fmt.Sprintf("Checking file permissions for %s", resolverFile),
check: checkResolverFilePermissions,
fixDescription: fmt.Sprintf("Setting file permissions for %s", resolverFile),
fix: fixResolverFilePermissions,
configKeySuffix: "check-resolver-file-permissions",
checkDescription: fmt.Sprintf("Checking file permissions for %s", resolverFile),
check: checkResolverFilePermissions,
fixDescription: fmt.Sprintf("Setting file permissions for %s", resolverFile),
fix: fixResolverFilePermissions,
cleanupDescription: fmt.Sprintf("Removing %s file", resolverFile),
cleanup: removeResolverFile,
},
{
configKeySuffix: "check-hosts-file-permissions",
checkDescription: fmt.Sprintf("Checking file permissions for %s", hostFile),
check: checkHostsFilePermissions,
fixDescription: fmt.Sprintf("Setting file permissions for %s", hostFile),
fix: fixHostsFilePermissions,
configKeySuffix: "check-hosts-file-permissions",
checkDescription: fmt.Sprintf("Checking file permissions for %s", hostFile),
check: checkHostsFilePermissions,
fixDescription: fmt.Sprintf("Setting file permissions for %s", hostFile),
fix: fixHostsFilePermissions,
cleanupDescription: fmt.Sprintf("Removing current user permission for %s file", hostFile),
cleanup: removeUserPermissionForHostsFile,
},
}

Expand All @@ -50,18 +52,22 @@ var traySetupChecks = [...]PreflightCheck{
flags: SetupOnly,
},
{
checkDescription: "Checking if launchd configuration for daemon exists",
check: checkIfDaemonPlistFileExists,
fixDescription: "Creating launchd configuration for daemon",
fix: fixDaemonPlistFileExists,
flags: SetupOnly,
checkDescription: "Checking if launchd configuration for daemon exists",
check: checkIfDaemonPlistFileExists,
fixDescription: "Creating launchd configuration for daemon",
fix: fixDaemonPlistFileExists,
flags: SetupOnly,
cleanupDescription: "Removing launchd configuration for daemon",
cleanup: removeDaemonPlistFile,
},
{
checkDescription: "Checking if launchd configuration for tray exists",
check: checkIfTrayPlistFileExists,
fixDescription: "Creating launchd configuration for tray",
fix: fixTrayPlistFileExists,
flags: SetupOnly,
checkDescription: "Checking if launchd configuration for tray exists",
check: checkIfTrayPlistFileExists,
fixDescription: "Creating launchd configuration for tray",
fix: fixTrayPlistFileExists,
flags: SetupOnly,
cleanupDescription: "Removing launchd configuration for tray",
cleanup: removeTrayPlistFile,
},
{
checkDescription: "Checking installed tray version",
Expand All @@ -71,18 +77,22 @@ var traySetupChecks = [...]PreflightCheck{
flags: SetupOnly,
},
{
checkDescription: "Checking if CodeReady Containers daemon is running",
check: checkIfDaemonAgentRunning,
fixDescription: "Starting CodeReady Containers daemon",
fix: fixDaemonAgentRunning,
flags: SetupOnly,
checkDescription: "Checking if CodeReady Containers daemon is running",
check: checkIfDaemonAgentRunning,
fixDescription: "Starting CodeReady Containers daemon",
fix: fixDaemonAgentRunning,
flags: SetupOnly,
cleanupDescription: "Unload CodeReady Containers daemon",
cleanup: unLoadDaemonAgent,
},
{
checkDescription: "Check if CodeReady Containers tray is running",
check: checkIfTrayAgentRunning,
fixDescription: "Starting CodeReady Containers tray",
fix: fixTrayAgentRunning,
flags: SetupOnly,
checkDescription: "Check if CodeReady Containers tray is running",
check: checkIfTrayAgentRunning,
fixDescription: "Starting CodeReady Containers tray",
fix: fixTrayAgentRunning,
flags: SetupOnly,
cleanupDescription: "Unload CodeReady Containers tray",
cleanup: unLoadTrayAgent,
},
}

Expand Down Expand Up @@ -117,6 +127,12 @@ func RegisterSettings() {
}

func CleanUpHost() {
logging.Warn("Cleanup is not supported for MacOS")
doCleanUpPreflightChecks(getPreflightChecks())
// A user can use setup with experiment flag
// and not use cleanup with same flag, to avoid
// any extra step/confusion we are just adding the checks
// which are behind the experiment flag. This way cleanup
// perform action in a sane way.
checks := getPreflightChecks()
checks = append(checks, traySetupChecks[:]...)
doCleanUpPreflightChecks(checks)
}
13 changes: 13 additions & 0 deletions pkg/os/launchd/launchd_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ func LoadPlist(label string) error {
return exec.Command("launchctl", "load", getPlistPath(label)).Run() // #nosec G204
}

// UnloadPlist Unloads a launchd agent's service
func UnloadPlist(label string) error {
return exec.Command("launchctl", "unload", getPlistPath(label)).Run() // #nosec G204
}

// RemovePlist removes a launchd agent plist config file
func RemovePlist(label string) error {
if _, err := goos.Stat(getPlistPath(label)); !goos.IsNotExist(err) {
return goos.Remove(getPlistPath(label))
}
return nil
}

// StartAgent starts a launchd agent
func StartAgent(label string) error {
return exec.Command("launchctl", "start", label).Run() // #nosec G204
Expand Down

0 comments on commit 598780d

Please sign in to comment.