mirror of https://git.sr.ht/~skiqqy/bin
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
7.4 KiB
357 lines
7.4 KiB
#!/usr/bin/env bash
|
|
# Simple script for managing skiqqy.xyz
|
|
|
|
domain='skiqqy.xyz' # Easy changing if I decide to switch domains.
|
|
|
|
# Dont Change These.
|
|
declare -A commands
|
|
commands[check]="check [-v] [SUBDOMAIN]\nCheck the status of a sub domain."
|
|
commands[list]="list\nShow avaible commands."
|
|
commands[help]="help COMMAND\nShow helpful information for a command, see:\n$ $(basename "$0") list"
|
|
commands[init]="init [-f]\nInit the configs."
|
|
commands[refresh]="refresh [KEY SECRET]\nRefresh domains and subdomains IP's."
|
|
commands[nginxd]="nginxd ARGUMENTS...\nWrapper for nginxd."
|
|
commands[update]="update\nUpdate the server, pulling and rebuilding the website."
|
|
commands[error]="[ERROR] Command entered DNE, please see:\n$ $(basename "$0") list"
|
|
commands[deploy]="deploy [PORT]\nDeploy an instance of skiqqy.xyz in a container."
|
|
|
|
### Helper Functions ###
|
|
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[1;33m'
|
|
GREEN='\033[92m'
|
|
NC='\033[0m'
|
|
|
|
# Usage: error MSG CODE
|
|
error() {
|
|
printf "[%bERROR%b] $1\n" "$RED" "$NC"
|
|
exit "$2"
|
|
}
|
|
|
|
# Usage: warning MSG
|
|
warning() {
|
|
printf "[%bWARNING%b] $1\n" "$YELLOW" "$NC"
|
|
}
|
|
|
|
# Usage: success MSG
|
|
success() {
|
|
printf "[%bSUCCESS%b] $1\n" "$GREEN" "$NC"
|
|
}
|
|
|
|
# Usage: tick MSG
|
|
tick() {
|
|
printf "%s %b✔%b\n" "$1" "$GREEN" "$NC"
|
|
}
|
|
|
|
# Usage: http_code URL
|
|
http_code()
|
|
{
|
|
local url
|
|
url="${1:-"$domain"}"
|
|
curl -s -o /dev/null --head -w "%{http_code}" "$url"
|
|
}
|
|
|
|
### Helper Functions ###
|
|
|
|
usage()
|
|
{
|
|
cat << EOF
|
|
$(basename "$0") ~ Simple $domain maintenance script.
|
|
|
|
Usage:
|
|
$ $(basename "$0") [options] COMMAND ARGS...
|
|
|
|
Options:
|
|
-h Shows this message.
|
|
-c DIRECTORY Specify config directory.
|
|
-d DOMAIN Specify the domain to operate on.
|
|
|
|
Commands:
|
|
$ $(basename "$0") list
|
|
|
|
Command Help:
|
|
$ $(basename "$0") help COMMAND
|
|
|
|
Written by Skiqqy
|
|
EOF
|
|
exit "$1"
|
|
}
|
|
|
|
# Setup config file
|
|
sinit()
|
|
{
|
|
local ret
|
|
local force
|
|
force=false
|
|
ret=0
|
|
|
|
while getopts 'f' opt
|
|
do
|
|
case "$opt" in
|
|
f)
|
|
force=true
|
|
;;
|
|
*)
|
|
error "Invalid Flag" 1
|
|
;;
|
|
esac
|
|
done
|
|
shift "$((OPTIND-1))"
|
|
|
|
mkdir -p "$confdir"
|
|
|
|
if "$force" || [ ! -f "$confdir/config" ]
|
|
then
|
|
ret=1
|
|
cat << EOF > "$confdir/config"
|
|
# Please Complete the config where necassary.
|
|
serv=$USER@localhost # ssh login for server.
|
|
nginxd=$USER@localhost # ssh login for nginx server.
|
|
nginxd_pm=Backups/$USER@localhost # Set to empty if ssh pass is not saved in pm instance.
|
|
serv_pm=Backups/$USER@localhost # Set to empty if ssh pass is not saved in pm instance.
|
|
godaddy_key_pm=godaddy/key # Set to empty if go daddy key is not saved in a pm instance.
|
|
godaddy_secret_pm=godaddy/secret # Set to empty if go daddy secret is not saved in a pm instance.
|
|
container=skiqqy # Set to empty if not inside a container.
|
|
EOF
|
|
printf 'Generated Config: %s\n' "$confdir/config"
|
|
|
|
read -rp 'Edit config? (Y/N): ' input
|
|
if [[ $input =~ ^(Y|y)((ES)|(es))?$ ]]
|
|
then
|
|
"$EDITOR" "$confdir/config"
|
|
ret=0
|
|
fi
|
|
fi
|
|
|
|
return "$ret"
|
|
}
|
|
|
|
# Print help info for a command.
|
|
shelp()
|
|
{
|
|
local out
|
|
local com
|
|
|
|
com="${1:-help}"
|
|
out="${commands[$com]}"
|
|
[ -z "$out" ] && out="${commands[error]}"
|
|
|
|
printf 'Usage: $ %s %b\n' "$(basename "$0")" "$out"
|
|
}
|
|
|
|
slist()
|
|
{
|
|
local out
|
|
out="${!commands[*]}"
|
|
out="${out//error /}"
|
|
printf 'Commands:\n\n%b\n' "${out// /\\n}"
|
|
}
|
|
|
|
scheck()
|
|
{
|
|
local dom
|
|
local verb
|
|
|
|
while getopts 'v' opt
|
|
do
|
|
case "$opt" in
|
|
v)
|
|
verb=true
|
|
;;
|
|
*)
|
|
error "Invalid Flag" 1
|
|
;;
|
|
esac
|
|
done
|
|
shift "$((OPTIND-1))"
|
|
|
|
verb=${verb:-false}
|
|
[ -n "$1" ] && dom="$1."
|
|
|
|
if ! "$verb"
|
|
then
|
|
printf '%s: %s\n' "$dom$domain" "$(http_code "https://$dom$domain")"
|
|
else
|
|
curl -I "https://$dom$domain"
|
|
fi
|
|
}
|
|
|
|
# srefresh KEY SECRET
|
|
srefresh()
|
|
{
|
|
local key
|
|
local secret
|
|
local url # Just temporary.
|
|
local timeout
|
|
# curl timeout in seconds
|
|
timeout=5
|
|
url='https://git.sr.ht/~skiqqy/skiqqy.xyz/blob/master/dns.py' # Just temporary
|
|
|
|
[ -n "$1" ] && key="$1"
|
|
[ -n "$2" ] && secret="$2"
|
|
|
|
if [ -z "$godaddy_key_pm" ]
|
|
then
|
|
read -rp 'Enter GoDaddy Key: ' key
|
|
elif [ -z "$key" ]
|
|
then
|
|
key="$(pm show "$godaddy_key_pm")"
|
|
fi
|
|
|
|
if [ -z "$godaddy_secret_pm" ]
|
|
then
|
|
read -rp 'Enter GoDaddy Secret: ' secret
|
|
elif [ -z "$secret" ]
|
|
then
|
|
secret="$(pm show "$godaddy_secret_pm")"
|
|
fi
|
|
|
|
while ! curl -s --connect-timeout "$timeout" "$url" > /dev/null
|
|
do
|
|
printf 'Failed to fetch script, is the internet up?'
|
|
sleep 60
|
|
done
|
|
|
|
python3 <(curl -s "$url") -k "$key" -s "$secret" # Just temporary.
|
|
}
|
|
|
|
snginxd()
|
|
{
|
|
local run
|
|
|
|
if [ -n "$nginxd" ]
|
|
then
|
|
# ssh to the nginx server.
|
|
sshpass -p "$(pm show "$nginxd_pm")" ssh -t "$nginxd" \
|
|
"
|
|
npath=\$(find /var/www -name nginxd) # Find the Script
|
|
npath=\"\${npath/nginxd/}\" # Get Path
|
|
cd \"\$npath\" || exit # Exit if something went wrong
|
|
sudo ./nginxd $*
|
|
"
|
|
else
|
|
npath=$(find /var/www -name nginxd) # Find the Script
|
|
npath="${npath/nginxd/}" # Get Path
|
|
cd "$npath" || return 1 # Exit if something went wrong
|
|
fi
|
|
}
|
|
|
|
# Todo: Tidy up
|
|
supdate()
|
|
{
|
|
local run
|
|
run="cd /var/www && git pull && make" # "typical" command
|
|
[ -n "$container" ] && run="sudo docker exec -it $container update"
|
|
|
|
if [ -n "$serv_pm" ]
|
|
then
|
|
sshpass -p "$(pm show "$serv_pm")" ssh -t "$serv" "$run"
|
|
else
|
|
ssh -t "$serv" "$run"
|
|
fi
|
|
}
|
|
|
|
sdeploy()
|
|
{
|
|
local port
|
|
local contd
|
|
local url
|
|
local gwiki
|
|
|
|
# Set vars
|
|
gwiki='https://git.sr.ht/~skiqqy/gwiki'
|
|
url='https://github.com/skiqqy/skiqqy.github.io' # Repo url for skiqqy.xyz
|
|
contd=
|
|
port=${1:-81} # Deploy to port 81 by default.
|
|
[[ ! $port =~ ^[0-9]+$ ]] && shelp deploy && return 1
|
|
|
|
# Find container tool
|
|
command -v podman > /dev/null && contd=podman
|
|
[ -z "$contd" ] && command -v docker > /dev/null && contd=docker
|
|
[ ! "$(whoami)" = root ] && [ "$contd" = docker ] && contd='sudo docker' # Assume sudo needed.
|
|
[ -z "$contd" ] && \
|
|
printf 'Cannot find a container tool, please install docker/podman' && \
|
|
return 1
|
|
|
|
# Print what will be done.
|
|
printf 'Deploying skiqqy.xyz, using:\n\n\tContainer Env: %s\n\tPort: %s\n\tRepository: %s\n\n' \
|
|
"$contd" "$port" "$url"
|
|
|
|
git clone "$gwiki" /tmp/gwiki
|
|
|
|
# Change working directories
|
|
pushd /tmp/gwiki || return 1
|
|
git pull # If /tmp/gwiki was not cloned (ie it was cloned previously) pull the latest
|
|
printf '\nBuilding gwiki, this may take a while...\n\n'
|
|
$contd build -t gwiki:latest .
|
|
|
|
if [ -n "$(podman ps -af name=skiqqy | tail -n 1)" ]
|
|
then
|
|
echo TODO: Prompt user to ask if they want to delete old container.
|
|
fi
|
|
|
|
$contd run --name skiqqy \
|
|
--restart=always \
|
|
-p "$port":80 \
|
|
--env CONFIG_HOME="$url" \
|
|
--env TIMER=60 \
|
|
gwiki:latest & # Fork the process, in the case of podman being used.
|
|
popd || return 1
|
|
}
|
|
|
|
main()
|
|
{
|
|
# Global Vars
|
|
godaddy_secret_pm=
|
|
godaddy_key_pm=
|
|
serv=
|
|
serv_pm=
|
|
container=
|
|
nginxd=
|
|
nginxd_pm=
|
|
|
|
while getopts 'hd:c:' opt
|
|
do
|
|
case "$opt" in
|
|
h)
|
|
usage 0
|
|
;;
|
|
d)
|
|
domain="$OPTARG"
|
|
;;
|
|
c)
|
|
confdir="$OPTARG"
|
|
;;
|
|
*)
|
|
usage 1
|
|
;;
|
|
esac
|
|
done
|
|
confdir=${confdir:-"$HOME/.config/$domain"}
|
|
|
|
shift "$((OPTIND - 1))" # Get rid of optargs
|
|
|
|
if ! sinit
|
|
then
|
|
printf 'Please Complete the config file in %s\n' "$confdir/config"
|
|
exit
|
|
fi
|
|
|
|
# shellcheck source=/dev/null
|
|
. "$confdir/config" # Load config settings.
|
|
if [ -z "$1" ]
|
|
then
|
|
usage 0
|
|
elif [ -n "${commands[$1]}" ]
|
|
then
|
|
local args
|
|
args="$1"
|
|
shift 1
|
|
|
|
s"$args" "$@" # Run the command
|
|
else
|
|
printf 'Invalid Command, see:\n$ %s list\n' "$(basename "$0")"
|
|
fi
|
|
}
|
|
|
|
main "$@"
|
|
|