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