Skip to content

Instantly share code, notes, and snippets.

@ProjectOrangeBox
Last active September 15, 2022 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProjectOrangeBox/71ece34a28b17030754f71e84b57f633 to your computer and use it in GitHub Desktop.
Save ProjectOrangeBox/71ece34a28b17030754f71e84b57f633 to your computer and use it in GitHub Desktop.
bin scripts including shelly and config
#!/bin/bash
OWNER="drpepper"
GROUP="administrators"
PUBLICDIRECTORY=""
# used by crontab
MAXLOGAGE="7 days"
MAXLOCKAGE="4 hours"
# Applications root path
ROOT="$SCRIPTDIRECTORY"
APPROOT="/foobar"
println "ROOT: $ROOT"
println "APPROOT: $APPROOT"
#!/bin/bash
source "$(cd `dirname $0` && pwd)/shelly.sh"
# load config file
source $CONFIGFILE
crontabInit
runScript sendEmail blocking
#!/bin/bash
source "$(cd `dirname $0` && pwd)/shelly.sh"
# load config file
source $CONFIGFILE
runAsRoot
# check if owner is valid user
ownerExists $OWNER
println "Owner $OWNER"
# check if group is valid group
groupExists $GROUP
println "Group $GROUP"
# start
safeMakeDirectory /var
safeMakeDirectory /var/downloads
safeMakeDirectory /var/emails
safeMakeDirectory /var/fork_output
safeMakeDirectory /var/gulp
safeMakeDirectory /var/isql
safeMakeDirectory /var/logs
safeMakeDirectory /var/sessions
safeMakeDirectory /var/tmp
safeMakeDirectory /var/uploads
safeMakeDirectory /var/views
safeMakeDirectory /var/xdebug
safeMakeDirectory /support
safeMakeDirectory /support/keys
safeMakeDirectory /support/migrations
println "Changing Directory Mode to 775."
find $ROOT -type d -exec chmod 775 {} \;
println "Changing Files Mode to 664."
find $ROOT -type f -exec chmod 664 {} \;
println "Changing Owner to $OWNER."
find $ROOT -type d -exec chown $OWNER {} \;
find $ROOT -type f -exec chown $OWNER {} \;
println "Changing Group to $GROUP."
find $ROOT -type d -exec chgrp $GROUP {} \;
find $ROOT -type f -exec chgrp $GROUP {} \;
# Read / Write directory
println "Adjust $ROOT/var directory to make Read/Writable."
find "$ROOT/var/" -type d -exec chmod 777 {} \;
find "$ROOT/var/" -type f -exec chmod 666 {} \;
# bin shell stuff
println "Adjust bin Scripts to make executable."
find "$ROOT/bin/" -type f -iname "*.sh" -exec chmod 777 {} \;
#!/bin/bash
source "$(cd `dirname $0` && pwd)/shelly.sh"
# load config file
source $CONFIGFILE
checkIfSet ROOT
checkIfSet APPROOT
runAsRoot
getPull /exportairtable
SERVERDIR="/exportairtable/server"
safeMakeDirectory "$SERVERDIR/logs";
safeMakeDirectory "$SERVERDIR/logs";
safeMakeDirectory "$SERVERDIR/var";
safeMakeDirectory "$SERVERDIR/var/var";
safeMakeDirectory "$SERVERDIR/var/var/csv";
safeMakeDirectory "$SERVERDIR/var/var/diffs";
safeMakeDirectory "$SERVERDIR/var/var/json";
safeMakeDirectory "$SERVERDIR/var/var/jsonzips";
safeMakeDirectory "$SERVERDIR/var/var/maps";
safeMakeDirectory "$SERVERDIR/var/var/merged";
safeMakeDirectory "$SERVERDIR/var/var/swapped";
safeMakeDirectory "$SERVERDIR/var/var/zips";
makeDirectoryR "$APPROOT"
makeDirectoryRW "$SERVERDIR/var"
makeDirectoryRW "$SERVERDIR/logs"
#!/bin/bash
function crontabInit() {
# find index.php based on root and public directory
setupPHP
# what is the EXE we are calling with the provided options
export EXE="$PHP $INDEXFILE"
# these should be set in the config.ini file
checkIfSet MAXLOGAGE
checkIfSet MAXLOCKAGE
println "---- $SCRIPTFILE ----"
cleanDirectory "$LOGDIRECTORY" "$MAXLOGAGE"
}
function setupPHP() {
println "Setting up PHP and index.php entry point."
#auto locate php
export PHP=$(which php)
# find index.php based on root and public directory
findIndex
# what is the EXE we are calling with the provided options
export EXE="$PHP $INDEXFILE"
println "PHP $PHP"
println "Index File $INDEXFILE"
println "Execute $EXE"
}
function runScript() {
local ENDPOINT=$1
local BLOCKING=$2
# make file safe name
local FILE="$( echo $1 | sed s:/:_:g )"
local DATETIME="$(date '+%Y-%m-%d')"
local LOCKFILE="$TEMPDIRECTORY/$FILE.lock.txt"
local SCRIPTLOGFILE="$LOGDIRECTORY/$DATETIME-$FILE.log"
local EXEENDPOINT="$EXE $ENDPOINT"
createLockFile "$LOCKFILE" "$ENDPOINT" "$MAXLOCKAGE"
if [ "$?" == "1" ]; then
# locked so exit runScript()
return 1
fi
if [ "$BLOCKING" == "blocking" ]; then
# in log file
println "$ENDPOINT Started Blocking" true
# Execute & clean up not in background
($EXEENDPOINT >> $SCRIPTLOGFILE ; removeLockFile "$LOCKFILE" "$ENDPOINT")
else
println "$ENDPOINT Started in Background" true
# Execute & clean up in background
($EXEENDPOINT >> $SCRIPTLOGFILE ; removeLockFile "$LOCKFILE" "$ENDPOINT") &
fi
return 0
}
function durationToSeconds() {
set -f
normalize () { echo $1 | tr '[:upper:]' '[:lower:]' | tr -d "\"\\\'" | sed 's/ *y\(ear\)\{0,1\} */y /g; s/ *d\(ay\)\{0,1\} */d /g; s/ *h\(our\)\{0,1\} */h /g; s/ *m\(in\(ute\)\{0,1\}\)\{0,1\} */m /g; s/ *s\(ec\(ond\)\{0,1\}\)\{0,1\} */s /g; s/\([ydhms]\)s/\1/g'; }
local value=$(normalize "$1")
local fallback=$(normalize "$2")
echo $value | grep -v '^[-+*/0-9ydhms ]\{0,30\}$' > /dev/null 2>&1
if [ $? -eq 0 ]; then
>&2 echo Invalid duration pattern \"$value\"
else
if [ "$value" = "" ]; then
[ "$fallback" != "" ] && durationToSeconds "$fallback"
else
sedtmpl () { echo "s/\([0-9]\+\)$1/(0\1 * $2)/g;"; }
local template="$(sedtmpl '\( \|$\)' 1) $(sedtmpl y '365 * 86400') $(sedtmpl d 86400) $(sedtmpl h 3600) $(sedtmpl m 60) $(sedtmpl s 1) s/) *(/) + (/g;"
echo $value | sed "$template" | bc
fi
fi
set +f
}
function println() {
local MSG="$(/bin/date) $1"
if test -n "$TERM"; then
echo $MSG
fi
if test -n "$SHELLYLOGFILE"; then
echo $MSG >> $SHELLYLOGFILE
fi
}
# cleanDirectory "/foo/bar" 60
function cleanDirectory() {
local DIRECTORY=$ROOT$1
local SECONDS=$(durationToSeconds "$2")
local FILES=$DIRECTORY/*
println "Cleaning the directory $DIRECTORY of files older than $2"
for F in $FILES
do
if [ `stat --format=%Y $F` -le $(( `date +%s` - $SECONDS )) ]; then
println "$F removed"
rm -f $F
fi;
done
}
# cleanFile "/foo/bar.txt" 60
function cleanFile() {
local FILE=$ROOT$1
local SECONDS=$(durationToSeconds "$2")
if test -f "$FILE"; then
if [ `stat --format=%Y $FILE` -le $(( `date +%s` - $SECONDS )) ]; then
println "Removing the file $FILE which is older than $2."
rm -f $FILE
fi
fi
}
# checkIfSet ROOT "foobar"
function checkIfSet() {
local testVar="$1"
local default="$2"
if [ -z "${!testVar}" ]; then
if [ -z "$default" ]; then
println "** Variable $1 not set and is required **"
exit 1
else
export ${testVar}=\""$default"\"
fi;
fi;
}
function createLockFile() {
local LOCKFILE="$1"
local ENDPOINT="$2"
local MAXLOCKAGE="$3"
# delete the lock file if it's more than X old
cleanFile "$LOCKFILE" "$MAXLOCKAGE"
# Lock file if already exists and return 1 (fail)
if [ -f "$LOCKFILE" ]; then
# file & screen
println "$ENDPOINT Locked on $( cat "$LOCKFILE" )" true
return 1 # failure
fi
# Create the lock file
echo $(date) >> "$LOCKFILE"
# file & screen
println "$ENDPOINT Lock file created" true
return 0 # success
}
function removeLockFile() {
local LOCKFILE="$1"
local ENDPOINT="$2"
if [ -f "$LOCKFILE" ]; then
local SECONDS=$(($(date +%s) - $(date +%s -r $LOCKFILE)))
rm $LOCKFILE;
println "$ENDPOINT Ended. Elapsed time $(displayHumanTime $SECONDS)" true
println "$ENDPOINT Lock File Removed" true
else
println "$ENDPOINT Ended." true
println "$ENDPOINT Lock File Missing" true
fi
}
function displayHumanTime() {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d days ' $D
(( $H > 0 )) && printf '%d hours ' $H
(( $M > 0 )) && printf '%d minutes ' $M
(( $D > 0 || $H > 0 || $M > 0 )) && printf 'and '
printf '%d seconds\n' $S
}
function directoryRequired() {
if [ -d "$1" ]; then
:
else
println "The directory $1 does not exist."
exit;
fi
}
function fileRequired() {
if [ -f "$1" ]; then
:
else
println "The file $1 does not exist."
exit
fi
}
# runAsRoot
function runAsRoot() {
if [ "$EUID" -ne 0 ]; then
println "Please run using sudo or as root"
exit
fi
}
# safeMakeDirectory "/foo/bar"
function safeMakeDirectory() {
if [ ! -d "$ROOT$1" ]; then
println "Making directory $1"
mkdir $ROOT$1
else
println "Directory $1 already exists."
fi
}
# groupExists "team"
function groupExists() {
# check if group is valid group
if grep -q "$1:" /etc/group; then
:
else
println "The Group $1 does not exist"
exit
fi
}
# ownerExists "jake"
function ownerExists() {
# check if owner is valid user
if grep -q "$1:" /etc/passwd; then
:
else
println "The Owner $1 does not exist."
exit
fi
}
function findIndex() {
local PATH="$ROOT$PUBLICDIRECTORY/index.php"
if [ -f "$PATH" ]; then
:
else
println "Could not locate index.php at $PATH."
exit
fi
export INDEXFILE="$PATH"
}
# checkForConfigFile $CONFIGFILE
function checkForConfigFile() {
if [ -f "$1" ]; then
:
else
println "The Config File $1 does not exist."
exit
fi
}
function rmFiles() {
local PATH="$ROOT$1"
#WIP
echo $PATH
}
# makeDirectoryRW "/foo/bar"
function makeDirectoryRW() {
println "Directory $ROOT$1"
println "Changing Directory Mode to 777."
find $ROOT$1 -type d -exec chmod 777 {} \;
println "Changing Files Mode to 666."
find $ROOT$1 -type f -exec chmod 666 {} \;
}
# makeDirectoryR "/foo/bar"
function makeDirectoryR() {
println "Directory $ROOT$1"
println "Changing Directory Mode to 775."
find $ROOT$1 -type d -exec chmod 775 {} \;
println "Changing Files Mode to 664."
find $ROOT$1 -type f -exec chmod 664 {} \;
}
# changeOwner "/foo/bar" "$OWNER"
function changeOwner()
{
println "Changing Files in $ROOT$1 Owner to $2"
find $ROOT$1 -type d -exec chown $2 {} \;
find $ROOT$1 -type f -exec chown $2 {} \;
}
# changeGroup "/foo/bar" "$GROUP"
function changeGroup()
{
println "Changing Files in $ROOT$1 Group to $2"
find $ROOT$1 -type d -exec chgrp $2 {} \;
find $ROOT$1 -type f -exec chgrp $2 {} \;
}
# getPull "/foo"
function getPull()
{
println "git pull in $ROOT$1"
git -C "$ROOT$1" pull
}
SCRIPTFILE=$(readlink -f $0)
SCRIPTDIRECTORY=`dirname $SCRIPTFILE`
# Config file path
CONFIGFILE="$SCRIPTDIRECTORY/config.ini"
# Is the config file there?
checkForConfigFile $CONFIGFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment