back to optional directory fixing
[feisty_meow.git] / scripts / site_avenger / shared_site_mgr.sh
1 #!/bin/bash
2
3 # Author: Kevin Wentworth
4 # Author: Chris Koeritz
5
6 # This contains a bunch of reusable functions that help out in managing websites.
7
8 # This script is sourced, and relies on the value of WORKDIR, which should
9 # point at the directory where the site management scripts are stored,
10 # especially this one.
11
12 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
13
14 # get our configuration loaded, if we know the config file.
15 # if there is none, we will use our default version.
16 export SITE_MANAGEMENT_CONFIG_FILE
17 if [ -z "$SITE_MANAGEMENT_CONFIG_FILE" ]; then
18   SITE_MANAGEMENT_CONFIG_FILE="$WORKDIR/config/default.app"
19   echo "Site management config file was not set.  Using default:"
20   echo "  $SITE_MANAGEMENT_CONFIG_FILE"
21 fi
22
23 # load in at least the default version to get us moving.
24 source "$SITE_MANAGEMENT_CONFIG_FILE"
25 test_or_die "loading site management configuration from: $SITE_MANAGEMENT_CONFIG_FILE"
26
27 # configure feisty revision control to ignore vendor folders.
28 export NO_CHECKIN_VENDOR=true
29
30 # tests that the main storage folder for apps exists.
31 function check_application_dir()
32 {
33   local appdir="$1"; shift
34   if [ ! -d "$appdir" ]; then
35     echo "Creating the apps directory: $appdir"
36     mkdir "$appdir"
37     test_or_die "Making apps directory when not already present"
38   fi
39 }
40
41 # tries to find an appropriate config file for the application.
42 function locate_config_file()
43 {
44   local app_dirname="$1"; shift
45
46   local configfile="$WORKDIR/config/${app_dirname}.app"
47   echo "config file?: $configfile"
48   if [ ! -f "$configfile" ]; then
49     # this is not a good config file.  we can't auto-guess the config.
50     echo -e "
51 There is no specific site configuration file in:
52   $configfile
53 We will continue onward using the default and hope that this project follows
54 the standard pattern for cakephp projects."
55     # we'll pull in the default config file we set earlier; this will
56     # reinitialize some variables based on the app name.
57   else
58     # they gave us a valid config file.  let's try using it.
59     export SITE_MANAGEMENT_CONFIG_FILE="$configfile"
60   fi
61
62   # try to load the config.
63   source "$SITE_MANAGEMENT_CONFIG_FILE"
64   test_or_die "loading site management configuration from: $SITE_MANAGEMENT_CONFIG_FILE"
65
66 }
67
68 # this function will seek out top-level directories in the target directory passed in.
69 # if there is only one directory, then it is returned (in the app_dirname variable).
70 # otherwise, the user is asked which directory to use.
71 # important: this sets a global variable app_dirname to the application's directory name.
72 function find_app_folder()
73 {
74   local appsdir="$1"; shift
75
76   # throw away any prior value so no confusion arises.
77   unset app_dirname
78   
79   # count number of directories...  if exactly one, then choose it.
80   numdirs=$(count_directories "$appsdir")
81
82   if [ $numdirs -eq 0 ]; then
83     sep
84     echo "There are no directories in the application directory:"
85     echo "  $appsdir"
86     echo "Please create a directory for the site storage, based on the application"
87     echo "name that you want to work on.  Or you can just pass the directory name"
88     echo "on the command line, e.g.:"
89     echo "  $(basename $0) turtle"
90     sep
91     exit 1
92   elif [ $numdirs -eq 1 ]; then
93     app_dirname="$(basename $(find "$appsdir" -mindepth 1 -maxdepth 1 -type d) )"
94     test_or_die "Guessing application folder"
95   else
96     # if more than one folder, force user to choose.
97     # Reference: https://askubuntu.com/questions/1705/how-can-i-create-a-select-menu-in-a-shell-script
98     holdps3="$PS3"
99     PS3='Please pick a folder for site initialization: '
100     options=( $(find "$appsdir" -mindepth 1 -maxdepth 1 -type d -exec basename {} ';') "Quit")
101     select app_dirname in "${options[@]}"; do
102       case $app_dirname in
103         "Quit") echo ; echo "Quitting from the script."; exit 1; ;;
104         *) echo ; echo "You picked folder '$app_dirname'" ; break; ;;
105       esac
106     done
107     if [ -z "$app_dirname" ]; then
108       echo "The folder was not provided.  This script needs a directory name"
109       echo "within which to initialize the site."
110       exit 1
111     fi
112     PS3="$holdps3"
113   fi
114   test_app_folder "$appsdir" "$app_dirname"
115   test_or_die "Testing application folder: $app_dirname"
116
117   echo "Application folder is: $app_dirname"
118 }
119
120 # ensures that the app directory name is valid.
121 function test_app_folder()
122 {
123   local appsdir="$1"; shift
124   local dir="$1"; shift
125
126   local combo="$appsdir/$dir"
127
128   if [ ! -d "$combo" ]; then
129     echo "Creating app directory: $combo"
130     mkdir "$combo"
131     test_or_die "Making application directory when not already present"
132   fi
133
134   locate_config_file "$dir"
135 }
136
137 # eases some permissions to enable apache to write log files and do other shopkeeping.
138 function fix_site_perms()
139 {
140   local app_dir="$1"; shift
141
142   local site_dir="$app_dir/$CHECKOUT_DIR_NAME"
143
144   if [ -f "$site_dir/bin/cake" ]; then
145     chmod -R a+rx "$site_dir/bin/cake"
146     test_or_die "Enabling execute bit on cake binary"
147   fi
148
149   if [ -d "$site_dir/logs" ]; then
150     chmod -R g+w "$site_dir/logs"
151     test_or_die "Enabling group write on site's Logs directory"
152   fi
153
154   if [ -d "$site_dir/tmp" ]; then
155     chmod -R g+w "$site_dir/tmp"
156     test_or_die "Enabling group write on site's tmp directory"
157   fi
158 }
159
160 # tosses out any cached object data that originated from the database.
161 function clear_orm_cache()
162 {
163   local site_dir="$1"; shift
164
165   if [ -f "$site_dir/bin/cake" ]; then
166     # flush any cached objects from db.
167     "$site_dir/bin/cake" orm_cache clear
168     test_or_die "Clearing ORM cache"
169   fi
170 }
171
172 # updates the revision control repository passed in.  this expects that the
173 # repo will live in a folder called "checkout_dirname" under the app path,
174 # which is the standard for our deployed sites.
175 # important: this also sets a global variable called site_store_path to the full
176 # path of the application.
177 function update_repo()
178 {
179   local full_app_dir="$1"; shift
180   local checkout_dirname="$1"; shift
181   local repo_root="$1"; shift
182   local repo_name="$1"; shift
183
184 echo here are parms in update repo:
185 var full_app_dir checkout_dirname repo_root repo_name
186
187   # forget any prior value, since we are going to validate the path.
188   unset site_store_path
189
190   pushd "$full_app_dir" &>/dev/null
191   test_or_die "Switching to our app dir '$full_app_dir'"
192
193   local complete_path="$full_app_dir/$checkout_dirname"
194
195   # see if the checkout directory exits.  the repo_found variable is set to
196   # non-empty if we find it and it's a valid git repo.
197   repo_found=
198   if [ -d "$checkout_dirname" ]; then
199     # checkout directory exists, so let's check it.
200     pushd "$checkout_dirname" &>/dev/null
201     test_or_die "Switching to our checkout directory: $checkout_dirname"
202
203     # ask for repository name (without .git).
204     if git rev-parse --git-dir > /dev/null 2>&1; then
205       # this is a valid git repo.
206       repo_found=yes
207     fi
208  
209     # we don't consider the state of having the dir exist but the repo be wrong as good.
210     if [ -z "$repo_found" ]; then
211       echo "There is a problem; this folder is not a valid repository:"
212       echo "  $full_app_dir"
213       echo "This script cannot continue unless the git repository is valid."
214       exit 1
215     fi
216     popd &>/dev/null
217   fi
218
219   if [ ! -z "$repo_found" ]; then
220     # a repository was found, so update the version here and leave.
221     echo "Repository $repo_name exists.  Updating it."
222     rgetem
223     test_or_die "Recursive checkout on: $complete_path"
224   else
225     # clone the repo since it wasn't found.
226     echo "Cloning repository $repo_name now."
227     git clone "$repo_root/$repo_name.git" $checkout_dirname
228     test_or_die "Git clone of repository: $repo_name"
229   fi
230
231 #not doing this here since powerup uses this and has no sudo.
232   #fix_site_perms "$complete_path"
233
234 #unused?
235   # construct the full path to where the app will actually live.
236   site_store_path="$complete_path"
237
238   popd &>/dev/null
239 }
240
241 # this function goes to the directory specified and makes it right with
242 # composer install.  this is as opposed to composer update, which could
243 # change the state. 
244 function composer_repuff()
245 {
246   local site_store_path="$1"; shift
247
248   pushd "$site_store_path" &>/dev/null
249   test_or_die "Switching to our app dir '$site_store_path'"
250
251   echo "Updating site with composer..."
252
253   composer -n install
254   test_or_die "Composer installation step on '$site_store_path'."
255   echo "Site updated."
256
257 #hmmm: argh global
258   dir="$site_store_path/$CHECKOUT_DIR_NAME/vendor/siteavenger/avcore"
259   if [ -d "$dir" ]; then
260     echo "Running avcore database migrations..."
261     logfile="$TMP/problem-avcore_db_migration-$(date_stringer).log"
262     ./bin/cake migrations migrate -p Avcore &>"$logfile"
263     if [ $? -ne 0 ]; then
264       echo "** FAILED: Database migrations for avcore.  Check log file in: $logfile"
265       # we keep going, because some sites aren't ready for this yet.
266     else
267       \rm "$logfile"
268       echo "Database for avcore migrated."
269     fi
270   fi
271
272   clear_orm_cache
273
274   popd &>/dev/null
275 }
276
277 # this function creates the links needed to make the site function properly given our
278 # various dependencies and infrastructure.
279 function create_site_links()
280 {
281   local site_store_path="$1"; shift
282   local theme_name="$1"; shift
283
284   echo "Creating symbolic links for site assets..."
285
286   # jump into the site path so we can start making relative links.
287   pushd "$site_store_path" &>/dev/null
288   test_or_die "Switching to our app dir '$site_store_path'"
289
290   pushd webroot &>/dev/null
291
292   # remove all symlinks that might plague us.
293   find . -maxdepth 1 -type l -exec rm -f {} ';'
294   test_or_die "Cleaning out links in webroot"
295
296   # link in the avcore plugin.
297   make_safe_link "../vendor/siteavenger/avcore/webroot" avcore
298
299   # make the link for our theme as a lower-case version of the theme.
300   themelower=${theme_name,,}
301   make_safe_link "../plugins/$theme_name/webroot" "$themelower"
302
303   # link in any favicon files.
304   if [ -d "../plugins/$theme_name/favicon" ]; then
305     local fave
306     for fave in "../plugins/$theme_name/favicon"/*; do
307       make_safe_link "$fave" .
308     done
309   fi
310
311   # get back out of webroot.
312   popd &>/dev/null
313
314   # hop up a level above where we had been.
315   pushd .. &>/dev/null
316
317   # link 'public' to webroot.
318   if [ -L public ]; then
319     # public is a symlink.
320     \rm public
321     test_or_die "Removing public directory symlink"
322   elif [ -d public ]; then
323     # public is a folder with default files.
324 #hmmm: is that safe?
325     \rm -rf public
326     test_or_die "Removing public directory and contents"
327   fi
328
329   # create the main 'public' symlink
330 #hmmm: argh global
331   make_safe_link $CHECKOUT_DIR_NAME/webroot public
332   test_or_die "Creating link to webroot called 'public'"
333
334 #hmmm: public/$themelower/im will be created automatically by system user with appropriate permissions
335
336   echo Created symbolic links.
337
338   popd &>/dev/null
339   popd &>/dev/null
340 }
341
342 # fetches composer to make sure it's up to date.
343 # (if powerup runs, composer install doesn't update git origin.)
344 function update_composer_repository()
345 {
346   local site_store_path="$1"; shift
347
348   pushd "$site_store_path" &>/dev/null
349
350   if git config remote.composer.url &>/dev/null; then
351     git pull composer
352     echo "Updated the composer repository."
353   else
354     echo "No composer repository was found for updating."
355   fi
356 }
357
358 # fixes the ownership for a site avenger or php application.
359 # this almost certainly will require sudo capability, if there are any ownership problems
360 # that need to be resolved.
361 function fix_appdir_ownership()
362 {
363   local appsdir="$1"; shift
364   local dir="$1"; shift
365
366   local combo="$appsdir/$dir"
367
368   # go with the default user running the script.
369   user_name="$USER"
370   if [ ! -z "$user_name" -a "$user_name" != "root" ]; then
371     echo "Chowning the app folder to be owned by: $user_name"
372 #hmmm: have to hope for now for standard group named after user 
373     chown -R "$user_name:$user_name" "$combo"
374     test_or_die "Chowning $combo to be owned by $user_name"
375   else
376     echo "user name failed checks for chowning, was found as '$user_name'"
377   fi
378
379   # 
380 #probably not enough for path!
381   fix_site_perms "$combo"
382 }
383
384