Skip to content

Commit

Permalink
chore(util) replicate runtimes workflow to sdks
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed Nov 14, 2023
1 parent 21732b1 commit 8ec3e83
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 92 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ V8 ?= 11.4.183.23
PCRE ?= 8.45
ZLIB ?= 1.2.13
LUAROCKS ?= 3.9.2
PROXY_WASM_GO_SDK ?= v0.21.0

# util/runtime.sh - no makefile target
NGX_BUILD_WASMER_RUSTFLAGS ?= -g -C opt-level=0 -C debuginfo=1
Expand Down
8 changes: 8 additions & 0 deletions util/_lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ DIR_NGX_ECHO_MODULE=$DIR_DOWNLOAD/echo-nginx-module
DIR_NGX_HEADERS_MORE_MODULE=$DIR_DOWNLOAD/headers-more-nginx-module
DIR_MOCKEAGAIN=$DIR_DOWNLOAD/mockeagain
DIR_PROXY_WASM_GO_SDK=$DIR_DOWNLOAD/proxy-wasm-go-sdk
DIR_PATCHED_PROXY_WASM_GO_SDK=$DIR_DOWNLOAD/proxy-wasm-go-sdk-patched
DIR_BUILDROOT=$DIR_WORK/buildroot
DIR_SRC_ROOT=$DIR_WORK/nginx-src
DIR_PATCHED_ROOT=$DIR_WORK/nginx-patched
Expand Down Expand Up @@ -485,6 +486,13 @@ get_default_runtime_dir() {
echo "$DIR_DIST_RUNTIMES/$runtime-$version"
}

get_default_proxy_wasm_sdk_version() {
local lang="$1"
local var_name=$(echo "PROXY_WASM_${lang^^}_SDK")

get_variable_from_makefile "$var_name"
}

invalid_usage() {
exec 1>&2

Expand Down
91 changes: 0 additions & 91 deletions util/build_proxy_wasm_go_sdk.sh

This file was deleted.

102 changes: 102 additions & 0 deletions util/sdk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash
set -e

SCRIPT_NAME=$(basename $0)
NGX_WASM_DIR=${NGX_WASM_DIR:-"$(
cd $(dirname $(dirname ${0}))
pwd -P
)"}
if [[ ! -f "${NGX_WASM_DIR}/util/_lib.sh" ]]; then
echo "Unable to source util/_lib.sh" >&2
exit 1
fi

source $NGX_WASM_DIR/util/_lib.sh

###############################################################################

show_usage() {
cat << EOF
Usage:
$SCRIPT_NAME -S <sdk> [options]
EOF
}

show_help() {
cat << EOF
Download or build a proxy-wasm SDK.
EOF
show_usage
cat << EOF
Arguments:
-S, --sdk <sdk> proxy-wasm SDK to build (e.g. 'go').
Options:
--download Download a proxy-wasm SDK source code.
This is the default.
--build Build a proxy-wasm SDK and example filters.
-V, --sdk-ver <ver> proxy-wasm SDK version to build (e.g. 'v0.2.1')
-f, --force, --clean Force a clean download or build.
-h, --help Print this message and exit.
EOF
}

###############################################################################

MODE="download"

while [[ "$1" ]]; do
case "$1" in
-S|--sdk)
shift
PROXY_WASM_SDK="$1"
;;
-V|--sdk-ver)
shift
PROXY_WASM_SDK_VERSION="$1"
;;
--download)
MODE="download"
;;
--build)
MODE="build"
;;
--clean|--force|-f)
CLEAN="clean"
;;
-h|--help)
show_help
exit 0
;;
esac
shift
done

if [[ -z "$PROXY_WASM_SDK" ]]; then
show_usage
fatal "Bad usage. Consult --help"
fi

if [[ -z "$PROXY_WASM_SDK_VERSION" ]]; then
PROXY_WASM_SDK_VERSION="$(get_default_proxy_wasm_sdk_version "$PROXY_WASM_SDK")"
fi

BUILD_SCRIPT=$NGX_WASM_DIR/util/sdks/$PROXY_WASM_SDK.sh

if "$BUILD_SCRIPT" "$MODE" "$PROXY_WASM_SDK_VERSION" "$CLEAN"; then
exit 0

else
notice "$MODE failed"
exit 1
fi
Empty file added util/sdks/as.sh
Empty file.
120 changes: 120 additions & 0 deletions util/sdks/go.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env bash
set -e

SCRIPT_NAME=$(basename $0)
NGX_WASM_DIR=${NGX_WASM_DIR:-"$(
cd $(dirname $(dirname $(dirname ${0})))
pwd -P
)"}
if [[ ! -f "${NGX_WASM_DIR}/util/_lib.sh" ]]; then
echo "Unable to source util/_lib.sh" >&2
exit 1
fi

