echo $"Starting Talend Runtime Registration ...."
# Read current Runtime host by using AWS instance Metadata API call, returned host address will be returned in system variable “host_address”.
host_address=$(curl http://169.254.169.254/latest/meta-data/public-hostname)
# Build MetaServlet JSON script – addServer.
metaservlet='{"actionName": "addServer","adminConsolePort": 8040,"authPass": "admin","authUser": "admin@company.com","commandPort": 8000,"description": "RemoteRT","filePort": 8001,"host": "'$host_address'","instance": "trun","label": "RemoteRT_'$host_address'","mgmtRegPort": 1099,"mgmtServerPort": 44444,"monitoringPort": 8888,"runtimePassword": "tadmin","runtimeUsername": "tadmin","shutdownBehavior": "Stop","timeoutUnknownState": "120","useSSL": false}'
# Run base64url.sh to encode “addServer” script within Base64.
metaservlet=`echo "$metaservlet" | /app/base64url.sh`
# Execute REST Call to TAC MetaServlet with encoded string and extract server ID from JSON response.
serverId=$(curl -s 'http://{TACHost}:8080/org.talend.administrator/metaServlet?' -X POST -H 'Content-Type: application/json' -d "$metaservlet" | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["id"]';)
# Export the server ID as Linux environment variables so it can be used by later process.
echo 'serverId='$serverId > /etc/environment