HiveBrain v1.2.0
Get Started
← Back to all entries
debugMinor

Force script to fail if "npm install" has network issues

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
scriptinstallforceissuesnpmfailhasnetwork

Problem

With the current Github outage, I noticed a problem with my packer setup. In a nutshell, I have a packer setup where I call a shell script that looks like this:

#!/bin/bash
set -e
...
npm install
...


I expected that if there is any error during npm install, the script should fail and packer should abort. What I found out is that the script continued, even though the output looks like this:

```
amazon-ebs: + npm install
amazon-ebs: npm ERR! fetch failed http://github.com/component/emitter/archive/1.0.1.tar.gz
amazon-ebs: npm WARN retry will retry, error on last attempt: Error: connect ETIMEDOUT 192.30.253.112:80
amazon-ebs: npm ERR! fetch failed https://github.com/LearnBoost/node-XMLHttpRequest/archive/0f36d0b5ebc03d85f860d42a64ae9791e1daa433.tar.gz
amazon-ebs: npm WARN retry will retry, error on last attempt: Error: fetch failed with status code 502
amazon-ebs: npm ERR! fetch failed https://github.com/component/global/archive/v2.0.1.tar.gz
amazon-ebs: npm WARN retry will retry, error on last attempt: Error: connect ETIMEDOUT 192.30.253.113:443
amazon-ebs: npm WARN deprecated node-uuid@1.4.8: Use uuid module instead
amazon-ebs:
amazon-ebs: > ws@0.4.31 install /opt/socks-proxy/node_modules/tomahawk/node_modules/socket.io/node_modules/engine.io/node_modules/ws
amazon-ebs: > (node-gyp rebuild 2> builderror.log) || (exit 0)
amazon-ebs:
amazon-ebs: make: Entering directory '/opt/socks-proxy/node_modules/tomahawk/node_modules/socket.io/node_modules/engine.io/node_modules/ws/build'
amazon-ebs: CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
amazon-ebs: bufferutil.target.mk:96: recipe for target 'Release/obj.target/bufferutil/src/bufferutil.o' failed
amazon-ebs: make: Leaving directory '/opt/socks-proxy/node_modules/tomahawk/node_modules/socket.io/node_modules/engine.io/node_modules/ws/build'
amazon-ebs:
amazon-ebs: > ws@0.4.31 install /opt/socks-proxy/node_modules/tomahawk/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-cl

Solution

Try piping your output to grep and then failing based off of the return code of grep:

npm install 2>&1 | grep "Error: connect ETIMEDOUT"


Per the grep documentation,


the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred.

If needed, you can "not" the return code or just invert the conditional so that:

if [ "$?" -ne "0" ]; then
    echo "Packer failed!"
    exit 1
fi


becomes instead:

if [ "$?" -ne "1" ]; then
    echo "Packer failed!"
    exit 1
fi

Code Snippets

npm install 2>&1 | grep "Error: connect ETIMEDOUT"
if [ "$?" -ne "0" ]; then
    echo "Packer failed!"
    exit 1
fi
if [ "$?" -ne "1" ]; then
    echo "Packer failed!"
    exit 1
fi

Context

StackExchange DevOps Q#1267, answer score: 4

Revisions (0)

No revisions yet.