diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000..2f4f378cbe --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +node_modules/ +.git/ +coverage/ + +.dockerignore +Dockerfile + +.gitignore +.env +.eslintrc +.editorconfig +.travis.yml + diff --git a/couchdb-setup.sh b/couchdb-setup.sh index 1abc51059c..d2bfc44761 100755 --- a/couchdb-setup.sh +++ b/couchdb-setup.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # Function for upsert of design docs upsert_design() { @@ -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 diff --git a/deploy/deploy.sh b/deploy/deploy.sh index 84c907c09c..2288addba9 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -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} @@ -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 @@ -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" diff --git a/deploy/deploy_rpi.sh b/deploy/deploy_rpi.sh index e6ecf0e802..961947f772 100644 --- a/deploy/deploy_rpi.sh +++ b/deploy/deploy_rpi.sh @@ -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 } @@ -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} diff --git a/docker/db-init/Dockerfile b/docker/db-init/Dockerfile index 131362eb8d..d768a3fd0e 100644 --- a/docker/db-init/Dockerfile +++ b/docker/db-init/Dockerfile @@ -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 diff --git a/docker/db-init/docker-entrypoint.sh b/docker/db-init/docker-entrypoint.sh index 8c40240a84..6aed9e436b 100755 --- a/docker/db-init/docker-entrypoint.sh +++ b/docker/db-init/docker-entrypoint.sh @@ -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" @@ -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 diff --git a/docker/db-init/rpi-Dockerfile b/docker/db-init/rpi-Dockerfile index 43b8223623..f6a89683c3 100644 --- a/docker/db-init/rpi-Dockerfile +++ b/docker/db-init/rpi-Dockerfile @@ -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 diff --git a/docker/planet-dev.yml b/docker/planet-dev.yml index c8679ab422..ec3e7db850 100644 --- a/docker/planet-dev.yml +++ b/docker/planet-dev.yml @@ -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: diff --git a/docker/planet-dev/Dockerfile b/docker/planet-dev/Dockerfile new file mode 100644 index 0000000000..8458f65f66 --- /dev/null +++ b/docker/planet-dev/Dockerfile @@ -0,0 +1,22 @@ +FROM node:6.11-stretch + +MAINTAINER Abdurrachman Mappuji + +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' diff --git a/docker/planet-dev/rpi-Dockerfile b/docker/planet-dev/rpi-Dockerfile new file mode 100644 index 0000000000..bad6abdb44 --- /dev/null +++ b/docker/planet-dev/rpi-Dockerfile @@ -0,0 +1,24 @@ +FROM arm32v7/node:6.11-stretch + +MAINTAINER Abdurrachman Mappuji + +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' diff --git a/docker/planet.yml b/docker/planet.yml new file mode 100644 index 0000000000..351cb23ea3 --- /dev/null +++ b/docker/planet.yml @@ -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" diff --git a/docker/planet/Dockerfile b/docker/planet/Dockerfile index 8458f65f66..d9891a8dca 100644 --- a/docker/planet/Dockerfile +++ b/docker/planet/Dockerfile @@ -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 +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 diff --git a/docker/planet/default.conf b/docker/planet/default.conf new file mode 100644 index 0000000000..4909112227 --- /dev/null +++ b/docker/planet/default.conf @@ -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; + } + +} diff --git a/docker/planet/docker-entrypoint.sh b/docker/planet/docker-entrypoint.sh new file mode 100755 index 0000000000..9d63c9216b --- /dev/null +++ b/docker/planet/docker-entrypoint.sh @@ -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;" + diff --git a/docker/planet/rpi-Dockerfile b/docker/planet/rpi-Dockerfile index bad6abdb44..f9c52d1c8a 100644 --- a/docker/planet/rpi-Dockerfile +++ b/docker/planet/rpi-Dockerfile @@ -1,24 +1,28 @@ -FROM arm32v7/node:6.11-stretch - +### STAGE 1: Build ### +# We label our stage as ‘builder’ +FROM arm32v7/node:6.11-stretch as builder MAINTAINER Abdurrachman Mappuji - -ENV APP planet - -# install required software before using nvm/node/npm/bower +COPY package.json ./ +## Storing node modules on a separate layer will prevent unnecessary npm installs at each build 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' + && npm install node-sass \ + && npm rebuild node-sass \ + && npm install \ + && 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 + + +### STAGE 2: Setup ### +FROM tobi312/rpi-nginx: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 diff --git a/docker/rpi-planet-dev.yml b/docker/rpi-planet-dev.yml index ea6d40c72c..338e13b32b 100644 --- a/docker/rpi-planet-dev.yml +++ b/docker/rpi-planet-dev.yml @@ -7,7 +7,7 @@ services: - "2200:5984" - "2201:5986" db-init: - image: treehouses/planet-dev:rpi-db-init-latest + image: treehouses/planet:rpi-db-init-latest depends_on: - couchdb planet: diff --git a/docker/rpi-planet.yml b/docker/rpi-planet.yml new file mode 100644 index 0000000000..9d3c5b684d --- /dev/null +++ b/docker/rpi-planet.yml @@ -0,0 +1,23 @@ +services: + couchdb: + expose: + - 5984 + image: treehouses/rpi-couchdb:2.0.0 + ports: + - "2200:5984" + - "2201:5986" + db-init: + image: treehouses/planet:rpi-db-init-latest + depends_on: + - couchdb + planet: + image: treehouses/planet:rpi-latest + ports: + - "80:80" + environment: + - HOST_PROTOCOL=http + - DB_HOST=127.0.0.1 + - DB_PORT=2200 + depends_on: + - couchdb +version: "2" diff --git a/src/environments/environment.prod.ts b/src/environments/environment.prod.ts index 5018742b0f..9cb5cc6f68 100644 --- a/src/environments/environment.prod.ts +++ b/src/environments/environment.prod.ts @@ -1,5 +1,5 @@ export const environment = { production: true, // Change this to Docker address - couchAddress: 'http://127.0.0.1:2200/' + couchAddress: 'planet-db-host:planet-db-port/' };