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