Skip to content

Commit

Permalink
Merge pull request #33 from derickson2402/dev
Browse files Browse the repository at this point in the history
Merge dev for v0.6.3 release
  • Loading branch information
derickson2402 authored Apr 14, 2022
2 parents 6123c7d + 697538e commit c522db4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 43 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ caen ./my_other_program < input.txt | caen my_program

## Installation

To use this script, you need to have Docker installed on your [macOS](https://docs.docker.com/desktop/mac/install/), [Windows](https://docs.docker.com/desktop/windows/install/), or [Linux](https://docs.docker.com/engine/install/) computer. With Docker installed, simply run the following command in a shell:
To use this script, you need to have Docker installed on your [macOS](https://docs.docker.com/desktop/mac/install/), [Windows](https://docs.docker.com/desktop/windows/install/), or [Linux](https://docs.docker.com/engine/install/) computer. Windows users, you will need to make sure to upgrade your existing WSL1 installation to WSL2 BEFORE installing Docker (see [#25](https://github.com/derickson2402/Dockerized-CAEN/issues/25)). Linux users, you will need to add yourself to the ```docker``` group (see [here](https://www.configserverfirewall.com/ubuntu-linux/add-user-to-docker-group-ubuntu/)).

With Docker installed, simply run the following command in a shell:

```bash
sudo /bin/bash -c 'wget \
Expand Down
123 changes: 81 additions & 42 deletions caen
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
CAEN_SCRIPT_VER=v0.6.2
CAEN_SCRIPT_VER=v0.6.3

# Shell settings to improve safety
set -fu -o pipefail
Expand All @@ -23,6 +23,7 @@ CAEN_USER=${CAEN_USER:="$(id -u):$(id -g)"}
CAEN_DEFAULT_ARGS="--rm -i --name caen-${RANDOM:=1} -e TERM=${TERM:=linux} --user ${CAEN_USER}"
CAEN_WORKING_DIR="-v '$(pwd):/code'"
HOST_OS=unknown
HOST_DOCKER_READY=false
CAEN_LATEST_VERSION=${CAEN_SCRIPT_VER}
# NOTE: there is a space at the end to force newline printing
MESSAGE_HELP=$(cat << EOF
Expand Down Expand Up @@ -95,11 +96,13 @@ function checkForUpdates() {
echo -e "Hold up! Could not find latest version of script from GitHub, but you appear"
echo -e "to be connected to the internet. Check your internet and try again, or go to"
echo -e "GitHub for more help:"
echo -e "\n$CAEN_URL\n"
echo -e "\n ${CAEN_URL}\n"
exit 1
elif [[ ${CAEN_VERSION} != "latest" ]]; then
# Can't check for script updates on special versions, just pull the newest container
docker pull ${CAEN_REPO_NAME}:${CAEN_VERSION}
echo -e "Heads up! There may be an update available for version ${CAEN_VERSION}."
echo -e "Run the following to get the latest version"
echo -e "\n sudo caen update\n"
elif [[ ${CAEN_SCRIPT_VER} != ${CAEN_LATEST_VERSION} ]]; then
echo -e "Heads up! There is an update available from ${CAEN_SCRIPT_VER} to ${CAEN_LATEST_VERSION}."
echo -e "To update, run the following:"
Expand All @@ -111,7 +114,7 @@ function checkForUpdates() {

# Make sure that Docker Engine is installed and running. Provide installation
# URL if not installed, or call host machine to launch daemon if not on
function startDocker() {
function checkDocker() {
if ( ! command -v docker &> /dev/null ); then
echo -e "Hold up! Docker is not installed on your computer!"
echo -e "You can download and install it here:"
Expand All @@ -122,8 +125,9 @@ function startDocker() {
else
echo -e "\n https://docs.docker.com/engine/install/\n"
fi
exit
elif ( ! docker ps &> /dev/null ); then
return
fi
if ( ! docker ps &> /dev/null ); then
echo -e "Waking up Moby the Whale..."
if [[ "$HOST_OS" == "mac" ]]; then
open /Applications/Docker.app
Expand All @@ -132,20 +136,71 @@ function startDocker() {
elif [[ "$HOST_OS" == "linux" ]]; then
sudo systemctl start docker
fi
sleep 5
# Docker takes up to about 1m to boot, need to wait for it
sleep 10
declare -i counter=0
while ( ! docker stats --no-stream &> /dev/null ); do
if [[ $((${counter} % 15)) == 0 ]]; then
echo -e "Waiting for Moby the Whale to wake up..."
fi
(( counter += 1 ))
echo -e "Waiting for Moby the Whale to wake up..."
sleep 15
if [[ counter -gt 2 ]]; then
sleep 1
if [[ counter -gt 60 ]]; then
echo -e "Couldn't wake up Moby :("
echo -e "Maybe try restarting Docker?"
exit
echo -e "Maybe try restarting Docker manually?"
exit 1
fi
done
echo "Hi Moby!"
return
fi

# All checks have passed, Docker is ready to go
HOST_DOCKER_READY=true
}

# Update the docker container environment and then update the wrapper script.
# This will delete the current script file and replace it
function updateCAEN() {
if [[ $(whoami) != "root" ]]; then
echo -e "Hold up! You can only run an update as root!"
echo -e "Try running with sudo:"
echo -e "\n sudo caen update\n"
exit 1
fi

# Check for internet connection
if ( wget -q --spider ${CAEN_URL} ); then
if [[ ${HOST_DOCKER_READY} == "true" ]]; then
echo "Updating container environment..."
docker pull ${CAEN_REPO_NAME}:${CAEN_VERSION}
else
echo "Cannot update container environment, Docker not responding."
fi
echo "Updating wrapper script..."
echo '#!/bin/bash' > /tmp/caen-update.sh
chmod +x /tmp/caen-update.sh
echo "/bin/bash -c 'wget -q https://raw.githubusercontent.com/derickson2402/Dockerized-CAEN/main/caen -O /usr/local/bin/caen && chmod +x /usr/local/bin/caen'" >> /tmp/caen-update.sh
/tmp/caen-update.sh && rm -f /tmp/caen-update.sh && echo "Done!" && exit
else
echo -e "Hold up! Could not reach GitHub.com! Check your internet connection and"
echo -e "try again."
exit 1
fi
}

# Poll the Docker daemon for the current container version, and print it along
# with the script version
function getVersion() {
if [[ ${HOST_DOCKER_READY} == "true" ]]; then
CONT_SHA=$(docker image ls -q ${CAEN_REPO_NAME}:${CAEN_VERSION})
CONT_VER=$(docker image inspect --format '{{json .}}' "$CONT_SHA" |
perl -nle 'print $& while m{"org.opencontainers.image.version":"\K[^"]+}g')
fi
echo -e "Dockerized-CAEN"
echo -e "Script version: ${CAEN_SCRIPT_VER}"
echo -e "Container version: ${CONT_VER:=unknown}"
exit
}

# Process the CLI args, set default vars, and check what program should be run.
Expand All @@ -154,39 +209,19 @@ function startDocker() {
function checkArgs() {
case $1 in
help)
echo -e "$MESSAGE_HELP" && exit
echo -e "$MESSAGE_HELP"
exit
;;
version)
CONT_SHA=$(docker image ls -q ${CAEN_REPO_NAME}:${CAEN_VERSION})
CONT_VER=$(docker image inspect --format '{{json .}}' "$CONT_SHA" |
perl -nle 'print $& while m{"org.opencontainers.image.version":"\K[^"]+}g')
echo -e "Dockerized-CAEN"
echo -e "Script version: ${CAEN_SCRIPT_VER}"
echo -e "Container version: ${CONT_VER:=unknown}"
exit
getVersion
;;
update)
if [[ $(whoami) != "root" ]]; then
echo -e "Hold up! You can only run an update as root!"
echo -e "Try running with sudo:"
echo -e "\n sudo caen update\n"
exit 1
fi
echo "Updating container environment..."
docker pull ${CAEN_REPO_NAME}:${CAEN_VERSION} > /dev/null
if [[ ${CAEN_VERSION} == "latest" ]]; then
echo "Updating wrapper script..."
echo '#!/bin/bash' > /tmp/caen-update.sh
chmod +x /tmp/caen-update.sh
echo "/bin/bash -c 'wget -q https://raw.githubusercontent.com/derickson2402/Dockerized-CAEN/main/caen -O /usr/local/bin/caen && chmod +x /usr/local/bin/caen'" >> /tmp/caen-update.sh
/tmp/caen-update.sh && rm -f /tmp/caen-update.sh && echo "Done!" && exit
else
echo "Heads up! Cannot update script for non-latest version. Please run with"
echo "no options set if you want to update the script too!"
exit
fi
updateCAEN
;;
perf)
if [[ ${HOST_DOCKER_READY} != "true" ]]; then
exit 1
fi
echo -e "Heads up! Perf requires elevated Docker privileges to run. This is"
echo -e "dangerous, so you should only do this with code that you trust!\n"
echo -e "Hit [Ctrl+c] to cancel, or enter to continue"
Expand All @@ -198,16 +233,20 @@ function checkArgs() {

# Execute the given program and its args using the CAEN container
function main() {
# Prep our host environment
identifyOS
startDocker
# Prep our host environment. Make sure to check for updates first in case
# there is a script bug, so user is more likely to fix their issue
checkForUpdates
identifyOS
if [ $# -eq 0 ]; then
echo -e "$MESSAGE_HELP" && exit
fi
checkDocker
checkArgs $1

# Check for stdin, allowing for file redirection to the container
if [[ ${HOST_DOCKER_READY} != "true" ]]; then
exit 1
fi
if [ ! -t 0 ]; then
eval -- docker run "${CAEN_DEFAULT_ARGS}" "${CAEN_ARGS}" "${CAEN_WORKING_DIR}" "${CAEN_REPO_NAME}":"${CAEN_VERSION}" "$@" < /dev/stdin
else
Expand Down

0 comments on commit c522db4

Please sign in to comment.