Окончено внедрение докера для развёртывания dev-окружения

This commit is contained in:
ErickSkrauch 2016-05-10 01:49:50 +03:00
parent 7b7ed0ad51
commit 23d6975399
28 changed files with 425 additions and 117 deletions

View File

@ -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

16
.env
View File

@ -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

3
.gitignore vendored
View File

@ -32,3 +32,6 @@ phpunit.phar
# npm debug
npm-debug*
# Docker override file
docker-compose.override.yml

View File

@ -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

106
README.md
View File

@ -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'
```

View File

@ -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"

13
docker/mariadb/Dockerfile Normal file
View File

@ -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"

34
docker/mariadb/init.sh Normal file
View File

@ -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

View File

9
docker/mariadb/run.sh Normal file
View File

@ -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

View File

@ -1,2 +1,3 @@
FROM nginx:1.9
COPY nginx.conf /etc/nginx/nginx.conf

8
docker/php/composer.sh Normal file
View File

@ -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 "$@"

View File

@ -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

44
docker/rabbitmq/init.sh Normal file
View File

@ -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

8
docker/rabbitmq/run.sh Normal file
View File

@ -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

View File

@ -0,0 +1,24 @@
<?php
$config = [
'components' => [
'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;

View File

@ -0,0 +1,4 @@
<?php
return [
'jwtSecret' => 'some-long-secret-key',
];

View File

@ -0,0 +1,18 @@
<?php
// NOTE: Make sure this file is not accessible when deployed to production
if (!in_array(@$_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'])) {
die('You are not allowed to access this file.');
}
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');
$config = require(__DIR__ . '/../../tests/codeception/config/api/acceptance.php');
(new yii\web\Application($config))->run();

View File

@ -0,0 +1,18 @@
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../../vendor/autoload.php');
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../../common/config/bootstrap.php');
require(__DIR__ . '/../config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../../common/config/main.php'),
require(__DIR__ . '/../../common/config/main-local.php'),
require(__DIR__ . '/../config/main.php'),
require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();

View File

@ -0,0 +1,26 @@
<?php
return [
'components' => [
'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',
],
],
];

View File

@ -0,0 +1,4 @@
<?php
return [
'fromEmail' => 'account@ely.by',
];

View File

@ -0,0 +1,7 @@
<?php
return [
'bootstrap' => ['gii'],
'modules' => [
'gii' => \yii\gii\Module::class,
],
];

View File

@ -0,0 +1,3 @@
<?php
return [
];

28
environments/docker/yii Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env php
<?php
/**
* Yii console bootstrap file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/console/config/bootstrap.php');
$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/console/config/main.php'),
require(__DIR__ . '/console/config/main-local.php')
);
$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);

View File

@ -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' => [

View File

@ -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'

View File

@ -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,