Created
October 17, 2016 16:01
-
-
Save dukejones/1fba9728b8325ae22e3713f36b082c84 to your computer and use it in GitHub Desktop.
user data for dockerized zcash
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 | |
IMG_LABEL=zcash | |
APP_INIT_URL=https://s3.amazonaws.com/your-bucket-app/myapp-init.tar.gz | |
function prepare_instance { | |
apt-get -y update | |
apt-get -y install nginx | |
curl -sSL https://get.docker.com/ | sh | |
mkdir /opt | |
curl -o /opt/deployer.sh http://169.254.169.254/latest/user-data | |
chmod 775 /opt/deployer.sh | |
} | |
# function download_app { | |
# curl -o /tmp/current.tar.gz $1 | |
# rm -rf /opt/app | |
# mkdir -p /opt/app | |
# tar zxvf /tmp/current.tar.gz -C /opt/app | |
# rm /tmp/current.tar.gz | |
# } | |
function dockerfile { | |
mkdir -p /opt/app | |
cat <<EOT > /opt/app/Dockerfile | |
FROM ubuntu:xenial | |
RUN apt-get -y update | |
RUN apt-get install -y build-essential pkg-config libc6-dev m4 g++-multilib autoconf libtool \ | |
ncurses-dev unzip git python zlib1g-dev wget bsdmainutils automake | |
RUN git clone https://github.com/zcash/zcash.git \ | |
&& cd zcash \ | |
&& git checkout v1.0.0-beta2 | |
RUN /zcash/zcutil/fetch-params.sh | |
RUN /zcash/zcutil/build.sh -j$(nproc) | |
RUN mkdir ~/.zcash | |
COPY zcash.conf /root/.zcash/ | |
EXPOSE 22 | |
CMD ["/zcash/src/zcashd"] | |
EOT | |
} | |
function build_image { | |
docker tag ${IMG_LABEL}:latest ${IMG_LABEL}:prev || echo "No built app" | |
docker build -t ${IMG_LABEL}:latest /opt/app | |
} | |
function run_container { | |
APP_CID=$(docker run -d ${IMG_LABEL}:latest) | |
APP_IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${APP_CID}) | |
} | |
function setup_proxy { | |
rm /etc/nginx/sites-enabled/* | |
cat <<EOT > /etc/nginx/sites-enabled/app.conf | |
map \$http_upgrade \$connection_upgrade { | |
default upgrade; | |
'' close; | |
} | |
upstream app.local { | |
server ${APP_IP}; | |
} | |
server { | |
listen 80; | |
location / { | |
proxy_pass http://app.local; | |
include /etc/nginx/proxy_params; | |
proxy_http_version 1.1; | |
proxy_set_header Upgrade \$http_upgrade; | |
proxy_set_header Connection \$connection_upgrade; | |
} | |
} | |
EOT | |
service nginx reload | |
} | |
function destroy_previous { | |
(docker ps -a --before="${APP_CID}" | awk '{ print $1,$2 }' | grep "${IMG_LABEL}" | awk '{print $1 }' | xargs -I {} docker stop {} | xargs -I {} docker rm {}) || echo "No previous container" | |
docker rmi ${IMG_LABEL}:prev || echo "No previous image" | |
} | |
if [ ! -f /opt/deployer.sh ]; | |
then | |
prepare_instance | |
download_app ${APP_INIT_URL} | |
else | |
download_app $1 | |
fi | |
dockerfile | |
build_image | |
run_container | |
setup_proxy | |
destroy_previous |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment