Skip to content

Commit

Permalink
Merge pull request #43 from patricklee2/remote-debug
Browse files Browse the repository at this point in the history
Remote debug
  • Loading branch information
rramachand21 committed Jun 12, 2018
2 parents 4cef102 + fc6794f commit a7255bd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 19 deletions.
1 change: 1 addition & 0 deletions 10.1.0/Dockerfile
Expand Up @@ -23,6 +23,7 @@ RUN npm install -g pm2 \
&& apk add tcptraceroute \
&& apk add bash \
&& cd /opt/startup \
&& npm install \
&& npm install npm@latest -g \
&& chmod 755 /opt/startup/init_container.sh

Expand Down
40 changes: 21 additions & 19 deletions 10.1.0/startup/generateStartupCommand.js
Expand Up @@ -20,17 +20,26 @@ if (typeof process.env.WEBSITE_ROLE_INSTANCE_ID !== 'undefined'

var startupCommand = fs.readFileSync(CMDFILE, 'utf8').trim();

const CUSTOM_STARTUP_CMD_FLAG = "/opt/startup/CUSTOM_STARTUP_CMD_FLAG";
fs.writeFileSync(CUSTOM_STARTUP_CMD_FLAG, "FALSE");
if (startupCommand) {
fs.writeFileSync(CUSTOM_STARTUP_CMD_FLAG, "TRUE"); // set CUSTOM_STARTUP_CMD_FLAG for remote debugging
}

// No user-provided startup command, check for scripts.start
const PACKAGE_JSON_FLAG = "/opt/startup/PACKAGE_JSON_FLAG";
fs.writeFileSync(PACKAGE_JSON_FLAG, "FALSE");
if (!startupCommand) {
var packageJsonPath = "/home/site/wwwroot/package.json";
var json = fs.existsSync(packageJsonPath) && JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
if (typeof json == 'object' && typeof json.scripts == 'object' && typeof json.scripts.start == 'string') {
console.log("Found scripts.start in package.json")
startupCommand = 'npm start';
fs.writeFileSync(PACKAGE_JSON_FLAG, "TRUE"); // set PACKAGE_JSON_FLAG for remote debugging
}
}

var finalCommand = startupCommand;
var nodeFile = startupCommand;

// No scripts.start; can we autodetect an app?
if (!startupCommand) {
Expand All @@ -39,38 +48,31 @@ if (!startupCommand) {
var filename = "/home/site/wwwroot/" + autos[i];
if (fs.existsSync(filename)) {
console.log("No startup command entered, but found " + filename);
finalCommand = filename;
nodeFile = filename;
break;
}
}
}

// Still nothing, run the default static site
if (!startupCommand && !finalCommand) {
if (!startupCommand && !nodeFile) {
console.log("No startup command or autodetected startup script " +
"found. Running default static site.");
finalCommand = DEFAULTAPP;
nodeFile = DEFAULTAPP;
}

if (finalCommand && fs.existsSync(finalCommand)) {
if (process.env.APPSVC_REMOTE_DEBUGGING == "TRUE")
{
if (process.env.APPSVC_REMOTE_DEBUGGING_BREAK == "TRUE")
{
startupCommand = "node --inspect-brk=0.0.0.0:" + process.env.APPSVC_TUNNEL_PORT + " " + finalCommand;
}
else
{
startupCommand = "node --inspect=0.0.0.0:" + process.env.APPSVC_TUNNEL_PORT + " " + finalCommand;
if (!startupCommand && nodeFile && fs.existsSync(nodeFile)) {
if (process.env.APPSVC_REMOTE_DEBUGGING == "TRUE") {
if (process.env.APPSVC_REMOTE_DEBUGGING_BREAK == "TRUE") {
startupCommand = "node --inspect-brk=0.0.0.0:" + process.env.APPSVC_TUNNEL_PORT + " " + nodeFile;
} else {
startupCommand = "node --inspect=0.0.0.0:" + process.env.APPSVC_TUNNEL_PORT + " " + nodeFile;
}
}
else
{
} else {
// Run with pm2
startupCommand = "pm2 start " + finalCommand + " --no-daemon";
startupCommand = "pm2 start " + nodeFile + " --no-daemon";
}
}


// Write to file
fs.writeFileSync(CMDFILE, startupCommand);
13 changes: 13 additions & 0 deletions 10.1.0/startup/init_container.sh
Expand Up @@ -21,8 +21,21 @@ ln -s /home/LogFiles "$PM2HOME"/logs
# Get environment variables to show up in SSH session
eval $(printenv | awk -F= '{print "export " $1"="$2 }' >> /etc/profile)

# feature flag for remote debugging for with npm
# set flag and restart site to remove these changes
if [ "$APPSVC_REMOTE_DEBUGGING" = "TRUE" ] && [ ! "$APPSETTING_REMOTE_DEBUGGING_FEATURE_FLAG" = "FALSE" ]
then
mv /usr/local/bin/node /usr/local/bin/node-original
mv /opt/startup/node-wrapper.sh /usr/local/bin/node
chmod a+x /usr/local/bin/node
sed -i 's/env node/env node-original/' /usr/local/lib/node_modules/npm/bin/npm-cli.js
sed -i 's/env node/env node-original/' /usr/local/lib/node_modules/pm2/bin/pm2
sed -i 's/env node/env node-original/' /opt/startup/generateStartupCommand.js
fi

echo "$@" > /opt/startup/startupCommand
node /opt/startup/generateStartupCommand.js
chmod 755 /opt/startup/startupCommand
STARTUPCOMMAND=$(cat /opt/startup/startupCommand)
echo "Running $STARTUPCOMMAND"
eval "exec $STARTUPCOMMAND" &
Expand Down
39 changes: 39 additions & 0 deletions 10.1.0/startup/node-wrapper.sh
@@ -0,0 +1,39 @@
#!/bin/sh

# check if inspect option is used
# hope we didnt match too much, should improve on this
args_contain_inspect=false
for arg in "$@"
do
case "$arg" in
--inspect*)
args_contain_inspect=true
;;
--debug*)
args_contain_inspect=true
;;
esac
done

package_json="FALSE"
if [ -e "/opt/startup/PACKAGE_JSON_FLAG" ]
then
package_json=`cat /opt/startup/PACKAGE_JSON_FLAG`
fi

custom_startup="FALSE"
if [ -e "/opt/startup/CUSTOM_STARTUP_CMD_FLAG" ]
then
custom_startup=`cat /opt/startup/CUSTOM_STARTUP_CMD_FLAG`
fi

# enable remote debugging when node started with npm
if [ "$APPSVC_REMOTE_DEBUGGING" = "TRUE" ] && ([ "$package_json" = "TRUE" ] || [ "$custom_startup" = "TRUE" ]) && [ $args_contain_inspect = false ]
then
node-original --inspect=0.0.0.0:$APPSVC_TUNNEL_PORT "$@"
elif [ "$APPSVC_REMOTE_DEBUGGING_BREAK" = "TRUE" ] && ([ "$package_json" = "TRUE" ] || [ "$custom_startup" = "TRUE" ]) && [ $args_contain_inspect = false ]
then
node-original --inspect-brk=0.0.0.0:$APPSVC_TUNNEL_PORT "$@"
else
node-original "$@"
fi

0 comments on commit a7255bd

Please sign in to comment.