Merge branch 'master' of feistymeow.org:feisty_meow
[feisty_meow.git] / scripts / site_avenger / revamp_cakelampvm.sh
1 #!/bin/bash
2
3 # fixes the cakelampvm permissions according to the way.
4
5 ##############
6
7 if [[ $EUID != 0 ]]; then
8   echo "This script must be run as root or sudo."
9   exit 1
10 fi
11
12 ##############
13
14 export WORKDIR="$( \cd "$(\dirname "$0")" && \pwd )"  # obtain the script's working directory.
15 export FEISTY_MEOW_APEX="$( \cd "$WORKDIR/../.." && \pwd )"
16
17 export NO_HELLO=right
18 source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh"
19 source "$FEISTY_MEOW_SCRIPTS/system/common_sysadmin.sh"
20
21 ##############
22
23 # new requirement is to get the sql root password, since we need to do some sql db configuration.
24 echo -n "Please enter the MySQL root account password: "
25 # turn off echo but remember former setting.
26 stty_orig=`stty -g`
27 stty -echo
28 read mysql_passwd
29 # turn echo back on.
30 stty $stty_orig
31 if [ -z "$mysql_passwd" ]; then
32   echo "This script must have the sql root password to proceed."
33   exit 1
34 fi
35
36 ##############
37
38 sep
39
40 echo "Regenerating feisty meow loading dock."
41
42 reconfigure_feisty_meow
43 test_or_die "feisty meow reconfiguration"
44 chown -R "$(logname)":"$(logname)" /home/$(logname)/.[a-zA-Z0-9]*
45 test_or_die "fix after reconfigured as sudo"
46
47 ##############
48
49 # set up some crucial users in the mysql db that we seem to have missed previously.
50
51 sep
52
53 echo "Adding users to the mysql database."
54
55 #hmmm: good application for hiding output unless error here.
56 mysql -u root -p"$mysql_passwd" &>/dev/null <<EOF
57   create user if not exists 'root'@'%' IDENTIFIED BY '$mysql_passwd';
58   grant all privileges on *.* TO 'root'@'%' with grant option;
59
60   create user if not exists 'wampcake'@'%' IDENTIFIED BY 'bakecamp';
61   grant all privileges on *.* TO 'wampcake'@'%' with grant option;
62
63   create user if not exists 'lampcake'@'%' IDENTIFIED BY 'bakecamp';
64   grant all privileges on *.* TO 'lampcake'@'%' with grant option;
65 EOF
66 test_or_die "configuring root, wampcake and lampcake users on mysql"
67
68 ##############
69
70 sep
71
72 echo "Making some important permission changes..."
73
74 # fix up the main web storage.
75 chown -R www-data:www-data /var/www 
76 test_or_die "chown www-data"
77 group_perm /var/www 
78 test_or_die "group_perm www-data"
79
80 ##############
81
82 # set up access on some important folders for the developer user.
83 chown -R developer:developer /home/developer /home/developer/.[a-zA-Z0-9]*
84 test_or_die "chown developer home"
85 harsh_perm /home/developer/.ssh
86 test_or_die "harsh_perm setting on developer .ssh"
87 chown -R developer:developer /etc/apache2 /etc/bind 
88 test_or_die "chown apache2 and bind to developer"
89 group_perm /etc/apache2 /etc/bind 
90 test_or_die "group perms on apache2 and bind"
91
92 ##############
93
94 # fix perms for fred user.
95 chown -R fred:fred /home/fred /home/archives/stuffing /home/fred/.[a-zA-Z0-9]*
96 test_or_die "chown fred home"
97 group_perm $HOME/apps
98 test_or_die "group perms on fred's apps"
99 harsh_perm /home/fred/.ssh
100 test_or_die "harsh_perm setting on fred .ssh"
101 chown -R fred:fred /opt/feistymeow.org 
102 test_or_die "chown feisty meow to fred"
103 group_perm /opt/feistymeow.org 
104 test_or_die "group perms on feisty meow"
105 group_perm /home/fred/apps/mapsdemo
106 test_or_die "group perms on mapsdemo app"
107
108 echo "Done with important permission changes."
109
110 ##############
111 #
112 # some slightly tricky bits start here.  we want to massage the vm into the
113 # best possible shape without needing to re-release it.
114 #
115 ##############
116
117 sep
118
119 echo "Updating developer welcome file."
120
121 # only update hello if they've still got the file there.  we don't want to
122 # keep forcing our hellos at people.
123 if [ -f "$HOME/hello.txt" ]; then
124   # copy the most recent hello file into place for the user.
125   \cp -f "$FEISTY_MEOW_APEX/production/sites/cakelampvm.com/hello.txt" "$HOME"
126   test_or_continue "copying hello file for user"
127 fi
128
129 ##############
130
131 # install a better editor app.
132
133 sep
134
135 echo "The script is about to install the bluefish editor and some dependencies.
136 If the app is not already installed, then this process takes about one minute
137 on a slow home DSL internet connection..."
138
139 apt-get install -y bluefish &> "/tmp/install_bluefish-$(logname).log"
140 test_or_continue "installing bluefish editor"
141
142 ##############
143
144 # deploy any site updates here to the VM's cakelampvm.com site.
145 #
146 # we want to upgrade the default apache site to the latest, since the new
147 # version mirrors the one on the internet (but with green checks instead
148 # of red X's) and since we also support https on the new default version.
149 # we can do this again later if needed, by upping the numbers on the apache
150 # site config files.  our original site was 000 and the new version is 001,
151 # which we've done as a prefix on the config for some reason.  makes the
152 # code below easy at least.
153 if [ -L /etc/apache2/sites-enabled/000-default.conf ]; then
154
155   sep
156
157   # the old site is in place still, so let's update that.
158   echo "Updating default web sites to latest version."
159
160   a2enmod ssl
161   test_or_die "enabling SSL for secure websites"
162
163   restart_apache
164   test_or_die "getting SSL loaded in apache"
165
166   a2dissite 000-default
167   test_or_die "disabling old apache site"
168
169   rm -f /etc/apache2/sites-available/000-default.conf 
170   test_or_die "removing old apache site"
171
172   # copy in our new version of the default page.
173 #hmmm: would be nice if this worked without mods for any new version, besides just 001.  see apache env var file below for example implem.
174   \cp -f $FEISTY_MEOW_APEX/production/sites/cakelampvm.com/rolling/default_page.001/* \
175       /etc/apache2/sites-available
176   test_or_die "installing new apache default sites"
177
178   # there should only be ours at this version level and with that prefix.
179   a2ensite 001-*
180   test_or_die "enabling new apache default sites"
181
182   restart_apache
183 fi
184
185 ##############
186
187 # fix up the apache site so that HSTS is disabled.  otherwise we can't view
188 # the https site for cakelampvm.com once the domain name switch has occurred.
189
190 sep
191
192 # we operate only on our own specialized tls conf file.  hopefully no one has messed with it besides revamp.
193 # note the use of the character class :blank: below to match spaces or tabs.
194 search_replace "^[[:blank:]]*Header always set Strict-Transport-Security.*" "# not good for cakelampvm.com -- Header always set Strict-Transport-Security \"max-age=63072000; includeSubdomains;\"" /etc/apache2/conf-library/tls-enabling.conf
195 if [ $? -ne 0 ]; then
196   echo the apache tls-enabling.conf file seems to have already been patched to disable strict transport security. 
197 else
198   restart_apache
199   echo successfully patched the apache tls-enabling.conf file to disable strict transport security. 
200 fi
201
202 ##############
203
204 # fix up bind so that we think of any address with cakelampvm.com on the end
205 # as being on the vm.  this is already true for some specific sites, but we
206 # want the wildcard enabled to ease the use of DNS for windows folks.
207
208 sep
209
210 grep -q "\*[[:blank:]]*IN A[[:blank:]]*10.28.42.20" /etc/bind/cakelampvm.com.conf
211 if [ $? -eq 0 ]; then
212   # already present.
213   echo the bind settings for wildcard domains off of cakelampvm.com seems to already be present. 
214 else
215   echo "
216
217
218 ;;;;;;
219
220 ; our bind magic, a wildcard domain, for all other sites with cakelampvm.com
221 ; in the domain.  this forces any other sites besides the ones above to route
222 ; to the actual vm IP address, which currently is singular and very fixated.
223 *                               IN A            10.28.42.20
224                                 IN HINFO        \"linux vm\" \"ubuntu\"
225
226 ;;;;;;
227
228
229
230 " >> /etc/bind/cakelampvm.com.conf
231   restart_bind
232   echo "successfully added wildcard domains to the cakelampvm.com bind configuration."
233 fi
234
235 ##############
236
237 # fix samba configuration for (ass-headed) default of read-only in user homes.
238 # why cripple a necessary feature by default?
239
240 sep
241
242 pattern="[#;][[:blank:]]*read only = yes"
243 replacement="read only = no"
244
245 # we just always do the replacement now, after realizing the sentinel pattern
246 # was acutally already in the file...  too much subtlety can get one into trouble.
247 sed -i "0,/$pattern/{s/$pattern/$replacement/}" /etc/samba/smb.conf
248 test_or_die "patching samba configuration to enable write acccess on user home dirs"
249 # sweet, looks like that worked...
250 restart_samba
251 echo successfully patched the samba configuration to enable writes on user home directories. 
252
253 ##############
254
255 # add the latest version of the cakelampvm environment variables for apache.
256
257 sep
258
259 # drop existing file, if already configured.  ignore errors.
260 a2disconf env_vars_cakelampvm &>/dev/null
261
262 # plug in the new version, just stomping anything there.
263 # note: we only expect to have one version of the env_vars dir at a time in place in feisty...
264 \cp -f $FEISTY_MEOW_APEX/production/sites/cakelampvm.com/rolling/env_vars.*/env_vars_cakelampvm.conf /etc/apache2/conf-available
265 test_or_die "copying environment variables file into place"
266
267 # enable the new version of the config file.
268 a2enconf env_vars_cakelampvm
269 test_or_die "enabling the new cakelampvm environment config for apache"
270
271 echo Successfully configured the apache2 environment variables needed for cakelampvm.
272
273 ##############
274
275 # add in a swap mount if not already configured.
276
277 sep
278
279 echo "Checking existing swap partition configuration.
280 "
281
282 # check for existing swap.
283 free | grep -q "Swap:[[:blank:]]*[1-9][0-9]"
284 if [ $? -ne 0 ]; then
285   # no swap in current session, so add it.
286   echo "Enabling ramdisk swap partition...
287 "
288   add_swap_mount
289   echo "
290 Enabled ramdisk swap partition for current boot session."
291 fi
292
293 # the above just gives this session a swap partition, but we want to have
294 # the vm boot with one also.
295
296 # check if there is already swap mentioned in the root crontab.  we will get root's
297 # crontab below since this script has to run as sudo.
298 crontab -l | grep -iq add_swap_mount
299 if [ $? -ne 0 ]; then
300   # no existing swap setup in crontab, so add it.
301   echo "
302 Adding a boot-time ramdisk swap partition...
303 "
304   # need to do it carefully, since sed won't add lines to a null file.  we thus
305   # create a temporary file to do our work in and ignore sed as a tool for this.
306   tmpfile="$(mktemp junk.XXXXXX)"
307   crontab -l 2>/dev/null >"$tmpfile"
308   echo "
309 # need to explicitly set any variables we will use.
310 FEISTY_MEOW_APEX=${FEISTY_MEOW_APEX}
311 # add swap space to increase memory available.
312 @reboot bash $FEISTY_MEOW_APEX/scripts/system/add_swap_mount.sh
313 " >>"$tmpfile"
314   # now install our new version of the crontab.
315   crontab "$tmpfile"
316   rm "$tmpfile"
317
318   echo "
319 Added boot-time ramdisk swap partition to crontab for root."
320 fi
321
322 ##############
323
324 sep
325
326 # repair the google config key for mapsdemo, which lives over in fred's account.
327 search_replace \
328   "^[[:blank:]]*'key'[[:blank:]]*=>[[:blank:]]*'AIzaSyCd2kfOBf8tyd-_m2aM_ayMoAq8r_M1yUk'," \
329   "\t'key' => 'AIzaSyCvKs62XudEWPZpA21rCGI1Dkh6UxSQzzQ'," \
330   ~fred/apps/mapsdemo/avenger5/config/config_google.php
331
332 if [ $? -ne 0 ]; then
333   echo Google config key for mapsdemo in fred account seems to already be patched.
334 else
335   echo Patched the google config key for mapsdemo app in fred account.
336 fi
337
338 ##############
339 ##############
340
341 # sequel--tell them they're great and show the hello again also.
342
343 sep
344
345 regenerate
346 test_or_die "regenerating feisty meow scripts"
347 chown -R "$(logname)":"$(logname)" /home/$(logname)/.[a-zA-Z0-9]*
348 test_or_die "fix after regenerate as sudo"
349 echo "
350
351
352 Thanks for revamping your cakelampvm.  :-)
353
354 You may want to update your current shell's feisty meow environment by typing:
355   reconfigure_feisty_meow
356 "
357
358 ##############
359
360