Skip to content

Instantly share code, notes, and snippets.

@rex
Created September 9, 2015 21:55
Show Gist options
  • Save rex/ac79952dd46db4181e5f to your computer and use it in GitHub Desktop.
Save rex/ac79952dd46db4181e5f to your computer and use it in GitHub Desktop.
PLATFORM=$(uname)
ISLINUX=0
WORKING_DIR=$(pwd)
CONFIG_DIR=$(cd ../config && pwd)
if [ $PLATFORM = 'Linux' ]; then
ISLINUX=1
OS_CODENAME=$(lsb_release -cs)
else
OS_CODENAME="trusty"
fi
if [ "$EXECUTE_COMMANDS" != "1" ]; then
EXECUTE_COMMANDS=0
else
if [ $PLATFORM != 'Linux' ]; then
echo "HEY. YOU. TURN OFF 'EXECUTE_COMMANDS' ON YOUR LOCAL MACHINE. IDIOT."
exit 1
fi
fi
function add-path {
local newpath="$1"
local path="$PATH"
prefixed-command "PREPENDING PATH" $newpath
set-env PATH "$newpath:$PATH"
}
function apt-install {
local pkgs="${@}"
prefixed-command "INSTALLING APT PACKAGES" $pkgs
run-command apt-get install -y --force-yes -qq $pkgs
}
function apt-repo {
local repo="$1"
prefixed-command "INSTALLING APT REPOSITORY" $repo
run-command add-apt-repository -y "$repo"
apt-update
}
function apt-source {
local dest="$1"
local os="$2"
local pkg="$3"
prefixed-command "INSTALLING APT SOURCE" "Package: $pkg from $dest [os: $os]"
run-command echo "deb $dest $os $pkg" >> /etc/apt/sources.list
run-command echo "deb-src $dest $os $pkg" >> /etc/apt/sources.list
apt-update
}
function apt-update {
prefixed-command "UPDATING APT PACKAGES"
run-command apt-get -qq update
}
function clone {
local repo="$1"
local localpath="$2"
prefixed-command "CLONING GIT REPOSITORY" "$repo -> $localpath"
run-command git clone $repo $localpath
}
function command {
local cmd="${@}"
prefixed-command "RUNNING COMMAND" $cmd
run-command $cmd
}
function debug-env {
prefixed-command "CURRENT LOADED ENV"
run-command env
}
function debug-path {
prefixed-command "CURRENT LOADED PATH" $PATH
}
function delete-if-exists {
local filepath="$1"
prefixed-command "DELETING OBJECT IF EXISTS" $filepath
run-command "[ -f $filepath -o -d $filepath -o -h $filepath ] && rm -rf $filepath"
}
function mk-dir {
local dirs="${@}"
for dir in $dirs; do
prefixed-command "CREATING DIRECTORY" $dir
run-command mkdir -p $dir
done
}
function nginx-enable {
local name="$1"
prefixed-command "ENABLING NGINX SITE" $name
symlink /etc/nginx/sites-available/${name}.conf /etc/nginx/sites-enabled/${name}.conf
}
function nginx-disable {
local name="$1"
prefixed-command "DISABLING NGINX SITE" $name
run-command "[ -e /etc/nginx/sites-enabled/$name.conf ] && rm /etc/nginx/sites-enabled/$name.conf"
}
function nginx-install {
local config="$1"
local name="$2"
prefixed-command "INSTALLING NGINX SITE" $config
place $config "/etc/nginx/sites-available/${name}.conf"
}
function own {
local owner="$1"
local items="${@:2}"
for item in $items; do
prefixed-command "SETTING OWNERSHIP OF ITEM" "$item -> $owner"
run-command chown $owner $item
done
}
function own-deep {
local owner="$1"
local items="${@:2}"
for item in $items; do
prefixed-command "SETTING RECURSIVE OWNERSHIP OF ITEM" "$item -> $owner"
run-command chown -R $owner $item
done
}
function pip-install {
local pkgs="${@}"
prefixed-command "INSTALLING PIP PACKAGES" $pkgs
run-command pip install --no-clean $pkgs
}
function place {
local src="$1"
local dest="$2"
prefixed-command "PLACING FILE" "$src -> $dest"
delete-if-exists $dest
run-command cp -f -b -r $src $dest
}
function prefixed-command {
local prefix="$1"
local cmd="${@:2}"
local bg_prefix="006"
local bg_postfix="005"
echo -e "\x1b[48;5;${bg_prefix}m\x1b[38;5;15m $prefix \x1b[48;5;${bg_postfix}m\x1b[38;5;15m $cmd \x1b[48;5;0m\x1b[38;5;15m"
}
function run-command {
local cmd="${@}"
if [ "$EXECUTE_COMMANDS" -eq "1" ]; then
prefixed-command "EXECUTING COMMAND" $cmd
eval $cmd
else
notice "NOT EXECUTING COMMAND" $cmd
fi
}
function run-command-in-directory {
local dir="$1"
local cmd="${@:2}"
prefixed-command "RUNNING COMMAND IN DIRECTORY" "$dir $cmd"
run-command "(cd $dir && $cmd)"
}
function service-restart {
local svc="$1"
prefixed-command "RESTARTING SERVICE" $svc
service-stop $svc
service-start $svc
}
function service-start {
local svc="$1"
prefixed-command "STARTING SERVICE" $svc
run-command service $svc start
}
function service-status {
local svc="$1"
prefixed-command "SERVICE STATUS" $svc
run-command service $svc status
}
function service-stop {
local svc="$1"
prefixed-command "STOPPING SERVICE" $svc
run-command service $svc stop
}
function set-env {
local key="$1"
local value="$2"
prefixed-command "SETTING ENVIRONMENT VARIABLE" "$key = $value"
run-command export $key=$value
}
function set-permissions {
local octal="$1"
local items="${@:2}"
for item in $items; do
prefixed-command "SETTING PERMISSIONS" "$item -> $octal"
run-command chmod $octal $item
done
}
function symlink {
local src="$1"
local dest="$2"
prefixed-command "CREATING SYMLINK" "$src -> $dest"
delete-if-exists $dest
run-command ln -s $src $dest
}
function touch-item {
local items="${@}"
for item in $items; do
prefixed-command "TOUCHING ITEM" $item
run-command touch $item
done
}
function notice {
local prefix="$1"
local msg="${@:2}"
local bg_prefix="001"
local bg_postfix="088"
echo -e "\x1b[48;5;${bg_prefix}m\x1b[38;5;15m $prefix \x1b[48;5;${bg_postfix}m\x1b[38;5;15m $msg \x1b[48;5;0m\x1b[38;5;15m"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment