Skip to content

Instantly share code, notes, and snippets.

@psaia
Created July 19, 2012 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaia/3146705 to your computer and use it in GitHub Desktop.
Save psaia/3146705 to your computer and use it in GitHub Desktop.
bash porn
#! /bin/sh
#
#
#
# NOTE: This script is intended to be executed by root
# Executing it as another user could have undesired effects
###########################################
# Globals
###########################################
# The build directory. Will be created if not already.
BUILD_DIR=./build
# Store the name of the map that needs to be created.
MAP_TYPE=$1
# The user
SU_USER=$2
# Nice colors make for nice logging.
BLDRED=${txtbld}$(tput setaf 1)
BLDBLU=${txtbld}$(tput setaf 4)
BLDYLW=${txtbld}$(tput setaf 2)
TXTRST=$(tput sgr0)
###########################################
# Functions
###########################################
# A fancy death method.
function die {
clean
echo ""
echo >&2 ${BLDRED}"$@"${TXTRST}"\n\n\n"
exit 1
}
# A fancy notification method.
function msg {
echo >&2 ${BLDBLU}"==> $@"${TXTRST}
}
# Merge all geo files into one nice google merc shapefile.
# @param 1 = merge directory
# @param 2 = name of output shape file
function merge_geo_shps {
INDEX=0
# Loop through each of the files.
for f in $(find $1 -iname "*.kmz" -or -iname "*.kml" -or -iname "*.shp")
do
# Output file to be processed.
echo " ==> $f"
if ((INDEX == 0)); then # First iteration.
# Create new shapefile out of the first, called merged.
su hfcdev -c "ogr2ogr -overwrite -nlt POLYGON -s_srs EPSG:4326 -t_srs EPSG:900913 -a_srs EPSG:900913 ./$BUILD_DIR/$2 $f"
else
# Append to merged.
su hfcdev -c "ogr2ogr -update -append -nlt POLYGON -s_srs EPSG:4326 -t_srs EPSG:900913 -a_srs EPSG:900913 ./$BUILD_DIR/$2 $f"
fi
let INDEX++
done
}
# Download XML and check for changes.
# If no changes, die.
function download_and_version {
CACHE_FILE=./$MAP_TYPE.cache
if [ ! -e $CACHE_FILE ]; then
msg "Downloading and caching "$CACHE_FILE" for the first time ("$1")"
su hfcdev -c "wget -qO - $1 > $CACHE_FILE"
else
# Save temp for comparison with existing
msg "Downloading fresh copy of "$1" to compare with last download"
su hfcdev -c "wget -qO - $1 > ./tmp.xml"
# do compare.
su hfcdev -c "cmp -s $CACHE_FILE ./tmp.xml"
if [ $? -eq 0 ]; then
rm ./tmp.xml
die "No Changes Since Last Execution"
else
rm ./tmp.xml
msg "There have been updates!"
fi
fi
}
# Create geojson out of a given shapefile.
function create_geojson {
msg "Creating GeoJSON"
su hfcdev -c "ogr2ogr -overwrite -f "GeoJSON" -simplify 0.90 -s_srs EPSG:900913 -t_srs EPSG:4326 $2 $1 -lco WRITE_BBOX=YES --debug on"
}
###########################################
# Process
###########################################
# Timestamp.
echo ${BLDYLW}"///"${TXTRST}
echo ${BLDYLW}"/// Generating Cook House Race Ratings at" `date`${TXTRST}
echo ${BLDYLW}"///"${TXTRST}
# Handle build directory.
if [ ! -d "$BUILD_DIR" ]; then
su hfcdev -c "mkdir $BUILD_DIR"
else
rm -rf $BUILD_DIR
su hfcdev -c "mkdir $BUILD_DIR"
fi
# Get the type of map to render.
if [ $MAP_TYPE = race-ratings ]; then
# Cook XML needed for parser.
XML=http://cookpolitical.com/sites/default/files/houseraces_flash.xml
# Get the xml we need to create the map.
download_and_version $XML
# Merge all files to one big one.
merge_geo_shps `pwd`/files-to-merge congressional-districts.shp
# Create geosjon with the shapefile we just formed.
create_geojson $BUILD_DIR/congressional-districts.shp
msg "Create Mapnik XML"
su hfcdev -c "php race-ratings.php $OUTPUT_FILENAME.geojson $BUILD_DIR $XML"
# Overwrite old stylesheet with new stylesheet.
msg "Updating cook-race-ratings.xml..."
su hfcdev -c "cp $BUILD_DIR/cook-race-ratings.xml $MAPNIK_STYLE_DIRECTORY/cook-race-ratings.xml"
# Need to delete from the repo first.
# Move to staging server.
msg "Copying districts-data.js to Animal..."
su hfcdev -c "scp -P 28343 $BUILD_DIR/districts-data.js hfcdev@animal.hfwebdev.com:/var/www/vhosts/cook.hfwebdev.com/map/app/js/districts-data.js"
# Need to delete from the repo first.
# Move to production server.
msg "Copying districts-data.js to Swedish Chef..."
su hfcdev -c "scp -P 50836 $BUILD_DIR/districts-data.js hfcdev@swedish-chef.hfwebdev.com:/var/www/vhosts/cookpolitical.com/map/app/js/districts-data.js"
# Does the server need to be restarted as well?
msg "Cleaing tile cache..."
su hfcdev -c "rm -rf /var/www/tiles/web/cache/stache/cook-race-ratings/*"
# Remove the huge geojson file, we don't need it.
msg "Removing geojson file..."
su hfcdev -c "rm $OUTPUT_FILENAME.geojson"
# Restart apache
msg "Restarting apache..."
su hfcsvn -c "service httpd restart"
# Seeding
msg "--------- Warming Grid Cache 4" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 4"
msg "--------- Warming PNG Cache 4" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 4"
msg "--------- Warming Grid Cache 5" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 5"
msg "--------- Warming PNG Cache 5" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 5"
msg "--------- Warming Grid Cache 6" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 6"
msg "--------- Warming PNG Cache 6" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 6"
msg "--------- Warming Grid Cache 7" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 7"
msg "--------- Warming PNG Cache 7" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 7"
msg "--------- Warming Grid Cache 8" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 8"
msg "--------- Warming PNG Cache 8" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 8"
msg "--------- Warming Grid Cache 9" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 9"
msg "--------- Warming PNG Cache 9" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 9"
msg "--------- Warming Grid Cache 10" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 10"
msg "--------- Warming PNG Cache 10" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e png 10"
msg "--------- Warming Grid Cache 11" ${1-`date`} "---------"
su hfcdev -c "tilestache-seed.py -c /var/www/tiles/hfcmaps.cfg -l grid-cook-race-ratings -b 5.2 -194.6 72.4 -49.2 -e json 11"
elif [ $MAP_TYPE = incumbent ]; then
else
die "Sorry, you must specify a valid map type name!"
fi
# Fin timestamp.
echo ${BLDYLW}"///"${TXTRST}
echo ${BLDYLW}"/// FIN at" `date`${TXTRST}
echo ${BLDYLW}"///"${TXTRST}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment