Skip to content

Commit

Permalink
Fix service script (#169)
Browse files Browse the repository at this point in the history
* Improve service init script and logs

- Fixed init service script to allow proper 'start' and 'stop' actions consistently.
  I kept struggling that service start only worked after killing the distro because
  sometimes it didn't start/stop properly leaving processes running and without pid
  file
- Added service 'status' action
- Added additional service debug by setting environment variable DEBUG, ie:
  wsl.exe -d wsl-vpnkit DEBUG=1 service wsl-vpnkit stop
- Changed order in startup welcome screen having stop followed by start,
  this makes it easier for users to just copy/paste from welcome screen to have
  service running
- Added log check command to see only the latest service execution logs

* Added service restart

- Added to README how to do start with status firt and how to enable DEBUG
- Run tests twice with and without DEBUG enabled

* Split build and test scripts

* Fix service script without alpine start-stop-daemon

* Fix service script without alpine start-stop-daemon

* Service script with error checks

* Fixed wsl-vpnkit.service
- stop and restart actions are performed consistently
Improved build.sh
- able to build inside vpn if using http_proxy

* Minor tweaks

* Split import  into dedicated script
Allow build with podman
Improve automated test timings

* Avoid status deleting pid file by mistake

* Add comment about wsl-gvproxy compilation

Co-authored-by: Jose Sa <[email protected]>
  • Loading branch information
josesa-xx and josesa-xx authored Dec 6, 2022
1 parent a074e1c commit 939233d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wsl-vpnkit.tar.gz
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ This will build and import the distro.
git clone https://github.com/sakai135/wsl-vpnkit.git
cd wsl-vpnkit/
./build.sh
./import.sh
./test.sh
```

Optionally you may build with `podman` instead of `docker` (default) by overriding environment variable `DOCKER`:
```sh
DOCKER=podman ./build.sh
```

## Using `wsl-vpnkit` as a standalone script

The `wsl-vpnkit` script can be used as a normal script in your existing distro. This is an example setup script for Ubuntu.
Expand Down
27 changes: 13 additions & 14 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#! /bin/sh -xe
#!/bin/bash -xe

# run from repo root
# ./build.sh

USERPROFILE="$(powershell.exe -c 'Write-Host -NoNewline $env:USERPROFILE')"
DUMP=wsl-vpnkit.tar.gz
TAG_NAME=wslvpnkit
: "${DOCKER:=docker}" # docker/podman command (default: docker)
DUMP=wsl-vpnkit.tar.gz # exported rootfs file
TAG_NAME=wslvpnkit # build tag

# build
docker build -t $TAG_NAME -f ./distro/Dockerfile .
CONTAINER_ID=$(docker create $TAG_NAME)
docker export $CONTAINER_ID | gzip > $DUMP
docker container rm $CONTAINER_ID
ls -la $DUMP

# reinstall
wsl.exe --unregister wsl-vpnkit || :
wsl.exe --import wsl-vpnkit --version 2 "$USERPROFILE\\wsl-vpnkit" $DUMP
rm $DUMP
build_args=()
[ -z "${http_proxy}" ] || build_args+=( --build-arg http_proxy="${http_proxy}" )
[ -z "${https_proxy}" ] || build_args+=( --build-arg https_proxy="${https_proxy}" )
[ -z "${no_proxy}" ] || build_args+=( --build-arg no_proxy="${no_proxy}" )
${DOCKER} build --network host "${build_args[@]}" --tag ${TAG_NAME} --file ./distro/Dockerfile .
CONTAINER_ID=$(${DOCKER} create ${TAG_NAME})
${DOCKER} export "${CONTAINER_ID}" | gzip > ${DUMP}
${DOCKER} container rm "${CONTAINER_ID}"
ls -la ${DUMP}
7 changes: 4 additions & 3 deletions distro/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM golang:1.18.8-alpine3.16 as build
FROM docker.io/library/golang:1.18.8-alpine3.16 as build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY cmd ./cmd
COPY pkg ./pkg
# wsl-gvproxy.exe is compiled as a windows GUI to support backgrounding
RUN GOOS=windows go build -ldflags '-H=windowsgui' -o bin/wsl-gvproxy.exe ./cmd/gvproxy && \
GOOS=linux CGO_ENABLED=0 go build -ldflags '-s -w' -o bin/wsl-vm ./cmd/vm && \
find ./bin -type f -exec sha256sum {} \;

FROM golang:1.18.8-alpine3.16 as licenses
FROM docker.io/library/golang:1.18.8-alpine3.16 as licenses
RUN apk add --no-cache git && \
apk list --installed && \
go install github.com/google/[email protected]
Expand All @@ -19,7 +20,7 @@ COPY pkg ./pkg
RUN go-licenses save ./cmd/gvproxy --save_path ./licenses/gvproxy && \
go-licenses save ./cmd/vm --save_path ./licenses/vm

FROM alpine:3.16.3
FROM docker.io/library/alpine:3.16.3
RUN apk add --no-cache openrc iptables && \
apk list --installed
ARG REF=https://example.com/
Expand Down
51 changes: 30 additions & 21 deletions distro/scripts/wsl-vpnkit.service
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
#! /bin/sh

EXECUTABLE="wsl-vpnkit" # starting point
SUB_PROCESS="wsl-vm" # forked sub processes
PID_PATH="/var/run/$EXECUTABLE.pid"
LOG_PATH="/var/log/$EXECUTABLE.log"

ret=0

_debug_check() {
if [ -n "$DEBUG" ]; then
set -x
test -f $PID_PATH && cat $PID_PATH
ps
fi
}

start() {
_debug_check
start-stop-daemon \
--pidfile $PID_PATH \
--make-pidfile \
--background \
--stdout $LOG_PATH \
--stderr $LOG_PATH \
--exec $EXECUTABLE \
--wait 1 --progress \
${DEBUG+--verbose} \
--start
ret=$?
status >/dev/null # sets ret var
}

stop() {
_debug_check
start-stop-daemon \
--pidfile $PID_PATH \
--retry 2 \
${DEBUG+--verbose} \
--stop
ret=$?
if status >/dev/null; then
pid=$(cat $PID_PATH)
kill -INT "$pid"
sleep 3
fi
if status >/dev/null; then
kill -TERM "$pid"
fi
rm -f $PID_PATH
# kill sub processes if still running
pgrep $SUB_PROCESS >/dev/null && pkill -9 $SUB_PROCESS
for subp in wget udhcpc wsl-gvproxy wsl-vm; do
while pgrep $subp >/dev/null; do
pkill -TERM $subp
sleep 1
done
done
if status >/dev/null; then
ret=1
else
ret=0
fi
}

status() {
_debug_check
test -f $PID_PATH && pgrep -P $(cat $PID_PATH) &>/dev/null
ret=$?
if [ $ret = 0 ]; then
_debug_check >&2
if test -f $PID_PATH && pgrep -P "$(cat $PID_PATH)" >/dev/null; then
echo Service $EXECUTABLE is running
ret=0
else
echo Service $EXECUTABLE is not running
ret=1
fi
}

restart() {
stop
start
ret=$?
return $ret
}

case "$1" in
Expand All @@ -66,7 +74,8 @@ case "$1" in
stop
;;
restart)
restart
stop
start
;;
status)
status
Expand All @@ -76,4 +85,4 @@ case "$1" in
exit 1
esac

exit $ret
exit $ret
14 changes: 14 additions & 0 deletions import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash -xe

# run from repo root
# ./import.sh

USERPROFILE=$(wslvar USERPROFILE)
DUMP=wsl-vpnkit.tar.gz

# build if necessary
[ -f ${DUMP} ] || ./build.sh

# reinstall
wsl.exe --unregister wsl-vpnkit || :
wsl.exe --import wsl-vpnkit --version 2 "${USERPROFILE}\\wsl-vpnkit" ${DUMP}
10 changes: 8 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#! /bin/sh -xe
#!/bin/sh -xe

# ensuring distro is stopped before running tests
wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit stop
if wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit status; then
wsl.exe -d wsl-vpnkit --cd /app service wsl-vpnkit stop || \
wsl.exe -t wsl-vpnkit
fi

Expand All @@ -15,8 +15,13 @@ for debug_value in '1' '2' ''; do
fi
echo "####### Test Round with debug_str [${debug_str}] #######" | grep --colour=always .

# inital status should be stopped
output=$(wsl.exe -d wsl-vpnkit --cd /app ${debug_str} service wsl-vpnkit status)||echo "ignoring exit code"
echo "$output" | grep --colour=always "Service wsl-vpnkit is not running"

# start service
wsl.exe -d wsl-vpnkit --cd /app ${debug_str} service wsl-vpnkit start
sleep 2
output=$(wsl.exe -d wsl-vpnkit --cd /app ${debug_str} service wsl-vpnkit status)
echo "$output" | grep --colour=always "Service wsl-vpnkit is running"

Expand All @@ -34,6 +39,7 @@ for debug_value in '1' '2' ''; do

# restart service
wsl.exe -d wsl-vpnkit --cd /app ${debug_str} service wsl-vpnkit restart
sleep 2
output=$(wsl.exe -d wsl-vpnkit --cd /app ${debug_str} service wsl-vpnkit status)
echo "$output" | grep --colour=always "Service wsl-vpnkit is running"

Expand Down

0 comments on commit 939233d

Please sign in to comment.