awslabs / route53-dynamic-dns-with-lambda

A Dynamic DNS system built with API Gateway, Lambda & Route 53.
Apache License 2.0
491 stars 166 forks source link

Absolutes paths for curl & jq #28

Closed chivakaa closed 5 years ago

chivakaa commented 5 years ago

Hi Sean,

I am wondered with your solution! Just one thing, when i execute locally the script 'route53-ddns-client.sh' works fine, but when i shedule on root crontab, variable 'myPublicIP' don't assign propper values:

get public IP from reflector to generate hash &/or set IP

myPublicIP=$(curl -q --$ipVersion -s -H "x-api-key: $apiKey" "$myAPIURL?mode=get" | jq -r '.return_message //empty')

And the script fails: [ -z "$myIp" ] && fail "Couldn't find your public IP"

Because root cron does not load env for 'curl' or 'jq'. You need to add absolute paths for each command. Absolute paths can be different for each OS.

Also maybe packages 'curl' or 'jq' are not present and system fails. Is recomended add some control code like this:

Define curl binaries path

if which curl | grep '\/curl' >/dev/null 2>&1 then printf "=== Found curl command\n" curl_path=which curl | awk '{print $1}' else printf "==== Missing curl command\n" exit 1 fi 2>/dev/null

Define jq binaries path

if which jq | grep '\/jq' >/dev/null 2>&1 then printf "=== Found jq command\n" jq_path=which jq | awk '{print $1}' else printf "==== Missing jq command\n" exit 1 fi 2>/dev/null

And remplace all 'curl' calls for variable $curl_path. The same for 'jq' command.

seangreathouse commented 5 years ago

Hey chivakaa,
Thanks for your complement and interest in the project.
I'm looking at your suggestion and wondering if the best route is to implement your auto-discovery, or to simply set those paths to variables at the top of the script.
If I implement auto-discovery, I'm using binaries to do it (which and awk in your example)
Is there a reason a system's cron can't find curl, but can find awk or which?
This solution needs to work for everyone.
Thanks in advance for any more thoughts you have on this.

chivakaa commented 5 years ago

Hi Sean,

Running script from cron could fail if cron does not load PATH environments where commands / binaries are placed. For instance my root cron has these environments:

HOME=/root LOGNAME=root PATH=/usr/bin:/bin LANG=en_GB.UTF-8 SHELL=/bin/sh PWD=/root

and because of that some commands are not found and scripts without absolute path can fail. From my point of view at least two possible options to solve the issue:

  1. Place all absolute paths for commans inside of scripts invoqued from cron
  2. Place PATH definition header on cron (like: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin)

I hope these info can helps!