Skip to content

Commit

Permalink
Merge pull request #504 from village-way/main
Browse files Browse the repository at this point in the history
fix: node-agent exit with 2, when file not found and remove useless s…
  • Loading branch information
kosmos-robot authored May 7, 2024
2 parents ffa2dfb + aa1671c commit 826b40d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 39 deletions.
27 changes: 23 additions & 4 deletions cmd/kubenest/node-agent/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,21 @@ func handleUpload(conn *websocket.Conn, params url.Values) {
_ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseInvalidFramePayloadData, "Invalid file_name or file_path"))
}

/*
0 → success
non-zero → failure
Exit code 1 indicates a general failure
Exit code 2 indicates incorrect use of shell builtins
Exit codes 3-124 indicate some error in job (check software exit codes)
Exit code 125 indicates out of memory
Exit code 126 indicates command cannot execute
Exit code 127 indicates command not found
Exit code 128 indicates invalid argument to exit
Exit codes 129-192 indicate jobs terminated by Linux signals
For these, subtract 128 from the number and match to signal code
Enter kill -l to list signal codes
Enter man signal for more information
*/
func handleCmd(conn *websocket.Conn, params url.Values) {
command := params.Get("command")
if command == "" {
Expand All @@ -210,13 +225,17 @@ func handleCmd(conn *websocket.Conn, params url.Values) {
cmd := exec.Command("sh", "-c", command)
out, err := cmd.CombinedOutput()
if err != nil {
log.Warnf("failed to execute command : %v", err)
_ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%d", cmd.ProcessState.ExitCode())))
exitCode := cmd.ProcessState.ExitCode()
log.Infof("Command : %s finished with exit code %d, output :%s", command, exitCode, string(out))
if strings.Contains(string(out), "No such file") {
exitCode = 127
}
_ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%d", exitCode)))
} else {
_ = conn.WriteMessage(websocket.TextMessage, out)
}
exitCode := cmd.ProcessState.ExitCode()
log.Infof("Command finished with exit code %d", exitCode)
log.Infof("Command : %s finished with exit code %d, output :%s", command, exitCode, string(out))
_ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%d", exitCode)))
}

Expand Down Expand Up @@ -286,7 +305,7 @@ func handleScript(conn *websocket.Conn, params url.Values, command []string) {
if err := cmd.Wait(); err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
log.Warnf("Command exited with non-zero status: %v", exitError)
log.Warnf("Command : %s exited with non-zero status: %v", command, exitError)
}
}
exitCode := cmd.ProcessState.ExitCode()
Expand Down
5 changes: 0 additions & 5 deletions hack/node-agent/build.sh

This file was deleted.

1 change: 0 additions & 1 deletion hack/node-agent/clean.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
sudo systemctl stop node-agent
sudo rm -rf /srv/node-agent
sudo rm /etc/systemd/system/node-agent.service
sudo rm ~/.config/pip/pip.conf
sudo systemctl daemon-reload
6 changes: 0 additions & 6 deletions hack/node-agent/count.sh

This file was deleted.

22 changes: 0 additions & 22 deletions hack/node-agent/install.sh

This file was deleted.

2 changes: 1 addition & 1 deletion hack/node-agent/node-agent.service
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[Unit]
Description=Web Command Service
Description=K8s in K8s Node Agent Service
After=network.target

[Service]
Expand Down

0 comments on commit 826b40d

Please sign in to comment.