Last active
April 3, 2017 18:08
-
-
Save SISheogorath/600405f1f36dc1d786500ffd14fb3675 to your computer and use it in GitHub Desktop.
Provides doctl on every plattform. Simple, fast, docker :D
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/sh | |
set -e | |
OPWD=`pwd` | |
DOCTL_DIR="$HOME/.doctl" | |
DOCTL_RCFILE="$HOME/.doctlrc" | |
DOCTL_IMAGETAG="doctl:local" | |
# Check that all we need exists | |
command_exists() { command -v $1 >/dev/null 2>&1 || { echo >&2 "I require $1 but it's not installed. Aborting."; exit 1; }; } | |
docker_installed() { command -v docker >/dev/null 2>&1 || { wget -O- http://get.docker.com | sh - ; }; command -v docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed and I can't install it myself. Aborting."; exit 1; }; } | |
command_exists git | |
command_exists wget | |
docker_installed | |
# Use sudo if docker socket is not writeable | |
[ -w /var/run/docker.sock ] || SUDO=sudo | |
# Load .doctlrc only if exists | |
if [ -e "$DOCTL_RCFILE" ]; then | |
source "$DOCTL_RCFILE" | |
fi | |
# Check for digital ocean token | |
if [ ! -v "DOTOKEN" ]; then | |
echo "Enter your digital ocean token: " | |
read DOTOKEN | |
echo "" | |
echo "Do you want to store your token in '$DOCTL_RCFILE'? (Y/n)" | |
read WRITE_RCFILE | |
[ "$WRITE_RCFILE" != "n" ] && echo "DOTOKEN=$DOTOKEN" >> $DOCTL_RCFILE | |
fi | |
# Check that doctl exists and is up-to-date | |
if [ ! -e "$DOCTL_DIR" ]; then | |
git clone https://github.com/digitalocean/doctl.git "$DOCTL_DIR" | |
cd "$DOCTL_DIR" | |
git fetch | |
fi | |
cd "$DOCTL_DIR" | |
# Check the git repository only every 24 hours for updates | |
if [ "$(($(date +%s)-$(stat -c %Y .git/FETCH_HEAD)))" -gt 86400 ]; then | |
git fetch | |
# Rebuild container if there was an update | |
if [ $(git diff origin/master | wc -l) -gt 0 ]; then | |
git pull | |
$SUDO docker build -t $DOCTL_IMAGETAG . | |
fi | |
fi | |
# Check that there is a image. If not build a new one | |
if [ $($SUDO docker images --format "{{.Repository}}:{{.Tag}}" | grep $DOCTL_IMAGETAG | wc -l) -eq 0 ]; then | |
$SUDO docker build -t $DOCTL_IMAGETAG . | |
fi | |
# Switch back to original directory | |
cd "$OPWD" | |
# Run doctl in container | |
$SUDO docker run -it --read-only --rm --tmpfs /root/:rw,noexec -e "DIGITALOCEAN_ACCESS_TOKEN=${DOTOKEN}" $DOCTL_IMAGETAG $@ | |
# Load .doctlrc only if exists | |
if [ -e "$DOCTL_RCFILE" ]; then | |
source "$DOCTL_RCFILE" | |
fi | |
# Check for digital ocean token | |
if [ ! -v "DOTOKEN" ]; then | |
echo "Enter your digital ocean token: " | |
read DOTOKEN | |
echo "" | |
echo "Do you want to store your token in '$DOCTL_RCFILE'? (Y/n)" | |
read WRITE_RCFILE | |
[ "$WRITE_RCFILE" != "n" ] && echo "DOTOKEN=$DOTOKEN" >> $DOCTL_RCFILE | |
fi | |
# Check that doctl exists and is up-to-date | |
[ ! -e "$DOCTL_DIR" ] && git clone https://github.com/digitalocean/doctl.git "$DOCTL_DIR" | |
cd "$DOCTL_DIR" | |
# Check the git repository only every 24 hours for updates | |
if [ "$(($(date +%s)-$(stat -c %Y .git/FETCH_HEAD)))" -gt 86400 ]; then | |
git fetch | |
# Rebuild container if there was an update | |
if [ $(git diff origin/master | wc -l) -gt 0 ]; then | |
git pull | |
$SUDO docker build -t $DOCTL_IMAGETAG . | |
fi | |
fi | |
# Check that there is a image. If not build a new one | |
if [ $($SUDO docker images --format "{{.Repository}}:{{.Tag}}" | grep $DOCTL_IMAGETAG | wc -l) -eq 0 ]; then | |
$SUDO docker build -t $DOCTL_IMAGETAG . | |
fi | |
# Switch back to original directory | |
cd "$OPWD" | |
# Run doctl in container | |
$SUDO docker run -it --read-only --rm --tmpfs /root/:rw,noexec -e "DIGITALOCEAN_ACCESS_TOKEN=${DOTOKEN}" $DOCTL_IMAGETAG $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solid!