Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # usage
- if [[ $# -lt 3 ]]; then
- echo "Usage: $0 <environment> <src-dir>i <dst-dir>"
- echo " <environment> - prod/stage"
- echo " <src-dir> - source dir where creatives are stored, e.g. ~/tmp/creatives/new"
- echo " <dst-dir> - destination directory where creatives should be written, e.g. 55"
- echo " Warning! Directory prefix is already set to /opt/ang/var/easybidder!"
- exit 1
- fi
- if [[ "$4" == "-d" ]];then
- readonly REPLACE=1
- fi
- readonly SXPSharedKey=~/.ssh/smartclip_shared_rsa
- readonly SSHOPTS="-i $SXPSharedKey -o StrictHostKeyChecking=no"
- readonly ENVIRONMENT=$1
- readonly SRCDIR=$2
- readonly DESTDIR=/opt/ang/var/easybidder/$3
- readonly PROGNAME=$(basename "$0")
- readonly LOCKFILE_DIR=/tmp
- readonly LOCK_FD=200
- # HOSTS setup
- HOSTS=$1
- lock() {
- local prefix=$1
- local fd=${2:-$LOCK_FD}
- local lock_file=$LOCKFILE_DIR/$prefix.lock
- # create lock file
- eval "exec $fd>$lock_file"
- # acquier the lock
- flock -n $fd \
- && return 0 \
- || return 1
- }
- eexit() {
- local error_str="$@"
- echo $error_str
- exit 1
- }
- main() {
- lock $PROGNAME \
- || eexit "Only one instance of $PROGNAME can run at one time."
- # echo $ENVIRONMENT
- # echo $SRCDIR
- # echo $HOSTS
- # echo $DESTDIR
- DIRNAME="creatives_$(date '+%F.%s')"
- # upload creatives to remote hosts
- for hh in $(cat $HOSTS); do
- echo -n "[$hh] Creating /tmp/$DIRNAME directory - "
- ssh $SSHOPTS ubuntu@${hh} "mkdir /tmp/$DIRNAME" > /dev/null && echo OK || echo FAIL
- echo -n "[$hh] Copying creatives - "
- scp $SSHOPTS $SRCDIR/*.creative ubuntu@${hh}:/tmp/$DIRNAME/ > /dev/null && echo OK || echo FAIL
- # delete old creatives if $4 == -d
- if [[ $REPLACE == 1 ]]; then
- echo -n "[$hh] Deleting $DESTDIR -"
- ssh $SSHOPTS ubuntu@${hh} "sudo -u anguser rm -rf $DESTDIR" && echo OK || echo FAIL
- fi
- # move creatives to the proper place
- ssh $SSHOPTS ubuntu@${hh} "sudo -u anguser mkdir -p $DESTDIR"
- echo -n "[$hh] Copying creatives to destination dir $DESTDIR - "
- ssh $SSHOPTS ubuntu@${hh} "sudo -u anguser cp /tmp/$DIRNAME/*.creative ${DESTDIR}/" && echo OK || echo FAIL
- done
- }
- main
Add Comment
Please, Sign In to add comment