Skip to content

Commit

Permalink
feat: cmd support stream log output kosmos-io#544
Browse files Browse the repository at this point in the history
Signed-off-by: wangdepeng <wangdepeng_yewu@cmss.chinamobile.com>
  • Loading branch information
village-way committed May 15, 2024
1 parent aab1f53 commit 0aa1653
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
36 changes: 16 additions & 20 deletions cmd/kubenest/node-agent/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -275,20 +262,28 @@ 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)
}
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
Expand All @@ -309,5 +304,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)))
}
6 changes: 3 additions & 3 deletions hack/node-agent/init.sh
Original file line number Diff line number Diff line change
@@ -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/
Expand Down
8 changes: 8 additions & 0 deletions hack/node-agent/start.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0aa1653

Please sign in to comment.