1 | #!/usr/bin/env bash |
2 |
|
3 | # Script variables |
4 | PYTHON_MAJOR=3 |
5 | PYTHON_MINOR=7 |
6 | ACCEPTABLE_PYTHON_COMMANDS="python3 python3.10 python3.9 python3.8 python3.7 python" |
7 | PYTHON_COMMAND= |
8 |
|
9 | SCRIPT_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. |
14 | while read -r requirement |
15 | do |
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)" |
19 | done < $SCRIPT_SOURCE_DIR/requirements.txt |
20 |
|
21 | # Plumbing |
22 | set -o pipefail |
23 |
|
24 | RESTORE=$(echo -en '\033[0m') |
25 | STANDOUT=$(echo -en '\033[7m') |
26 | RED=$(echo -en '\033[00;31m') |
27 | GREEN=$(echo -en '\033[00;32m') |
28 | YELLOW=$(echo -en '\033[00;33m') |
29 | PURPLE=$(echo -en '\033[00;35m') |
30 | LIGHTGRAY=$(echo -en '\033[00;37m') |
31 | LRED=$(echo -en '\033[01;31m') |
32 | LGREEN=$(echo -en '\033[01;32m') |
33 | LYELLOW=$(echo -en '\033[01;33m') |
34 | LBLUE=$(echo -en '\033[01;34m') |
35 | LCYAN=$(echo -en '\033[01;36m') |
36 |
|
37 |
|
38 |
|
39 | function usage { |
40 | echo "Usage: sudo $0 <prod|test>" |
41 | exit 0 |
42 | } |
43 |
|
44 | if [ "$EUID" -ne 0 ]; then |
45 | echo "error: must run as root" |
46 | usage |
47 | fi |
48 |
|
49 | # Handle argument input |
50 | case $1 in |
51 | prod) |
52 | ;; |
53 | test) |
54 | ;; |
55 | *) |
56 | usage |
57 | ;; |
58 | esac |
59 |
|
60 | MODE=$1 |
61 |
|
62 | # Determine Python interpreter to use. Takes from list of acceptable |
63 | # interpreters if user didn't supply one. |
64 | echo "${LIGHTGRAY}Determining Python interpreter${RESTORE}" |
65 | if [ -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 |
73 | fi |
74 |
|
75 | # Expand interpreter command, verify with `import sys` test instruction |
76 | PYTHON_COMMAND=$(command -v $PYTHON_COMMAND) |
77 | if [ -z $PYTHON_COMMAND ]; then |
78 | echo " ${BOLD}${LRED}Python interpreter not found${RESTORE}" |
79 | exit 1 |
80 | fi |
81 | if [ -h "$PYTHON_COMMAND" ]; then |
82 | PYTHON_COMMAND=$(readlink -f $PYTHON_COMMAND) # Expand symlink, if "python3" |
83 | fi |
84 | echo " Trying interpreter [ ${LYELLOW}$PYTHON_COMMAND${RESTORE} ]" |
85 | if ! $PYTHON_COMMAND -c "import sys"; then |
86 | echo " ${BOLD}${LRED}Executable is not a Python interpreter${RESTORE}" |
87 | exit 1 |
88 | fi |
89 |
|
90 | # Verifying installed Python version meets minimum requirements |
91 | echo "${LIGHTGRAY}Checking Python version${RESTORE} [ needs ${LIGHTGRAY}>=$PYTHON_MAJOR.$PYTHON_MINOR${RESTORE} ]" |
92 | PYTHON_VERSION_STRING=$($PYTHON_COMMAND -c "print('.'.join([str(a) for a in __import__('sys').version_info[:3]]))") |
93 | if ! $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 |
97 | fi |
98 | echo " Version [ ${LCYAN}$PYTHON_VERSION_STRING${RESTORE} ] acceptable" |
99 |
|
100 | # Verifying required modules are installed |
101 | echo "${LIGHTGRAY}Checking Python modules installed${RESTORE}" |
102 | for MODULE in $(seq 1 $(wc -w <<< $REQUIRED_PIP_MODULES)) |
103 | do |
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" |
116 | done |
117 | echo " ${GREEN}All required modules found${RESTORE}" |
118 |
|
119 | function runcmd { # Indents program output before passing to stdout |
120 | "$@" 2>&1 | sed 's/^/ /'; |
121 | } |
122 |
|
123 |
|
124 | # Run templating engine for static site content |
125 | echo "${LIGHTGRAY}Running HTML templating script${RESTORE}" |
126 | runcmd $SCRIPT_SOURCE_DIR/build.py $HTML_OUT |
127 | if [ $? != 0 ]; then |
128 | echo " ${BOLD}${LRED}Error running templating script${RESTORE}" |
129 | exit 1 |
130 | else |
131 | echo " ${GREEN}Templating successful${RESTORE}" |
132 | fi |
133 |
|
134 | echo "${LIGHTGRAY}Copying templated files${RESTORE}" |
135 | runcmd rm -rv /var/www/josh |
136 | runcmd mkdir -p /var/www/josh |
137 | runcmd cp -rv $SCRIPT_SOURCE_DIR/build/* /var/www/josh |
138 | echo " ${GREEN}Files copied successfully${RESTORE}" |
139 |
|
140 | # Install nginx configuration |
141 | echo "${LIGHTGRAY}Installing nginx configuration${RESTORE}" |
142 | if [ ! -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 |
145 | fi |
146 | runcmd cp -v $SCRIPT_SOURCE_DIR/nginx/nginx.conf /etc/nginx/nginx.conf |
147 | if [ ! -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 |
150 | fi |
151 | runcmd rm -rv /etc/nginx/sites/joshstock.in |
152 | runcmd mkdir -p /etc/nginx/sites/joshstock.in |
153 | runcmd cp -rv $SCRIPT_SOURCE_DIR/nginx/$MODE/* /etc/nginx/sites/joshstock.in |
154 | echo " ${GREEN}nginx configuration successfully installed${RESTORE}" |
155 |
|
156 | # Install configuration for resty-gitweb subdomain |
157 | echo "${LIGHTGRAY}Installing resty-gitweb configuration${RESTORE}" |
158 | if [ ! -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 |
161 | fi |
162 | runcmd cp -v $SCRIPT_SOURCE_DIR/resty-gitweb.yaml /etc/resty-gitweb.yaml |
163 | echo " ${GREEN}resty-gitweb configuration successfully installed${RESTORE}" |
164 |
|
165 | # Reload nginx |
166 | if [[ ! $(lsof -i TCP:80) =~ "nginx" ]]; then |
167 | echo "${LIGHTGRAY}(Re)starting nginx${RESTORE}" |
168 | systemctl restart nginx |
169 | else |
170 | echo "${LIGHTGRAY}Reloading nginx${RESTORE}" |
171 | systemctl reload nginx |
172 | fi |
173 | if [ $? != 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 |
177 | else |
178 | echo "${GREEN}nginx loaded new configuration successfully${RESTORE}" |
179 | echo "${BOLD}${LGREEN}Successful deployment!${RESTORE}" |
180 | exit 0 |
181 | fi |
182 |
|