Skip to content

Commit

Permalink
move the start script here
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrence-forooghian committed Jul 25, 2024
1 parent cd23782 commit 31e5014
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
93 changes: 93 additions & 0 deletions bin/interception-proxy-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env bash

# Runs the server as a background service, exiting once the server is ready to receive requests.
#
# Intended for use in SDKs’ CI jobs. Must be run from the root of this repository.

set -e

# We run as the current user so that we can access the generated server certificate (in ~/.mitmproxy) without having to worry about permissions. TODO consider instead generating our own cert and telling mitmproxy to use it, then we also won’t have to have that step where we run mitmproxy once just to generate the certs
start_systemd_service () {
systemd_service=$(cat <<SYSTEMD_SERVICE
[Unit]
Description=Ably SDK Test Proxy
[Service]
WorkingDirectory=$(pwd)
ExecStart=npx interception-proxy
User=$USER
[Install]
WantedBy=multi-user.target
SYSTEMD_SERVICE
)

# https://stackoverflow.com/questions/84882/sudo-echo-something-etc-privilegedfile-doesnt-work
echo "${systemd_service}" | sudo tee /etc/systemd/system/ably-sdk-test-proxy.service 1>/dev/null

echo "Starting ably-sdk-test-proxy systemd service..." 1>&2
sudo systemctl start ably-sdk-test-proxy.service
echo "Started ably-sdk-test-proxy systemd service." 1>&2
}

start_launchd_daemon () {
launchd_daemon=$(cat <<LAUNCHD_DAEMON
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ably.test.proxy</string>
<key>WorkingDirectory</key>
<string>$(pwd)</string>
<key>ProgramArguments</key>
<array>
<string>npx</string>
<string>interception-proxy</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
LAUNCHD_DAEMON
)

# https://stackoverflow.com/questions/84882/sudo-echo-something-etc-privilegedfile-doesnt-work
echo "${launchd_daemon}" | sudo tee /Library/LaunchDaemons/com.ably.test.proxy.plist

echo "Loading ably-sdk-test-proxy launchd daemon..." 1>&2
sudo launchctl load /Library/LaunchDaemons/com.ably.test.proxy.plist
echo "Loaded ably-sdk-test-proxy launchd daemon." 1>&2
}

check_daemon_still_running () {
if uname | grep Linux 1>/dev/null
then
systemctl is-active --quiet ably-sdk-test-proxy.service
elif uname | grep Darwin 1>/dev/null
then
launchctl print system/com.ably.test.proxy 1>/dev/null
fi
}

if uname | grep Linux 1>/dev/null
then
start_systemd_service
elif uname | grep Darwin 1>/dev/null
then
start_launchd_daemon
else
echo "Unsupported system $(uname); exiting" 1>&2
exit 1
fi

echo "Waiting for sdk-test-proxy server to start on port 8001..." 1>&2

# https://stackoverflow.com/questions/27599839/how-to-wait-for-an-open-port-with-netcat
while ! nc -z localhost 8001; do
# Check that the service hasn’t failed (else we’ll be waiting forever)
check_daemon_still_running
sleep 0.5
done

echo "sdk-test-proxy server is now listening on port 8001." 1>&2
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "interception-proxy",
"version": "0.1.0",
"main": "index.js",
"bin": "bin/interception-proxy",
"bin": {
"interception-proxy": "bin/interception-proxy",
"interception-proxy-ci": "bin/interception-proxy-ci"
},
"files": [
"dist"
],
Expand Down

0 comments on commit 31e5014

Please sign in to comment.