From e9e167e4122b29084d79e1ad8ccbeaf318bdcfb0 Mon Sep 17 00:00:00 2001 From: Michal Szczur Date: Sun, 18 Sep 2016 22:17:06 +0200 Subject: [PATCH 1/2] Added Docker config --- Dockerfile | 38 +++++++++++++-- docker-compose.yml | 27 ++++++++++ docker.sh | 119 +++++++++++++++++++++++++++++++++++++++++++++ entrypoint.sh | 5 ++ package.json | 4 +- 5 files changed, 188 insertions(+), 5 deletions(-) create mode 100644 docker-compose.yml create mode 100755 docker.sh create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index cb51733..6e5ab9b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,35 @@ -# Builds a Docker to deliver dist/ -FROM nginx:latest -COPY dist/ /usr/share/nginx/html \ No newline at end of file +FROM node:5 + +MAINTAINER mkruczek@pgs-soft.com + +ARG USERID=1000 + +RUN usermod -u $USERID www-data && \ + mkdir -p /var/www && \ + mkdir -p /app && \ + chown -R www-data:www-data /var/www && \ + chown -R www-data:www-data /app + +RUN npm config set registry https://registry.npmjs.org/ && \ + npm install --allow-root -g -d --silent webpack && \ + npm install --allow-root -g -d --silent webpack-merge && \ + npm install --allow-root -g -d --silent webpack-dev-server && \ + npm install --allow-root -g -d --silent karma && \ + npm install --allow-root -g -d --silent protractor && \ + npm install --allow-root -g -d --silent typescript && \ + npm install --allow-root -g -d --silent rimraf + +USER www-data + +ADD package.json /app/package.json + +RUN cd /app/ && \ + npm cache clear && \ + npm config set registry https://registry.npmjs.org/ && \ + /usr/local/bin/node --stack-size=128000 /usr/local/bin/npm install --no-bin-links --quiet + +WORKDIR /var/www/frontend + +COPY entrypoint.sh /entrypoint.sh + +CMD ["/bin/bash", "/entrypoint.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a4f721b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,27 @@ +frontend: + image: $APP_NAME:$APP_VERSION + volumes: + - .:/var/www/frontend + +hub: + image: selenium/hub + environment: + - GRID_TIMEOUT=30000 + +firefox: + image: selenium/node-firefox + links: + - hub + - frontend + environment: + SCREEN_WIDTH: "1366" + SCREEN_HEIGHT: "768" + +chrome: + image: selenium/node-chrome + links: + - hub + - frontend + environment: + SCREEN_WIDTH: "1366" + SCREEN_HEIGHT: "768" \ No newline at end of file diff --git a/docker.sh b/docker.sh new file mode 100755 index 0000000..f60487b --- /dev/null +++ b/docker.sh @@ -0,0 +1,119 @@ +#!/bin/bash + +export TASK_NAME=$1; +export APP_VERSION=$(echo $(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g')) +echo "App image version: $APP_VERSION" +export APP_NAME=$(echo $(cat package.json | grep name | head -1 | awk -F: '{ print $2 }' | sed 's/[",\r]//g')) +echo $APP_NAME; +export USERID=$(id -u); +echo "User ID: $USERID"; + +function runBashInsideImage { + docker-compose run --service-ports --entrypoint /bin/bash frontend +} + +function buildProject { + if docker history -q "${APP_NAME}:${APP_VERSION}" > /dev/null 2>&1; then + echo "${APP_NAME}:${APP_VERSION} exists" + else + docker build -t "${APP_NAME}:${APP_VERSION}" --build-arg USERID=$USERID . + fi + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + BUILD_STATUS=$(docker-compose up frontend) + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + echo "$BUILD_STATUS"; + if echo "$BUILD_STATUS" | grep -q "exited with code 1"; then + return 1; + else + return 0; + fi +} + +function runUnitTests { + if ! docker history -q "${APP_NAME}:${APP_VERSION}" > /dev/null 2>&1; then + buildProject + fi + + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + BUILD_STATUS=`docker-compose up unit` + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + echo "$BUILD_STATUS"; + if echo "$BUILD_STATUS" | grep -q "exited with code 1"; then + return 1; + else + return 0; + fi +} + +function runAcceptanceTests { + if ! docker history -q "${APP_NAME}:${APP_VERSION}" > /dev/null 2>&1; then + buildProject + fi + + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + docker-compose up -d hub + docker-compose up -d firefox + docker-compose up -d chrome + sleep 20; + BUILD_STATUS=`docker-compose up e2e` + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + echo "$BUILD_STATUS"; + if echo "$BUILD_STATUS" | grep -q "exited with code 1"; then + return 1; + else + return 0; + fi +} + +function gulpServe { + if ! docker history -q "${APP_NAME}:${APP_VERSION}" > /dev/null 2>&1; then + buildProject + fi + docker-compose kill > /dev/null 2>&1 + docker-compose rm -f -v > /dev/null 2>&1 + docker-compose -f docker-compose.yml -f docker-compose.local.yml run --service-ports -d --entrypoint bash frontend /var/www/frontend/entrypoint_serve.sh +} + +if [[ $TASK_NAME == 'build' ]] + then + buildProject +fi + +if [[ $TASK_NAME == 'unit' ]] + then + runUnitTests +fi + +if [[ $TASK_NAME == 'e2e' ]] + then + runAcceptanceTests +fi + +if [[ $TASK_NAME == 'run' ]] + then + runBashInsideImage +fi + +if [[ $TASK_NAME == 'serve' ]] + then + gulpServe +fi + + +if [[ $TASK_NAME == '' ]]; then + echo -e "\n\033[0;31mAll tasks are running within docker containers !!! \033[0m\n" + echo -e "Available commands:"; + echo -e "'build' \t- is running 'gulp build'"; + echo -e "'unit' \t- is running unit tests"; + echo -e "'e2e' \t- is running e2e tests"; + echo -e "'run' \t- is running docker container in attach mode (you will be logged in)"; + echo -e "'serve' \t- is running 'gulp server' task, app config available in 'docker-compose.local.yml' that extends from 'docker-compose.yml'"; +fi + +exit $? \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..59587e6 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash -e + +export NODE_PATH="/app/node_modules" + +npm run watch \ No newline at end of file diff --git a/package.json b/package.json index 9b83e7e..67bdfeb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cv_generator_frontend", - "version": "1.0.0", + "version": "1.0.3", "description": "Generator for CV's in PGS Software", "keywords": [ "angular2", @@ -128,7 +128,7 @@ "typedoc": "^0.4.5", "typescript": "2.0.0", "url-loader": "^0.5.7", - "webpack": "^2.1.0-beta.21", + "webpack": "^2.1.0-beta.22", "webpack-dev-middleware": "^1.6.1", "webpack-dev-server": "^2.1.0-beta.2", "webpack-md5-hash": "^0.0.5", From 62a07cdbb505998dab2f27678f4b54ef131c5816 Mon Sep 17 00:00:00 2001 From: Michal Szczur Date: Sun, 18 Sep 2016 23:02:30 +0200 Subject: [PATCH 2/2] Improved configuration for Docker --- Dockerfile | 21 ++++++++++++--------- config/webpack.dev.js | 2 +- config/webpack.prod.js | 2 +- package.json | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6e5ab9b..c6e43de 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,13 +11,14 @@ RUN usermod -u $USERID www-data && \ chown -R www-data:www-data /app RUN npm config set registry https://registry.npmjs.org/ && \ - npm install --allow-root -g -d --silent webpack && \ - npm install --allow-root -g -d --silent webpack-merge && \ - npm install --allow-root -g -d --silent webpack-dev-server && \ - npm install --allow-root -g -d --silent karma && \ - npm install --allow-root -g -d --silent protractor && \ - npm install --allow-root -g -d --silent typescript && \ - npm install --allow-root -g -d --silent rimraf + npm install --allow-root -g -d --silent \ + webpack \ + webpack-merge \ + webpack-dev-server \ + karma \ + protractor \ + typescript \ + rimraf USER www-data @@ -26,10 +27,12 @@ ADD package.json /app/package.json RUN cd /app/ && \ npm cache clear && \ npm config set registry https://registry.npmjs.org/ && \ - /usr/local/bin/node --stack-size=128000 /usr/local/bin/npm install --no-bin-links --quiet + /usr/local/bin/node --stack-size=192000 /usr/local/bin/npm install --no-bin-links --quiet + +EXPOSE 3000 8080 WORKDIR /var/www/frontend COPY entrypoint.sh /entrypoint.sh -CMD ["/bin/bash", "/entrypoint.sh"] \ No newline at end of file +CMD ["/bin/bash", "/entrypoint.sh"] diff --git a/config/webpack.dev.js b/config/webpack.dev.js index 38bcee4..80810df 100644 --- a/config/webpack.dev.js +++ b/config/webpack.dev.js @@ -16,7 +16,7 @@ const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin'); * Webpack Constants */ const ENV = process.env.ENV = process.env.NODE_ENV = 'development'; -const HOST = process.env.HOST || 'localhost'; +const HOST = process.env.HOST || '0.0.0.0'; const PORT = process.env.PORT || 3000; const HMR = helpers.hasProcessFlag('hot'); const METADATA = webpackMerge(commonConfig({env: ENV}).metadata, { diff --git a/config/webpack.prod.js b/config/webpack.prod.js index 0574ce2..3898a14 100644 --- a/config/webpack.prod.js +++ b/config/webpack.prod.js @@ -21,7 +21,7 @@ const WebpackMd5Hash = require('webpack-md5-hash'); * Webpack Constants */ const ENV = process.env.NODE_ENV = process.env.ENV = 'production'; -const HOST = process.env.HOST || 'localhost'; +const HOST = process.env.HOST || '0.0.0.0'; const PORT = process.env.PORT || 8080; const METADATA = webpackMerge(commonConfig({env: ENV}).metadata, { host: HOST, diff --git a/package.json b/package.json index 67bdfeb..5991b80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cv_generator_frontend", - "version": "1.0.3", + "version": "1.0.4", "description": "Generator for CV's in PGS Software", "keywords": [ "angular2",