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.
15 export SITE_MANAGEMENT_CONFIG_FILE
16 if [ -z "$SITE_MANAGEMENT_CONFIG_FILE" ]; then
17 SITE_MANAGEMENT_CONFIG_FILE="$WORKDIR/config/default.app"
19 source "$SITE_MANAGEMENT_CONFIG_FILE"
20 test_or_die "loading site management configuration from: $SITE_MANAGEMENT_CONFIG_FILE"
22 # configure feisty revision control to ignore vendor folders.
23 export NO_CHECKIN_VENDOR=true
25 # tests that the main storage folder for apps exists.
26 function check_application_dir()
28 local appdir="$1"; shift
29 if [ ! -d "$appdir" ]; then
30 echo "Creating the apps directory: $appdir"
32 test_or_die "Making apps directory when not already present"
36 # this function will seek out top-level directories in the target directory passed in.
37 # if there is only one directory, then it is returned (in the app_dirname variable).
38 # otherwise, the user is asked which directory to use.
39 # important: this sets a global variable app_dirname to the application's directory name.
40 function find_app_folder()
42 local appsdir="$1"; shift
44 # throw away any prior value so no confusion arises.
47 # count number of directories... if exactly one, then choose it.
48 numdirs=$(count_directories "$appsdir")
50 if [ $numdirs -eq 0 ]; then
52 echo "There are no directories in the application directory:"
54 echo "Please create a directory for the site storage, based on the application"
55 echo "name that you want to work on. Or you can just pass the directory name"
56 echo "on the command line, e.g.:"
57 echo " $(basename $0) turtle"
60 elif [ $numdirs -eq 1 ]; then
61 app_dirname="$(basename $(find "$appsdir" -mindepth 1 -maxdepth 1 -type d) )"
62 test_or_die "Guessing application folder"
64 # if more than one folder, force user to choose.
65 # Reference: https://askubuntu.com/questions/1705/how-can-i-create-a-select-menu-in-a-shell-script
67 PS3='Please pick a folder for site initialization: '
68 options=( $(find "$appsdir" -mindepth 1 -maxdepth 1 -type d -exec basename {} ';') "Quit")
69 select app_dirname in "${options[@]}"; do
71 "Quit") echo ; echo "Quitting from the script."; exit 1; ;;
72 *) echo ; echo "You picked folder '$app_dirname'" ; break; ;;
75 if [ -z "$app_dirname" ]; then
76 echo "The folder was not provided. This script needs a directory name"
77 echo "within which to initialize the site."
82 test_app_folder "$appsdir" "$app_dirname"
83 test_or_die "Testing application folder: $app_dirname"
85 echo "Application folder is: $app_dirname"
88 # ensures that the app directory name is valid.
89 function test_app_folder()
91 local appsdir="$1"; shift
94 local combo="$appsdir/$dir"
96 if [ ! -d "$combo" ]; then
97 echo "Creating app directory: $combo"
99 test_or_die "Making application directory when not already present"
103 # eases some permissions to enable apache to write log files and do other shopkeeping.
104 function fix_site_perms()
106 local site_dir="$1"; shift
108 if [ -f "$site_dir/bin/cake" ]; then
109 chmod -R a+rx "$site_dir/bin/cake"
110 test_or_die "Enabling execute bit on cake binary"
113 if [ -d "$site_dir/logs" ]; then
114 chmod -R g+w "$site_dir/logs"
115 test_or_die "Enabling group write on site's Logs directory"
118 if [ -d "$site_dir/tmp" ]; then
119 chmod -R g+w "$site_dir/tmp"
120 test_or_die "Enabling group write on site's tmp directory"
124 # tosses out any cached object data that originated from the database.
125 function clear_orm_cache()
127 local site_dir="$1"; shift
129 if [ -f "$site_dir/bin/cake" ]; then
130 # flush any cached objects from db.
131 "$site_dir/bin/cake" orm_cache clear
132 test_or_die "Clearing ORM cache"
136 # updates the revision control repository passed in. this expects that the
137 # repo will live in a folder called "checkout_dirname" under the app path,
138 # which is the standard for our deployed sites.
139 # important: this also sets a global variable called site_store_path to the full
140 # path of the application.
141 function update_repo()
143 local full_app_dir="$1"; shift
144 local checkout_dirname="$1"; shift
145 local repo_root="$1"; shift
146 local repo_name="$1"; shift
148 # forget any prior value, since we are going to validate the path.
149 unset site_store_path
151 pushd "$full_app_dir" &>/dev/null
152 test_or_die "Switching to our app dir '$full_app_dir'"
154 local complete_path="$full_app_dir/$checkout_dirname"
156 # see if the checkout directory exits. the repo_found variable is set to
157 # non-empty if we find it and it's a valid git repo.
159 if [ -d "$checkout_dirname" ]; then
160 # checkout directory exists, so let's check it.
161 pushd "$checkout_dirname" &>/dev/null
162 test_or_die "Switching to our checkout directory: $checkout_dirname"
164 # ask for repository name (without .git).
165 if git rev-parse --git-dir > /dev/null 2>&1; then
166 # this is a valid git repo.
170 # we don't consider the state of having the dir exist but the repo be wrong as good.
171 if [ -z "$repo_found" ]; then
172 echo "There is a problem; this folder is not a valid repository:"
173 echo " $full_app_dir"
174 echo "This script cannot continue unless the git repository is valid."
180 if [ ! -z "$repo_found" ]; then
181 # a repository was found, so update the version here and leave.
182 echo "Repository $repo_name exists. Updating it."
184 test_or_die "Recursive checkout on: $complete_path"
186 # clone the repo since it wasn't found.
187 echo "Cloning repository $repo_name now."
188 git clone "$repo_root/$repo_name.git" $checkout_dirname
189 test_or_die "Git clone of repository: $repo_name"
192 fix_site_perms "$complete_path"
194 # construct the full path to where the app will actually live.
195 site_store_path="$complete_path"
200 # this function goes to the directory specified and makes it right with
201 # composer install. this is as opposed to composer update, which could
203 function composer_repuff()
205 local site_store_path="$1"; shift
207 pushd "$site_store_path" &>/dev/null
208 test_or_die "Switching to our app dir '$site_store_path'"
210 echo "Updating site with composer..."
213 test_or_die "Composer installation step on '$site_store_path'."
217 dir="$site_store_path/$CHECKOUT_DIR_NAME/vendor/siteavenger/avcore"
218 if [ -d "$dir" ]; then
219 echo "Running avcore database migrations..."
220 logfile="$TMP/problem-avcore_db_migration-$(date_stringer).log"
221 ./bin/cake migrations migrate -p Avcore &>"$logfile"
222 if [ $? -ne 0 ]; then
223 echo "** FAILED: Database migrations for avcore. Check log file in: $logfile"
224 # we keep going, because some sites aren't ready for this yet.
227 echo "Database for avcore migrated."
236 # this function creates the links needed to make the site function properly given our
237 # various dependencies and infrastructure.
238 function create_site_links()
240 local site_store_path="$1"; shift
241 local theme_name="$1"; shift
243 echo "Creating symbolic links for site assets..."
245 # jump into the site path so we can start making relative links.
246 pushd "$site_store_path" &>/dev/null
247 test_or_die "Switching to our app dir '$site_store_path'"
249 pushd webroot &>/dev/null
251 # remove all symlinks that might plague us.
252 find . -maxdepth 1 -type l -exec rm -f {} ';'
253 test_or_die "Cleaning out links in webroot"
255 # link in the avcore plugin.
256 make_safe_link "../vendor/siteavenger/avcore/webroot" avcore
258 # make the link for our theme as a lower-case version of the theme.
259 themelower=${theme_name,,}
260 make_safe_link "../plugins/$theme_name/webroot" "$themelower"
262 # link in any favicon files.
263 if [ -d "../plugins/$theme_name/favicon" ]; then
265 for fave in "../plugins/$theme_name/favicon"/*; do
266 make_safe_link "$fave" .
270 # get back out of webroot.
273 # hop up a level above where we had been.
276 # link 'public' to webroot.
277 if [ -L public ]; then
278 # public is a symlink.
280 test_or_die "Removing public directory symlink"
281 elif [ -d public ]; then
282 # public is a folder with default files.
285 test_or_die "Removing public directory and contents"
288 # create the main 'public' symlink
290 make_safe_link $CHECKOUT_DIR_NAME/webroot public
291 test_or_die "Creating link to webroot called 'public'"
293 #hmmm: public/$themelower/im will be created automatically by system user with appropriate permissions
295 echo Created symbolic links.
301 # fetches composer to make sure it's up to date.
302 # (if powerup runs, composer install doesn't update git origin.)
303 function update_composer_repository()
305 local site_store_path="$1"; shift
307 pushd "$site_store_path" &>/dev/null
309 if git config remote.composer.url &>/dev/null; then
311 echo "Updated the composer repository."
313 echo "No composer repository was found for updating."