#!/bin/bash # Vipin Madhavanunni this=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1) exit_fuc() { if type "docker" > /dev/null 2>&1 ; then this_check=$(docker-compose ps web-$this 2> /dev/null | tail -1) if [[ $this_check == *"web-$this"* ]]; then docker-compose rm -f web-$this fi rm -rf /tmp/gh-pages-$this rm -f docker-compose-$this.yml fi } trap exit_fuc 0 docker_check() { docker_id=$(docker info 2> /dev/null | grep ^ID:) if [ -z "$docker_id" ]; then echo "Unable to connect to docker ..." echo " May be need to use sudo ..." exit 1 fi } docker_gen_file() { cat > docker-compose-$this.yml << EOL web-$this: image: unws/gh-pages:$tag volumes: - /tmp/gh-pages-$this:/html ports: - "$port:$port" EOL } docker_run() { mkdir /tmp/gh-pages-$this docker pull unws/gh-pages:$tag cat docker-compose-$this.yml docker-compose -f docker-compose-$this.yml run web-$this -s $gitsrc -p $port } get_ip() { if type "ifconfig" > /dev/null 2>&1 ; then webip=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') else webip="0.0.0.0" fi } jekyll_run() { if [[ "$post_limit" -gt 0 ]]; then echo -e "\033[1;33m[WARN] Telling jekyll to only generate the last $post_limit posts. \033[0m" if [ -f build ]; then ./build -r -l $post_limit -p $port else bundle exec jekyll serve --port $port --limit_posts "$post_limit" fi else if [ -f build ]; then ./build -r -p $port else bundle exec jekyll serve --port $port fi fi } pwd_safe() { cpwd=$(pwd) if [ "$cpwd" != "/html" ]; then echo "Looks this is running inside host .." cd /tmp/gh-pages-$this echo "Switching to temp ..." pwd else pwd fi } usage() { echo "$0 usage:" && grep " .)\ #" $0; exit 0; } ### MAIN ### post_limit=0 build_run=0 build_port=0 build_tag=0 build_dsrc=0 build_limit=0 build_page=0 tag="latest" port=4000 [ $# -eq 0 ] && usage while getopts "d:n:s:p:t:r:h" arg; do case $arg in p) # Jekyll port (optional) port=${OPTARG} build_port=1 ;; t) # docker image tag (optional) tag=${OPTARG} build_tag=1 ;; d) # default source, used internal. (Not required) default_src=${OPTARG} build_dsrc=1 ;; n) # number of post limit for generatation. (optional) post_limit=${OPTARG} build_limit=1 ;; s) # git source utl for the site. (mandatory) page=${OPTARG} build_page=1 ;; r) # run the docker-compose with url. (mandatory to run) gitsrc=${OPTARG} build_run=1 ;; h | *) # Display help. usage exit 0 ;; esac done ### Host ### if [ "$build_run" -eq 1 ]; then docker_check docker_gen_file docker_run exit $? fi ### Container ### pwd_safe if [ "$build_page" -eq 1 ]; then git clone $page . git submodule update --init --recursive elif [ "$build_dsrc" -eq 1 ]; then git clone $default_src . git submodule update --init --recursive fi get_ip echo "" echo "-- Open http://$webip:$port/ --" echo "" echo "-- CTL+C to exit --" jekyll_run > /dev/null exit $?