From f83e98818a4023015058ca8a6d6a6e718570cfd1 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 2 May 2016 11:01:38 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B8=D1=87=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D0=BA=D0=BE=D0=BD=D1=84=D0=B8=D0=B3=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=BA=D0=B5=D1=80=D0=B0=20(=D0=BF=D0=BE=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D1=83=D0=B0=D0=BB=D0=B0=D0=BC=20=D0=B8=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BF=D0=B8=D0=BF=D0=B0=D1=81=D1=82=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 8 ++++++ .env | 16 +++++++++++ Dockerfile | 24 +++++++++++++++++ docker-compose.yml | 36 +++++++++++++++++++++++++ nginx/Dockerfile | 2 ++ nginx/nginx.conf | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 153 insertions(+) create mode 100644 .dockerignore create mode 100644 .env create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 nginx/Dockerfile create mode 100644 nginx/nginx.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..19bcbdb --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git/* +.env +api/config/*-local.php +common/config/*-local.php +console/config/*-local.php +api/runtime +console/runtime +api/web/assets diff --git a/.env b/.env new file mode 100644 index 0000000..b068e70 --- /dev/null +++ b/.env @@ -0,0 +1,16 @@ +# Whether to enable debug mode in Yii. If not set this will be 0. +YII_DEBUG=1 + +# The application mode. If not set, this will be 'prod' +YII_ENV=dev + +# The log trace level. If not set, this will be 0 +#YII_TRACELEVEL=0 + +# Make sure that you provide a different unique cookie validation key in production +COOKIE_VALIDATION_KEY="SeCrEt_DeV_Key--DO-NOT-USE-IN-PRODUCTION!" + +# DB credentials. Default is to fetch the host form docker vars and use 'web' as db name, username and password +#DB_DSN=mysql:host=my.dbhost.com;dbname=web +#DB_USER=user +#DB_PASSWORD=secret diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7577d6f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +#FROM codemix/yii2-base:2.0.8-apache +FROM codemix/yii2-base:2.0.8-php-fpm +#FROM codemix/yii2-base:2.0.8-hhvm + + +# Composer packages are installed first. This will only add packages +# that are not already in the yii2-base image. +COPY composer.json /var/www/html/ +COPY composer.lock /var/www/html/ +RUN composer self-update --no-progress && \ + composer install --no-progress --ignore-platform-reqs + +# Copy the working dir to the image's web root +COPY . /var/www/html + +# The following directories are .dockerignored to not pollute the docker images +# with local logs and published assets from development. So we need to create +# empty dirs and set right permissions inside the container. +RUN mkdir api/runtime api/web/assets console/runtime \ + && chown www-data:www-data api/runtime api/web/assets console/runtime + +# Expose everything under /var/www (vendor + html) +# This is only required for the nginx setup +VOLUME ["/var/www"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..4c4d378 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +app: + build: ./ + expose: + - "9000" + volumes: + - ./:/var/www/html/ + links: + - db + environment: + ENABLE_ENV_FILE: 1 + ENABLE_LOCALCONF: 1 + API_TOKEN: "78bb3e46d818793a299ccfcedee213d5ecad07f7" + +web: + build: ./nginx + ports: + - "8080:80" + links: + - app + volumes_from: + - app + +db: + image: mariadb:10.0 + ports: + - "3306:3306" + expose: + - "3306" + environment: + MYSQL_ROOT_PASSWORD: secret-root + MYSQL_DATABASE: web + MYSQL_USER: web + MYSQL_PASSWORD: web + + # Uncomment to autostart at boottime + #restart: always diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..ceab9b8 --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,2 @@ +FROM nginx:1.9 +COPY nginx.conf /etc/nginx/nginx.conf diff --git a/nginx/nginx.conf b/nginx/nginx.conf new file mode 100644 index 0000000..83e92db --- /dev/null +++ b/nginx/nginx.conf @@ -0,0 +1,67 @@ +user nginx; +worker_processes 1; + +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + #gzip on; + + server { + listen 80; + + set $root_path '/var/www/html'; + set $api_path '${root_path}/api/web'; + set $frontend_path '${root_path}/frontend/dist'; + + root $root_path; + charset utf-8; + client_max_body_size 100M; + + location / { + alias $frontend_path; + index index.html; + try_files $uri /index.html =404; + } + + location /api { + try_files $uri /api/web/index.php?$args; + } + + location ~* \.php$ { + fastcgi_pass app:9000; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + } + + # Раздача статики для frontend + location ~* ^.+\.(html|jpg|jpeg|gif|png|svg|js|json|css|zip|rar|eot|ttf|woff|ico) { + root $frontend_path; + expires max; + access_log off; + } + + location ~* \.(htaccess|htpasswd|svn|git) { + deny all; + } + } + +} From 7b7ed0ad51d4f758e5a281a33465831af696bb29 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Thu, 5 May 2016 11:15:53 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=D0=A3=D0=B6=D0=B5=20=D0=BF=D0=BE=D1=87?= =?UTF-8?q?=D1=82=D0=B8=20=D0=B2=D1=81=D1=91=20=D0=BD=D0=B0=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BD=D0=BE=20(=D0=BD=D0=BE=20=D1=81=20node-?= =?UTF-8?q?dev-server=20=D0=B5=D1=81=D1=82=D1=8C=20=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BB=D1=8B)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 1 + Dockerfile | 10 +- docker-compose.yml | 106 +++++++++++++------ {nginx => docker/nginx}/Dockerfile | 0 {nginx => docker/nginx}/nginx.conf | 0 wait-for-it.sh | 161 +++++++++++++++++++++++++++++ 6 files changed, 239 insertions(+), 39 deletions(-) rename {nginx => docker/nginx}/Dockerfile (100%) rename {nginx => docker/nginx}/nginx.conf (100%) create mode 100755 wait-for-it.sh diff --git a/.dockerignore b/.dockerignore index 19bcbdb..13ee54e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -6,3 +6,4 @@ console/config/*-local.php api/runtime console/runtime api/web/assets +#vendor diff --git a/Dockerfile b/Dockerfile index 7577d6f..0f89512 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,12 @@ -#FROM codemix/yii2-base:2.0.8-apache FROM codemix/yii2-base:2.0.8-php-fpm -#FROM codemix/yii2-base:2.0.8-hhvm - # Composer packages are installed first. This will only add packages # that are not already in the yii2-base image. COPY composer.json /var/www/html/ COPY composer.lock /var/www/html/ -RUN composer self-update --no-progress && \ - composer install --no-progress --ignore-platform-reqs + +RUN composer self-update --no-progress \ + && composer install --no-progress --ignore-platform-reqs # Copy the working dir to the image's web root COPY . /var/www/html @@ -17,7 +15,7 @@ COPY . /var/www/html # with local logs and published assets from development. So we need to create # empty dirs and set right permissions inside the container. RUN mkdir api/runtime api/web/assets console/runtime \ - && chown www-data:www-data api/runtime api/web/assets console/runtime + && chown www-data:www-data api/runtime api/web/assets console/runtime # Expose everything under /var/www (vendor + html) # This is only required for the nginx setup diff --git a/docker-compose.yml b/docker-compose.yml index 4c4d378..fb11214 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,36 +1,76 @@ -app: - build: ./ - expose: - - "9000" - volumes: - - ./:/var/www/html/ - links: - - db - environment: - ENABLE_ENV_FILE: 1 - ENABLE_LOCALCONF: 1 - API_TOKEN: "78bb3e46d818793a299ccfcedee213d5ecad07f7" +version: '2' +services: + web: + build: ./docker/nginx + ports: + - "80:80" + links: + - app + volumes_from: + - app -web: - build: ./nginx - ports: - - "8080:80" - links: - - app - volumes_from: - - app + app: + build: . + expose: + - "9000" + volumes: + - ./:/var/www/html/ + links: + - db + - redis + - rabbitmq + depends_on: + - node-dev-server + - app-console-account-queue + environment: + ENABLE_ENV_FILE: 1 + ENABLE_LOCALCONF: 1 + API_TOKEN: "78bb3e46d818793a299ccfcedee213d5ecad07f7" -db: - image: mariadb:10.0 - ports: - - "3306:3306" - expose: - - "3306" - environment: - MYSQL_ROOT_PASSWORD: secret-root - MYSQL_DATABASE: web - MYSQL_USER: web - MYSQL_PASSWORD: web + app-console-account-queue: + image: php:7.0-cli + volumes: + - ./:/var/www/html/ + working_dir: /var/www/html/ + command: ./wait-for-it.sh rabbitmq:5672 -- ./yii account-queue + links: + - db + - redis + - rabbitmq - # Uncomment to autostart at boottime - #restart: always + node-dev-server: + build: ./frontend + ports: + - "8080:8080" + volumes: + - ./frontend/:/usr/src/app/ + - /usr/src/app/node_modules + environment: + DOCKERIZED: "true" + + db: + image: mariadb:10.0 + expose: + - "3306" + environment: + MYSQL_ROOT_PASSWORD: secret-root + MYSQL_DATABASE: ely_accounts + MYSQL_USER: ely-accounts-user + MYSQL_PASSWORD: ely-accounts-password + TERM: dumb + # Uncomment to autostart at boottime + #restart: always + + redis: + image: redis:3.0 + expose: + - "6379" + + rabbitmq: + image: rabbitmq:3.6 + expose: + - "5672" + environment: + RABBITMQ_DEFAULT_USER: ely-accounts-app + RABBITMQ_DEFAULT_PASS: app-password + RABBITMQ_DEFAULT_VHOST: /account.ely.by diff --git a/nginx/Dockerfile b/docker/nginx/Dockerfile similarity index 100% rename from nginx/Dockerfile rename to docker/nginx/Dockerfile diff --git a/nginx/nginx.conf b/docker/nginx/nginx.conf similarity index 100% rename from nginx/nginx.conf rename to docker/nginx/nginx.conf diff --git a/wait-for-it.sh b/wait-for-it.sh new file mode 100755 index 0000000..eca6c3b --- /dev/null +++ b/wait-for-it.sh @@ -0,0 +1,161 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +cmdname=$(basename $0) + +echoerr() { if [[ $QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $TIMEOUT -gt 0 ]]; then + echoerr "$cmdname: waiting $TIMEOUT seconds for $HOST:$PORT" + else + echoerr "$cmdname: waiting for $HOST:$PORT without a timeout" + fi + start_ts=$(date +%s) + while : + do + (echo > /dev/tcp/$HOST/$PORT) >/dev/null 2>&1 + result=$? + if [[ $result -eq 0 ]]; then + end_ts=$(date +%s) + echoerr "$cmdname: $HOST:$PORT is available after $((end_ts - start_ts)) seconds" + break + fi + sleep 1 + done + return $result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $QUIET -eq 1 ]]; then + timeout $TIMEOUT $0 --quiet --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & + else + timeout $TIMEOUT $0 --child --host=$HOST --port=$PORT --timeout=$TIMEOUT & + fi + PID=$! + trap "kill -INT -$PID" INT + wait $PID + RESULT=$? + if [[ $RESULT -ne 0 ]]; then + echoerr "$cmdname: timeout occurred after waiting $TIMEOUT seconds for $HOST:$PORT" + fi + return $RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + hostport=(${1//:/ }) + HOST=${hostport[0]} + PORT=${hostport[1]} + shift 1 + ;; + --child) + CHILD=1 + shift 1 + ;; + -q | --quiet) + QUIET=1 + shift 1 + ;; + -s | --strict) + STRICT=1 + shift 1 + ;; + -h) + HOST="$2" + if [[ $HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + HOST="${1#*=}" + shift 1 + ;; + -p) + PORT="$2" + if [[ $PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + PORT="${1#*=}" + shift 1 + ;; + -t) + TIMEOUT="$2" + if [[ $TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + CLI="$@" + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$HOST" == "" || "$PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +TIMEOUT=${TIMEOUT:-15} +STRICT=${STRICT:-0} +CHILD=${CHILD:-0} +QUIET=${QUIET:-0} + +if [[ $CHILD -gt 0 ]]; then + wait_for + RESULT=$? + exit $RESULT +else + if [[ $TIMEOUT -gt 0 ]]; then + wait_for_wrapper + RESULT=$? + else + wait_for + RESULT=$? + fi +fi + +if [[ $CLI != "" ]]; then + if [[ $RESULT -ne 0 && $STRICT -eq 1 ]]; then + echoerr "$cmdname: strict mode, refusing to execute subprocess" + exit $RESULT + fi + exec $CLI +else + exit $RESULT +fi From 23d69753994dd39ed9807ed054a71c4b81420e8e Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Tue, 10 May 2016 01:49:50 +0300 Subject: [PATCH 3/3] =?UTF-8?q?=D0=9E=D0=BA=D0=BE=D0=BD=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B2=D0=BD=D0=B5=D0=B4=D1=80=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=BA=D0=B5=D1=80=D0=B0=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=B2=D1=91=D1=80=D1=82=D1=8B=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20dev-=D0=BE=D0=BA=D1=80=D1=83=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 19 +++- .env | 16 --- .gitignore | 3 + Dockerfile | 53 +++++++-- README.md | 106 ++++++++++-------- docker-compose.yml | 66 +++++------ docker/mariadb/Dockerfile | 13 +++ docker/mariadb/init.sh | 34 ++++++ docker/mariadb/mariadb.cnf | 0 docker/mariadb/run.sh | 9 ++ docker/nginx/Dockerfile | 1 + docker/php/composer.sh | 8 ++ docker/rabbitmq/Dockerfile | 10 ++ docker/rabbitmq/init.sh | 44 ++++++++ docker/rabbitmq/run.sh | 8 ++ wait-for-it.sh => docker/wait-for-it.sh | 0 environments/docker/api/config/main-local.php | 24 ++++ .../docker/api/config/params-local.php | 4 + environments/docker/api/web/index-test.php | 18 +++ environments/docker/api/web/index.php | 18 +++ .../docker/common/config/main-local.php | 26 +++++ .../docker/common/config/params-local.php | 4 + .../docker/console/config/main-local.php | 7 ++ .../docker/console/config/params-local.php | 3 + environments/docker/yii | 28 +++++ environments/index.php | 14 +++ tests/codeception/api/functional.suite.yml | 4 +- tests/codeception/config/config.php | 2 +- 28 files changed, 425 insertions(+), 117 deletions(-) delete mode 100644 .env create mode 100644 docker/mariadb/Dockerfile create mode 100644 docker/mariadb/init.sh create mode 100644 docker/mariadb/mariadb.cnf create mode 100644 docker/mariadb/run.sh create mode 100644 docker/php/composer.sh create mode 100644 docker/rabbitmq/Dockerfile create mode 100644 docker/rabbitmq/init.sh create mode 100644 docker/rabbitmq/run.sh rename wait-for-it.sh => docker/wait-for-it.sh (100%) create mode 100644 environments/docker/api/config/main-local.php create mode 100644 environments/docker/api/config/params-local.php create mode 100644 environments/docker/api/web/index-test.php create mode 100644 environments/docker/api/web/index.php create mode 100644 environments/docker/common/config/main-local.php create mode 100644 environments/docker/common/config/params-local.php create mode 100644 environments/docker/console/config/main-local.php create mode 100644 environments/docker/console/config/params-local.php create mode 100644 environments/docker/yii diff --git a/.dockerignore b/.dockerignore index 13ee54e..75687c0 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,9 +1,16 @@ .git/* .env -api/config/*-local.php -common/config/*-local.php -console/config/*-local.php -api/runtime -console/runtime + +# vendor будет заполнен уже внутри контейнера +vendor +# frontend и его node_modules внутри контейнера не нужны +frontend + +# Все -local файлы +*/config/*-local.php + +# Все временные файлы +runtime + +# Их по идее и не должно образовываться, но мало ли api/web/assets -#vendor diff --git a/.env b/.env deleted file mode 100644 index b068e70..0000000 --- a/.env +++ /dev/null @@ -1,16 +0,0 @@ -# Whether to enable debug mode in Yii. If not set this will be 0. -YII_DEBUG=1 - -# The application mode. If not set, this will be 'prod' -YII_ENV=dev - -# The log trace level. If not set, this will be 0 -#YII_TRACELEVEL=0 - -# Make sure that you provide a different unique cookie validation key in production -COOKIE_VALIDATION_KEY="SeCrEt_DeV_Key--DO-NOT-USE-IN-PRODUCTION!" - -# DB credentials. Default is to fetch the host form docker vars and use 'web' as db name, username and password -#DB_DSN=mysql:host=my.dbhost.com;dbname=web -#DB_USER=user -#DB_PASSWORD=secret diff --git a/.gitignore b/.gitignore index 63f5aae..a218a54 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ phpunit.phar # npm debug npm-debug* + +# Docker override file +docker-compose.override.yml diff --git a/Dockerfile b/Dockerfile index 0f89512..5ba392e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,51 @@ -FROM codemix/yii2-base:2.0.8-php-fpm +FROM php:7.0-fpm -# Composer packages are installed first. This will only add packages -# that are not already in the yii2-base image. -COPY composer.json /var/www/html/ -COPY composer.lock /var/www/html/ +ENV PATH $PATH:/root/.composer/vendor/bin -RUN composer self-update --no-progress \ - && composer install --no-progress --ignore-platform-reqs +# PHP extensions come first, as they are less likely to change between Yii releases +RUN apt-get update \ + && apt-get -y install \ + git \ + g++ \ + libicu-dev \ + libmcrypt-dev \ + zlib1g-dev \ + --no-install-recommends \ + + # Install PHP extensions + && docker-php-ext-install intl \ + && docker-php-ext-install pdo_mysql \ + && docker-php-ext-install mbstring \ + && docker-php-ext-install mcrypt \ + && docker-php-ext-install zip \ + && docker-php-ext-install bcmath \ + + && apt-get purge -y g++ \ + && apt-get autoremove -y \ + && rm -r /var/lib/apt/lists/* \ + + # Don't clear our valuable environment vars in PHP + && echo "\nclear_env = no" >> /usr/local/etc/php-fpm.conf \ + + # Fix write permissions with shared folders + && usermod -u 1000 www-data + +# Поставим xdebug отдельно, т.к. потом его потенциально придётся отсюда убирать +RUN yes | pecl install xdebug \ + && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \ + && echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \ + && echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini + +# Next composer and global composer package, as their versions may change from time to time +RUN curl -sS https://getcomposer.org/installer | php \ + && mv composer.phar /usr/local/bin/composer.phar \ + && composer.phar global require --no-progress "fxp/composer-asset-plugin:~1.1.4" \ + && composer.phar global require --no-progress "hirak/prestissimo:~0.2.2" + +COPY ./docker/php/composer.sh /usr/local/bin/composer +RUN chmod a+x /usr/local/bin/composer + +WORKDIR /var/www/html # Copy the working dir to the image's web root COPY . /var/www/html diff --git a/README.md b/README.md index 45e56ad..40c27cb 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,70 @@ -Yii 2 Advanced Project Template -=============================== +# Account Ely.by -Yii 2 Advanced Project Template is a skeleton [Yii 2](http://www.yiiframework.com/) application best for -developing complex Web applications with multiple tiers. +## Развёртывание dev -The template includes three tiers: front end, back end, and console, each of which -is a separate Yii application. +Предварительно нужно установить [git](https://git-scm.com/downloads), +[docker](https://docs.docker.com/engine/installation/) и его +[docker-compose](https://docs.docker.com/compose/install/). -The template is designed to work in a team development environment. It supports -deploying the application in different environments. +Сливаем репозиторий: -Documentation is at [docs/guide/README.md](docs/guide/README.md). +```sh +git clone git@bitbucket.org:ErickSkrauch/ely.by-account.git account.ely.by.local +cd account.ely.by.local +``` -[![Latest Stable Version](https://poser.pugx.org/yiisoft/yii2-app-advanced/v/stable.png)](https://packagist.org/packages/yiisoft/yii2-app-advanced) -[![Total Downloads](https://poser.pugx.org/yiisoft/yii2-app-advanced/downloads.png)](https://packagist.org/packages/yiisoft/yii2-app-advanced) -[![Build Status](https://travis-ci.org/yiisoft/yii2-app-advanced.svg?branch=master)](https://travis-ci.org/yiisoft/yii2-app-advanced) +Выполняем первый запуск контейнеров: -DIRECTORY STRUCTURE -------------------- +```sh +docker-compose up -d +``` + +Далее нужно влезть в работающие контейнеры и сделать что-нибудь, что их настроит. + +### Как влезть в работающий контейнер + +Сперва, с помощью команды `docker ps` мы увидим все запущенные контейнеры. Нас интересуют значения из первой колонки +CONTAINER ID. Узнать, чему они соответствуют можно прочитав название IMAGE из 2 колонки. Чтобы выполнить команду +внутри работабщего контейнера, нужно выполнить: ``` -common - config/ contains shared configurations - mail/ contains view files for e-mails - models/ contains model classes used in both backend and frontend -console - config/ contains console configurations - controllers/ contains console controllers (commands) - migrations/ contains database migrations - models/ contains console-specific model classes - runtime/ contains files generated during runtime -backend - assets/ contains application assets such as JavaScript and CSS - config/ contains backend configurations - controllers/ contains Web controller classes - models/ contains backend-specific model classes - runtime/ contains files generated during runtime - views/ contains view files for the Web application - web/ contains the entry script and Web resources -frontend - assets/ contains application assets such as JavaScript and CSS - config/ contains frontend configurations - controllers/ contains Web controller classes - models/ contains frontend-specific model classes - runtime/ contains files generated during runtime - views/ contains view files for the Web application - web/ contains the entry script and Web resources - widgets/ contains frontend widgets -vendor/ contains dependent 3rd-party packages -environments/ contains environment-based overrides -tests contains various tests for the advanced application - codeception/ contains tests developed with Codeception PHP Testing Framework +docker exec -it a7c267b27f49 /bin/bash +``` + +Где `a7c267b27f49` - одно из значений из первой колонки. Для выхода из контейнера используем `exit`. + +------------------------- + +Так вот, нам нужно выполнить ряд команд. Здесь и далее я буду писать имена контейнеров в их соответствии с compose +файлом, но в реалиях их нужно будет заменить на реальные значения: + +```sh +# Настройка php контейнера +docker exec -it app php init --env=Docker +docker exec -it app php composer install +docker exec -it app php ./yii migrate --interactive=0 + +# Настройка node контейнера +docker exec -it node-dev-server npm i +docker exec -it node-dev-server npm --prefix ./webpack i ./webpack + +# Настройка rabbitmq контейнера +docker exec -it rabbitmq /init.sh +``` + +После этого перезапускаем все контейнеры командой: + +```sh +docker-compose restart +``` + +## Тестирование php бэкэнда + +```sh +# Прежде чем тестировать, необходимо накатить миграции +docker exec -it db6366f120ee php tests/codeception/bin/yii migrate --interactive=0 +# Собрать все тестовые окружения +docker exec -it db6366f120ee /bin/sh -c 'cd tests; ./../vendor/bin/codecept build' +# И запустить собственно процесс тестирования +docker exec -it db6366f120ee /bin/sh -c 'cd tests; ./../vendor/bin/codecept run' ``` diff --git a/docker-compose.yml b/docker-compose.yml index fb11214..6f7de65 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,17 @@ version: '2' services: + app: + links: + - db + - redis + - rabbitmq + depends_on: + - app-console-account-queue + + environment: + ENABLE_ENV_FILE: 1 + ENABLE_LOCALCONF: 1 + web: build: ./docker/nginx ports: @@ -15,28 +27,9 @@ services: - "9000" volumes: - ./:/var/www/html/ - links: - - db - - redis - - rabbitmq - depends_on: - - node-dev-server - - app-console-account-queue environment: ENABLE_ENV_FILE: 1 ENABLE_LOCALCONF: 1 - API_TOKEN: "78bb3e46d818793a299ccfcedee213d5ecad07f7" - - app-console-account-queue: - image: php:7.0-cli - volumes: - - ./:/var/www/html/ - working_dir: /var/www/html/ - command: ./wait-for-it.sh rabbitmq:5672 -- ./yii account-queue - links: - - db - - redis - - rabbitmq node-dev-server: build: ./frontend @@ -44,33 +37,26 @@ services: - "8080:8080" volumes: - ./frontend/:/usr/src/app/ - - /usr/src/app/node_modules environment: DOCKERIZED: "true" + app-console-account-queue: + build: . + volumes: + - ./:/var/www/html/ + command: ./docker/wait-for-it.sh rabbitmq:5672 -- ./yii account-queue + links: + - db + - redis + - rabbitmq + db: - image: mariadb:10.0 - expose: - - "3306" - environment: - MYSQL_ROOT_PASSWORD: secret-root - MYSQL_DATABASE: ely_accounts - MYSQL_USER: ely-accounts-user - MYSQL_PASSWORD: ely-accounts-password - TERM: dumb - # Uncomment to autostart at boottime - #restart: always + build: ./docker/mariadb redis: image: redis:3.0 - expose: - - "6379" rabbitmq: - image: rabbitmq:3.6 - expose: - - "5672" - environment: - RABBITMQ_DEFAULT_USER: ely-accounts-app - RABBITMQ_DEFAULT_PASS: app-password - RABBITMQ_DEFAULT_VHOST: /account.ely.by + build: ./docker/rabbitmq + ports: + - "15672:15672" diff --git a/docker/mariadb/Dockerfile b/docker/mariadb/Dockerfile new file mode 100644 index 0000000..a203ef6 --- /dev/null +++ b/docker/mariadb/Dockerfile @@ -0,0 +1,13 @@ +FROM mariadb:10.0 + +COPY mariadb.cnf /etc/mysql/conf.d + +# Add script to create default users / vhosts +ADD init.sh /init.sh +ADD run.sh /run.sh + +# Run rabbitmq, execute init configuration and then shutdown +RUN chmod +x /init.sh /run.sh \ + && /init.sh + +ENTRYPOINT "/run.sh" diff --git a/docker/mariadb/init.sh b/docker/mariadb/init.sh new file mode 100644 index 0000000..9628fa5 --- /dev/null +++ b/docker/mariadb/init.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Копипаста. Я не знаю, что тут происходит + +set -e +set -x + +mysql_install_db + +# Start the MySQL daemon in the background. +/usr/sbin/mysqld & +mysql_pid=$! + +until mysqladmin ping &>/dev/null; do + echo -n "."; sleep 0.2 +done + +# Конец рандомной копипасты + +# Устаналиваем беспарольный доступ для рута +mysql -e "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '' WITH GRANT OPTION" + +# Создаём базу данных для приложения и для тестов +mysql -e "CREATE DATABASE IF NOT EXISTS ely_accounts CHARACTER SET utf8 COLLATE utf8_general_ci" +mysql -e "CREATE DATABASE IF NOT EXISTS ely_accounts_test CHARACTER SET utf8 COLLATE utf8_general_ci" + +# Tell the MySQL daemon to shutdown. +mysqladmin shutdown + +# Wait for the MySQL daemon to exit. +wait $mysql_pid + +# Сохраняем состояние базы данных +tar czvf default_mysql.tar.gz /var/lib/mysql diff --git a/docker/mariadb/mariadb.cnf b/docker/mariadb/mariadb.cnf new file mode 100644 index 0000000..e69de29 diff --git a/docker/mariadb/run.sh b/docker/mariadb/run.sh new file mode 100644 index 0000000..c8dffad --- /dev/null +++ b/docker/mariadb/run.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -e +set -x + +# first, if the /var/lib/mysql directory is empty, unpack it from our predefined db +[ "$(ls -A /var/lib/mysql)" ] && echo "Running with existing database in /var/lib/mysql" || ( echo 'Populate initial db'; tar xpzvf default_mysql.tar.gz ) + +/usr/sbin/mysqld diff --git a/docker/nginx/Dockerfile b/docker/nginx/Dockerfile index ceab9b8..231bba4 100644 --- a/docker/nginx/Dockerfile +++ b/docker/nginx/Dockerfile @@ -1,2 +1,3 @@ FROM nginx:1.9 + COPY nginx.conf /etc/nginx/nginx.conf diff --git a/docker/php/composer.sh b/docker/php/composer.sh new file mode 100644 index 0000000..517b5d0 --- /dev/null +++ b/docker/php/composer.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +if [ -n "$API_TOKEN" ] +then + php /usr/local/bin/composer.phar config -g github-oauth.github.com $API_TOKEN +fi + +exec php /usr/local/bin/composer.phar "$@" diff --git a/docker/rabbitmq/Dockerfile b/docker/rabbitmq/Dockerfile new file mode 100644 index 0000000..6d18711 --- /dev/null +++ b/docker/rabbitmq/Dockerfile @@ -0,0 +1,10 @@ +FROM rabbitmq:3.6 + +RUN rabbitmq-plugins enable rabbitmq_management \ + && rabbitmq-plugins enable rabbitmq_web_stomp \ + && rabbitmq-plugins enable rabbitmq_mqtt + +# Add script to create default users / vhosts +ADD init.sh /init.sh + +RUN chmod +x /init.sh diff --git a/docker/rabbitmq/init.sh b/docker/rabbitmq/init.sh new file mode 100644 index 0000000..ec7be76 --- /dev/null +++ b/docker/rabbitmq/init.sh @@ -0,0 +1,44 @@ +#!/bin/sh + +#( sleep 10 ; \ +# +## Create users +#rabbitmqctl add_user ely-accounts-app app-password ; \ +#rabbitmqctl add_user ely-accounts-tester tester-password ; \ +# +## Set user rights +#rabbitmqctl set_user_tags ely-accounts-app administrator ; \ +#rabbitmqctl set_user_tags ely-accounts-tester administrator ; \ +# +## Create vhosts +#rabbitmqctl add_vhost /account.ely.by ; \ +#rabbitmqctl add_vhost /account.ely.by/tests ; \ +# +## Set vhost permissions +#rabbitmqctl set_permissions -p /account.ely.by ely-accounts-app ".*" ".*" ".*" ; \ +#rabbitmqctl set_permissions -p /account.ely.by/tests ely-accounts-tester ".*" ".*" ".*" ; \ +#) & +#rabbitmq-server $@ + +#service rabbitmq-server start + +# Create users +rabbitmqctl add_user ely-accounts-app app-password +rabbitmqctl add_user ely-accounts-tester tester-password + +# Set user rights +rabbitmqctl set_user_tags ely-accounts-app administrator +rabbitmqctl set_user_tags ely-accounts-tester administrator + +# Create vhosts +rabbitmqctl add_vhost /account.ely.by +rabbitmqctl add_vhost /account.ely.by/tests + +# Set vhost permissions +rabbitmqctl set_permissions -p /account.ely.by ely-accounts-app ".*" ".*" ".*" +rabbitmqctl set_permissions -p /account.ely.by/tests ely-accounts-tester ".*" ".*" ".*" + +#service rabbitmq-server stop + +# Сохраняем состояние рэбита +#tar czvf default_rabbitmq.tar.gz /var/lib/rabbitmq/mnesia diff --git a/docker/rabbitmq/run.sh b/docker/rabbitmq/run.sh new file mode 100644 index 0000000..324fcf2 --- /dev/null +++ b/docker/rabbitmq/run.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +set -e +set -x + +[ "$(ls -A /var/lib/rabbitmq/mnesia)" ] && echo "Running with existing rabbitmq in /var/lib/rabbitmq" || ( echo 'Populate initial rabbitmq'; tar xpzvf default_rabbitmq.tar.gz ) + +rabbitmq-server diff --git a/wait-for-it.sh b/docker/wait-for-it.sh similarity index 100% rename from wait-for-it.sh rename to docker/wait-for-it.sh diff --git a/environments/docker/api/config/main-local.php b/environments/docker/api/config/main-local.php new file mode 100644 index 0000000..224f3dd --- /dev/null +++ b/environments/docker/api/config/main-local.php @@ -0,0 +1,24 @@ + [ + 'request' => [ + // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation + 'cookieValidationKey' => '', + ], + 'reCaptcha' => [ + 'secret' => '', + ], + ], +]; + +if (!YII_ENV_TEST) { + // configuration adjustments for 'dev' environment + $config['bootstrap'][] = 'debug'; + $config['modules']['debug'] = \yii\debug\Module::class; + + $config['bootstrap'][] = 'gii'; + $config['modules']['gii'] = \yii\gii\Module::class; +} + +return $config; diff --git a/environments/docker/api/config/params-local.php b/environments/docker/api/config/params-local.php new file mode 100644 index 0000000..2481e4b --- /dev/null +++ b/environments/docker/api/config/params-local.php @@ -0,0 +1,4 @@ + 'some-long-secret-key', +]; diff --git a/environments/docker/api/web/index-test.php b/environments/docker/api/web/index-test.php new file mode 100644 index 0000000..1a1d417 --- /dev/null +++ b/environments/docker/api/web/index-test.php @@ -0,0 +1,18 @@ +run(); diff --git a/environments/docker/api/web/index.php b/environments/docker/api/web/index.php new file mode 100644 index 0000000..6038167 --- /dev/null +++ b/environments/docker/api/web/index.php @@ -0,0 +1,18 @@ +run(); diff --git a/environments/docker/common/config/main-local.php b/environments/docker/common/config/main-local.php new file mode 100644 index 0000000..57ca3c0 --- /dev/null +++ b/environments/docker/common/config/main-local.php @@ -0,0 +1,26 @@ + [ + 'db' => [ + 'dsn' => 'mysql:host=db;dbname=ely_accounts', + 'username' => 'root', + 'password' => '', + ], + 'mailer' => [ + 'useFileTransport' => true, + ], + 'redis' => [ + 'hostname' => 'redis', + 'password' => null, + 'port' => 6379, + 'database' => 0, + ], + 'amqp' => [ + 'host' => 'rabbitmq', + 'port' => 5672, + 'user' => 'ely-accounts-app', + 'password' => 'app-password', + 'vhost' => '/account.ely.by', + ], + ], +]; diff --git a/environments/docker/common/config/params-local.php b/environments/docker/common/config/params-local.php new file mode 100644 index 0000000..c573d3a --- /dev/null +++ b/environments/docker/common/config/params-local.php @@ -0,0 +1,4 @@ + 'account@ely.by', +]; diff --git a/environments/docker/console/config/main-local.php b/environments/docker/console/config/main-local.php new file mode 100644 index 0000000..96bd118 --- /dev/null +++ b/environments/docker/console/config/main-local.php @@ -0,0 +1,7 @@ + ['gii'], + 'modules' => [ + 'gii' => \yii\gii\Module::class, + ], +]; diff --git a/environments/docker/console/config/params-local.php b/environments/docker/console/config/params-local.php new file mode 100644 index 0000000..d0b9c34 --- /dev/null +++ b/environments/docker/console/config/params-local.php @@ -0,0 +1,3 @@ +run(); +exit($exitCode); diff --git a/environments/index.php b/environments/index.php index 3fe42a0..fc854f2 100644 --- a/environments/index.php +++ b/environments/index.php @@ -43,6 +43,20 @@ return [ 'api/config/main-local.php', ], ], + 'Docker' => [ + 'path' => 'docker', + 'setWritable' => [ + 'api/runtime', + 'api/web/assets', + ], + 'setExecutable' => [ + 'yii', + 'tests/codeception/bin/yii', + ], + 'setCookieValidationKey' => [ + 'api/config/main-local.php', + ], + ], 'Production' => [ 'path' => 'prod', 'setWritable' => [ diff --git a/tests/codeception/api/functional.suite.yml b/tests/codeception/api/functional.suite.yml index 617cc31..a99b42f 100644 --- a/tests/codeception/api/functional.suite.yml +++ b/tests/codeception/api/functional.suite.yml @@ -11,11 +11,11 @@ modules: Yii2: configFile: '../config/api/functional.php' Redis: - host: localhost + host: redis port: 6379 database: 1 AMQP: - host: localhost + host: rabbitmq port: 5672 username: 'ely-accounts-tester' password: 'tester-password' diff --git a/tests/codeception/config/config.php b/tests/codeception/config/config.php index 7ce75ca..c6ea34b 100644 --- a/tests/codeception/config/config.php +++ b/tests/codeception/config/config.php @@ -14,7 +14,7 @@ return [ ], 'components' => [ 'db' => [ - 'dsn' => 'mysql:host=localhost;dbname=ely_accounts_test', + 'dsn' => 'mysql:host=db;dbname=ely_accounts_test', ], 'mailer' => [ 'useFileTransport' => true,