From c0d3824d8e4c977972c6908d9b72ad5691bb5399 Mon Sep 17 00:00:00 2001 From: Henry Barreto Date: Wed, 18 Sep 2024 10:51:28 -0300 Subject: [PATCH 1/2] feat(api,ssh,gateway): remove refresh and use air As refresh package, used to rebuild the service when the code changes, seem don't be maintained anymore, we have decided to change to air, a more modern alternative. Beyond that, we also added 'dlv' to the air, allowing use to attach a debug to the service through the container's IP on the port `:2345`. With the container's IP in your hands, you just need to connect to it using the 'dlv' connect command. It is worth to notice that, as `agent`'s container runs on host network, you don't need to specify the IP when debugging it. ```sh dlv connect :2345 ``` As the code built in the container lives in another GOPATH, you can need to remap this path to your local environment. To perform that, you can open or create the file `~/.config/dlv/config.yml`, and insert this snippet. ```yml substitute-path: [ { from: "/go", to: "" } ] ``` --- .gitignore | 26 ++++++++++++-------------- api/.air.toml | 34 ++++++++++++++++++++++++++++++++++ api/Dockerfile | 3 ++- api/entrypoint-dev.sh | 2 +- gateway/.air.toml | 32 ++++++++++++++++++++++++++++++++ gateway/Dockerfile | 3 ++- gateway/entrypoint-dev.sh | 2 +- gateway/refresh.yml | 14 -------------- ssh/.air.toml | 32 ++++++++++++++++++++++++++++++++ ssh/Dockerfile | 3 ++- ssh/entrypoint-dev.sh | 2 +- 11 files changed, 119 insertions(+), 34 deletions(-) create mode 100644 api/.air.toml create mode 100644 gateway/.air.toml delete mode 100644 gateway/refresh.yml create mode 100644 ssh/.air.toml diff --git a/.gitignore b/.gitignore index 8748b8cca6a..83c42847b22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,21 @@ +.env.override +docker-compose.override.yml + *~ +*/tmp +*/node_modules +*.orig + api_private_key api_public_key ssh_private_key -/go.sum -/ui/node_modules/ -*.orig -.env.override -docker-compose.override.yml -gateway/gateway -api/api -ssh/ssh -agent/agent -agent/shellhub.key -cli/cli + go.work go.work.sum -connector/.keys -connector/connector -*/tmp +go.sum + +cli/cli +agent/shellhub.key # Directory used by devscripts/run-agent to store binaries bin/agent diff --git a/api/.air.toml b/api/.air.toml new file mode 100644 index 00000000000..40132cd7eb4 --- /dev/null +++ b/api/.air.toml @@ -0,0 +1,34 @@ +root = "." +tmp_dir = "tmp" + +[build] +pre_cmd = [] +cmd = "go build -gcflags=\"all=-N -l\" -o ./tmp/main ." +post_cmd = [] +bin = "" +full_bin = "dlv exec ./tmp/main" +args_bin = [ + "--listen=0.0.0.0:2345", + "--headless", + "--continue", + "--accept-multiclient", + "--", + "server", +] +delay = 500 +exclude_dir = ["assets", "tmp", "vendor", "testdata"] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +rerun = false +rerun_delay = 500 +send_interrupt = false +stop_on_error = false diff --git a/api/Dockerfile b/api/Dockerfile index 6ad449dc104..bdb7b528f52 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -40,7 +40,8 @@ ARG GOPROXY ENV GOPROXY ${GOPROXY} RUN apk add --update openssl build-base docker-cli -RUN go install github.com/markbates/refresh@v1.11.1 && \ +RUN go install github.com/air-verse/air@v1.52.3 && \ + go install github.com/go-delve/delve/cmd/dlv@v1.23.0 && \ go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3 && \ go install github.com/vektra/mockery/v2/...@v2.20.0 diff --git a/api/entrypoint-dev.sh b/api/entrypoint-dev.sh index 0a254f3f418..c948845fd5a 100755 --- a/api/entrypoint-dev.sh +++ b/api/entrypoint-dev.sh @@ -12,4 +12,4 @@ fi ln -sf $PWD/api /api -refresh run +air diff --git a/gateway/.air.toml b/gateway/.air.toml new file mode 100644 index 00000000000..d862777bfe6 --- /dev/null +++ b/gateway/.air.toml @@ -0,0 +1,32 @@ +root = "." +tmp_dir = "tmp" + +[build] +pre_cmd = [] +cmd = "go build -gcflags=\"all=-N -l\" -o ./tmp/main ." +post_cmd = [] +bin = "" +full_bin = "dlv exec ./tmp/main" +args_bin = [ + "--listen=0.0.0.0:2345", + "--headless", + "--continue", + "--accept-multiclient", +] +delay = 500 +exclude_dir = ["assets", "tmp", "vendor", "testdata"] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +rerun = false +rerun_delay = 500 +send_interrupt = false +stop_on_error = false diff --git a/gateway/Dockerfile b/gateway/Dockerfile index 55cac5d3faa..3c26a176a5b 100644 --- a/gateway/Dockerfile +++ b/gateway/Dockerfile @@ -40,7 +40,8 @@ RUN mkdir -p /var/run/openresty /etc/letsencrypt && \ curl -sSL https://ssl-config.mozilla.org/ffdhe2048.txt -o /etc/shellhub-gateway/dhparam.pem RUN apk add --update openssl build-base -RUN go install github.com/markbates/refresh@v1.11.1 && \ +RUN go install github.com/air-verse/air@v1.52.3 && \ + go install github.com/go-delve/delve/cmd/dlv@v1.23.0 && \ go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3 && \ go install github.com/vektra/mockery/v2/...@v2.20.0 diff --git a/gateway/entrypoint-dev.sh b/gateway/entrypoint-dev.sh index 04efe7ab132..269beeec38f 100755 --- a/gateway/entrypoint-dev.sh +++ b/gateway/entrypoint-dev.sh @@ -2,4 +2,4 @@ ln -sf $PWD/gateway /gateway -refresh run +air diff --git a/gateway/refresh.yml b/gateway/refresh.yml deleted file mode 100644 index 0384cdecd75..00000000000 --- a/gateway/refresh.yml +++ /dev/null @@ -1,14 +0,0 @@ -app_root: /go/src/github.com/shellhub-io/shellhub -ignored_folders: -- vendor -included_extensions: -- .go -build_target_path: "" -build_path: /go/src/github.com/shellhub-io/shellhub/gateway -build_flags: [] -build_delay: 200ns -binary_name: gateway -command_flags: [] -command_env: [] -enable_colors: true -log_name: "" diff --git a/ssh/.air.toml b/ssh/.air.toml new file mode 100644 index 00000000000..d862777bfe6 --- /dev/null +++ b/ssh/.air.toml @@ -0,0 +1,32 @@ +root = "." +tmp_dir = "tmp" + +[build] +pre_cmd = [] +cmd = "go build -gcflags=\"all=-N -l\" -o ./tmp/main ." +post_cmd = [] +bin = "" +full_bin = "dlv exec ./tmp/main" +args_bin = [ + "--listen=0.0.0.0:2345", + "--headless", + "--continue", + "--accept-multiclient", +] +delay = 500 +exclude_dir = ["assets", "tmp", "vendor", "testdata"] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +rerun = false +rerun_delay = 500 +send_interrupt = false +stop_on_error = false diff --git a/ssh/Dockerfile b/ssh/Dockerfile index 2ae59ec48ac..dece6b3453c 100644 --- a/ssh/Dockerfile +++ b/ssh/Dockerfile @@ -40,7 +40,8 @@ ARG GOPROXY ENV GOPROXY ${GOPROXY} RUN apk add --update openssl -RUN go install github.com/markbates/refresh@v1.11.1 && \ +RUN go install github.com/air-verse/air@v1.52.3 && \ + go install github.com/go-delve/delve/cmd/dlv@v1.23.0 && \ go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53.3 && \ go install github.com/vektra/mockery/v2/...@v2.20.0 diff --git a/ssh/entrypoint-dev.sh b/ssh/entrypoint-dev.sh index 5d9569a0444..3a6ab189532 100755 --- a/ssh/entrypoint-dev.sh +++ b/ssh/entrypoint-dev.sh @@ -9,4 +9,4 @@ if [ ! -f /var/run/secrets/ssh_private_key ]; then openssl genrsa -out /var/run/secrets/ssh_private_key 2048 fi -refresh run +air From c8a1deade6d761f71170cc91009ed359dc44532f Mon Sep 17 00:00:00 2001 From: Henry Barreto Date: Wed, 18 Sep 2024 10:55:09 -0300 Subject: [PATCH 2/2] refactor(agent): change dlv listen to tcp socket instead --- agent/.air.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agent/.air.toml b/agent/.air.toml index dd0be824a6b..7fd4d2ed376 100644 --- a/agent/.air.toml +++ b/agent/.air.toml @@ -2,18 +2,18 @@ root = "." tmp_dir = "tmp" [build] -pre_cmd = ["rm ./tmp/dlv.sock"] +pre_cmd = [] cmd = "go build -tags docker -ldflags \"-X main.AgentVersion=latest\" -gcflags=\"all=-N -l\" -o ./tmp/main ." post_cmd = [] bin = "" full_bin = "dlv exec ./tmp/main" args_bin = [ - "--listen unix:./tmp/dlv.sock", + "--listen=0.0.0.0:2345", "--headless", "--continue", "--accept-multiclient", ] -delay = 1000 +delay = 500 exclude_dir = ["assets", "tmp", "vendor", "testdata"] exclude_file = [] exclude_regex = ["_test.go"]