source $NGX_WASM_DIR/util/_lib.sh

###############################################################################

download_go_sdk() {
local version="$1"
local clean="$2"

if [[ "$clean" = "clean" ]]; then
rm -rfv "$DIR_PROXY_WASM_GO_SDK"
fi

if [[ -d "$DIR_PROXY_WASM_GO_SDK" ]]; then
notice "updating the proxy-wasm-go-sdk repository..."
pushd $DIR_PROXY_WASM_GO_SDK
git fetch
git reset --hard $version
popd
else
notice "cloning proxy-wasm-go-sdk repository, version ${version}..."
git clone \
-c advice.detachedHead=false --depth 1 \
--branch $version \
https://github.com/tetratelabs/proxy-wasm-go-sdk.git \
$DIR_PROXY_WASM_GO_SDK
fi
}

build_go_sdk() {
local version="$1"
local clean="$2"

if [[ ! -x "$(command -v tinygo)" ]]; then
fatal "missing 'tinygo', is TinyGo installed?"
fi

local hasher=sha1sum
if [[ ! -x "$(command -v $hasher)" ]]; then
hasher=shasum
fi

local hash_src=$(find $DIR_PROXY_WASM_GO_SDK -type f \( -name '*.go' -or -name '*.mod' \) -exec $hasher {} \; \
| $hasher | awk '{ print $1 }')

if [[ -d "$DIR_PATCHED_PROXY_WASM_GO_SDK" \
&& -f "$DIR_PATCHED_PROXY_WASM_GO_SDK/.hash" \
&& $(cat "$DIR_PATCHED_PROXY_WASM_GO_SDK/.hash") == $(echo $hash_src) ]];
then
exit
fi

rm -rf $DIR_PATCHED_PROXY_WASM_GO_SDK
cp -R $DIR_PROXY_WASM_GO_SDK $DIR_PATCHED_PROXY_WASM_GO_SDK
echo $hash_src > "$DIR_PATCHED_PROXY_WASM_GO_SDK/.hash"

pushd $DIR_PATCHED_PROXY_WASM_GO_SDK
set +e
patch --forward --ignore-whitespace examples/http_auth_random/main.go <<'EOF'
@@ -21,7 +21,7 @@ import (
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
-const clusterName = "httpbin"
+const clusterName = "httpbin:2000"
func main() {
proxywasm.SetVMContext(&vmContext{})
EOF
patch --forward --ignore-whitespace examples/dispatch_call_on_tick/main.go <<'EOF'
@@ -88,7 +88,7 @@ func (ctx *pluginContext) OnTick() {
} else {
headers = append(headers, [2]string{":path", "/fail"})
}
- if _, err := proxywasm.DispatchHttpCall("web_service", headers, nil, nil, 5000, ctx.callBack); err != nil {
+ if _, err := proxywasm.DispatchHttpCall("web_service:81", headers, nil, nil, 5000, ctx.callBack); err != nil {
proxywasm.LogCriticalf("dispatch httpcall failed: %v", err)
}
}
EOF
set -e

notice "compiling Go examples..."

make build.examples || exit 0

cd examples

find . -name "*.wasm" | while read f; do
# flatten module names, for example:
# "./shared_queue/sender/main.wasm" to "go_shared_queue_sender.wasm"
name=$(echo "$f" | sed 's,./\(.*\)/main.wasm,go_\1.wasm,;s,/,_,g')
cp -av "$f" "$DIR_TESTS_LIB_WASM/$name"
done
popd
}

###############################################################################

mode="$1"
version="$2"
clean="$3"

if [ "$mode" = "download" ]; then
download_go_sdk "$version" "$clean"
else
build_go_sdk "$version" "$clean"
fi
5 changes: 5 additions & 0 deletions util/setup_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ if [[ -n "$NGX_WASM_RUNTIME" ]] && ! [[ -n "$NGX_WASM_RUNTIME_LIB" ]]; then
$NGX_WASM_DIR/util/runtime.sh -R "$NGX_WASM_RUNTIME"
fi

if [[ ! -d "$DIR_PROXY_WASM_GO_SDK" ]]; then
notice "downloading proxy-wasm go SDK..."
$NGX_WASM_DIR/util/sdk.sh -S go --clean
fi

notice "done"

# vim: ft=sh ts=4 sts=4 sw=4:
2 changes: 1 addition & 1 deletion util/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ if [[ "$CI" == 'true' ]]; then
export NGX_BUILD_FORCE=1
fi

$NGX_WASM_DIR/util/build_proxy_wasm_go_sdk.sh
$NGX_WASM_DIR/util/sdk.sh -S go --build

args=()

Expand Down

0 comments on commit 8ec3e83

Please sign in to comment.