3f73cfa8f5a3de5fbd347529cd4dc346eb2a000d
[iankelling.org] / setup.sh
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 2 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21 usage() {
22 cat <<EOF
23 Usage: ${0##*/} [OPTIONS] [DOMAIN_NAME]
24 Setup dependencies, apache, and gitweb. Then call build.rb.
25
26 Default DOMAIN_NAME is iankelling.org. Domain is expected to resolve
27 so we can get a let's encrypt cert.
28
29 This script depends on a few other git repos of mine: distro-functions
30 and basic-https-conf.
31
32
33 -p PORT Apache port, default is 443.
34 -h|--help Print help and exit.
35
36 Note: Uses GNU getopt options parsing style
37 EOF
38 exit $1
39 }
40
41 ##### begin command line parsing ########
42
43 port=443
44 temp=$(getopt -l help, p:h "$@") || usage 1
45 eval set -- "$temp"
46 while true; do
47 case $1 in
48 -p) port=$2; shift 2 ;;
49 -h|--help) usage ;;
50 --) shift; break ;;
51 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
52 esac
53 done
54
55 ##### end command line parsing ########
56
57 x="$(readlink -f "$BASH_SOURCE")"
58 script_dir=${x%/*}
59 cd $script_dir
60
61 domain=${1:-iankelling.org} # use argument for testing site
62 gitroot=/a/bin/githtml
63
64 if [[ $EUID != 0 ]]; then
65 s=sudo
66 fi
67
68 deb8=false
69 if grep -xFq 'VERSION_ID="8"' /etc/os-release; then
70 deb8=true
71 fi
72
73 shopt -s extglob
74
75 type -P a2enmod &>/dev/null || $s apt-get -y install apache2
76 type -P sqlite3 &>/dev/null || $s apt-get -y install sqlite3
77 type -P ffmpeg &>/dev/null || $s apt-get -y install ffmpeg
78
79
80 pkgs=(
81 # build.rb dependencies
82 ruby-pygments.rb ruby-safe-yaml ruby-sass
83 # python pkgs used for o(n^2) voting blog entry
84 python-bcrypt python-passlib
85 # gitweb pkgs
86 gitweb highlight
87 )
88 if $deb8; then
89 pkgs+=(build-essential ruby-dev)
90 else
91 pkgs+=(ruby-redcarpet)
92 fi
93
94 $s apt-get -y install ${pkgs[@]}
95 if $deb8; then
96 sudo gem install redcarpet
97 fi
98 chmod og+x _site/on2vote/vote.py
99
100
101 # debian has the package gitweb, which seems to mainly
102 # have some example apache config, and a minimal gitweb config.
103 # I'll just use the config as example and not use the package.
104 # It's example apache config seems to say we can use cgi or cgid,
105 # and googling cgid it seems a newer faster alternative. I also
106 # depend on this in my o(n^2) python script.
107 $s a2enmod cgid
108
109
110 # additional settings from browsing https://git-scm.com/docs/gitweb.conf
111 $s dd of=/etc/gitweb.conf <<EOF
112 \$feature{'highlight'}{'default'} = [1];
113 # highlighting doesn't work on files without extension.
114 # I noticed in terminal "highlight file" won't do it (unknown file type)
115 # hightlight < file will do it, and it's online documentation
116 # suggests it reads shebang. Todo: file a bug for gitweb
117 # to make highlight read shebangs.
118 our \$projectroot = "$gitroot";
119 # not documented at https://git-scm.com/docs/gitweb.conf,
120 # but it's in the debian conf, so use it.
121 # directory to use for temp files.
122 our \$git_temp = "/tmp";
123 push @git_base_url_list, "https://$domain/git";
124 our @extra_breadcrumbs = (
125 [ '$domain' => '/' ],
126 );
127 our \$home_link_str = 'git';
128 our \$site_footer = '$PWD/_site/gitweb-footer.html';
129 push @stylesheets, "/css/gitweb-site.css";
130 our \$favicon = '/assets/favicon.png';
131 # default is 25, cuts off descriptions.
132 our \$projects_list_description_width = 50;
133 # a bit superflous since they are all me
134 our \$omit_owner = true;
135 # highlight scripts with no extension, uses a patch
136 # that is on it's way upstream.
137 our \$highlight_force = 1;
138 our \$home_text = "$script_dir/_site/gitweb_home.html";
139 our \$projects_list_group_categories = 1;
140 EOF
141
142 web-conf -p $port - apache2 $domain <<EOF
143 # to run python script on my site:
144 <Directory /var/www/$domain/html/on2vote>
145 # to run python scripts with cgi
146 Options +ExecCGI
147 AddHandler cgi-script .py
148 </Directory>
149
150 <Directory "/var/www/$domain/html/cgi">
151 Options +ExecCGI
152 SetHandler cgi-script
153 </Directory>
154
155 # redirect some old paths when I was using jekyll.
156 Redirect permanent /10-14-2014/On2-vote-results.html /blog/on2-vote-results.html
157 Redirect permanent /09-29-2014/say-On2.html /blog/say-on2.html
158 Redirect permanent /08-07-2014/uninstalling-setup.html /blog/python-uninstall.html
159 Redirect permanent /08-01-2014/publising-my-technical-notes.html /blog/publishing-my-technical-notes.html
160
161 # All below is for gitweb + git-http-web.
162 # A simple builtin way to have a read only git website.
163 # I didn't find any significantly better alternatives out there.
164 SetEnv GIT_PROJECT_ROOT $gitroot
165 SetEnv GIT_HTTP_EXPORT_ALL
166
167 # note: cgi scripts can go anywhere into the filesystem,
168 # so there is no need to do a directory block for $gitroot
169
170 # fot git-http-web
171 <Directory /usr/lib/git-core>
172 AllowOverride None
173 Require all granted
174 </Directory>
175
176 <Directory /usr/share/gitweb>
177 Options +FollowSymLinks +ExecCGI
178 AddHandler cgi-script .cgi
179 </Directory>
180
181 # from man-git-http-backend, so git-http-web ang gitweb can both be used.
182 # it is instead of this:
183 # #ScriptAlias / /usr/lib/git-core/git-http-backend/
184 ScriptAliasMatch \\
185 "(?x)^/git/(.*/(HEAD | \\
186 info/refs | \\
187 objects/(info/[^/]+ | \\
188 [0-9a-f]{2}/[0-9a-f]{38} | \\
189 pack/pack-[0-9a-f]{40}\\.(pack|idx)) | \\
190 git-(upload|receive)-pack))\$" \\
191 /usr/lib/git-core/git-http-backend/\$1
192
193
194
195 # man-git-http-backend claims we should do this, but
196 # it causes no css/images to be displayed. Instead,
197 # just stick with the standard gitweb example directive
198 # from debian.
199 #ScriptAlias /git /usr/share/gitweb/gitweb.cgi/
200 Alias /git /usr/share/gitweb
201 EOF
202
203
204
205 $script_dir/gitweb-descriptions $gitroot
206
207 $script_dir/build.rb
208 $s rm -rf /var/www/$domain/html
209 $s ln -sT $script_dir/_site /var/www/$domain/html