Index

joshstock.in / 4febf83

Source for serving and static templating/compiling of https://joshstock.in.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
15814 Jan 2023 23:375aaa26aWebsite rewriteJosh Stockin13823G

Blob @ joshstock.in / deploy

text/x-shellscript6670 bytesdownload raw
1#!/usr/bin/env bash
2
3# Script variables
4PYTHON_MAJOR=3
5PYTHON_MINOR=7
6ACCEPTABLE_PYTHON_COMMANDS="python3 python3.10 python3.9 python3.8 python3.7 python"
7PYTHON_COMMAND=
8
9SCRIPT_SOURCE_DIR=$(dirname -- "${BASH_SOURCE[0]}")
10
11# If pip modules are named differently from their internal Python modules, the
12# requirements.txt file will use the format `pip_name:py_name` per line for
13# each requirement.
14while read -r requirement
15do
16 [[ $requirement =~ ^#.* ]] && continue # Ignore comment line
17 REQUIRED_PIP_MODULES="$REQUIRED_PIP_MODULES $(sed -r "s/:.*//g" <<< $requirement)"
18 REQUIRED_PYTHON_MODULES="$REQUIRED_PYTHON_MODULES $(sed -r "s/.*://g" <<< $requirement)"
19done < $SCRIPT_SOURCE_DIR/requirements.txt
20
21# Plumbing
22set -o pipefail
23
24RESTORE=$(echo -en '\033[0m')
25STANDOUT=$(echo -en '\033[7m')
26RED=$(echo -en '\033[00;31m')
27GREEN=$(echo -en '\033[00;32m')
28YELLOW=$(echo -en '\033[00;33m')
29PURPLE=$(echo -en '\033[00;35m')
30LIGHTGRAY=$(echo -en '\033[00;37m')
31LRED=$(echo -en '\033[01;31m')
32LGREEN=$(echo -en '\033[01;32m')
33LYELLOW=$(echo -en '\033[01;33m')
34LBLUE=$(echo -en '\033[01;34m')
35LCYAN=$(echo -en '\033[01;36m')
36
37
38
39function usage {
40 echo "Usage: sudo $0 <prod|test>"
41 exit 0
42}
43
44if [ "$EUID" -ne 0 ]; then
45 echo "error: must run as root"
46 usage
47fi
48
49# Handle argument input
50case $1 in
51 prod)
52 ;;
53 test)
54 ;;
55 *)
56 usage
57 ;;
58esac
59
60MODE=$1
61
62# Determine Python interpreter to use. Takes from list of acceptable
63# interpreters if user didn't supply one.
64echo "${LIGHTGRAY}Determining Python interpreter${RESTORE}"
65if [ -z $PYTHON_COMMAND ]; then
66 for COMMAND in $ACCEPTABLE_PYTHON_COMMANDS
67 do
68 if command -v $COMMAND &> /dev/null; then
69 PYTHON_COMMAND=$COMMAND
70 break
71 fi
72 done
73fi
74
75# Expand interpreter command, verify with `import sys` test instruction
76PYTHON_COMMAND=$(command -v $PYTHON_COMMAND)
77if [ -z $PYTHON_COMMAND ]; then
78 echo " ${BOLD}${LRED}Python interpreter not found${RESTORE}"
79 exit 1
80fi
81if [ -h "$PYTHON_COMMAND" ]; then
82 PYTHON_COMMAND=$(readlink -f $PYTHON_COMMAND) # Expand symlink, if "python3"
83fi
84echo " Trying interpreter [ ${LYELLOW}$PYTHON_COMMAND${RESTORE} ]"
85if ! $PYTHON_COMMAND -c "import sys"; then
86 echo " ${BOLD}${LRED}Executable is not a Python interpreter${RESTORE}"
87 exit 1
88fi
89
90# Verifying installed Python version meets minimum requirements
91echo "${LIGHTGRAY}Checking Python version${RESTORE} [ needs ${LIGHTGRAY}>=$PYTHON_MAJOR.$PYTHON_MINOR${RESTORE} ]"
92PYTHON_VERSION_STRING=$($PYTHON_COMMAND -c "print('.'.join([str(a) for a in __import__('sys').version_info[:3]]))")
93if ! $PYTHON_COMMAND -c "import sys;exit(not(sys.version_info.major==$PYTHON_MAJOR and sys.version_info.minor>=$PYTHON_MINOR))"; then
94 echo " ${RED}Python version must be ${RESTORE}[ ${LCYAN}>=$PYTHON_MAJOR.$PYTHON_MINOR${RESTORE} ]${RED}."\
95 "Installed is ${RESTORE}[ ${LCYAN}$PYTHON_VERSION_STRING${RESTORE} ]"
96 exit 1
97fi
98echo " Version [ ${LCYAN}$PYTHON_VERSION_STRING${RESTORE} ] acceptable"
99
100# Verifying required modules are installed
101echo "${LIGHTGRAY}Checking Python modules installed${RESTORE}"
102for MODULE in $(seq 1 $(wc -w <<< $REQUIRED_PIP_MODULES))
103do
104 PIP_MODULE=$(awk -v N=$MODULE '{print $N}' <<< "$REQUIRED_PIP_MODULES")
105 PYTHON_MODULE=$(awk -v N=$MODULE '{print $N}' <<< "$REQUIRED_PYTHON_MODULES")
106 if ! $PYTHON_COMMAND -c "import $PYTHON_MODULE" &> /dev/null; then
107 echo " ${BOLD}${LRED}Required Python module ${RESTORE}[ ${BOLD}${LBLUE}$PYTHON_MODULE${RESTORE} ] ${BOLD}${LRED}not found.${RESTORE}"
108 echo " Attempting install with ${PURPLE}$PYTHON_COMMAND -m pip install $PIP_MODULE${RESTORE}"
109 $PYTHON_COMMAND -m pip install $PIP_MODULE
110 if ! $PYTHON_COMMAND -c "import $PYTHON_MODULE" &> /dev/null; then
111 echo " ${BOLD}${LRED}Required Python module ${RESTORE}[ ${BOLD}${LBLUE}$PYTHON_MODULE${RESTORE} ] ${BOLD}${LRED}not found${RESTORE}"
112 exit 1
113 fi
114 fi
115 echo " Module [ ${LBLUE}$PYTHON_MODULE${RESTORE} ] found"
116done
117echo " ${GREEN}All required modules found${RESTORE}"
118
119function runcmd { # Indents program output before passing to stdout
120 "$@" 2>&1 | sed 's/^/ /';
121}
122
123
124# Run templating engine for static site content
125echo "${LIGHTGRAY}Running HTML templating script${RESTORE}"
126runcmd $SCRIPT_SOURCE_DIR/build.py $HTML_OUT
127if [ $? != 0 ]; then
128 echo " ${BOLD}${LRED}Error running templating script${RESTORE}"
129 exit 1
130else
131 echo " ${GREEN}Templating successful${RESTORE}"
132fi
133
134echo "${LIGHTGRAY}Copying templated files${RESTORE}"
135runcmd rm -rv /var/www/josh
136runcmd mkdir -p /var/www/josh
137runcmd cp -rv $SCRIPT_SOURCE_DIR/build/* /var/www/josh
138echo " ${GREEN}Files copied successfully${RESTORE}"
139
140# Install nginx configuration
141echo "${LIGHTGRAY}Installing nginx configuration${RESTORE}"
142if [ ! -f $SCRIPT_SOURCE_DIR/nginx/nginx.conf ]; then
143 echo " ${BOLD}${LRED}Core configuration file ${RESTORE}[ ${BOLD}${LBLUE}$SCRIPT_SOURCE_DIR/nginx/nginx.conf${RESTORE} ] ${BOLD}${LRED}not found${RESTORE}"
144 exit 1
145fi
146runcmd cp -v $SCRIPT_SOURCE_DIR/nginx/nginx.conf /etc/nginx/nginx.conf
147if [ ! -d $SCRIPT_SOURCE_DIR/nginx/$MODE ]; then
148 echo " ${BOLD}${LRED}Directory ${RESTORE}[ ${BOLD}${LBLUE}$SCRIPT_SOURCE_DIR/nginx/$MODE${RESTORE} ] ${BOLD}${LRED}not found${RESTORE}"
149 exit 1
150fi
151runcmd rm -rv /etc/nginx/sites/joshstock.in
152runcmd mkdir -p /etc/nginx/sites/joshstock.in
153runcmd cp -rv $SCRIPT_SOURCE_DIR/nginx/$MODE/* /etc/nginx/sites/joshstock.in
154echo " ${GREEN}nginx configuration successfully installed${RESTORE}"
155
156# Install configuration for resty-gitweb subdomain
157echo "${LIGHTGRAY}Installing resty-gitweb configuration${RESTORE}"
158if [ ! -f $SCRIPT_SOURCE_DIR/resty-gitweb.yaml ]; then
159 echo " ${BOLD}${LRED}Core configuration file ${RESTORE}[ ${BOLD}${LBLUE}$SCRIPT_SOURCE_DIR/resty-gitweb.yaml${RESTORE} ] ${BOLD}${LRED}not found${RESTORE}"
160 exit 1
161fi
162runcmd cp -v $SCRIPT_SOURCE_DIR/resty-gitweb.yaml /etc/resty-gitweb.yaml
163echo " ${GREEN}resty-gitweb configuration successfully installed${RESTORE}"
164
165# Reload nginx
166if [[ ! $(lsof -i TCP:80) =~ "nginx" ]]; then
167 echo "${LIGHTGRAY}(Re)starting nginx${RESTORE}"
168 systemctl restart nginx
169else
170 echo "${LIGHTGRAY}Reloading nginx${RESTORE}"
171 systemctl reload nginx
172fi
173if [ $? != 0 ]; then
174 echo "${BOLD}${LRED}nginx configuration not accepted or nginx could not be restarted. Dumping systemctl status...${RESTORE}"
175 runcmd systemctl status nginx
176 exit 1
177else
178 echo "${GREEN}nginx loaded new configuration successfully${RESTORE}"
179 echo "${BOLD}${LGREEN}Successful deployment!${RESTORE}"
180 exit 0
181fi
182