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