Skip to content

Commit

Permalink
Prod version of planet docker (connects #4) (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
empeje authored Nov 7, 2017
1 parent 7cf040a commit 3d0905d
Show file tree
Hide file tree
Showing 18 changed files with 251 additions and 78 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/
.git/
coverage/

.dockerignore
Dockerfile

.gitignore
.env
.eslintrc
.editorconfig
.travis.yml

14 changes: 10 additions & 4 deletions couchdb-setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

# Function for upsert of design docs
upsert_design() {
Expand All @@ -24,21 +24,27 @@ insert_docs() {
}

# Options are -u for username -w for passWord and -p for port number
while getopts "u:w:p:" option; do
while getopts "u:w:p:h:" option; do
case $option in
u) COUCHUSER=${OPTARG};;
w) COUCHPASSWORD=${OPTARG};;
p) PORT=${OPTARG};;
h) HOST=${OPTARG};;
esac
done

if [ -z "$HOST" ]
then
HOST=127.0.0.1
fi

# Default port for CouchDB accessed from host machine is 2200
PORT=${PORT:-2200}
if [ -z "$COUCHUSER" ]
then
COUCHURL=http://127.0.0.1:$PORT
COUCHURL=http://$HOST:$PORT
else
COUCHURL=http://$COUCHUSER:$COUCHPASSWORD@127.0.0.1:$PORT
COUCHURL=http://$COUCHUSER:$COUCHPASSWORD@$HOST:$PORT
fi

# Add CouchDB standard databases
Expand Down
19 changes: 15 additions & 4 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

