-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preflight: Kill daemon process if running with a different version
This pr try to kill an existing daemon procecss if it is running with different version than expected. It will allow setup to run required version of daemon.
- Loading branch information
1 parent
0a46284
commit 4de2dce
Showing
199 changed files
with
28,299 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package preflight | ||
|
||
import ( | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/code-ready/crc/pkg/crc/daemonclient" | ||
"github.com/code-ready/crc/pkg/crc/logging" | ||
"github.com/shirou/gopsutil/v3/process" | ||
) | ||
|
||
func killDaemonProcess() error { | ||
crcProcessName := "crc" | ||
if runtime.GOOS == "windows" { | ||
crcProcessName = "crc.exe" | ||
} | ||
processes, err := process.Processes() | ||
if err != nil { | ||
return err | ||
} | ||
for _, process := range processes { | ||
// Ignore the error from process.Name() because errors | ||
// come from processes which are not owned by the current user | ||
// on Mac - https://github.com/shirou/gopsutil/issues/1288 | ||
name, _ := process.Name() | ||
if name == crcProcessName { | ||
cmdLine, err := process.CmdlineSlice() | ||
if err != nil { | ||
return err | ||
} | ||
if isDaemonProcess(cmdLine) { | ||
logging.Debugf("Got the pid for %s : %d", name, process.Pid) | ||
if err := process.Kill(); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// isDaemonProcess return true if any of the following conditions met | ||
// - crc daemon <options> | ||
// - crc --log-level=<level> daemon <options> | ||
// - crc --log-level <level> daemon <options> | ||
func isDaemonProcess(cmdLine []string) bool { | ||
if len(cmdLine) >= 2 && cmdLine[1] == "daemon" { | ||
return true | ||
} | ||
if len(cmdLine) >= 3 && strings.HasPrefix(cmdLine[1], "--log-level") && cmdLine[2] == "daemon" { | ||
return true | ||
} | ||
if len(cmdLine) >= 4 && cmdLine[1] == "--log-level" && cmdLine[3] == "daemon" { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
func olderDaemonVersionRunning() error { | ||
// Here daemonclient.GetVersionFromDaemonAPI() can return the error | ||
// if the daemon is not running or daemon version API is not responding | ||
// in both situation we can't check if daemon is running with an older | ||
// version of crc or not, so we are just ignoring the error from it. | ||
v, err := daemonclient.GetVersionFromDaemonAPI() | ||
if err != nil { | ||
return nil | ||
} | ||
return daemonclient.CheckIfOlderVersion(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.