Refactor mount_dest.sh to use functions

pull/58/head
Dan Puttick 2017-08-09 19:04:57 -04:00
parent fce66420c8
commit dd427b8c8e
3 changed files with 64 additions and 67 deletions

View File

@ -12,7 +12,7 @@ clean(){
} }
check_is_root() { check_is_root() {
if [ "${ID}" -ne 0 ]; then if [ "${ID}" -ne 0 ]; then # -ne is an integer comparison instead of a string comparison
echo "GROOMER: This script has to be run as root." echo "GROOMER: This script has to be run as root."
exit exit
fi fi
@ -35,8 +35,8 @@ run_groomer() {
main() { main() {
set -eu # exit when a line returns non-0 status, treat unset variables as errors set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap clean EXIT TERM INT # run clean when the script ends or is interrupted trap clean EXIT TERM INT # run clean when the script ends or is interrupted
check_is_root
source ./config.sh # get config values source ./config.sh # get config values
check_is_root
if [ "${DEBUG}" = true ]; then if [ "${DEBUG}" = true ]; then
set -x set -x
fi fi

View File

@ -1,78 +1,76 @@
#!/bin/bash #!/bin/bash
# set -e (exit when a line returns non-0 status) and -x (xtrace) flags
set -e
set -x
# Import constants from config file
source ./config.sh
if ! [ "${ID}" -ge "1000" ]; then
echo "GROOMER: mount_keys.sh cannot run as root."
exit
fi
clean(){ clean(){
if [ ${DEBUG} = true ]; then if [ "${DEBUG}" = true ]; then
sleep 20 sleep 20
# Copy the temporary logfile to the destination key # Copy the temporary logfile to the destination key
cp ${DEBUG_LOG} "${DST_MNT}/groomer_debug_log.txt" cp "${DEBUG_LOG}" "${DST_MNT}/groomer_debug_log.txt"
fi
echo "GROOMER: Cleaning up in mount_keys.sh."
${SYNC} # Write anything in memory to disk
# Unmount source and destination
pumount "${SRC}"
pumount "${DST}"
exit
}
check_not_root() {
if ! [ "${ID}" -ge "1000" ]; then
echo "GROOMER: mount_keys.sh cannot run as root."
exit
fi fi
echo "GROOMER: Cleaning up in mount_keys.sh."
# Write anything in memory to disk
${SYNC}
# Unmount source and destination
pumount ${SRC}
# Clean up and unmount destination
pumount ${DST}
exit
} }
trap clean EXIT TERM INT check_source_exists() {
if [ ! -b "${DEV_SRC}" ]; then
echo "GROOMER: Source device (${DEV_SRC}) does not exist."
exit
fi
}
# Check that a device is available on /dev/source_key (symlinked to /dev/sda or sdb) check_dest_exists() {
if [ ! -b ${DEV_SRC} ]; then if [ ! -b "${DEV_DST}" ]; then
echo "GROOMER: Source device (${DEV_SRC}) does not exist." echo "GROOMER: Destination device (${DEV_DST}) does not exist."
exit exit
fi fi
}
# Check that a device is available on /dev/dest_key (symlinked to /dev/sda or sdb) unmount_dest_if_mounted() {
if [ ! -b ${DEV_DST} ]; then if ${MOUNT}|grep "${DST}"; then
echo "GROOMER: Destination device (${DEV_DST}) does not exist." ${PUMOUNT} "${DST}" || true
exit fi
fi }
# If there is already a device mounted on /media/dst, unmount it mount_dest_partition() {
if ${MOUNT}|grep ${DST}; then if "${PMOUNT}" -w "${DEV_DST}1" "${DST}"; then # pmount automatically mounts on /media/ (at /media/dst in this case).
${PUMOUNT} ${DST} || true echo "GROOMER: Destination USB device (${DEV_DST}1) mounted at ${DST_MNT}"
fi else
echo "GROOMER: Unable to mount ${DEV_DST}1 on ${DST_MNT}"
# uid= only works on a vfat FS. What should wedo if we get an ext* FS ? exit
# What does this ^ comment mean? fi
}
# Mount the first partition of DST (/dev/dest_key1)
# pmount automatically mounts on /media/ (at /media/dst in this case).
${PMOUNT} -w "${DEV_DST}1" ${DST}
if [ ${?} -ne 0 ]; then
echo "GROOMER: Unable to mount ${DEV_DST}1 on ${DST_MNT}"
exit
else
echo "GROOMER: Destination USB device (${DEV_DST}1) mounted at ${DST_MNT}"
# Remove any existing "FROM_PARTITION_" directories
rm -rf "/media/${DST}/FROM_PARTITION_"*
prepare_dest_partition() {
rm -rf "/media/${DST}/FROM_PARTITION_"* # Remove any existing "FROM_PARTITION_" directories
# Prepare temp dirs and make sure they're empty if they already exist # Prepare temp dirs and make sure they're empty if they already exist
mkdir -p "${TEMP}" mkdir -p "${TEMP}"
mkdir -p "${LOGS}" mkdir -p "${LOGS}"
rm -rf "${TEMP}/"* rm -rf "${TEMP:?}/"*
rm -rf "${LOGS}/"* rm -rf "${LOGS:?}/"*
fi }
# Now that destination is mounted and prepared, run the groomer main() {
./groomer.sh set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap clean EXIT TERM INT
set -x
source ./config.sh
check_not_root
check_source_exists
check_dest_exists
unmount_dest_if_mounted
mount_dest_partition
prepare_dest_partition
./groomer.sh
}
main

View File

@ -17,7 +17,6 @@ done
main() { main() {
set -eu # exit when a line returns non-0 status, treat unset variables as errors set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap killed EXIT TERM INT # run clean when the script ends or is interrupted trap killed EXIT TERM INT # run clean when the script ends or is interrupted
check_is_root
source ./config.sh # get config values source ./config.sh # get config values
run_timidity run_timidity
} }