Skip to content

Instantly share code, notes, and snippets.

@orlandothoeny
Created October 9, 2020 18:24
Show Gist options
  • Save orlandothoeny/7704233f96138f92346040c00c8baa7d to your computer and use it in GitHub Desktop.
Save orlandothoeny/7704233f96138f92346040c00c8baa7d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Usage: See help
# $ bash mutagen.sh --help
COMPOSE_PROJECT_NAME=drupal
SYNC_APPDATA_SOURCE=.
SYNC_APPDATA=${COMPOSE_PROJECT_NAME}-appdata
SYNC_APPDATA_DRUPAL=${SYNC_APPDATA}-drupal
SYNC_APPDATA_NGINX=${SYNC_APPDATA}-nginx
SYNC_APPDATA_CONFIG_SOURCE=./config
SYNC_APPDATA_CONFIG_DRUPAL=${SYNC_APPDATA}-config-drupal
SYNC_PUBLIC_FILES_SOURCE=../public_files
SYNC_PUBLIC_FILES=${COMPOSE_PROJECT_NAME}-public-files
SYNC_PUBLIC_FILES_DRUPAL=${SYNC_PUBLIC_FILES}-drupal
SYNC_PUBLIC_FILES_NGINX=${SYNC_PUBLIC_FILES}-nginx
SYNC_PRIVATE_FLIES_SOURCE=../private_files
SYNC_PRIVATE_FLIES=${COMPOSE_PROJECT_NAME}-private-files
SYNC_PRIVATE_FILES_DRUPAL=${SYNC_PRIVATE_FLIES}-drupal
SYNC_PRIVATE_FILES_NGINX=${SYNC_PRIVATE_FLIES}-nginx
function terminate_syncs() {
mutagen sync terminate --label-selector project="${COMPOSE_PROJECT_NAME}"
}
function pause_syncs() {
mutagen sync pause --label-selector project="${COMPOSE_PROJECT_NAME}"
}
function resume_syncs() {
mutagen sync resume --label-selector project="${COMPOSE_PROJECT_NAME}"
}
function flush_syncs() {
mutagen sync flush --label-selector project="${COMPOSE_PROJECT_NAME}"
}
function list_syncs() {
mutagen sync list --label-selector project="${COMPOSE_PROJECT_NAME}"
}
function syncs_exist() {
if [[ -z $(list_syncs | grep 'No synchronization sessions found') ]]; then
echo 1
else
echo 0
fi
}
function monitor_syncs() {
mutagen sync monitor --label-selector project="${COMPOSE_PROJECT_NAME}"
}
function do_containers_exist() {
if [[ -z "$(docker-compose ps | grep "${COMPOSE_PROJECT_NAME}")" ]]; then
echo 0
else
echo 1
fi
}
function get_container_name() {
local serviceName=${1}
local existingContainerName=$(docker ps | grep ${COMPOSE_PROJECT_NAME}_${serviceName} | awk '{print $NF}')
if [[ -n "${existingContainerName}" ]]; then
echo "${existingContainerName}"
else
# Use default name schema assigned when a compose project is first run.
echo ${COMPOSE_PROJECT_NAME}_${serviceName}_1
fi
}
create_syncs_drupal() {
local shouldOverwriteContainerData=${1}
local containerToHostSyncMode
if [[ "${shouldOverwriteContainerData}" == 1 ]]; then
containerToHostSyncMode=one-way-safe
else
containerToHostSyncMode=two-way-resolved
fi
local containerName=drupal
mutagen sync create \
--name=${SYNC_APPDATA_DRUPAL} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval-alpha=10 \
--watch-polling-interval-beta=120 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode=one-way-safe \
--probe-mode=assume \
--symlink-mode=posix-raw \
--ignore-vcs \
--ignore ./config,./docker,./frontend-build,./Jenkins,*.sql,*.tar.gz \
${SYNC_APPDATA_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/code
mutagen sync create \
--name=${SYNC_APPDATA_CONFIG_DRUPAL} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval-alpha=30 \
--watch-polling-interval-beta=30 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode="${containerToHostSyncMode}" \
--probe-mode=assume \
--symlink-mode=posix-raw \
--ignore-vcs \
${SYNC_APPDATA_CONFIG_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/code/config
mutagen sync create \
--name=${SYNC_PUBLIC_FILES_DRUPAL} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval=10 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode="${containerToHostSyncMode}" \
--probe-mode=assume \
--ignore-vcs \
${SYNC_PUBLIC_FILES_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/public_files
mutagen sync create \
--name=${SYNC_PRIVATE_FILES_DRUPAL} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval=10 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode="${containerToHostSyncMode}" \
--probe-mode=assume \
--ignore-vcs \
${SYNC_PRIVATE_FLIES_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/private_files
}
function create_syncs_nginx() {
local containerName=nginx
mutagen sync create \
--name=${SYNC_APPDATA_NGINX} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval-alpha=30 \
--watch-polling-interval-beta=120 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode=one-way-safe \
--probe-mode=assume \
--symlink-mode=posix-raw \
--ignore-vcs \
--ignore ./docker,./frontend-build,./Jenkins,./vendor,*.sql,*.tar.gz \
${SYNC_APPDATA_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/code
mutagen sync create \
--name=${SYNC_PUBLIC_FILES_NGINX} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval=10 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode=one-way-safe \
--probe-mode=assume \
--ignore-vcs \
${SYNC_PUBLIC_FILES_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/public_files
mutagen sync create \
--name=${SYNC_PRIVATE_FILES_NGINX} \
--label project="${COMPOSE_PROJECT_NAME}" \
--watch-polling-interval=10 \
--default-owner-beta=drupal \
--default-group-beta=drupal \
--sync-mode=one-way-safe \
--probe-mode=assume \
--ignore-vcs \
${SYNC_PRIVATE_FLIES_SOURCE} docker://drupal@$(get_container_name ${containerName})/app/private_files
}
function create_syncs() {
local shouldOverwriteContainerData=0
if [[ $# -gt 0 ]]; then
if [[ "$1" = 'overwite' ]]; then
shouldOverwriteContainerData=1
fi
fi
create_syncs_drupal ${shouldOverwriteContainerData}
create_syncs_nginx
}
function create_overwrite_syncs() {
create_syncs overwite
}
function overwrite_container_data() {
create_overwrite_syncs
}
function stop() {
printf '=> Pausing syncs\n'
pause_syncs
}
function check_requirements() {
if ! command -v mutagen &> /dev/null
then
printf 'Mutagen is not installed.\n'
printf 'You can install it using Homebrew:\n'
printf '$ brew install mutagen-io/mutagen/mutagen\n'
printf 'Or using an alternative method, see: https://mutagen.io/documentation/introduction/installation\n'
exit
fi
}
check_requirements
if [[ $# -gt 0 ]]; then
if [[ "$1" == 'up' ]]; then
shift 1
if [[ "${1}" == '--build' ]]; then
printf '=> Building required Docker base images\n'
build_required_docker_images
fi
if [[ $(do_containers_exist) == 0 ]]; then
printf '=> No preexisting containers were found\n'
printf '=> Starting Docker containers\n'
docker-compose up -d "$@"
printf '=> Overwriting container volumes with host data\n'
overwrite_container_data
flush_syncs
terminate_syncs
else
printf '=> Starting Docker containers\n'
docker-compose up -d "$@"
fi
if [[ $(syncs_exist) == 0 ]]; then
printf '=> Creating volume syncs\n'
create_syncs
else
printf '=> Resuming volume syncs\n'
resume_syncs
fi
docker-compose up
elif [[ "$1" == 'stop' ]]; then
stop
elif [[ "$1" == 'terminate-syncs' ]]; then
terminate_syncs
elif [[ "$1" == 'list-syncs' ]]; then
list_syncs
elif [[ "$1" == 'monitor-syncs' ]]; then
monitor_syncs
elif [[ "$1" == '--help' ]]; then
printf 'Available commands:\n'
printf ' up [options]\n'
printf ' - Creates syncs and starts Docker containers\n'
printf ' Checks if any preexisting containers exist.\n'
printf ' If not it does a one-time sync from Host to containers that overwrites any data on the containers.\n'
printf ' - Options:\n'
printf ' All options that are available for the docker-compose command.\n'
printf ' --build Additionally builds all required base Docker images\n'
printf ' stop\n'
printf ' - Pauses syncs and stops the currently cunning Docker containers\n'
printf ' terminate-syncs\n'
printf ' - Terminates the syncs created for this project.\n'
printf ' list-syncs\n'
printf ' - Displays a list of the syncs created for this project.\n'
printf ' monitor-syncs\n'
printf ' - Monitors the syncs created for this project.\n'
else
printf 'Unknown command\n'
printf 'Run "bash mutagen.sh --help" for usage\n'
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment