diff --git a/cmd/kubenest/node-agent/app.go b/cmd/kubenest/node-agent/app.go index 2dbe17e9d..136324d03 100644 --- a/cmd/kubenest/node-agent/app.go +++ b/cmd/kubenest/node-agent/app.go @@ -216,27 +216,14 @@ Enter man signal for more information */ func handleCmd(conn *websocket.Conn, params url.Values) { command := params.Get("command") + args := params["args"] + // if the command is file, the file should have execute permission if command == "" { log.Warnf("No command specified %v", params) _ = conn.WriteMessage(websocket.TextMessage, []byte("No command specified")) return } - - cmd := exec.Command("sh", "-c", command) - out, err := cmd.CombinedOutput() - if err != nil { - 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 : %s finished with exit code %d, output :%s", command, exitCode, string(out)) - _ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%d", exitCode))) + execCmd(conn, command, args) } func handleScript(conn *websocket.Conn, params url.Values, command []string) { @@ -275,12 +262,16 @@ func handleScript(conn *websocket.Conn, params url.Values, command []string) { continue } } - - // Execute the Python script executeCmd := append(command, tempFile.Name()) executeCmd = append(executeCmd, args...) + // Execute the Python script + execCmd(conn, executeCmd[0], executeCmd[1:]) +} + +func execCmd(conn *websocket.Conn, command string, args []string) { + // #nosec G204 - cmd := exec.Command(executeCmd[0], executeCmd[1:]...) + cmd := exec.Command(command, args...) stdout, err := cmd.StdoutPipe() if err != nil { log.Warnf("Error obtaining command output pipe: %v", err) @@ -288,7 +279,12 @@ func handleScript(conn *websocket.Conn, params url.Values, command []string) { defer stdout.Close() if err := cmd.Start(); err != nil { - log.Warnf("Error starting command: %v", err) + errStr := strings.ToLower(err.Error()) + log.Warnf("Error starting command: %v, %s", err, errStr) + if strings.Contains(errStr, "no such file") { + exitCode := 127 + _ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%d", exitCode))) + } } // processOutput @@ -309,5 +305,6 @@ func handleScript(conn *websocket.Conn, params url.Values, command []string) { } } exitCode := cmd.ProcessState.ExitCode() + log.Infof("Command : %s finished with exit code %d", command, exitCode) _ = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%d", exitCode))) } diff --git a/hack/node-agent/init.sh b/hack/node-agent/init.sh index 843b61f95..4d0ae3340 100644 --- a/hack/node-agent/init.sh +++ b/hack/node-agent/init.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -set -e -sed -i 's/^WEB_USER=.*/WEB_USER=$(WEB_USER)/' /app/agent.env -sed -i 's/^WEB_PASS=.*/WEB_PASS=$(WEB_PASS)/' /app/agent.env + +WEB_USER="$WEB_USER" sed -i 's/^WEB_USER=.*/WEB_USER='"$WEB_USER"'/' /app/agent.env +WEB_PASS="$WEB_PASS" sed -i 's/^WEB_PASS=.*/WEB_PASS='"$WEB_PASS"'/' /app/agent.env sha256sum /app/node-agent > node-agent.sum sha256sum /host-path/node-agent >> node-agent.sum rsync -avz /app/ /host-path/ diff --git a/hack/node-agent/start.sh b/hack/node-agent/start.sh index a0ced6898..d32f2404f 100755 --- a/hack/node-agent/start.sh +++ b/hack/node-agent/start.sh @@ -1,6 +1,14 @@ #!/usr/bin/env bash img_app_sum=$(head -n 1 /srv/node-agent/node-agent.sum | cut -d' ' -f1) host_app_sum=$(sed -n '2p' /srv/node-agent/node-agent.sum | cut -d' ' -f1) +if [ -z "$img_app_sum" ] || [ -z "$host_app_sum" ]; then + echo "can not get app sum, restart node-agent" + sudo systemctl daemon-reload + sudo systemctl enable node-agent + sudo systemctl stop node-agent + sudo systemctl start node-agent +fi +return 0 if [ "$img_app_sum" == "$host_app_sum" ]; then echo "app is same, skip restart node-agent" else