Index

joshstock.in / 5881a19

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
15423 Jul 2022 22:420892789Update deploy script to automatically install Python modulesJosh Stockin162G

Blob @ joshstock.in / deploy

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