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