#!/bin/bash # This script is for use on the machine where the nightly Expeditee binaries are to be # uploaded. Put this script in the same folder as the uploads are meant to go. # When any binary's log *.out file is being uploaded via secure shell (which is fast), # this script's called. The invocation of this script should go into .ssh/authorizedkeys. # Then this script will delete all other log and binary files in this folder for the same # os-release that are not of the current date (in case it's the uploaded file itself). # The code didn't work if the binary was processed independent of the log file, since the # bin's filename (and hence target os) could not be determined as it was still being # uploaded. origdir=`pwd` # default binary directory to where this script lives, then go there # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in bindir=`dirname "$0"` #dirname "$(readlink -f "$0")" pushd $bindir # http://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script date=`date +%Y-%m-%d` # code from Puka # the file uploaded is the one with the latest timestamp # ls -rt lists files by time, then get the last one # the file pattern looks for rke-currentdate-.out files #incoming_file=$1 incoming_file=`ls -rt rke-*$date*.out 2>/dev/null | tail -n 1` #echo "Incoming file: $incoming_file" >> out.txt # http://stackoverflow.com/questions/21620406/how-do-i-pause-my-shell-script-for-1-second-before-continuing #sleep 2m # arrays # make sure -linux64 comes before linux, because we'll be matching against these # strings, so need the longest strings to be matched first. rke_suffix=("-linux64" "-linux" "-windows" "-mac-MountainLion") os_substr=("-linux64" "-linux" "-windows" "-MountainLion") #bin_suffix=("-linux64" "-linux" "-windows.exe" "-MacOS-intel-MountainLion.dmg") # http://stackoverflow.com/questions/2172352/in-bash-how-can-i-check-if-a-string-begins-with-some-value # http://stackoverflow.com/questions/229551/string-contains-in-bash rke_file_pattern= if [[ $incoming_file == "rke-"* ]] ; then for suffix in ${rke_suffix[@]}; do if [[ $incoming_file == *"$suffix"* ]]; then rke_file_pattern="rke-*${suffix}.out" break fi done # ONE WAY: #echo "found $rke_file_pattern" # -f to make it silent: will also be silent if there's nothing to rm #rm -f $rke_file_pattern # OR: # get rid of all files of the pattern that are not of the current date # http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array # setting for fileglobbing: # for using wildcards to get matching files on filesystem into array # "The nullglob option causes the array to be empty if there are no matched." # http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array # See shopt command at http://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html shopt -s nullglob files=($rke_file_pattern) for file in ${files[@]}; do # set nullglob earlier, so won't be here if there's no file that matched if [[ $file != "rke-$date${suffix}.out" ]]; then rm -rf $file fi done shopt -u nullglob # We now know the OS for the binary associated with the rke log # that's being uploaded too. Delete all bins for this OS except # any with the current date. bin_file_pattern= # binaries start with "Expeditee-1-" for os_str in ${os_substr[@]}; do if [[ $incoming_file == *"$os_str"* ]]; then bin_file_pattern="Expeditee-1-*$os_str*" break fi done # ONE WAY: #echo "found $bin_file_pattern" # -f to make it silent: will also be silent if there's nothing to rm #rm -rf $bin_file_pattern # OR: # get rid of all files of the pattern that are not of the current date # http://stackoverflow.com/questions/10981439/reading-filenames-into-an-array shopt -s nullglob files=($bin_file_pattern) for file in ${files[@]}; do # set nullglob earlier, so won't be here if there's no file that matched if [[ $file != *"$date"* ]] || [[ $file != *"$os_str"* ]]; then echo "$file" rm -rf $file fi done shopt -u nullglob fi # clean up ._Expeditee-1-YYYY-MM-DD-MacOS-intel-MountainLion.dmg temp files #ls -la "._Expeditee*dmg" rm -rf "._Expeditee*dmg" popd