updated to add new FEISTY_MEOW_APPS_DIR
[feisty_meow.git] / scripts / site_avenger / revamp_cakelampvm_v002.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 if [[ ! $(hostname) == *cakelampvm* ]]; then
13   echo "This script is only designed to be run on the cakelampvm host."
14   exit 1
15 fi
16
17 ##############
18
19 export THISDIR="$( \cd "$(\dirname "$0")" && \pwd )"  # obtain the script's working directory.
20 export FEISTY_MEOW_APEX="$( \cd "$THISDIR/../.." && \pwd )"
21
22 export NO_HELLO=right
23 source "$FEISTY_MEOW_APEX/scripts/core/launch_feisty_meow.sh"
24 # load dependencies for our script.
25 source "$FEISTY_MEOW_SCRIPTS/system/common_sysadmin.sh"
26 source "$FEISTY_MEOW_SCRIPTS/security/password_functions.sh"
27
28 ##############
29
30 # it's a requirement to have sql root password, since we may need some sql db configuration.
31 load_password /etc/mysql/secret_password mysql_passwd
32 if [ -z "$mysql_passwd" ]; then
33   read_password "Please enter the MySQL root account password:" mysql_passwd
34 fi
35 if [ -z "$mysql_passwd" ]; then
36   echo "This script must have the sql root password to proceed."
37   exit 1
38 else
39   store_password /etc/mysql/secret_password "$mysql_passwd"
40 fi
41
42 ##############
43
44 sep
45
46 echo "Regenerating feisty meow loading dock."
47
48 regenerate
49 exit_on_error "regenerating feisty meow configuration"
50 chown -R "$(fm_username)":"$(fm_username)" /home/$(fm_username)/.[a-zA-Z0-9]*
51 exit_on_error "fix after reconfigured as sudo"
52
53 ##############
54
55 # set up some crucial users in the mysql db that we seem to have missed previously.
56
57 sep
58
59 echo "Adding users to the mysql database."
60
61 #hmmm: good application for hiding output unless error here.
62 mysql -u root -p"$mysql_passwd" &>/dev/null <<EOF
63   create user if not exists 'root'@'%' IDENTIFIED BY '$mysql_passwd';
64   grant all privileges on *.* TO 'root'@'%' with grant option;
65
66   create user if not exists 'wampcake'@'%' IDENTIFIED BY 'bakecamp';
67   grant all privileges on *.* TO 'wampcake'@'%' with grant option;
68
69   create user if not exists 'lampcake'@'%' IDENTIFIED BY 'bakecamp';
70   grant all privileges on *.* TO 'lampcake'@'%' with grant option;
71 EOF
72 exit_on_error "configuring root, wampcake and lampcake users on mysql"
73
74 ##############
75
76 sep
77
78 echo "Making some important permission changes..."
79
80 ##############
81
82 # fix up the main web storage.
83 chown -R www-data:www-data /var/www 
84 exit_on_error "chown www-data"
85 group_perm /var/www 
86 exit_on_error "group_perm www-data"
87
88 ##############
89
90 # set up access on some important folders for the developer user.
91 chown -R developer:developer /home/developer /home/developer/.[a-zA-Z0-9]*
92 exit_on_error "chown developer home"
93 harsh_perm /home/developer/.ssh
94 exit_on_error "harsh_perm setting on developer .ssh"
95
96
97 ##############
98
99 # give the developer control over the apache and bind config files, as well
100 # as giving the user ownership of the local feisty meow repository.
101 chown -R developer:developer /etc/apache2 /etc/bind 
102 exit_on_error "chown apache2 and bind to developer"
103 group_perm /etc/apache2 /etc/bind 
104 exit_on_error "group perms on apache2 and bind"
105 chown -R developer:developer /opt/feistymeow.org 
106 exit_on_error "chown feisty meow to developer"
107 group_perm /opt/feistymeow.org 
108 exit_on_error "group perms on feisty meow"
109
110 ##############
111
112 # fix perms for fred user.
113 chown -R fred:fred /home/fred /home/archives/stuffing /home/fred/.[a-zA-Z0-9]*
114 exit_on_error "chown fred home"
115 if [ -d "$FEISTY_MEOW_APPS_DIR" ]; then
116   group_perm $FEISTY_MEOW_APPS_DIR
117   exit_on_error "group perms on fred's apps"
118 fi
119 harsh_perm /home/fred/.ssh
120 exit_on_error "harsh_perm setting on fred .ssh"
121 group_perm /home/fred/apps/mapsdemo
122 exit_on_error "group perms on mapsdemo app"
123
124 echo "...done with permission changes."
125
126 ##############
127 #
128 # some slightly tricky bits start here.  we want to massage the vm into the
129 # best possible shape without needing to re-release it.
130 #
131 ##############
132
133 sep
134
135 echo "Updating developer welcome file."
136
137 # only update hello if they've still got the file there.  we don't want to
138 # keep forcing our hellos at people.
139 if [ -f "$HOME/hello.txt" ]; then
140   # copy the most recent hello file into place for the user.
141   \cp -f "$FEISTY_MEOW_APEX/production/sites/cakelampvm.com/hello.txt" "$HOME"
142   continue_on_error "copying hello file for user"
143 fi
144
145 ##############
146
147 # deploy any site updates here to the VM's cakelampvm.com site.
148 #
149 # we want to upgrade the default apache site to the latest, since the new
150 # version mirrors the one on the internet (but with green checks instead
151 # of red X's) and since we also support https on the new default version.
152 # we can do this again later if needed, by upping the numbers on the apache
153 # site config files.  our original site was 000 and the new version is 001,
154 # which we've done as a prefix on the config for some reason.  makes the
155 # code below easy at least.
156 if [ -L /etc/apache2/sites-enabled/000-default.conf ]; then
157
158   sep
159
160   # the old site is in place still, so let's update that.
161   echo "Updating default web sites to latest version."
162
163   a2enmod ssl
164   exit_on_error "enabling SSL for secure websites"
165
166   restart_apache
167   exit_on_error "getting SSL loaded in apache"
168
169   a2dissite 000-default
170   exit_on_error "disabling old apache site"
171
172   rm -f /etc/apache2/sites-available/000-default.conf 
173   exit_on_error "removing old apache site"
174
175   # copy in our new version of the default page.
176 #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.
177   \cp -f $FEISTY_MEOW_APEX/production/sites/cakelampvm.com/rolling/default_page.001/* \
178       /etc/apache2/sites-available
179   exit_on_error "installing new apache default sites"
180
181   # there should only be ours at this version level and with that prefix.
182   a2ensite 001-*
183   exit_on_error "enabling new apache default sites"
184
185   restart_apache
186 fi
187
188 ##############
189
190 # fix up the apache site so that HSTS is disabled.  otherwise we can't view
191 # the https site for cakelampvm.com once the domain name switch has occurred.
192
193 sep
194
195 # we operate only on our own specialized tls conf file.  hopefully no one has messed with it besides revamp.
196 # note the use of the character class :blank: below to match spaces or tabs.
197 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
198 if [ $? -ne 0 ]; then
199   echo the apache tls-enabling.conf file seems to have already been patched to disable strict transport security. 
200 else
201   restart_apache
202   echo successfully patched the apache tls-enabling.conf file to disable strict transport security. 
203 fi
204
205 ##############
206
207 # fix up bind so that we think of any address with cakelampvm.com on the end
208 # as being on the vm.  this is already true for some specific sites, but we
209 # want the wildcard enabled to ease the use of DNS for windows folks.
210
211 sep
212
213 grep -q "\*[[:blank:]]*IN A[[:blank:]]*10.28.42.20" /etc/bind/cakelampvm.com.conf
214 if [ $? -eq 0 ]; then
215   # already present.
216   echo the bind settings for wildcard domains off of cakelampvm.com seems to already be present. 
217 else
218   echo "
219
220
221 ;;;;;;
222
223 ; our bind magic, a wildcard domain, for all other sites with cakelampvm.com
224 ; in the domain.  this forces any other sites besides the ones above to route
225 ; to the actual vm IP address, which currently is singular and very fixated.
226 *                               IN A            10.28.42.20
227                                 IN HINFO        \"linux vm\" \"ubuntu\"
228
229 ;;;;;;
230
231
232
233 " >> /etc/bind/cakelampvm.com.conf
234   restart_bind
235   echo "successfully added wildcard domains to the cakelampvm.com bind configuration."
236 fi
237
238 ##############
239
240 # fix samba configuration for screwy default of read-only in user homes.
241 # why cripple a necessary feature by default?
242
243 sep
244
245 pattern="[#;][[:blank:]]*read only = yes"
246 replacement="read only = no"
247
248 # we just always do the replacement now rather than making it conditional,
249 # after realizing the sentinel pattern was actually already in the file...
250 # too much subtlety can get one into trouble.
251 sed -i "0,/$pattern/{s/$pattern/$replacement/}" /etc/samba/smb.conf
252 exit_on_error "patching samba configuration to enable write acccess on user home dirs"
253 echo successfully patched the samba configuration to enable writes on user home directories. 
254
255 # add in a disabling of the archive bit mapping feature, which hoses up the execute bit
256 # in an attempt to save the sad old DOS archive bit across the samba connection.
257 grep -q "map archive" /etc/samba/smb.conf
258 # if the phrase wasn't found, we need to add it.
259 if [ $? -ne 0 ]; then
260   sed -i "s/\[global\]/\[global\]\n\nmap archive = no/" /etc/samba/smb.conf
261   exit_on_error "patching samba configuration to turn off archive bit mapping feature"
262   echo Successfully fixed Samba to not use the archive bit mapping feature.
263 fi
264
265 # sweet, looks like that worked...
266 restart_samba
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 exit_on_error "copying environment variables file into place"
281
282 # enable the new version of the config file.
283 a2enconf env_vars_cakelampvm
284 exit_on_error "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 # we will only add swap now if explicitly asked for it.  this is to avoid creating
295 # a swap file where the vm is running on an SSD, since that can use up the SSD's lifespan
296 # too quickly.
297 if [ ! -z "$ADD_SWAP" ]; then
298   echo "Checking existing swap partition configuration.
299 "
300
301   # check for existing swap.
302   free | grep -q "Swap:[[:blank:]]*[1-9][0-9]"
303   if [ $? -ne 0 ]; then
304     # no swap in current session, so add it.
305     echo "Enabling ramdisk swap partition...
306 "
307     add_swap_mount
308     echo "
309 Enabled ramdisk swap partition for current boot session."
310   fi
311
312   # the above just gives this session a swap partition, but we want to have
313   # the vm boot with one also.
314
315   # check if there is already swap mentioned in the root crontab.  we will get root's
316   # crontab below since this script has to run as sudo.
317   crontab -l | grep -iq add_swap_mount
318   if [ $? -ne 0 ]; then
319     # no existing swap setup in crontab, so add it.
320     echo "
321 Adding a boot-time ramdisk swap partition...
322 "
323     # need to do it carefully, since sed won't add lines to a null file.  we thus
324     # create a temporary file to do our work in and ignore sed as a tool for this.
325     tmpfile="$(mktemp junk.XXXXXX)"
326     crontab -l 2>/dev/null >"$tmpfile"
327     echo "
328 # need to explicitly set any variables we will use.
329 FEISTY_MEOW_APEX=${FEISTY_MEOW_APEX}
330 # add swap space to increase memory available.
331 @reboot bash $FEISTY_MEOW_APEX/scripts/system/add_swap_mount.sh
332 " >>"$tmpfile"
333     # now install our new version of the crontab.
334     crontab "$tmpfile"
335     rm "$tmpfile"
336
337     echo "
338 Added boot-time ramdisk swap partition to crontab for root."
339   fi
340 fi
341
342 ##############
343
344 sep
345
346 echo Adding site avenger packages to composer.
347 # add in site avenger dependencies so we can build avcore properly.
348 pushd ~ &>/dev/null
349 sudo -u $(fm_username) composer config -g repositories.siteavenger composer https://packages.siteavenger.com/
350 popd &>/dev/null
351
352 ##############
353
354 # make the apache umask set group permissions automatically, so we stop having weird
355 # permission issues on temp dirs.
356
357 sep
358
359 grep -q "umask" /etc/apache2/envvars
360 if [ $? -eq 0 ]; then
361   # already present.
362   echo the umask configuration for apache already appears to be set.
363 else
364   echo "
365
366 # set umask to enable group read/write on files and directories.
367 umask 002
368
369 " >> /etc/apache2/envvars
370   restart_apache
371   echo "successfully changed apache umask configuration to enable group read/write"
372 fi
373
374 ##############
375 ##############
376
377 # sequel--tell them they're great and show the hello again also.
378
379 sep
380
381 regenerate
382 exit_on_error "regenerating feisty meow scripts"
383 chown -R "$(fm_username)":"$(fm_username)" /home/$(fm_username)/.[a-zA-Z0-9]*
384 exit_on_error "fix after regenerate as sudo"
385 echo "
386
387
388 Thanks for revamping your cakelampvm.  :-)
389
390 You may want to update your current shell's feisty meow environment by typing:
391   regenerate
392 "
393
394 ##############
395
396