Last active
March 9, 2020 00:33
-
-
Save costa/b5a283918ea0b631eba2fbaf2aac4b57 to your computer and use it in GitHub Desktop.
simple docker-machine-based dev helper
This file contains hidden or 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
| dev() { | |
| local machine_name | |
| local dir_name | |
| local proj_name | |
| local host | |
| local host_user | |
| local host_path | |
| local ssh_cmd | |
| if ! dev-setup | |
| then return $? | |
| fi | |
| if [ -z "$1" ] | |
| then echo "USAGE: dev <docker host command...> # FROM A PROJECT DIRECTORY, DEV_DOCKER_MACHINE needs to be set, AFTER dev-init...!" 1>&2 | |
| return 999 | |
| fi | |
| echo "dev'ing $dir_name -- DO NOT make local file changes until this finishes!" | |
| while ! $ssh_cmd "$host_user@$host" "( mkdir -p $host_path && cd $host_path && sudo chown -R $host_user: . )" | |
| do sleep 3 | |
| done | |
| while ! rsync -vrlptDz --del -e "$ssh_cmd" . "$host_user@$host:$host_path/" | |
| do sleep 3 | |
| done | |
| $ssh_cmd "$host_user@$host" "( cd $host_path && $@ )" && \ | |
| ( while ! rsync -vrlptDz --del -e "$ssh_cmd" "$host_user@$host:$host_path/" .; do sleep 3; done ) | |
| } | |
| dev-tunnel() { | |
| local machine_name | |
| local dir_name | |
| local proj_name | |
| local host | |
| local host_user | |
| local host_path | |
| local ssh_cmd | |
| if ! dev-setup | |
| then return $? | |
| fi | |
| lport="${1:-8880}" | |
| svc="${2:-web}" | |
| port="${3:-80}" | |
| ip=$( docker-machine ssh "$machine_name" "( cd $host_path && sudo docker-compose -p $proj_name ps | grep _${svc}_ | cut -d' ' -f1 | xargs -n 1 sudo docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' | cut -d' ' -f1 )" ) # TODO escape me, also, couldn't find a way to print the first element in map range (go templates!) | |
| if [ -z "$ip" ] | |
| then echo "Error getting the service's local IP, maybe dead." 1>&2 | |
| return 3 | |
| fi | |
| $ssh_cmd "$host_user@$host" -L "$lport:$ip:$port" -N | |
| } | |
| dev-setup() { # NOTE aux | |
| machine_name="$DEV_DOCKER_MACHINE" | |
| if [ -z "$machine_name" ] | |
| then echo "export DEV_DOCKER_MACHINE=<your dev instance> # and pre-set zone somehow as well" 1>&2 | |
| return 999 | |
| fi | |
| dir_name="$( basename `pwd` )" | |
| proj_name="$DEV_PROJ_NAME" | |
| if [ -z "$proj_name" ] | |
| then if [ "`pwd`" == "$HOME" ] | |
| then echo "ERROR: to be run from some project directory" 1>&2 | |
| return 999 | |
| fi | |
| proj_name="$dir_name" | |
| fi | |
| host=`docker-machine url $machine_name | cut -d: -f2 | cut -c3-` | |
| host_user=${DEV_DOCKER_MACHINE_USER:-docker-user} | |
| host_path="/home/$host_user/proj/$dir_name" | |
| ssh_cmd="ssh -i ~/.docker/machine/machines/$machine_name/id_rsa -F /dev/null -o IdentitiesOnly=yes -o PasswordAuthentication=no -o CheckHostIP=no -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectionAttempts=3 -o ConnectTimeout=10 -o ControlMaster=no -o ControlPath=none -p 22" | |
| # TODO? -o BatchMode=yes | |
| } | |
| # NOTE docker-compose version is hard-coded to 1.19.0 (too lazy to be bothered) | |
| dev-init() { | |
| local machine_name | |
| if [ -z "$DEV_DOCKER_MACHINE_OPTIONS" ] | |
| then echo "Error: DEV_DOCKER_MACHINE_OPTIONS needs to be set ('--driver ...' etc)" 1>&2 | |
| return 999 | |
| fi | |
| machine_name="$1" | |
| if [ -z "$machine_name" ] | |
| then echo "USAGE: dev-init <machine name>" 1>&2 | |
| fi | |
| echo "INFO Booting the machine..." 1>&2 | |
| while ! docker-machine create $DEV_DOCKER_MACHINE_OPTIONS "$machine_name" | |
| do docker-machine rm -f "$machine_name" | |
| done | |
| echo "INFO Installing docker* with apt..." 1>&2 | |
| while ! docker-machine ssh "$machine_name" 'sudo bash -c "curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose"' | |
| do sleep 3 | |
| done | |
| echo "INFO Setting up mail..." 1>&2 | |
| while ! docker-machine ssh "$machine_name" 'curl https://gist.githubusercontent.com/costa/a325e7fdc5a803e1e5425fd36a784fe7/raw/cc88de0689cdf41c0dbba46195c9d38a73d83c48/gcloud-machine-sendgrid-mail-setup.sh | sudo sh' | |
| do sleep 3 | |
| done | |
| echo "INFO Some final touches..." 1>&2 | |
| while ! docker-machine ssh "$machine_name" mkdir proj | |
| do sleep 3 | |
| done | |
| while ! docker-machine ssh $machine_name 'echo "vm.max_map_count=262144" | sudo tee -a /etc/sysctl.conf' | |
| do sleep 3 | |
| done | |
| # TODO swapppp | |
| # sudo fallocate -l 30G /swapfile | |
| # sudo chmod 600 /swapfile | |
| # sudo mkswap /swapfile | |
| # sudo swapon /swapfile | |
| # echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab | |
| echo "Very good, now (recommended) $ echo 'export DEV_DOCKER_MACHINE=$machine_name' >> ~/.bashrc" | |
| echo "and (very recommended) $ docker-machine ssh $machine_name 'sudo apt-get remove --auto-remove -y sshguard; sudo reboot'" | |
| } | |
| dev-once() { | |
| local machine_name | |
| machine_name=test-`date +"%Y%m%d%H%M%S"`-dev | |
| dev-init $machine_name && \ | |
| DEV_DOCKER_MACHINE="$machine_name" dev "bash -xc '$*'" && docker-machine rm -f $machine_name | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment