Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Containerization #91

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,3 +19,4 @@

# Generated by Composer
/vendor/
composer.phar
Copy link

@kamazee kamazee May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think /data/ or at least /data/mysql/ has to be added to ignores because mysql data that is mounted from there makes the working copy dirty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kamazee thanks. Have cleaned this up.

6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,12 @@ Modify `local_config.php` according to your local development environment.
Create a new MySQL/MariaDB database using `sql/database.sql`, create database
schema `sql/schema.sql` and insert fixtures using `sql/fixtures.sql`.

## Docker compose

Running `docker-compose up --build` will bring a docker development environment up.

This generates the file `local_config.php` which may clash if you already have one for non-docker development. The database is stored in the `data` directory. Stopping the docker containers, deleting that directory, and then bringing the containers back up will recreate the DB from scratch.

## Tests

Application unit tests can be executed in development environment after
Expand Down
9 changes: 9 additions & 0 deletions containers/db/my.cnf
@@ -0,0 +1,9 @@

# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
25 changes: 25 additions & 0 deletions containers/installer/Dockerfile
@@ -0,0 +1,25 @@
FROM debian:9-slim

USER root

# Get Debian up-to-date
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y git \
mariadb-client wget curl iputils-ping \
ca-certificates lsb-release apt-transport-https gnupg

# Install 3rd party PHP 7.2 packages
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions 7.2 while actually 7.4 is installed.
Shouldn't it just be removed? It's obvious that PHP packages are installed from a third party repository.

RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee -a /etc/apt/sources.list.d/php.list

RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y php7.4 php7.4-common php7.4-cli \
php7.4-mysql php7.4-curl php7.4-xml php7.4-mbstring \
php7.4-intl php7.4-zip php7.4-sqlite3 unzip

# Make the default directory you
WORKDIR /var/app

# CMD tail -f README.md
CMD sh /var/app/containers/installer/entrypoint.sh
40 changes: 40 additions & 0 deletions containers/installer/entrypoint.sh
@@ -0,0 +1,40 @@

set -e
set -x

# Generate a local_config.php if it doesn't already exist.
if [ ! -f "/var/app/local_config.php" ]; then
echo "<?php" > /var/app/local_config.php
echo "" >> /var/app/local_config.php
echo "/**" >> /var/app/local_config.php
echo " * Generated from containers/installer/entrypoint.sh" >> /var/app/local_config.php

echo " * Feel free to edit. Delete to have it re-generated from" >> /var/app/local_config.php
echo " * scratch on next 'docker-compose up'" >> /var/app/local_config.php
echo " */" >> /var/app/local_config.php
echo "" >> /var/app/local_config.php
echo "\$site_data = [" >> /var/app/local_config.php
echo " 'method' => 'http'," >> /var/app/local_config.php
echo " 'url' => '127.0.0.1'," >> /var/app/local_config.php
echo " 'basedir' => ''," >> /var/app/local_config.php
echo " 'email' => 'php-bugs@lists.php.net'," >> /var/app/local_config.php
echo " 'doc_email' => 'doc-bugs@lists.php.net'," >> /var/app/local_config.php
echo " 'security_email' => 'security@php.net'," >> /var/app/local_config.php
echo " 'db' => 'phpbugsdb'," >> /var/app/local_config.php
echo " 'db_user' => 'nobody'," >> /var/app/local_config.php
echo " 'db_pass' => 'bugs_password'," >> /var/app/local_config.php
echo " 'db_host' => 'db'," >> /var/app/local_config.php
echo " 'patch_tmp' => \"{\$ROOT_DIR}/uploads/patches/\"," >> /var/app/local_config.php
echo "];" >> /var/app/local_config.php
echo "" >> /var/app/local_config.php
echo "define('DEVBOX', true);" >> /var/app/local_config.php
fi


if [ ! -f "composer.phar" ]; then
echo "composer.phar does not exist, downloading.";
curl "https://getcomposer.org/download/2.0.9/composer.phar" -o composer.phar
fi
php composer.phar install

echo "Installer is finished."
27 changes: 27 additions & 0 deletions containers/web/Dockerfile
@@ -0,0 +1,27 @@
FROM debian:9-slim

USER root

# Get Debian up-to-date
RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y git \
mariadb-client wget curl iputils-ping \
ca-certificates lsb-release apt-transport-https gnupg

# bsdmainutils

# Install 3rd party PHP 7.2 packages
RUN echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | tee -a /etc/apt/sources.list.d/php.list

RUN wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

RUN apt-get update -qq \
&& DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y php7.4 php7.4-common php7.4-cli \
php7.4-mysql php7.4-curl php7.4-xml php7.4-mbstring \
php7.4-intl php7.4-redis php7.4-zip php7.4-sqlite3 unzip

# Make the default directory you
WORKDIR /var/app

# CMD tail -f README.md
CMD sh /var/app/containers/web/entrypoint.sh
14 changes: 14 additions & 0 deletions containers/web/entrypoint.sh
@@ -0,0 +1,14 @@

set -e
set -x


echo "Wait for DB before starting site.";

php scripts/wait_for_db.php

echo "DB, site should be available."

php -S 0.0.0.0:80 -t www/


42 changes: 42 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,42 @@
version: "3"
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: sql_root_password
MYSQL_DATABASE: phpbugsdb
MYSQL_USER: nobody
MYSQL_PASSWORD: "bugs_password"
networks:
default_network:
ports:
- "3306:3306"
volumes:
- ./containers/db/my.cnf:/etc/mysql/my.cnf
- ./data/mysql:/var/lib/mysql
# The next lines init the DB. Rename them to force correct order
- ./sql/schema.sql:/docker-entrypoint-initdb.d/2_schemas.sql
- ./sql/fixtures.sql:/docker-entrypoint-initdb.d/3_fixtures.sql
installer:
build: containers/installer
environment:
# - COMPOSER_CACHE_DIR=/var/app/var/cache/composer
- COMPOSER_ALLOW_SUPERUSER=1
networks:
default_network:
volumes:
- .:/var/app
web:
build: containers/web
depends_on:
- "db"
- "installer"
networks:
default_network:
ports:
- "80:80"
volumes:
- .:/var/app
networks:
default_network:

56 changes: 56 additions & 0 deletions scripts/create_bug.php
@@ -0,0 +1,56 @@
<?php

declare(strict_types = 1);

$ROOT_DIR = __DIR__ . '/../';
require_once __DIR__.'/../local_config.php';

$pdo = new \PDO(
'mysql:host='.$site_data["db_host"].';dbname='.$site_data["db"].';charset=utf8',
$site_data["db_user"],
$site_data["db_pass"],
[
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
]
);

$res = $pdo->prepare('
INSERT INTO bugdb (
package_name,
bug_type,
email,
sdesc,
ldesc,
php_version,
php_os,
passwd,
reporter_name,
status,
ts1,
private,
visitor_ip
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, "Open", NOW(), ?, INET6_ATON(?))
');

$params = [
$package_name = "Web Site",
$bug_type = "Bug",
$email = 'john@example.com',
$sdesc = 'short desc',
$ldesc = 'This is a long description',
$php_version = '8.0.2',
$php_os = '',
$passwd = 'pass12345',
$reporter_name = 'danack',
// $status,
// $ts1,
$private = 'N',
$visitor_ip = '127.0.0.1'
];

$result = $res->execute($params);

var_dump($result);

37 changes: 37 additions & 0 deletions scripts/create_comment.php
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

$ROOT_DIR = __DIR__ . '/../';
require_once __DIR__.'/../local_config.php';

$pdo = new \PDO(
'mysql:host='.$site_data["db_host"].';dbname='.$site_data["db"].';charset=utf8',
$site_data["db_user"],
$site_data["db_pass"],
[
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
]
);

$res = $pdo->prepare("
INSERT INTO bugdb_comments (bug, email, reporter_name, comment, comment_type, ts, visitor_ip)
VALUES (?, ?, ?, ?, ?, NOW(), INET6_ATON(?))
");

$params = [
$bug = 2,
$email = 'dan_test@example.com',
$reporter_name = 'John',
$comment = 'The day shall not save them',
$comment_type = 'unknown',
// $ts,
$visitor_ip = '127.0.0.1'
];

$result = $res->execute($params);

var_dump($result);

35 changes: 35 additions & 0 deletions scripts/wait_for_db.php
@@ -0,0 +1,35 @@
<?php

declare(strict_types = 1);
// ugh
$ROOT_DIR = __DIR__ . '/../';
require_once __DIR__.'/../local_config.php';

$maxTimeToWait = 120;
$startTime = microtime(true);

do {
try {
$pdo = new \PDO(
'mysql:host='.$site_data["db_host"].';dbname='.$site_data["db"].';charset=utf8',
$site_data["db_user"],
$site_data["db_pass"],
[
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
]
);

$pdo->query('SELECT 1');
echo "DB appears to be available.\n";
exit(0);
} catch (\Exception $e) {
echo "DB not available yet.\n";
}

sleep(1);
} while ((microtime(true) - $startTime) < $maxTimeToWait);

echo "DB failed to be available in $maxTimeToWait seconds.\n";
exit(-1);