-
-
Save KaweMaximo/a29fad7d898527aec63301167bb034aa to your computer and use it in GitHub Desktop.
Cache para ephemeral VMs usando GCS + fuse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
GREEN='\033[0;32m' | |
YELLOW='\033[1;33m' | |
ORANGE='\033[0;33m' | |
RED='\033[0;31m' | |
DEFAULT='\033[0m' | |
BUCKET=challenge-correction-cache | |
HOME_DIR=/home/deploy | |
MOUNT_DIR=cache-bucket | |
TAR_FILE=cache.tar.gz | |
show_help=false | |
help_command=rehydrate | |
help_info=" Rehydrate the GCS bucket cache with the local yarn cache." | |
help() { | |
echo | |
echo -e " Usage: ${GREEN}cache ${YELLOW}[command]" | |
echo | |
echo " Displays help information." | |
echo | |
echo " Commands:" | |
echo " - rehydrate" | |
echo " - restore" | |
echo | |
echo -e " Run ${GREEN}cache ${ORANGE}help ${YELLOW}[command] ${DEFAULT}for more information on specific commands." | |
echo | |
} | |
show_command_help() { | |
echo | |
echo -e " Usage: ${GREEN}cache ${YELLOW}$help_command" | |
echo | |
echo $help_info | |
echo | |
} | |
rehydrate() { | |
if [[ "x$show_help" == "xtrue" ]] ; then | |
help_command=rehydrate | |
help_info=" Rehydrate the GCS bucket cache with the local yarn cache." | |
show_command_help | |
return | |
fi | |
(cd $HOME_DIR && \ | |
tar -zcg cache.snap -f $TAR_FILE .cache && \ | |
cp $TAR_FILE "$MOUNT_DIR"/"$TAR_FILE") | |
} | |
restore() { | |
if [[ "x$show_help" == "xtrue" ]] ; then | |
help_command=restore | |
help_info=" Restore yarn cache from GCS bucket." | |
show_command_help | |
return | |
fi | |
(cd $HOME_DIR && \ | |
cp "$MOUNT_DIR"/"$TAR_FILE" $TAR_FILE && \ | |
tar -xzf $TAR_FILE) | |
} | |
check_fs() { | |
if ! mount -l | grep fuse &>/dev/null; then | |
echo | |
echo -e " ${ORANGE}[WARN] ${DEFAULT}Remote file system not mounted." | |
echo | |
echo " Creating user-space file system for interacting with GCS bucket..." | |
(cd $HOME_DIR && fusermount -u cache-bucket/ 2>&1) | |
if ! result=$(cd $HOME_DIR && gcsfuse $BUCKET $MOUNT_DIR 2>&1) ; then | |
gcs_fail=true | |
fi | |
[[ "x$gcs_fail" == "xtrue" ]] && echo -e "\n ${RED}[ERROR] ${DEFAULT}Failed to create user-space file system:" >&2 | |
fmt_result="$(echo $result | sed 's/^/ /g')" | |
echo -e "\n${fmt_result}" | |
[[ "x$gcs_fail" == "xtrue" ]] && exit 1 | |
fi | |
} | |
check_fs | |
case "$1" in | |
-h | --help) | |
help | |
exit 0 | |
;; | |
help) | |
case "$2" in | |
rehydrate) | |
show_help=true | |
rehydrate | |
exit 0 | |
;; | |
restore) | |
show_help=true | |
restore | |
exit 0 | |
;; | |
"") | |
help | |
exit 0 | |
;; | |
*) | |
echo | |
echo -e " ${RED}[ERROR] ${DEFAULT}Unknown command: ${YELLOW}$2" >&2 | |
help | |
exit 1 | |
;; | |
esac | |
;; | |
rehydrate) | |
rehydrate | |
exit 0 | |
;; | |
restore) | |
restore | |
exit 0 | |
;; | |
-* | --*) | |
echo | |
echo -e " ${RED}[ERROR] ${DEFAULT}Unknown option: ${YELLOW}$1" >&2 | |
help | |
exit 1 | |
;; | |
*) | |
help | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VMs using GCS