DOCKER_ORG=treehouses
DOCKER_REPO=planet-dev
DOCKER_REPO=planet
DOCKER_REPO_DEV=planet-dev
VERSION=$(cat package.json | grep version | awk '{print$2}' | awk '{print substr($0, 2, length($0) - 3)}')
BRANCH=$TRAVIS_BRANCH
COMMIT=${TRAVIS_COMMIT::8}
Expand All @@ -17,26 +18,36 @@ build_message(){

build_docker() {
build_message Build the docker images ...
docker build -f ./docker/planet/Dockerfile ./docker/planet -t $DOCKER_ORG/$DOCKER_REPO:$VERSION-$BRANCH-$COMMIT
docker build -f ./docker/db-init/Dockerfile ./docker/db-init -t $DOCKER_ORG/$DOCKER_REPO:db-init-$VERSION-$BRANCH-$COMMIT
docker build -f ./docker/planet-dev/Dockerfile -t $DOCKER_ORG/$DOCKER_REPO_DEV:$VERSION-$BRANCH-$COMMIT ./docker/planet-dev
docker build -f ./docker/planet/Dockerfile -t $DOCKER_ORG/$DOCKER_REPO:$VERSION-$BRANCH-$COMMIT .
docker build -f ./docker/db-init/Dockerfile -t $DOCKER_ORG/$DOCKER_REPO:db-init-$VERSION-$BRANCH-$COMMIT .
}

tag_latest_docker() {
build_message Tag latest docker images ...
docker tag $DOCKER_ORG/$DOCKER_REPO_DEV:$VERSION-$BRANCH-$COMMIT $DOCKER_ORG/$DOCKER_REPO_DEV:latest
docker tag $DOCKER_ORG/$DOCKER_REPO:$VERSION-$BRANCH-$COMMIT $DOCKER_ORG/$DOCKER_REPO:latest
docker tag $DOCKER_ORG/$DOCKER_REPO:db-init-$VERSION-$BRANCH-$COMMIT $DOCKER_ORG/$DOCKER_REPO:db-init-latest
}

push_docker() {
build_message Pushing docker images ...
docker push $DOCKER_ORG/$DOCKER_REPO_DEV:$VERSION-$BRANCH-$COMMIT
sleep 5s
docker push $DOCKER_ORG/$DOCKER_REPO:$VERSION-$BRANCH-$COMMIT
sleep 5s
docker push $DOCKER_ORG/$DOCKER_REPO:db-init-$VERSION-$BRANCH-$COMMIT
sleep 5s
}

push_latest_docker() {
build_message Pushing latest docker images ...
docker push $DOCKER_ORG/$DOCKER_REPO_DEV:latest
sleep 5s
docker push $DOCKER_ORG/$DOCKER_REPO:latest
sleep 5s
docker push $DOCKER_ORG/$DOCKER_REPO:db-init-latest
sleep 5s
}

docker login -u $DOCKER_USER -p $DOCKER_PASS
Expand All @@ -49,4 +60,4 @@ if [[ $BRANCH = master ]];
fi

build_message Building Raspberry Pi docker image...
ssh -o StrictHostKeyChecking=no -p 22 travis@docker.ole.org 'bash -s' -- < ./deploy/deploy_rpi.sh --branch="$BRANCH" --commit="$TRAVIS_COMMIT" --pull="$TRAVIS_PULL_REQUEST" --duser="$DOCKER_USER" --dpass="$DOCKER_PASS"
ssh -o StrictHostKeyChecking=no -p 22 travis@kraken.ole.org 'bash -s' -- < ./deploy/deploy_rpi.sh --branch="$BRANCH" --commit="$TRAVIS_COMMIT" --pull="$TRAVIS_PULL_REQUEST" --duser="$DOCKER_USER" --dpass="$DOCKER_PASS"
14 changes: 10 additions & 4 deletions deploy/deploy_rpi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,31 @@ remove_temporary_folders(){

build_docker() {
build_message Build the docker images ...
build_message Deploy planet as $DOCKER_ORG/$DOCKER_REPO:rpi-$VERSION-$BRANCH-$COMMIT
build_message Deploy planet as $DOCKER_ORG/$DOCKER_REPO_DEV:rpi-$VERSION-$BRANCH-$COMMIT
build_message Deploy planet production as $DOCKER_ORG/$DOCKER_REPO:rpi-$VERSION-$BRANCH-$COMMIT
build_message Deploy db-init as $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-$VERSION-$BRANCH-$COMMIT
docker build -f ./docker/planet/rpi-Dockerfile -t $DOCKER_ORG/$DOCKER_REPO:rpi-$VERSION-$BRANCH-$COMMIT ./docker/planet
docker build -f ./docker/db-init/rpi-Dockerfile -t $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-$VERSION-$BRANCH-$COMMIT ./docker/db-init
docker build -f ./docker/planet-dev/rpi-Dockerfile -t $DOCKER_ORG/$DOCKER_REPO_DEV:rpi-$VERSION-$BRANCH-$COMMIT ./docker/planet-dev
docker build -f ./docker/planet/rpi-Dockerfile -t $DOCKER_ORG/$DOCKER_REPO:rpi-$VERSION-$BRANCH-$COMMIT .
docker build -f ./docker/db-init/rpi-Dockerfile -t $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-$VERSION-$BRANCH-$COMMIT .
}

tag_latest_docker() {
build_message Tag latest docker images ...
docker tag $DOCKER_ORG/$DOCKER_REPO_DEV:rpi-$VERSION-$BRANCH-$COMMIT $DOCKER_ORG/$DOCKER_REPO_DEV:rpi-latest
docker tag $DOCKER_ORG/$DOCKER_REPO:rpi-$VERSION-$BRANCH-$COMMIT $DOCKER_ORG/$DOCKER_REPO:rpi-latest
docker tag $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-$VERSION-$BRANCH-$COMMIT $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-latest
}

push_docker() {
build_message Pushing docker images ...
docker push $DOCKER_ORG/$DOCKER_REPO_DEV:rpi-$VERSION-$BRANCH-$COMMIT
docker push $DOCKER_ORG/$DOCKER_REPO:rpi-$VERSION-$BRANCH-$COMMIT
docker push $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-$VERSION-$BRANCH-$COMMIT
}

push_latest_docker() {
build_message Pushing latest docker images ...
docker push $DOCKER_ORG/$DOCKER_REPO_DEV:rpi-latest
docker push $DOCKER_ORG/$DOCKER_REPO:rpi-latest
docker push $DOCKER_ORG/$DOCKER_REPO:rpi-db-init-latest
}
Expand All @@ -102,7 +107,8 @@ create_footprint() {
RANDOM_FINGERPRINT=$(random_generator)
login_docker
DOCKER_ORG=treehouses
DOCKER_REPO=planet-dev
DOCKER_REPO=planet
DOCKER_REPO_DEV=planet-dev
BRANCH=$branch
COMMIT=${commit::8}

Expand Down
12 changes: 8 additions & 4 deletions docker/db-init/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
FROM node:6.11-alpine

WORKDIR /root

RUN apk update ;\
apk add --no-cache curl git
apk add --no-cache curl git ;\
#CORS DOWNLOAD
npm install -g add-cors-to-couchdb

WORKDIR /root

COPY ./docker-entrypoint.sh /root/docker-entrypoint.sh
COPY ./docker/db-init/docker-entrypoint.sh /root/docker-entrypoint.sh
COPY ./couchdb-setup.sh /root/couchdb-setup.sh
ADD ./design /root/design

CMD sh ./docker-entrypoint.sh
17 changes: 3 additions & 14 deletions docker/db-init/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#!/bin/bash

#CORS DOWNLOAD
git clone https://github.com/pouchdb/add-cors-to-couchdb.git
#!/bin/sh

#WAIT_TIME
echo "Waiting for couchdb to start"
Expand All @@ -13,15 +10,7 @@ until curl couchdb:5984 || [ $WAIT_TIME -eq 180 ]; do
done

#CORS SETUP
cd add-cors-to-couchdb
npm install
node bin.js http://couchdb:5984
cd -
add-cors-to-couchdb http://couchdb:5984

#MIGRATOR
curl -X PUT http://couchdb:5984/_users
curl -X PUT http://couchdb:5984/_replicator
curl -X PUT http://couchdb:5984/_global_changes
curl -X PUT http://couchdb:5984/meetups
curl -X PUT http://couchdb:5984/courses
curl -X PUT http://couchdb:5984/courses/_design/course-validators -d @../../design/courses/course-validators.json
./couchdb-setup.sh -p 5984 -h couchdb
13 changes: 8 additions & 5 deletions docker/db-init/rpi-Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM arm32v7/node:6.11-slim
FROM hypriot/rpi-node:6.10.0-alpine
RUN apk update ;\
apk add --no-cache curl git ;\
#CORS DOWNLOAD
npm install -g add-cors-to-couchdb

WORKDIR /root

RUN apt-get update ;\
apt-get install git curl -y

COPY ./docker-entrypoint.sh /root/docker-entrypoint.sh
COPY ./docker/db-init/docker-entrypoint.sh /root/docker-entrypoint.sh
COPY ./couchdb-setup.sh /root/couchdb-setup.sh
ADD ./design /root/design

CMD sh ./docker-entrypoint.sh
2 changes: 1 addition & 1 deletion docker/planet-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
- "2200:5984"
- "2201:5986"
db-init:
image: treehouses/planet-dev:db-init-latest
image: treehouses/planet:db-init-latest
depends_on:
- couchdb
planet:
Expand Down
22 changes: 22 additions & 0 deletions docker/planet-dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:6.11-stretch

MAINTAINER Abdurrachman Mappuji <[email protected]>

ENV APP planet

# install required software before using nvm/node/npm/bower
RUN apt-get update \
&& apt-get install -y curl git python build-essential \
# install angular cli
&& npm install -g @angular/cli

# exposes port 3000 but your app may use any port specified in it
EXPOSE 3000

VOLUME ["/$APP"]
WORKDIR /$APP

#ADD ./docker-entrypoint.sh docker-entrypoint.sh

#ENTRYPOINT ["/bin/bash", "-c"]
CMD bash -c 'npm install && ng serve'
24 changes: 24 additions & 0 deletions docker/planet-dev/rpi-Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM arm32v7/node:6.11-stretch

MAINTAINER Abdurrachman Mappuji <[email protected]>

ENV APP planet

# install required software before using nvm/node/npm/bower
RUN apt-get update \
&& apt-get install -y curl git python build-essential \
# install angular cli
&& npm install -g @angular/cli

RUN npm rebuild node-sass

# exposes port 3000 but your app may use any port specified in it
EXPOSE 3000

VOLUME ["/$APP"]
WORKDIR /$APP

#ADD ./docker-entrypoint.sh docker-entrypoint.sh

#ENTRYPOINT ["/bin/bash", "-c"]
CMD bash -c 'npm install && npm rebuild node-sass && ng serve --host 0.0.0.0 --disable-host-check'
23 changes: 23 additions & 0 deletions docker/planet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
couchdb:
expose:
- 5984
image: klaemo/couchdb
ports:
- "2200:5984"
- "2201:5986"
db-init:
image: treehouses/planet:db-init-latest
depends_on:
- couchdb
planet:
image: treehouses/planet:latest
ports:
- "80:80"
environment:
- HOST_PROTOCOL=http
- DB_HOST=127.0.0.1
- DB_PORT=2200
depends_on:
- couchdb
version: "2"
39 changes: 20 additions & 19 deletions docker/planet/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
FROM node:6.11-stretch

### STAGE 1: Build ###
# We label our stage as ‘builder’
FROM node:6.11-alpine as builder
MAINTAINER Abdurrachman Mappuji <[email protected]>
COPY package.json ./
## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app
WORKDIR /ng-app
COPY . .
## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod

ENV APP planet

# install required software before using nvm/node/npm/bower
RUN apt-get update \
&& apt-get install -y curl git python build-essential \
# install angular cli
&& npm install -g @angular/cli

# exposes port 3000 but your app may use any port specified in it
EXPOSE 3000

VOLUME ["/$APP"]
WORKDIR /$APP

#ADD ./docker-entrypoint.sh docker-entrypoint.sh

#ENTRYPOINT ["/bin/bash", "-c"]
CMD bash -c 'npm install && ng serve'
### STAGE 2: Setup ###
FROM nginx:1.13.3-alpine
## Copy our default nginx config
COPY docker/planet/default.conf /etc/nginx/conf.d/
## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*
## From ‘builder’ stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/dist /usr/share/nginx/html
COPY docker/planet/docker-entrypoint.sh ./
CMD sh ./docker-entrypoint.sh
27 changes: 27 additions & 0 deletions docker/planet/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
server {

listen 80;

sendfile on;

default_type application/octet-stream;


gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1100;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 9;


root /usr/share/nginx/html;


location / {
try_files $uri $uri/ /index.html =404;
}

}
17 changes: 17 additions & 0 deletions docker/planet/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

if [ -z "$HOST_PROTOCOL" ]
then
PROTOCOL="http"
elif [ "$HOST_PROTOCOL" = "https" ]
then
PROTOCOL="https"
else
PROTOCOL="http"
fi

sed -i -e "s#planet-db-host#$PROTOCOL://$DB_HOST#g" /usr/share/nginx/html/main*
sed -i -e "s#planet-db-port#$DB_PORT#g" /usr/share/nginx/html/main*

nginx -g "daemon off;"

Loading

0 comments on commit 3d0905d

Please sign in to comment.