Skip to content

Instantly share code, notes, and snippets.

@vkhatri
Created July 31, 2016 09:50
Show Gist options
  • Save vkhatri/a40f25df823d0984ebaa52c124f53c4d to your computer and use it in GitHub Desktop.
Save vkhatri/a40f25df823d0984ebaa52c124f53c4d to your computer and use it in GitHub Desktop.
Shell Script to Create Environment Variables for Marathon Application Dynamic IP and Port
#!/bin/bash
# Info: Shell Wrapper to populate Marathon Application
# Containers IP & Port ENV Variables.
#
# Variables:
# MARATHON_GROUP: marathon group name
# e.g. Marathon APP - /group0/app
# MARATHON_GROUP=group0
# e.g. Marathon APP - /group0/group1/app
# MARATHON_GROUP=group1-group0
# e.g. Marathon APP - /app
# MARATHON_GROUP=
#
# MARATHON_APPS: marathon apps name
# e.g. Marathon Apps - /group/app0, /group/app1, /group/app2
# MARATHON_APPS="app0 app1 app2"
# e.g. Marathon Apps - /app0, /app1, /app2
# MARATHON_APPS="app0 app1 app2"
#
# e.g.
# Marathon Apps - /web/nginx /web/apache /web/tomcat (Group Name: web, App Name: "nginx apache tomcat")
# MARATHON_GROUP=web
# MARATHON_APPS="nginx apache tomcat"
#
# Marathon App - /web/nginx (Group Name: web, App Name: nginx)
# MARATHON_GROUP=web
# MARATHON_APPS="nginx"
#
# Marathon App - /nginx (Group Name: , App Name: nginx)
# MARATHON_GROUP=
# MARATHON_APPS="nginx"
GROUP_VAR=$(echo $MARATHON_GROUP | tr -s '-' '_')
for APP in $MARATHON_APPS
do
# for export variable name
APP_VAR=$(echo $APP | tr -s '-' '_')
COUNTER=0
if [ -z "$MARATHON_GROUP" ]; then
result=$(dig _${APP}._tcp.marathon.mesos SRV +short | grep mesos | awk '{print $3":"$4}')
else
result=$(dig _${APP}-${MARATHON_GROUP}._tcp.marathon.mesos SRV +short | grep mesos | awk '{print $3":"$4}')
fi
# for row in $(dig _${APP}-${MARATHON_GROUP}._tcp.marathon.mesos SRV +short | awk '{print $3":"$4}')
for row in $result
do
port=$(echo $row | cut -f1 -d:)
agent=$(echo $row | cut -f2 -d:)
ip=$(dig $agent +short| xargs)
if [ -z "$MARATHON_GROUP" ]; then
export ${APP_VAR}_ip_${COUNTER}=$ip
export ${APP_VAR}_port_${COUNTER}=$port
else
export ${APP_VAR}_${GROUP_VAR}_ip_${COUNTER}=$ip
export ${APP_VAR}_${GROUP_VAR}_port_${COUNTER}=$port
fi
let COUNTER++
done
done
# print apps environment variables
# if [ -z "$MARATHON_GROUP" ]; then
# env | egrep -i `echo $MARATHON_APPS| sed -s -e 's/ /|/g' -e 's/-/_/g'` | sort
# else
# env | grep $GROUP_VAR | sort
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment