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