Permalink
Find file Copy path
52dda84 Jan 10, 2019
1 contributor

Users who have contributed to this file

executable file 482 lines (394 sloc) 14.5 KB
#!/usr/bin/env bash
JINX_VERSION="0.2.0"
# Define location of configuration
JINX_CONFIG_FOLDER="$HOME/.jinx"
JINX_CONFIG_FILE="$JINX_CONFIG_FOLDER/config"
# Create configuration file if it doesn't exist.
if [[ ! -f $JINX_CONFIG_FILE ]]
then
mkdir -p $JINX_CONFIG_FOLDER
cat <<EOF > $JINX_CONFIG_FILE
#
# .jinx/config
# Configuration file for jinx
#
# Each configuration key must stand on its own line with no comments.
# The comments above each variable explain their purpose.
#
# Where you nginx.conf and other files are located. Must end in /.
nginx_path=/etc/nginx/
# Where to find the configuration templates for new sites.
# Gets appended to the nginx path. Must not contain slashes
config_path=configurations
# Which editor to use for editing sites
editor=nano
# Decides whether or not to use colored output.
grumpy=0
EOF
date +%s > "$JINX_CONFIG_FOLDER/last_update_check"
echo -e "\033[1;33mFirst run!\033[0m Creating default configuration in ~/.jinx/config"
echo "Pardon the interruption, we will now continue running your command."
echo ""
fi
JINX_IS_GRUMPY=$(grep -oh "grumpy=.*" $JINX_CONFIG_FILE | sed "s/grumpy=//")
# Define colors and formats for making everything pretty.
COLOR_RED=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;31m"`
# COLOR_RED_LIGHT=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;31m"`
COLOR_GREEN=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;32m"`
# COLOR_GREEN_LIGHT=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;32m"`
COLOR_ORANGE=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;33m"`
COLOR_YELLOW=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;33m"`
# COLOR_BLUE=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;34m"`
# COLOR_BLUE_LIGHT=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;34m"`
# COLOR_PURPLE=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;35m"`
# COLOR_PURPLE_LIGHT=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;35m"`
COLOR_CYAN=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;36m"`
# COLOR_CYAN_LIGHT=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;36m"`
# COLOR_GRAY=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;30m"`
# COLOR_GRAY_LIGHT=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;37m"`
# COLOR_BLACK=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0;30m"`
# COLOR_WHITE=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1;37m"`
FORMAT_BOLD=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[1m"`
FORMAT_UNDERLINE=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[4m"`
FORMAT_END=`[[ $JINX_IS_GRUMPY -eq 1 ]] && echo "" || echo "\033[0m"`
# Functions
function jinx_help {
echo -e "${FORMAT_BOLD}${FORMAT_UNDERLINE}Available commands:${FORMAT_END}"
echo -e ""
echo -e "${COLOR_CYAN}start${FORMAT_END} start nginx service"
echo -e "${COLOR_CYAN}restart${FORMAT_END} restart nginx service"
echo -e "${COLOR_CYAN}stop${FORMAT_END} stop nginx service"
echo -e "${COLOR_CYAN}logs${FORMAT_END} get nginx error logs"
echo -e ""
echo -e "${COLOR_CYAN}site activate <name> [--restart|-r]${FORMAT_END} activate a site"
echo -e "${COLOR_CYAN}site deactivate <name> [--restart|-r]${FORMAT_END} deactivate a site"
echo -e "${COLOR_CYAN}site delete <name> [--yes|-y]${FORMAT_END} delete a site"
echo -e "${COLOR_CYAN}site create <name> [<template>]${FORMAT_END} create a site from template"
echo -e "${COLOR_CYAN}site edit <name>${FORMAT_END} edit a site .conf file with editor"
echo -e ""
echo -e "${COLOR_CYAN}config <key>${FORMAT_END} get config value from ~/.jinx/config"
echo -e "${COLOR_CYAN}config <key> <value>${FORMAT_END} set config value in ~/.jinx/config"
echo -e ""
echo -e "${COLOR_CYAN}update${FORMAT_END} update jinx to the latest version"
echo ""
}
function jinx_service {
OS="$(uname -a)"
case "$OS" in
*Ubuntu*)
sudo systemctl "$1" nginx > /dev/null
;;
Darwin*)
sudo brew services "$1" nginx > /dev/null
;;
*)
echo -e "${COLOR_RED}Failure.${FORMAT_END} Automatic $1 not supported on your system. Please $1 nginx manually."
exit 1
;;
esac
if [ $? -eq 0 ]
then
case "$1" in
start) echo -e "${COLOR_GREEN}Success.${FORMAT_END} Started nginx service on your system.";;
stop) echo -e "${COLOR_GREEN}Success.${FORMAT_END} Stopped nginx service on your system.";;
restart) echo -e "${COLOR_GREEN}Success.${FORMAT_END} Restarted nginx service on your system.";;
esac
fi
}
function jinx_optional_restart {
case "$1" in
--restart|-r) jinx_service "restart";;
esac
}
function jinx_config_set {
local END_OF_PATH
local JINX_CONFIG_VALUE="$2"
if [[ -z "$1" ]] || [[ -z "$2" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please specify a configuration key and value."
exit 1
fi
if [[ "$1" == "nginx_path" ]]
then
local END_OF_PATH=$(echo "${2: -1}");
if [[ "$END_OF_PATH" != "/" ]]
then
local JINX_CONFIG_VALUE="$2/"
fi
fi
if [[ "$1" == "config_path" ]]
then
local JINX_CONFIG_VALUE=$(echo $2 | sed -e "s#/##g")
fi
grep -q "$1=.*" $JINX_CONFIG_FILE \
&& sed -i -e "s#$1=.*#$1=$JINX_CONFIG_VALUE#" $JINX_CONFIG_FILE \
|| echo "$1=$JINX_CONFIG_VALUE" >> $JINX_CONFIG_FILE
echo -e "${COLOR_GREEN}Success.${FORMAT_END} Updated setting '$1' to '$JINX_CONFIG_VALUE'."
exit 0
}
function jinx_config_get {
if [[ -z "$1" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please specify a configuration key."
exit 1
fi
echo $(grep -oh "$1=.*" $JINX_CONFIG_FILE | sed "s/$1=//")
exit 0
}
function jinx_site_activate {
if [[ -z "$1" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please specify a site name."
exit 1
fi
local NGINX_PATH=$(jinx_config_get "nginx_path")
local FILE_PATH="$NGINX_PATH/sites-available/$1.conf"
if [[ ! -f "$FILE_PATH" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Site '$1' does not exist."
exit 1
fi
ln -sfn $FILE_PATH $NGINX_PATH/sites-enabled/ > /dev/null
echo -e "${COLOR_GREEN}Success.${FORMAT_END} Activated site '$1'."
jinx_optional_restart "$2"
exit 0
}
function jinx_site_deactivate {
if [[ -z "$1" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please specify a site name."
exit 1
fi
local NGINX_PATH=$(jinx_config_get "nginx_path")
local FILE_PATH="$NGINX_PATH/sites-enabled/$1.conf"
if [[ ! -f "$FILE_PATH" ]]
then
echo -e "${COLOR_ORANGE}Failure.${FORMAT_END} Site '$1' is not activated."
exit 1
fi
rm $FILE_PATH
echo -e "${COLOR_GREEN}Success.${FORMAT_END} Deactivated site '$1'."
jinx_optional_restart "$2"
exit 0
}
function jinx_site_edit {
if [[ -z "$1" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please specify a site name."
exit 1
fi
local EDITOR=$(jinx_config_get "editor")
local NGINX_PATH=$(jinx_config_get "nginx_path")
local FILE_PATH="$NGINX_PATH/sites-available/$1.conf"
if [[ ! -f "$FILE_PATH" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Site '$1' does not exist."
exit 1
fi
if [[ -z "$EDITOR" ]]
then
local EDITOR=nano
echo -e "${COLOR_YELLOW}No preferred editor.${FORMAT_END} Falling back to nano. Please set the EDITOR configuration key"
fi
$EDITOR $FILE_PATH
exit 0
}
function jinx_site_create {
local CONFIG_TEMPLATE="default"
if [[ -z "$1" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please specify a site name."
exit 1
fi
if [[ ! -z "$2" ]]
then
local CONFIG_TEMPLATE="$2"
fi
local NGINX_PATH=$(jinx_config_get "nginx_path")
local CONFIG_PATH=$(jinx_config_get "config_path")
local TEMPLATE_PATH="$NGINX_PATH$CONFIG_PATH/"
local TEMPLATE_FILE="$TEMPLATE_PATH$CONFIG_TEMPLATE.conf"
local NEW_FILE_PATH="$NGINX_PATH/sites-available/$1.conf"
if [[ ! -f "$TEMPLATE_FILE" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Configuration template '$CONFIG_TEMPLATE' does not exist."
exit 1
fi
if [[ -f "$NEW_FILE_PATH" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Site '$1' already exists. Please choose another name."
exit 1
fi
cp "$TEMPLATE_FILE" "$NEW_FILE_PATH"
sed -i -e "s/___/$1/g" "$NEW_FILE_PATH"
echo -e "${COLOR_GREEN}Success.${FORMAT_END} Site '$1' was created and can now be activated."
exit 0
}
function jinx_site_delete {
local NGINX_PATH=$(jinx_config_get "nginx_path")
local FILE_PATH_AVAILABLE="$NGINX_PATH/sites-available/$1.conf"
local FILE_PATH_ENABLED="$NGINX_PATH/sites-enabled/$1.conf"
if [[ -f $FILE_PATH_ENABLED ]]
then
echo -e "${COLOR_RED}${FORMAT_UNDERLINE}${FORMAT_BOLD}ABORTING. Site '$1' is currently activated!${FORMAT_END}"
echo -e "If you really want to delete it, please deactivate the site first."
exit 2
fi
if [[ ! -f $FILE_PATH_AVAILABLE ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Site '$1' does not exist."
exit 1
fi
if [[ ! "$2" == "-y" ]] && [[ ! "$2" == "--yes" ]]
then
echo -e "${COLOR_ORANGE}Careful:${FORMAT_END} Are you sure you want to delete site '$1'? (y/N): \c"
read DELETE_CONSENT
local COMP_DELETE_CONSENT=$(echo $DELETE_CONSENT | tr '[A-Z]' '[a-z]')
else
local COMP_DELETE_CONSENT="y"
fi
if [[ "$COMP_DELETE_CONSENT" == "y" || "$COMP_DELETE_CONSENT" == "yes" ]]
then
rm $FILE_PATH_AVAILABLE
echo -e "${COLOR_GREEN}Success.${FORMAT_END} Site '$1' was deleted."
exit 0
fi
echo -e "${COLOR_RED}Aborting.${FORMAT_END} Did not receive 'y' or 'yes' as answer, so not deleting site."
exit 1
}
function jinx_logs {
local NGINX_PATH=$(jinx_config_get "nginx_path")
local NGINX_CONFIG_FILE="$NGINX_PATH/nginx.conf"
local NGINX_LOGS=$(grep -oh "error_log .*" "$NGINX_CONFIG_FILE" | sed "s/^error_log //")
tail -f $(echo "$NGINX_LOGS" | tr -d ";")
}
function jinx_get_latest_release {
echo $(curl -s https://api.github.com/repos/pretzelhands/jinx/releases/latest)
}
function jinx_get_json_value {
echo $(echo $1 | python -c "import sys, json; print json.load(sys.stdin)['$2']")
}
# TODO: I need to make this prettier
function jinx_get_yesterday_timestamp {
echo $(python -c "import time; from datetime import datetime, timedelta; yesterday=datetime.now()-timedelta(days=1); print str(time.mktime(yesterday.timetuple()))[:-2]")
}
function jinx_check_version {
local JINX_LAST_UPDATE_CHECK=$(cat "$JINX_CONFIG_FOLDER/last_update_check")
local JINX_LAST_UPDATE_LIMIT=$(jinx_get_yesterday_timestamp)
if [[ $JINX_LAST_UPDATE_CHECK -gt $JINX_LAST_UPDATE_LIMIT ]]
then
return
fi
local JINX_LATEST_RELEASE=$(jinx_get_latest_release)
local JINX_LATEST_VERSION=$(jinx_get_json_value "$JINX_LATEST_RELEASE" "tag_name")
if [[ "$JINX_VERSION" != "$JINX_LATEST_VERSION" ]]
then
echo -e "${COLOR_YELLOW}There is a new version of jinx available.${FORMAT_END}"
echo -e "Please run 'jinx update' to get the latest version"
fi
}
function jinx_update {
echo -n "Grabbing latest release from GitHub... "
local JINX_EXECUTABLE=$(which jinx)
local JINX_TMP="/var/tmp/jinx"
local JINX_LATEST_RELEASE=$(jinx_get_latest_release)
local JINX_LATEST_TARBALL=$(jinx_get_json_value "$JINX_LATEST_RELEASE" "tarball_url")
local JINX_LATEST_VERSION=$(jinx_get_json_value "$JINX_LATEST_RELEASE" "tag_name")
if [[ "$JINX_VERSION" == "$JINX_LATEST_VERSION" ]]
then
echo -e "${COLOR_GREEN}done.${FORMAT_END}\n${COLOR_GREEN}You are already using the latest version of jinx!${FORMAT_END}"
exit 0
fi
echo -e "${COLOR_GREEN}done.${FORMAT_END}"
echo -n "Grabbing latest tarball from GitHub... "
mkdir -p $JINX_TMP
mkdir -p $JINX_TMP/unpacked
curl -L "$JINX_LATEST_TARBALL" -o "$JINX_TMP/jinx.tar.gz" --silent
echo -e "${COLOR_GREEN}done.${FORMAT_END}"
echo -n "Unpacking jinx executable and installing ... "
tar xzf "$JINX_TMP/jinx.tar.gz" -C "$JINX_TMP/unpacked" --strip-components 1
mv "$JINX_TMP/unpacked/jinx" "$JINX_EXECUTABLE"
echo -e "${COLOR_GREEN}done.${FORMAT_END}"
rm -r /var/tmp/jinx
exit 0
}
#
# Sanity checks
#
# We require at least one command
if [[ -z "$1" ]]
then
echo ""
jinx_help
exit 0
fi
jinx_check_version
# Process arguments in order
for arg in "$@"
do
case $arg in
help)
jinx_help
exit 0
;;
restart)
jinx_service "restart"
;;
stop)
jinx_service "stop"
;;
start)
jinx_service "start"
;;
logs)
jinx_logs
;;
update)
jinx_update
;;
version)
echo -e "$JINX_VERSION"
exit 0
;;
config)
if [[ -z "$2" ]]
then
echo -e "${COLOR_RED}Failure.${FORMAT_END} Please provide a key to fetch or a key and value to set."
exit 1
fi
if [[ -z "$3" ]]
then
jinx_config_get "$2"
elif [[ ! -z "$3" ]]
then
jinx_config_set "$2" "$3"
fi
;;
site)
if [[ "$2" == "activate" ]]
then
jinx_site_activate $3 $4
elif [[ "$2" == "deactivate" ]]
then
jinx_site_deactivate $3 $4
elif [[ "$2" == "edit" ]]
then
jinx_site_edit $3
elif [[ "$2" == "create" ]]
then
jinx_site_create $3 $4
elif [[ "$2" == "delete" ]]
then
jinx_site_delete $3 $4
else
echo -e "${COLOR_RED}Please provide a subcommand.${FORMAT_END}\n"
jinx_help
exit 1
fi
;;
*)
echo -e "${COLOR_RED}Command was not recognized.${FORMAT_END}\n"
exit 1
;;
esac
done