From 486c8c32f9dd9e5505543be905dca75b2a42bd97 Mon Sep 17 00:00:00 2001 From: Raphael Date: Tue, 29 Jul 2025 22:19:15 +0000 Subject: [PATCH 1/2] fix error when image doesnt contain netcat --- .../make_k8s_activity_service.go | 31 +++++++++++++++++-- .../scripts/monitor_disk_spec_storage.sh | 30 ++++++++++++++++++ pkg/server/scripts/monitor_files_storage.sh | 24 ++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 pkg/server/scripts/monitor_disk_spec_storage.sh create mode 100644 pkg/server/scripts/monitor_files_storage.sh diff --git a/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go b/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go index b9631bc..bcdd443 100755 --- a/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go +++ b/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go @@ -2,9 +2,12 @@ package kubernetes_runtime_service import ( "encoding/base64" + "io/ioutil" + "log" "math/rand" "os" "strconv" + "strings" "github.com/ovvesley/akoflow/pkg/server/entities/k8s_job_entity" "github.com/ovvesley/akoflow/pkg/server/entities/workflow_activity_entity" @@ -84,15 +87,39 @@ func (m *MakeK8sActivityService) getPortAkoFlowServer() string { func (m *MakeK8sActivityService) addCommandToMonitorFilesStorage(command string, path string) string { port := m.getPortAkoFlowServer() - command += `ls -lR $ACTIVITY_MOUNT_PATH > /tmp/du_output.txt; echo "Preparing to start request"; body=$(cat /tmp/du_output.txt); body_length=$(printf %s "$body" | wc -c); echo "Start request"; { echo -ne "POST /akoflow-server/internal/storage/` + path + `/?activityId=$ACTIVITY_ID HTTP/1.1\r\n"; echo -ne "Host: $AKOFLOW_SERVER_SERVICE_SERVICE_HOST\r\n"; echo -ne "Content-Type: text/plain\r\n"; echo -ne "Content-Length: $body_length\r\n"; echo -ne "Connection: close\r\n"; echo -ne "\r\n"; echo -ne "$body"; } | nc $AKOFLOW_SERVER_SERVICE_SERVICE_HOST ` + port + `; echo "End request"; ` + scriptPath := "/app/pkg/server/scripts/monitor_files_storage.sh" + scriptBytes, err := ioutil.ReadFile(scriptPath) + if err != nil { + log.Printf("Erro ao ler script monitor_files_storage.sh: %v", err) + return command + } + script := string(scriptBytes) + + script = strings.ReplaceAll(script, "$1", path) + script = strings.ReplaceAll(script, "${2:-8080}", port) + script = strings.ReplaceAll(script, "$2", port) + + command += script + "; " return command } func (m *MakeK8sActivityService) addCommandToMonitorDiskSpecStorage(command string, path string) string { port := m.getPortAkoFlowServer() - command += `df -h > /tmp/du_output.txt; echo "Preparing to start request"; body=$(cat /tmp/du_output.txt); body_length=$(printf %s "$body" | wc -c); echo "Start request"; { echo -ne "POST /akoflow-server/internal/storage/` + path + `/?activityId=$ACTIVITY_ID HTTP/1.1\r\n"; echo -ne "Host: $AKOFLOW_SERVER_SERVICE_SERVICE_HOST\r\n"; echo -ne "Content-Type: text/plain\r\n"; echo -ne "Content-Length: $body_length\r\n"; echo -ne "Connection: close\r\n"; echo -ne "\r\n"; echo -ne "$body"; } | nc $AKOFLOW_SERVER_SERVICE_SERVICE_HOST ` + port + `; echo "End request"; ` + scriptPath := "/app/pkg/server/scripts/monitor_disk_spec_storage.sh" + + scriptBytes, err := ioutil.ReadFile(scriptPath) + if err != nil { + log.Printf("Erro ao ler script monitor_disk_spec_storage.sh: %v", err) + return command + } + script := string(scriptBytes) + + script = strings.ReplaceAll(script, "$1", path) + script = strings.ReplaceAll(script, "${2:-8080}", port) + script = strings.ReplaceAll(script, "$2", port) + command += script + "; " return command } diff --git a/pkg/server/scripts/monitor_disk_spec_storage.sh b/pkg/server/scripts/monitor_disk_spec_storage.sh new file mode 100644 index 0000000..ed66557 --- /dev/null +++ b/pkg/server/scripts/monitor_disk_spec_storage.sh @@ -0,0 +1,30 @@ +#!/bin/bash +PATH_PARAM="$1" +PORT="${2:-8080}" + +df -h $ACTIVITY_MOUNT_PATH > /tmp/disk_spec_output.txt +echo "Preparing to start request" +body=$(cat /tmp/disk_spec_output.txt) +body_length=$(printf %s "$body" | wc -c) +echo "Start request" + +if command -v nc >/dev/null 2>&1; then + { echo -ne "POST /akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID HTTP/1.1\r\n"; \ + echo -ne "Host: $AKOFLOW_SERVER_SERVICE_SERVICE_HOST\r\n"; \ + echo -ne "Content-Type: text/plain\r\n"; \ + echo -ne "Content-Length: $body_length\r\n"; \ + echo -ne "Connection: close\r\n"; \ + echo -ne "\r\n"; \ + echo -ne "$body"; } | nc $AKOFLOW_SERVER_SERVICE_SERVICE_HOST $PORT +elif command -v curl >/dev/null 2>&1; then + curl -X POST "http://$AKOFLOW_SERVER_SERVICE_SERVICE_HOST:$PORT/akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID" -H "Content-Type: text/plain" --data "$body" +elif command -v wget >/dev/null 2>&1; then + wget --post-data="$body" --header="Content-Type: text/plain" "http://$AKOFLOW_SERVER_SERVICE_SERVICE_HOST:$PORT/akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID" -O /dev/null +elif [ "$BASH_VERSION" ] && [ -e /dev/tcp ]; then + exec 3<>/dev/tcp/$AKOFLOW_SERVER_SERVICE_SERVICE_HOST/$PORT + echo -ne "POST /akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID HTTP/1.1\r\nHost: $AKOFLOW_SERVER_SERVICE_SERVICE_HOST\r\nContent-Type: text/plain\r\nContent-Length: $body_length\r\nConnection: close\r\n\r\n$body" >&3 +else + echo "No HTTP client available (nc, curl, wget or bash)" >&2 +fi + +echo -e "\nEnd request" \ No newline at end of file diff --git a/pkg/server/scripts/monitor_files_storage.sh b/pkg/server/scripts/monitor_files_storage.sh new file mode 100644 index 0000000..410545c --- /dev/null +++ b/pkg/server/scripts/monitor_files_storage.sh @@ -0,0 +1,24 @@ +#!/bin/bash +PATH_PARAM="$1" +PORT="${2:-8080}" + +ls -lR $ACTIVITY_MOUNT_PATH > /tmp/du_output.txt +echo "Preparing to start request" +body=$(cat /tmp/du_output.txt) +body_length=$(printf %s "$body" | wc -c) +echo "Start request" + +if command -v nc >/dev/null 2>&1; then + { echo -ne "POST /akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID HTTP/1.1\r\n"; echo -ne "Host: $AKOFLOW_SERVER_SERVICE_SERVICE_HOST\r\n"; echo -ne "Content-Type: text/plain\r\n"; echo -ne "Content-Length: $body_length\r\n"; echo -ne "Connection: close\r\n"; echo -ne "\r\n"; echo -ne "$body"; } | nc $AKOFLOW_SERVER_SERVICE_SERVICE_HOST $PORT +elif command -v curl >/dev/null 2>&1; then + curl -X POST "http://$AKOFLOW_SERVER_SERVICE_SERVICE_HOST:$PORT/akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID" -H "Content-Type: text/plain" --data "$body" +elif command -v wget >/dev/null 2>&1; then + wget --post-data="$body" --header="Content-Type: text/plain" "http://$AKOFLOW_SERVER_SERVICE_SERVICE_HOST:$PORT/akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID" -O /dev/null +elif [ "$BASH_VERSION" ] && [ -e /dev/tcp ]; then + exec 3<>/dev/tcp/$AKOFLOW_SERVER_SERVICE_SERVICE_HOST/$PORT + echo -ne "POST /akoflow-server/internal/storage/$PATH_PARAM/?activityId=$ACTIVITY_ID HTTP/1.1\r\nHost: $AKOFLOW_SERVER_SERVICE_SERVICE_HOST\r\nContent-Type: text/plain\r\nContent-Length: $body_length\r\nConnection: close\r\n\r\n$body" >&3 +else + echo "No HTTP client available (nc, curl, wget or bash)" >&2 +fi + +echo -e "\nEnd request" \ No newline at end of file From 992ea78c4b770ccd564b13040af045ba4cb6d7b7 Mon Sep 17 00:00:00 2001 From: Raphael Garcia Date: Thu, 7 Aug 2025 20:28:01 +0000 Subject: [PATCH 2/2] refactor replace in .sh and readfile --- .../make_k8s_activity_service.go | 29 ++++++------------- .../scripts/monitor_disk_spec_storage.sh | 4 +-- pkg/server/scripts/monitor_files_storage.sh | 4 +-- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go b/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go index bcdd443..b47a235 100755 --- a/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go +++ b/pkg/server/runtimes/kubernetes_runtime/kubernetes_runtime_service/make_k8s_activity_service.go @@ -2,8 +2,6 @@ package kubernetes_runtime_service import ( "encoding/base64" - "io/ioutil" - "log" "math/rand" "os" "strconv" @@ -12,6 +10,7 @@ import ( "github.com/ovvesley/akoflow/pkg/server/entities/k8s_job_entity" "github.com/ovvesley/akoflow/pkg/server/entities/workflow_activity_entity" "github.com/ovvesley/akoflow/pkg/server/entities/workflow_entity" + "github.com/ovvesley/akoflow/pkg/shared/utils/utils_read_file" ) type MakeK8sActivityService struct { @@ -89,16 +88,11 @@ func (m *MakeK8sActivityService) addCommandToMonitorFilesStorage(command string, port := m.getPortAkoFlowServer() scriptPath := "/app/pkg/server/scripts/monitor_files_storage.sh" - scriptBytes, err := ioutil.ReadFile(scriptPath) - if err != nil { - log.Printf("Erro ao ler script monitor_files_storage.sh: %v", err) - return command - } - script := string(scriptBytes) + utilsReadFile := utils_read_file.New() + script := utilsReadFile.ReadFile(scriptPath) - script = strings.ReplaceAll(script, "$1", path) - script = strings.ReplaceAll(script, "${2:-8080}", port) - script = strings.ReplaceAll(script, "$2", port) + script = strings.ReplaceAll(script, "#PATH_PARAM#", path) + script = strings.ReplaceAll(script, "#PORT#", port) command += script + "; " return command @@ -108,16 +102,11 @@ func (m *MakeK8sActivityService) addCommandToMonitorDiskSpecStorage(command stri port := m.getPortAkoFlowServer() scriptPath := "/app/pkg/server/scripts/monitor_disk_spec_storage.sh" - scriptBytes, err := ioutil.ReadFile(scriptPath) - if err != nil { - log.Printf("Erro ao ler script monitor_disk_spec_storage.sh: %v", err) - return command - } - script := string(scriptBytes) + utilsReadFile := utils_read_file.New() + script := utilsReadFile.ReadFile(scriptPath) - script = strings.ReplaceAll(script, "$1", path) - script = strings.ReplaceAll(script, "${2:-8080}", port) - script = strings.ReplaceAll(script, "$2", port) + script = strings.ReplaceAll(script, "#PATH_PARAM#", path) + script = strings.ReplaceAll(script, "#PORT#", port) command += script + "; " return command diff --git a/pkg/server/scripts/monitor_disk_spec_storage.sh b/pkg/server/scripts/monitor_disk_spec_storage.sh index ed66557..3ba195b 100644 --- a/pkg/server/scripts/monitor_disk_spec_storage.sh +++ b/pkg/server/scripts/monitor_disk_spec_storage.sh @@ -1,6 +1,6 @@ #!/bin/bash -PATH_PARAM="$1" -PORT="${2:-8080}" +PATH_PARAM="#PATH_PARAM#" +PORT="#PORT#" df -h $ACTIVITY_MOUNT_PATH > /tmp/disk_spec_output.txt echo "Preparing to start request" diff --git a/pkg/server/scripts/monitor_files_storage.sh b/pkg/server/scripts/monitor_files_storage.sh index 410545c..fc9a81d 100644 --- a/pkg/server/scripts/monitor_files_storage.sh +++ b/pkg/server/scripts/monitor_files_storage.sh @@ -1,6 +1,6 @@ #!/bin/bash -PATH_PARAM="$1" -PORT="${2:-8080}" +PATH_PARAM="#PATH_PARAM#" +PORT="#PORT#" ls -lR $ACTIVITY_MOUNT_PATH > /tmp/du_output.txt echo "Preparing to start request"