caw/caw.fish

123 lines
3 KiB
Fish
Executable file

#!/usr/bin/fish
if test "$argv[1]" = init
if test ! -f "./Cawfile"
echo "✨ Creating Cawfile..."
echo "# Cawfile, created $(date)
set server example.fowl
set app $(pwd | path basename)
set ignore ""
set local_build_command ""
set build_command ""
" >./Cawfile
else
echo "🙅🏻 Cawfile already created, not overwriting!"
end
return
end
if test ! -f "$PWD/Cawfile"
echo "😔 No Cawfile found, exiting..."
return
end
source "$PWD/Cawfile"
if test "$server" = ""
echo "🤔 Server not set in Cawfile"
return
end
if test "$app" = ""
echo "🧐 App not set in Cawfile"
return
end
if test "$ignore" != ""
set ignore "|$ignore"
end
function deploy
echo "🚚 Deploying $app on $server"
sleep 1
if test "$local_build_command" != ""
fish -c "$local_build_command"
end
# Pre-ping server so we wake-up wireguard
ping $server -c 1 >/dev/null
# Pack up source code, but ignore common .git, target, .env and extra $ignore variable
# send to server and start building using cargo
tar zcf - (ls -a . | tail -n +3 | grep -vE ".git|target|.env|Cawfile$ignore") \
| ssh $server "
$build_env
mkdir -p /tmp/caw/$app
cd /tmp/caw/$app && ls . | grep -v target | xargs rm -rf
mkdir -p /usr/bin/caw
echo '📦️🛠️ Beginning unpacking & compilation'
tar zxvf - && $build_command
"
# Pre-ping server so we wake-up wireguard
ping $server -c 1 >/dev/null
# echo standard openrc config and send to server and start service and add to startup
echo "#!/sbin/openrc-run
supervisor=supervise-daemon
name=$app
$env
command=/usr/bin/caw/$app
command_user=$app:$app
command_background=true
output_log=\"/var/log/$app.log\"
error_log=\"/var/log/$app.log\"
depend() {
need net localmount
after firewall
touch /var/log/$app.log
chown $app:$app /var/log/$app.log
}
" | ssh $server "
cat - > /etc/init.d/$app
chmod +x /etc/init.d/$app
groupadd caw
useradd $app -G caw
rc-update add $app default
service $app restart
"
end
if test "$argv[1]" = deploy
deploy
else if test "$argv[1]" = logs
ssh "$server" "tail -f /var/log/$app.log"
else if test "$argv[1]" = restart
ssh "$server" "service $app restart"
else if test "$argv[1]" = stop
ssh "$server" "service $app stop"
else if test "$argv[1]" = stats
ssh -t "$server" "htop -F '$app'"
else
echo "🐦‍⬛ caw - A small fly alternative, written in Fish, compatible with Alpine Linux
deploy - 🚚 Deploy to server based on information in Cawfile
logs - 📜 Tail the logs on the server
restart - 💡Restart the service on the server
stop - 🛑 Stop the service on the server
stats - 📈 Open htop on the app's server with filter for app
init - 🆕 Create Cawfile in current directory, if not already available
✨ Written by Bram Dingelstad (https://bram.dingelstad.works)
"
end