Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active November 3, 2022 10:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderofsalvation/0b882acb97bd54063c2c131852168b83 to your computer and use it in GitHub Desktop.
Save coderofsalvation/0b882acb97bd54063c2c131852168b83 to your computer and use it in GitHub Desktop.
My favorite (awesome) eco-friendly server patterns

tiny servers: the serverpark killers

auto-suspend-wakeup webservices

stop wasting cpu/mem-resources on idling services:

#!/bin/sh
while sleep 1s; do
  set +e
  echo wakeup
  timeout -s15 3h node server.js            # send SIGTERM after 3 hours
  echo sleeping  
  echo "HTTP/1.1 302 OK\nrefresh:1;url=/\n\n" | nc -N -lp $PORT
done

make sure environment variables PORT=9000 exist

auto-suspend-wakeup containers (podman/docker)

This can also be achieved thru systemd-proxy, however with podman this runs rootless:

#!/bin/sh
export ENGINE=$(which podman || which docker)
while sleep 1s; do
  set +e
  echo starting
  test -z $($ENGINE ps -a --filter=name=$IMAGE| head -n-1) && $ENGINE run -d --rm -p "$PORT:80" --name $IMAGE $IMAGE
  $ENGINE start $IMAGE
  sleep 3h;          # go to sleep after 3 hours
  $ENGINE stop $IMAGE
  echo sleeping
  echo "HTTP/1.1 302 OK\nrefresh:1;url=/\n\n" | nc -N -lp $PORT  
done

make sure environment variables PORT=9000 and CONTAINER=thttpd exist.

git pull from CI/CD

export JOB="git reset --hard && git pull origin master"
export PORT=9000
socat -t2 TCP4-LISTEN:$PORT,fork,max-children=1,forever,reuseaddr SYSTEM:"$JOB",pty,echo=0;
curl http://yourserver:9000 --http0.9 # run this in your CI

now run 'curl http://yourserver:9000' from you CI/CD shellscript to trigger git pull

tiny CI/CD http service

export JOB="ls -la"
export PORT=9000
socat -t2 TCP4-LISTEN:$PORT,fork,max-children=3,forever,reuseaddr SYSTEM:"$JOB",pty,echo=0;
curl http://localhost:9000 --http0.9 # run this in another terminal

modify max-children to configure maximum parallel jobs

tiny CI/CD http service + SSL

export JOB="ls -la"
export PORT=9000
test -f  cert.pem || { # create SSL temporary certificates
  openssl req -newkey rsa:2048 -nodes -keyout private.key -x509 -days 365 -out public.crt
  cat private.key public.crt > cert.pem
}
socat -t2 OPENSSL-LISTEN:$PORT,cert=cert.pem,verify=0,fork,max-children=3,forever,reuseaddr SYSTEM:"$JOB",pty,echo=0;
curl -k http://localhost:9000  # run this in another terminal

you can omit -k when symlinking a letsencrypt-generated certificate to cert.pem.

you can modify max-children to configure maximum parallel jobs

Distributed process over a cluster

cluster.sh

#!/bin/bash
mycluster="serverA serverB"

rpc(){
  foo(){ echo hello from $(hostname) $*; }
  "$@"
}

for server in $mycluster; do
  ssh $server "$(declare -f rpc); rpc foo $*" 
done
$ ./cluster.sh 123
hello from serverA 123
hello from serverB 123

cute gitops utility to turn servers into PaaS platforms

$ cd myapp
$ git init
$ wget "https://raw.githubusercontent.com/coderofsalvation/podi/master/podi" && chmod +x podi
$ ./podi init git@myserver:/dir/to/deploy
$ git push git@myserver
(pipeline is triggered)
# PROFIT!

podi only requires ssh+git installed on your tiny server

1MB redbean proxy/https-server (incl. sqlite + lua scripting)

redbean can serve 1 million+ gzip encoded responses per second on a cheap personal computer.

Optional: download docker-image here

300kb static website server

$(which docker || which podman) run -it --rm -p 3000:3000 -v $(pwd):/home/static my-static-website

NOTE: for JS patterns see my JS patterns-page

NOTE: for LUA patterns see my LUA patterns-page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment