From 6b5662b18bc5cf807cc99fd0fa3b24f2fc8fe452 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Sat, 26 Nov 2016 16:27:32 +0100 Subject: [PATCH 1/5] chore: remove inactive browserstack wrapper package No longer uses the "inactive" browserstack wrapper package from npm, which was just responsible for creating the readyfile. We can create the readyfile ourself and get rid of the npm package. --- package.json | 1 - scripts/browserstack/start_tunnel.js | 59 -------------------- scripts/browserstack/start_tunnel.sh | 72 ++++++++++++++++++++++++- scripts/browserstack/teardown_tunnel.sh | 8 +-- 4 files changed, 72 insertions(+), 68 deletions(-) delete mode 100644 scripts/browserstack/start_tunnel.js diff --git a/package.json b/package.json index 2e73f3692064..2a42fe397698 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "@types/node": "^6.0.34", "@types/run-sequence": "0.0.27", "@types/rx": "^2.5.33", - "browserstacktunnel-wrapper": "^2.0.0", "conventional-changelog": "^1.1.0", "express": "^4.14.0", "firebase-tools": "^2.2.1", diff --git a/scripts/browserstack/start_tunnel.js b/scripts/browserstack/start_tunnel.js deleted file mode 100644 index 54c5087e8c79..000000000000 --- a/scripts/browserstack/start_tunnel.js +++ /dev/null @@ -1,59 +0,0 @@ -'use strict'; -/** - * Start the BrowserStack tunnel. Once its started it creates a file so the callee can wait - * for the tunnel to be started. - */ - -var fs = require('fs'); -var http = require('http'); -var BrowserStackTunnel = require('browserstacktunnel-wrapper'); - -var HOSTNAME = 'localhost'; -var PORTS = [9876, 9877]; -var ACCESS_KEY = process.env.BROWSER_STACK_ACCESS_KEY; -var READY_FILE = process.env.BROWSER_PROVIDER_READY_FILE; -var TUNNEL_IDENTIFIER = process.env.TRAVIS_JOB_NUMBER; - -// We need to start fake servers, otherwise the tunnel does not start. -var fakeServers = []; -var hosts = []; - -PORTS.forEach(function(port) { - fakeServers.push(http.createServer(function() {}).listen(port)); - hosts.push({ - name: HOSTNAME, - port: port, - sslFlag: 0 - }); -}); - -var tunnel = new BrowserStackTunnel({ - key: ACCESS_KEY, - localIdentifier: TUNNEL_IDENTIFIER, - hosts: hosts -}); - -console.log('Starting tunnel on ports', PORTS.join(', ')); - -// Emit a `newer_available` event to force an update of the Browserstack binaries (necessary due to Travis caching) -// This also starts a new tunnel after the latest binaries are available. -tunnel.emit('newer_available'); - -tunnel.once('started', function(error) { - if (error) { - console.error('Can not establish the tunnel', error); - } else { - console.log('Tunnel established.'); - fakeServers.forEach(function(server) { - server.close(); - }); - - if (READY_FILE) { - fs.writeFile(READY_FILE, process.pid); - } - } -}); - -tunnel.on('error', function(error) { - console.error(error); -}); diff --git a/scripts/browserstack/start_tunnel.sh b/scripts/browserstack/start_tunnel.sh index da351371daf7..d8b909bf5c28 100755 --- a/scripts/browserstack/start_tunnel.sh +++ b/scripts/browserstack/start_tunnel.sh @@ -1,3 +1,71 @@ -export BROWSER_STACK_ACCESS_KEY=`echo $BROWSER_STACK_ACCESS_KEY | rev` +#!/bin/bash -node ./scripts/browserstack/start_tunnel.js & +set -e -o pipefail + +# Workaround for Travis CI cookbook https://github.com/travis-ci/travis-ci/issues/4862, +# where $PATH will be extended with relative paths to the NPM binaries. +PATH=`echo $PATH | sed -e 's/:\.\/node_modules\/\.bin//'` + +TUNNEL_FILE="BrowserStackLocal-linux-x64.zip" +TUNNEL_URL="https://www.browserstack.com/browserstack-local/$TUNNEL_FILE" +TUNNEL_DIR="/tmp/browserstack-tunnel" +TUNNEL_LOG="$LOGS_DIR/browserstack-tunnel.log" + +BROWSER_STACK_ACCESS_KEY=`echo $BROWSER_STACK_ACCESS_KEY | rev` + +# Cleanup and create the folder structure for the tunnel connector. +rm -rf $TUNNEL_DIR $BROWSER_PROVIDER_READY_FILE +mkdir -p $TUNNEL_DIR +touch $TUNNEL_LOG + +cd $TUNNEL_DIR + +# Download the saucelabs connect binaries. +curl $TUNNEL_URL -o $TUNNEL_FILE 2> /dev/null 1> /dev/null + +# Extract the saucelabs connect binaries from the tarball. +mkdir -p browserstack-tunnel +unzip -q $TUNNEL_FILE -d browserstack-tunnel + +# Cleanup the download directory. +rm $TUNNEL_FILE + +ARGS="" + +# Set tunnel-id only on Travis, to make local testing easier. +if [ ! -z "$TRAVIS_JOB_NUMBER" ]; then + ARGS="$ARGS --local-identifier $TRAVIS_JOB_NUMBER" +fi + +echo "Starting Browserstack Local in the background, logging into:" +echo " $TUNNEL_LOG" +echo " ---" +echo " $ARGS" + +# Extension to the BrowserStackLocal binaries, because those can't create a readyfile. +function create_ready_file { + + # To be able to exit the tail properly we need to have a sub shell spawned, which is + # used to track the state of tail. + sleep 120 & + + TIMER_PID=$! + + # Disown the background process, because we don't want to show any messages when killing + # the timer. + disown + + # When the tail recognizes the `Ctrl-C` log message the BrowserStack Tunnel is up. + { + tail -n0 -f $TUNNEL_LOG --pid $TIMER_PID | { sed '/Ctrl/q' && kill -9 $TIMER_PID; }; + } &> /dev/null + + echo "BrowserStack Tunnel ready" + + touch $BROWSER_PROVIDER_READY_FILE +} + +browserstack-tunnel/BrowserStackLocal -k $BROWSER_STACK_ACCESS_KEY $ARGS &>> $TUNNEL_LOG & + +# Wait for the tunnel to be ready and create the readyfile with the Browserstack PID +create_ready_file & diff --git a/scripts/browserstack/teardown_tunnel.sh b/scripts/browserstack/teardown_tunnel.sh index ba09b621e803..f95970de9f63 100755 --- a/scripts/browserstack/teardown_tunnel.sh +++ b/scripts/browserstack/teardown_tunnel.sh @@ -5,13 +5,9 @@ set -e -o pipefail echo "Shutting down Browserstack tunnel" -PID=$(cat $BROWSER_PROVIDER_READY_FILE); +killall BrowserStackLocal -# Resolving the PID from the readyfile. -kill $PID - - -while [[ -n `ps -ef | grep $PID | grep -v "grep"` ]]; do +while [[ -n `ps -ef | grep "BrowserStackLocal" | grep -v "grep"` ]]; do printf "." sleep .5 done From 168afd6bc479e8c879e795c005072cee30248654 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Tue, 29 Nov 2016 16:26:06 +0100 Subject: [PATCH 2/5] Fix incorrect comments --- scripts/browserstack/start_tunnel.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/browserstack/start_tunnel.sh b/scripts/browserstack/start_tunnel.sh index d8b909bf5c28..fe21022352d7 100755 --- a/scripts/browserstack/start_tunnel.sh +++ b/scripts/browserstack/start_tunnel.sh @@ -20,10 +20,10 @@ touch $TUNNEL_LOG cd $TUNNEL_DIR -# Download the saucelabs connect binaries. +# Download the browserstack local binaries. curl $TUNNEL_URL -o $TUNNEL_FILE 2> /dev/null 1> /dev/null -# Extract the saucelabs connect binaries from the tarball. +# Extract the browserstack local binaries from the tarball. mkdir -p browserstack-tunnel unzip -q $TUNNEL_FILE -d browserstack-tunnel From a64876e897ab37e1da07b62647653e64cfba6c6f Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 30 Nov 2016 17:15:41 +0100 Subject: [PATCH 3/5] Don't wait for tunnel if timer exceeded --- .travis.yml | 1 + scripts/browserstack/start_tunnel.sh | 2 +- scripts/browserstack/waitfor_tunnel.sh | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index aba30cd4acff..7fda603158ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,6 +24,7 @@ env: - BROWSER_STACK_ACCESS_KEY=BWCd4SynLzdDcv8xtzsB - ARCH=linux-x64 - BROWSER_PROVIDER_READY_FILE=/tmp/angular-material2-build/readyfile + - BROWSER_PROVIDER_ERROR_FILE=/tmp/angular-material2-build/errorfile # GITHUB_TOKEN_ANGULAR - secure: "fq/U7VDMWO8O8SnAQkdbkoSe2X92PVqg4d044HmRYVmcf6YbO48+xeGJ8yOk0pCBwl3ISO4Q2ot0x546kxfiYBuHkZetlngZxZCtQiFT9kyId8ZKcYdXaIW9OVdw3Gh3tQyUwDucfkVhqcs52D6NZjyE2aWZ4/d1V4kWRO/LMgo=" matrix: diff --git a/scripts/browserstack/start_tunnel.sh b/scripts/browserstack/start_tunnel.sh index fe21022352d7..0131769c3a67 100755 --- a/scripts/browserstack/start_tunnel.sh +++ b/scripts/browserstack/start_tunnel.sh @@ -47,7 +47,7 @@ function create_ready_file { # To be able to exit the tail properly we need to have a sub shell spawned, which is # used to track the state of tail. - sleep 120 & + { sleep 120; touch $BROWSER_PROVIDER_ERROR_FILE; } & TIMER_PID=$! diff --git a/scripts/browserstack/waitfor_tunnel.sh b/scripts/browserstack/waitfor_tunnel.sh index cb2e7d15324c..0f30fbcbd04f 100755 --- a/scripts/browserstack/waitfor_tunnel.sh +++ b/scripts/browserstack/waitfor_tunnel.sh @@ -1,5 +1,6 @@ #!/bin/bash +TUNNEL_LOG="$LOGS_DIR/browserstack-tunnel.log" # Wait for Connect to be ready before exiting # Time out if we wait for more than 2 minutes, so the process won't run forever. @@ -12,6 +13,11 @@ while [ ! -f $BROWSER_PROVIDER_READY_FILE ]; do echo echo "Timed out after 2 minutes waiting for tunnel ready file" exit 5 + elif [ -f $BROWSER_PROVIDER_ERROR_FILE ]; then + echo + echo "An error occurred while starting the tunnel. See error:" + cat $TUNNEL_LOG + exit 5 fi printf "." From e5ece42ec3d6073483a40547929aa03c657019e2 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 30 Nov 2016 20:59:46 +0100 Subject: [PATCH 4/5] Fix race conditions --- scripts/browserstack/waitfor_tunnel.sh | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/browserstack/waitfor_tunnel.sh b/scripts/browserstack/waitfor_tunnel.sh index 0f30fbcbd04f..23f26952ecd0 100755 --- a/scripts/browserstack/waitfor_tunnel.sh +++ b/scripts/browserstack/waitfor_tunnel.sh @@ -6,6 +6,14 @@ TUNNEL_LOG="$LOGS_DIR/browserstack-tunnel.log" # Time out if we wait for more than 2 minutes, so the process won't run forever. let "counter=0" +# Exit the process if there are errors reported. Print the tunnel log to the console. +if [ -f $BROWSER_PROVIDER_ERROR_FILE ]; then + echo + echo "An error occurred while starting the tunnel. See error:" + cat $TUNNEL_LOG + exit 5 +fi + while [ ! -f $BROWSER_PROVIDER_READY_FILE ]; do let "counter++" @@ -13,11 +21,6 @@ while [ ! -f $BROWSER_PROVIDER_READY_FILE ]; do echo echo "Timed out after 2 minutes waiting for tunnel ready file" exit 5 - elif [ -f $BROWSER_PROVIDER_ERROR_FILE ]; then - echo - echo "An error occurred while starting the tunnel. See error:" - cat $TUNNEL_LOG - exit 5 fi printf "." From eb232abdab7fe5c2cb2a64808be5c4af60040a3a Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 30 Nov 2016 21:17:18 +0100 Subject: [PATCH 5/5] Add empty line before logging that the tunnel is ready. --- scripts/browserstack/start_tunnel.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/browserstack/start_tunnel.sh b/scripts/browserstack/start_tunnel.sh index 0131769c3a67..de270502a489 100755 --- a/scripts/browserstack/start_tunnel.sh +++ b/scripts/browserstack/start_tunnel.sh @@ -60,6 +60,7 @@ function create_ready_file { tail -n0 -f $TUNNEL_LOG --pid $TIMER_PID | { sed '/Ctrl/q' && kill -9 $TIMER_PID; }; } &> /dev/null + echo echo "BrowserStack Tunnel ready" touch $BROWSER_PROVIDER_READY_FILE