3 # Author: Kevin Wentworth
4 # Author: Chris Koeritz
6 # This contains a bunch of reusable functions that help out in managing websites.
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.
12 source "$FEISTY_MEOW_SCRIPTS/core/launch_feisty_meow.sh"
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"
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"
27 # configure feisty revision control to ignore vendor folders.
28 export NO_CHECKIN_VENDOR=true
30 # tests that the main storage folder for apps exists.
31 function check_application_dir()
33 local appdir="$1"; shift
34 if [ ! -d "$appdir" ]; then
35 echo "Creating the apps directory: $appdir"
37 test_or_die "Making apps directory when not already present"
41 # tries to find an appropriate config file for the application.
42 function locate_config_file()
44 local app_dirname="$1"; shift
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.
51 There is no specific site configuration file in:
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.
58 # they gave us a valid config file. let's try using it.
59 export SITE_MANAGEMENT_CONFIG_FILE="$configfile"
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"
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()
74 local appsdir="$1"; shift
76 # throw away any prior value so no confusion arises.
79 # count number of directories... if exactly one, then choose it.
80 numdirs=$(count_directories "$appsdir")
82 if [ $numdirs -eq 0 ]; then
84 echo "There are no directories in the application directory:"
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"
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"
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
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
103 "Quit") echo ; echo "Quitting from the script."; exit 1; ;;
104 *) echo ; echo "You picked folder '$app_dirname'" ; break; ;;
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."
114 test_app_folder "$appsdir" "$app_dirname"
115 test_or_die "Testing application folder: $app_dirname"
117 echo "Application folder is: $app_dirname"
120 # ensures that the app directory name is valid.
121 function test_app_folder()
123 local appsdir="$1"; shift
124 local dir="$1"; shift
126 local combo="$appsdir/$dir"
128 if [ ! -d "$combo" ]; then
129 echo "Creating app directory: $combo"
131 test_or_die "Making application directory when not already present"
134 locate_config_file "$dir"
137 # eases some permissions to enable apache to write log files and do other shopkeeping.
138 function fix_site_perms()
140 local app_dir="$1"; shift
142 local site_dir="$app_dir/$CHECKOUT_DIR_NAME"
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"
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"
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"
160 # tosses out any cached object data that originated from the database.
161 function clear_orm_cache()
163 local site_dir="$1"; shift
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"
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()
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
184 echo here are parms in update repo:
185 var full_app_dir checkout_dirname repo_root repo_name
187 # forget any prior value, since we are going to validate the path.
188 unset site_store_path
190 pushd "$full_app_dir" &>/dev/null
191 test_or_die "Switching to our app dir '$full_app_dir'"
193 local complete_path="$full_app_dir/$checkout_dirname"
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.
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"
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.
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."
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."
223 test_or_die "Recursive checkout on: $complete_path"
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"
231 #not doing this here since powerup uses this and has no sudo.
232 #fix_site_perms "$complete_path"
235 # construct the full path to where the app will actually live.
236 site_store_path="$complete_path"
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
244 function composer_repuff()
246 local site_store_path="$1"; shift
248 pushd "$site_store_path" &>/dev/null
249 test_or_die "Switching to our app dir '$site_store_path'"
251 echo "Updating site with composer..."
254 test_or_die "Composer installation step on '$site_store_path'."
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.
268 echo "Database for avcore migrated."
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()
281 local site_store_path="$1"; shift
282 local theme_name="$1"; shift
284 echo "Creating symbolic links for site assets..."
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'"
290 pushd webroot &>/dev/null
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"
296 # link in the avcore plugin.
297 make_safe_link "../vendor/siteavenger/avcore/webroot" avcore
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"
303 # link in any favicon files.
304 if [ -d "../plugins/$theme_name/favicon" ]; then
306 for fave in "../plugins/$theme_name/favicon"/*; do
307 make_safe_link "$fave" .
311 # get back out of webroot.
314 # hop up a level above where we had been.
317 # link 'public' to webroot.
318 if [ -L public ]; then
319 # public is a symlink.
321 test_or_die "Removing public directory symlink"
322 elif [ -d public ]; then
323 # public is a folder with default files.
326 test_or_die "Removing public directory and contents"
329 # create the main 'public' symlink
331 make_safe_link $CHECKOUT_DIR_NAME/webroot public
332 test_or_die "Creating link to webroot called 'public'"
334 #hmmm: public/$themelower/im will be created automatically by system user with appropriate permissions
336 echo Created symbolic links.
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()
346 local site_store_path="$1"; shift
348 pushd "$site_store_path" &>/dev/null
350 if git config remote.composer.url &>/dev/null; then
352 echo "Updated the composer repository."
354 echo "No composer repository was found for updating."
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()
363 local appsdir="$1"; shift
364 local dir="$1"; shift
366 local combo="$appsdir/$dir"
368 # go with the default user running the script.
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"
376 echo "user name failed checks for chowning, was found as '$user_name'"
380 #probably not enough for path!
381 fix_site_perms "$combo"