Skip to content

Instantly share code, notes, and snippets.

@leighb2282
Created September 10, 2023 11:03
Show Gist options
  • Select an option

  • Save leighb2282/b863aac714ba3d7892b9eda18c698c8f to your computer and use it in GitHub Desktop.

Select an option

Save leighb2282/b863aac714ba3d7892b9eda18c698c8f to your computer and use it in GitHub Desktop.
edge-console a script used in a pre-puppet FreeBSD environment for software deployment
#!/bin/bash
# edge-console.sh Version 0.10
# Template Script to automate pushing of specific packages
# Previous Update: Created options for updating cache (-f) and checking a regions edge version (-v)
# Latest Update: Createed option to push cached version to the CDN (-p)
# Next update: Create option to Update ssh keys on target nodes
# Leigh Burton
# Last updated: Mon 07 Jul 2014 16:00:56 PDT
# Grabbing hostname (used for cache)
hname=$(echo $HOSTNAME)
# Setting API location
apiloc=""
apiarg="/v1/r/list_nodes.php"
# Setting short for bc-s
sect_bcs="expression=%26intersect%28@bitcast.node.bitcast-s"
installer="/deployment/do_wowza_install"
opssrv=""
devsrv=""
### SETTING CACHE LOCATION ###
if [ "$hname" == $opssrv ]; then
cacheloc="/home/pkgpush/edgeconsole/cache"
elif [ "$hname" == "Chimera" ]; then
cacheloc="/home/lburton/Documents/scripts/edgeconsole/cache"
else
echo "Not sreops or Chimera, exiting"
exit 1
fi
echo $hname [$cacheloc]
echo ""
# Usage Function
usage()
{
echo "Available Switches:"
echo "'-f' Cache Fix, Check cache verson and if outdated pull new version."
echo "'-p us|eu|ap|in' Push cached version to the Specified regions edge nodes"
echo "'-v us|eu|ap|in' Query active nodes in a specificed region for their active version"
echo "'-h' Help/Manual (your looking at it)"
}
if [ "$#" == "0" ]; then
usage
exit 1
fi
#############################################
# Poll nodes for list of nodes to deploy to #
#############################################
nodepoll()
{
usedge=$(curl -ks -d "$sect_bcs,@nodes.region.us,@nodes.node_maintenance.0%29" $apiloc$apiarg | sed 's/node/"\n"/g; s/"//g; s/}//g; s/]//g' | awk -F'[:|,]' '{print $2}')
euedge=$(curl -ks -d "$sect_bcs,@nodes.region.emea,@nodes.node_maintenance.0%29" $apiloc$apiarg | sed 's/node/"\n"/g; s/"//g; s/}//g; s/]//g' | awk -F'[:|,]' '{print $2}')
apedge=$(curl -ks -d "$sect_bcs,@nodes.region.apac,@nodes.node_maintenance.0%29" $apiloc$apiarg | sed 's/node/"\n"/g; s/"//g; s/}//g; s/]//g' | awk -F'[:|,]' '{print $2}')
inedge=$(curl -ks -d "$sect_bcs,@nodes.region.in,@nodes.node_maintenance.0%29" $apiloc$apiarg | sed 's/node/"\n"/g; s/"//g; s/}//g; s/]//g' | awk -F'[:|,]' '{print $2}')
}
#############################################
# parsing the $OPTARGS for region selection #
#############################################
regionquery()
{
nodepoll
if [ "$OPTARG" == "us" ]; then
edge=$usedge
region="US Edges"
elif [ "$OPTARG" == "eu" ]; then
edge=$euedge
region="EU Edges"
elif [ "$OPTARG" == "ap" ]; then
edge=$apedge
region="AP Edges"
elif [ "$OPTARG" == "in" ]; then
edge=$inedge
region="IN Edges"
else
echo "Not a valid region"
exit 1
fi
}
###############################
# cachefix (-f) #
# Keep cache files up to date #
###############################
cachefix()
{
### $devsrv CHECKS ###
# Pull the name of the newest wowza edge version on devsrv
cdnv=$(ssh -q lburton@$devsrv "ls -lt /usr/local/deploy/NEXT_CDN_RELEASE | grep CDN | head -1" | awk '{print $9}')
# get the md5 hash of that file (on devsrv)
mdcdnv=$(ssh -q lburton@$devsrv "md5 /usr/local/deploy/NEXT_CDN_RELEASE/$cdnv" | awk '{print $4}')
#get the full path
p2d="/usr/local/deploy/NEXT_CDN_RELEASE/$cdnv"
### CACHE CHECKS ###
# Pull the name of the newest wowza edge version in cache
cdnc=$(ls -lt $cacheloc | grep CDN | head -1 | awk '{print $9}')
# get the md5 hash of that file (in cache)
mdcdnc=$(md5sum $cacheloc/$cdnc | awk '{print $1}')
#Print that to console
echo " Dev1: $cdnv $mdcdnv"
echo "Cache: $cdnc $mdcdnc"
### COMPARE CACHE to DEV1 ###
if [ "$mdcdnc" == "$mdcdnv" ]; then
echo "Hashes match! no activity Needed"
else
echo "$Hashes don't match! Delete from cache, Re-populate Cache and push to edge"
#Purge cache
rm -v $cacheloc/$cdnc
rm -v $cacheloc/do_wowza_install
#Repopulate cache
scp lburton@$devsrv:$p2d $cacheloc/.
cd $cacheloc
svn export $installer
fi
}
###########################################
# pusher (-p us|eu|ap|in) #
# Push cached version to specified region #
###########################################
pusher()
{
regionquery
echo $region
wec=$(ls -lt $cacheloc | grep CDN | head -1 | awk '{print $9}' | sed 's/.taz//g;')
cdnc=$(ls -lt $cacheloc | grep CDN | head -1 | awk '{print $9}')
for node in $edge
do
ver=$(curl -sm 6 $node:1935/status.xml | grep 'Release' | sed 's/<[^<]*>//' | sed 's/<[^<]*>//' | awk '{print $1}')
if [ "$ver" == "$wec" ]; then
echo "Versions match, No need to install"
else
echo -e "\n\nPushing files to $node"
ssh -o ConnectTimeout=5 lburton@$node "rm -r deployedge && mkdir deployedge"
scp -o ConnectTimeout=5 $cacheloc/$cdnc lburton@$node:deployedge/;
scp -o ConnectTimeout=5 $cacheloc/do_wowza_install lburton@$node:deployedge/;
fi
echo "CTRL+C time if needed (2 Seconds)"
sleep 2
done
exit 0
}
##############################################
# versioner (-v us|eu|ap|in) #
# Query edge nodes for their current version #
##############################################
versioner()
{
regionquery
echo $region
for node in $edge
do
ver=$(curl -sm 6 $node:1935/status.xml | grep 'Release' | sed 's/<[^<]*>//' | sed 's/<[^<]*>//' | awk '{print $1}')
echo $node $ver
done
exit 0
}
# See if there are any arguments passed
while getopts "f h p: v:" o; do
case "${o}" in
f)
cachefix
exit 0
;;
h)
usage
exit 0
;;
p)
pusher
exit 0
;;
v)
versioner
exit 0
;;
\?)
echo "Invalid option: -$o" >&2
usage
exit 1
esac
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment