From ba5ccee8962b35428d25619d83189c86d2de3507 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Fri, 16 Nov 2018 12:42:07 +0530 Subject: [PATCH 1/3] Update volume handling and continer booting flow for linux and darwin Signed-off-by: Riddhesh Sanghvi --- src/WordPress.php | 160 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 128 insertions(+), 32 deletions(-) diff --git a/src/WordPress.php b/src/WordPress.php index 531e619..cf80b20 100644 --- a/src/WordPress.php +++ b/src/WordPress.php @@ -428,38 +428,9 @@ private function configure_site_files() { $custom_conf_source = SITE_WP_TEMPLATE_ROOT . '/config/nginx/user.conf.mustache'; $process_user = posix_getpwuid( posix_geteuid() ); - $volumes = [ - [ 'name' => 'htdocs', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/app' ], - [ 'name' => 'config_nginx', 'path_to_symlink' => dirname( dirname( $site_nginx_default_conf ) ) ], - [ 'name' => 'config_php', 'path_to_symlink' => dirname( dirname( $site_php_ini ) ) ], - [ 'name' => 'log_php', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/php' ], - [ 'name' => 'log_nginx', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx' ], - [ - 'name' => 'data_postfix', - 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/spool' - ], - [ - 'name' => 'ssl_postfix', - 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/ssl' - ], - [ - 'name' => 'config_postfix', - 'path_to_symlink' => $this->site_data['site_fs_path'] . '/config/postfix' - ], - ]; - - if ( 'db' === $this->site_data['db_host'] ) { - $volumes[] = [ - 'name' => 'data_db', - 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/db' - ]; - } - \EE::log( 'Creating WordPress site ' . $this->site_data['site_url'] ); \EE::log( 'Copying configuration files.' ); - $this->docker->create_volumes( $this->site_data['site_url'], $volumes ); - $default_conf_content = $this->generate_default_conf( $this->site_data['app_sub_type'], $this->cache_type, $server_name ); $local = ( 'db' === $this->site_data['db_host'] ) ? true : false; @@ -489,13 +460,19 @@ private function configure_site_files() { try { $this->dump_docker_compose_yml( [ 'nohttps' => true ] ); $this->fs->dumpFile( $site_conf_env, $env_content ); - \EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'postfix' ] ); + if ( ! IS_DARWIN ) { + \EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'postfix' ] ); + } \EE\Site\Utils\set_postfix_files( $this->site_data['site_url'], $this->site_data['site_fs_path'] . '/services' ); $this->fs->dumpFile( $site_nginx_default_conf, $default_conf_content ); $this->fs->copy( $custom_conf_source, $custom_conf_dest ); $this->fs->remove( $this->site_data['site_fs_path'] . '/app/html' ); $this->fs->dumpFile( $site_php_ini, $php_ini_content ); - \EE\Site\Utils\restart_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php' ] ); + if ( IS_DARWIN ) { + \EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php', 'postfix' ] ); + } else { + \EE\Site\Utils\restart_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php' ] ); + } } catch ( \Exception $e ) { $this->catch_clean( $e ); } @@ -505,10 +482,129 @@ private function configure_site_files() { * Generate and place docker-compose.yml file. * * @param array $additional_filters Filters to alter docker-compose file. + * * @ignorecommand */ public function dump_docker_compose_yml( $additional_filters = [] ) { + $site_conf_dir = $this->site_data['site_fs_path'] . '/config'; + $site_nginx_default_conf = $site_conf_dir . '/nginx/conf.d/main.conf'; + $site_php_ini = $site_conf_dir . '/php/php/php.ini'; + + $volumes = [ + 'nginx' => [ + [ + 'name' => 'htdocs', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/app', + 'container_path' => '/var/www', + ], + [ + 'name' => 'config_nginx', + 'path_to_symlink' => dirname( dirname( $site_nginx_default_conf ) ), + 'container_path' => '/usr/local/openresty/nginx/conf', + 'skip_darwin' => true, + ], + [ + 'name' => 'config_nginx', + 'path_to_symlink' => $site_nginx_default_conf, + 'container_path' => '/usr/local/openresty/nginx/conf/conf.d/main.conf', + 'skip_linux' => true, + 'skip_volume' => true, + ], + [ + 'name' => 'log_nginx', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/nginx', + 'container_path' => '/var/log/nginx', + ], + ], + 'php' => [ + [ + 'name' => 'htdocs', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/app', + 'container_path' => '/var/www', + ], + [ + 'name' => 'config_php', + 'path_to_symlink' => dirname( dirname( $site_php_ini ) ), + 'container_path' => '/usr/local/etc', + 'skip_darwin' => true, + ], + [ + 'name' => 'config_php', + 'path_to_symlink' => $site_php_ini, + 'container_path' => '/usr/local/etc/php/php/php.ini', + 'skip_linux' => true, + 'skip_volume' => true, + ], + [ + 'name' => 'log_php', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/logs/php', + 'container_path' => '/var/log/php', + ], + + ], + 'postfix' => [ + [ + 'name' => '/dev/log', + 'path_to_symlink' => '/dev/log', + 'container_path' => '/dev/log', + 'skip_volume' => true, + 'skip_darwin' => true, + ], + [ + 'name' => 'data_postfix', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/spool', + 'container_path' => '/var/spool/postfix', + ], + [ + 'name' => 'ssl_postfix', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/postfix/ssl', + 'container_path' => '/etc/ssl/postfix', + ], + [ + 'name' => 'config_postfix', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/config/postfix', + 'container_path' => '/etc/postfix', + 'skip_darwin' => true, + ], + ], + ]; + + if ( 'db' === $this->site_data['db_host'] ) { + $volumes['db'] = [ + [ + 'name' => 'db_data', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/data', + 'container_path' => '/var/lib/mysql', + ], + [ + 'name' => 'db_conf', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf', + 'container_path' => '/etc/mysql', + 'skip_darwin' => true, + ], + // [ + // // TODO: Generate Template. + // 'name' => '', + // 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf', + // 'container_path' => '/etc/mysql/my.cnf', + // 'skip_linux' => true, + // 'skip_volume' => true, + // ], + [ + 'name' => 'db_logs', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/logs', + 'container_path' => '/var/log/mysql', + ], + ]; + } + + if ( ! IS_DARWIN && empty( $this->docker->get_volumes_by_label( $this->site_data['site_url'] ) ) ) { + foreach ( $volumes as $volume ) { + $this->docker->create_volumes( $this->site_data['site_url'], $volume ); + } + } + $site_docker_yml = $this->site_data['site_fs_path'] . '/docker-compose.yml'; $filter = []; @@ -523,7 +619,7 @@ public function dump_docker_compose_yml( $additional_filters = [] ) { $filter[ $key ] = $addon_filter; } - $docker_compose_content = $site_docker->generate_docker_compose_yml( $filter ); + $docker_compose_content = $site_docker->generate_docker_compose_yml( $filter, $volumes ); $this->fs->dumpFile( $site_docker_yml, $docker_compose_content ); } From 177b02fc0cdf0907cedcf402a5ac1e3a13128abb Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Fri, 16 Nov 2018 12:53:04 +0530 Subject: [PATCH 2/3] Update volume handling in docker-compose Signed-off-by: Riddhesh Sanghvi --- src/Site_WP_Docker.php | 89 +++++++++++++++---------------- templates/docker-compose.mustache | 2 +- 2 files changed, 43 insertions(+), 48 deletions(-) diff --git a/src/Site_WP_Docker.php b/src/Site_WP_Docker.php index f3ecdcc..90e2d5b 100644 --- a/src/Site_WP_Docker.php +++ b/src/Site_WP_Docker.php @@ -12,10 +12,11 @@ class Site_WP_Docker { * @param array $filters Array of flags to determine the docker-compose.yml generation. * Empty/Default -> Generates default WordPress docker-compose.yml * ['le'] -> Enables letsencrypt in the generation. + * @param array $volumes Array containing volume info passable to \EE_DOCKER::get_mounting_volume_array(). * * @return String docker-compose.yml content string. */ - public function generate_docker_compose_yml( array $filters = [] ) { + public function generate_docker_compose_yml( array $filters = [], $volumes ) { $img_versions = \EE\Utils\get_image_versions(); $base = []; @@ -26,31 +27,31 @@ public function generate_docker_compose_yml( array $filters = [] ) { ], ]; - // db configuration. - $db['service_name'] = [ 'name' => 'db' ]; - $db['image'] = [ 'name' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'] ]; - $db['restart'] = $restart_default; - $db['labels'] = [ - 'label' => [ - 'name' => 'io.easyengine.site=${VIRTUAL_HOST}', - ], - ]; - $db['volumes'] = [ - [ - 'vol' => [ - 'name' => 'data_db:/var/lib/mysql', + if ( in_array( 'db', $filters, true ) ) { + // db configuration. + $db['service_name'] = [ 'name' => 'db' ]; + $db['image'] = [ 'name' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'] ]; + $db['restart'] = $restart_default; + $db['labels'] = [ + 'label' => [ + 'name' => 'io.easyengine.site=${VIRTUAL_HOST}', ], - ], - ]; - $db['environment'] = [ - 'env' => [ - [ 'name' => 'MYSQL_ROOT_PASSWORD' ], - [ 'name' => 'MYSQL_DATABASE' ], - [ 'name' => 'MYSQL_USER' ], - [ 'name' => 'MYSQL_PASSWORD' ], - ], - ]; - $db['networks'] = $network_default; + ]; + $db['volumes'] = [ + [ + 'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['db'] ), + ], + ]; + $db['environment'] = [ + 'env' => [ + [ 'name' => 'MYSQL_ROOT_PASSWORD' ], + [ 'name' => 'MYSQL_DATABASE' ], + [ 'name' => 'MYSQL_USER' ], + [ 'name' => 'MYSQL_PASSWORD' ], + ], + ]; + $db['networks'] = $network_default; + } // PHP configuration. $php['service_name'] = [ 'name' => 'php' ]; $php['image'] = [ 'name' => 'easyengine/php:' . $img_versions['easyengine/php'] ]; @@ -71,11 +72,7 @@ public function generate_docker_compose_yml( array $filters = [] ) { ]; $php['volumes'] = [ [ - 'vol' => [ - [ 'name' => 'htdocs:/var/www' ], - [ 'name' => 'config_php:/usr/local/etc' ], - [ 'name' => 'log_php:/var/log/php' ], - ], + 'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['php'] ), ], ]; $php['environment'] = [ @@ -126,11 +123,7 @@ public function generate_docker_compose_yml( array $filters = [] ) { $nginx['environment']['env'][] = [ 'name' => 'HTTPS_METHOD=nohttps' ]; } $nginx['volumes'] = [ - 'vol' => [ - [ 'name' => 'htdocs:/var/www' ], - [ 'name' => 'config_nginx:/usr/local/openresty/nginx/conf' ], - [ 'name' => 'log_nginx:/var/log/nginx' ], - ], + 'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['nginx'] ), ]; $nginx['labels'] = [ 'label' => [ @@ -195,12 +188,7 @@ public function generate_docker_compose_yml( array $filters = [] ) { ], ]; $postfix['volumes'] = [ - 'vol' => [ - [ 'name' => '/dev/log:/dev/log' ], - [ 'name' => 'data_postfix:/var/spool/postfix' ], - [ 'name' => 'ssl_postfix:/etc/ssl/postfix' ], - [ 'name' => 'config_postfix:/etc/postfix' ], - ], + 'vol' => \EE_DOCKER::get_mounting_volume_array( $volumes['postfix'] ), ]; $postfix['networks'] = $network_default; @@ -223,7 +211,7 @@ public function generate_docker_compose_yml( array $filters = [] ) { $base[] = $redis; } - $volumes = [ + $external_volumes = [ 'external_vols' => [ [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'htdocs' ], [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'config_nginx' ], @@ -246,18 +234,25 @@ public function generate_docker_compose_yml( array $filters = [] ) { ]; if ( in_array( 'db', $filters, true ) ) { - $base[] = $db; - $volumes['external_vols'][] = [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'data_db' ]; + $base[] = $db; + $external_volumes['external_vols'] = [ + [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_data' ], + [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_conf' ], + [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_logs' ], + ]; } else { $network['enable_backend_network'] = true; } $binding = [ - 'services' => $base, - 'network' => $network, - 'created_volumes' => $volumes, + 'services' => $base, + 'network' => $network, ]; + if ( ! IS_DARWIN ) { + $binding['created_volumes'] = $external_volumes; + } + $docker_compose_yml = mustache_render( SITE_WP_TEMPLATE_ROOT . '/docker-compose.mustache', $binding ); return $docker_compose_yml; diff --git a/templates/docker-compose.mustache b/templates/docker-compose.mustache index fba289b..65aea9b 100644 --- a/templates/docker-compose.mustache +++ b/templates/docker-compose.mustache @@ -33,7 +33,7 @@ services: {{#volumes}} volumes: {{#vol}} - - "{{name}}" + - "{{.}}" {{/vol}} {{/volumes}} {{#environment}} From 8a2efd247e4b0c6a730ae3afbcaa7c8c8426e6df Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 19 Nov 2018 17:06:50 +0530 Subject: [PATCH 3/3] Update db configuration for darwin Signed-off-by: Riddhesh Sanghvi --- src/Site_WP_Docker.php | 4 +- src/WordPress.php | 19 ++-- templates/my.cnf.mustache | 189 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 10 deletions(-) create mode 100644 templates/my.cnf.mustache diff --git a/src/Site_WP_Docker.php b/src/Site_WP_Docker.php index 90e2d5b..c63668d 100644 --- a/src/Site_WP_Docker.php +++ b/src/Site_WP_Docker.php @@ -235,11 +235,11 @@ public function generate_docker_compose_yml( array $filters = [], $volumes ) { if ( in_array( 'db', $filters, true ) ) { $base[] = $db; - $external_volumes['external_vols'] = [ + $external_volumes['external_vols'] = array_merge( $external_volumes['external_vols'], [ [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_data' ], [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_conf' ], [ 'prefix' => $filters['site_prefix'], 'ext_vol_name' => 'db_logs' ], - ]; + ] ); } else { $network['enable_backend_network'] = true; } diff --git a/src/WordPress.php b/src/WordPress.php index cf80b20..5368456 100644 --- a/src/WordPress.php +++ b/src/WordPress.php @@ -469,6 +469,10 @@ private function configure_site_files() { $this->fs->remove( $this->site_data['site_fs_path'] . '/app/html' ); $this->fs->dumpFile( $site_php_ini, $php_ini_content ); if ( IS_DARWIN ) { + if ( 'db' === $this->site_data['db_host'] ) { + $db_conf_file = $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf'; + $this->fs->copy( SITE_WP_TEMPLATE_ROOT . '/my.cnf.mustache', $db_conf_file ); + } \EE\Site\Utils\start_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php', 'postfix' ] ); } else { \EE\Site\Utils\restart_site_containers( $this->site_data['site_fs_path'], [ 'nginx', 'php' ] ); @@ -583,14 +587,13 @@ public function dump_docker_compose_yml( $additional_filters = [] ) { 'container_path' => '/etc/mysql', 'skip_darwin' => true, ], - // [ - // // TODO: Generate Template. - // 'name' => '', - // 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf', - // 'container_path' => '/etc/mysql/my.cnf', - // 'skip_linux' => true, - // 'skip_volume' => true, - // ], + [ + 'name' => 'db_conf', + 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/conf/my.cnf', + 'container_path' => '/etc/mysql/my.cnf', + 'skip_linux' => true, + 'skip_volume' => true, + ], [ 'name' => 'db_logs', 'path_to_symlink' => $this->site_data['site_fs_path'] . '/services/mariadb/logs', diff --git a/templates/my.cnf.mustache b/templates/my.cnf.mustache new file mode 100644 index 0000000..415ee01 --- /dev/null +++ b/templates/my.cnf.mustache @@ -0,0 +1,189 @@ +# MariaDB database server configuration file. +# +# You can copy this file to one of: +# - "/etc/mysql/my.cnf" to set global options, +# - "~/.my.cnf" to set user-specific options. +# +# One can use all long options that the program supports. +# Run program with --help to get a list of available options and with +# --print-defaults to see which it would actually understand and use. +# +# For explanations see +# http://dev.mysql.com/doc/mysql/en/server-system-variables.html + +# This will be passed to all mysql clients +# It has been reported that passwords should be enclosed with ticks/quotes +# escpecially if they contain "#" chars... +# Remember to edit /etc/mysql/debian.cnf when changing the socket location. +[client] +port = 3306 +socket = /var/run/mysqld/mysqld.sock + +# Here is entries for some specific programs +# The following values assume you have at least 32M ram + +# This was formally known as [safe_mysqld]. Both versions are currently parsed. +[mysqld_safe] +socket = /var/run/mysqld/mysqld.sock +nice = 0 + +[mysqld] +# +# * Basic Settings +# +#user = mysql +pid-file = /var/run/mysqld/mysqld.pid +socket = /var/run/mysqld/mysqld.sock +port = 3306 +basedir = /usr +datadir = /var/lib/mysql +tmpdir = /tmp +lc_messages_dir = /usr/share/mysql +lc_messages = en_US +skip-external-locking +# +# Instead of skip-networking the default is now to listen only on +# localhost which is more compatible and is not less secure. +#bind-address = 127.0.0.1 +# +# * Fine Tuning +# +max_connections = 100 +connect_timeout = 5 +wait_timeout = 600 +max_allowed_packet = 16M +thread_cache_size = 128 +sort_buffer_size = 4M +bulk_insert_buffer_size = 16M +tmp_table_size = 32M +max_heap_table_size = 32M +# +# * MyISAM +# +# This replaces the startup script and checks MyISAM tables if needed +# the first time they are touched. On error, make copy and try a repair. +myisam_recover_options = BACKUP +key_buffer_size = 128M +#open-files-limit = 2000 +table_open_cache = 400 +myisam_sort_buffer_size = 512M +concurrent_insert = 2 +read_buffer_size = 2M +read_rnd_buffer_size = 1M +# +# * Query Cache Configuration +# +# Cache only tiny result sets, so we can fit more in the query cache. +query_cache_limit = 128K +query_cache_size = 64M +# for more write intensive setups, set to DEMAND or OFF +#query_cache_type = DEMAND +# +# * Logging and Replication +# +# Both location gets rotated by the cronjob. +# Be aware that this log type is a performance killer. +# As of 5.1 you can enable the log at runtime! +#general_log_file = /var/log/mysql/mysql.log +#general_log = 1 +# +# Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf. +# +# we do want to know about network errors and such +#log_warnings = 2 +# +# Enable the slow query log to see queries with especially long duration +#slow_query_log[={0|1}] +slow_query_log_file = /var/log/mysql/mariadb-slow.log +long_query_time = 10 +#log_slow_rate_limit = 1000 +#log_slow_verbosity = query_plan + +#log-queries-not-using-indexes +#log_slow_admin_statements +# +# The following can be used as easy to replay backup logs or for replication. +# note: if you are setting up a replication slave, see README.Debian about +# other settings you may need to change. +#server-id = 1 +#report_host = master1 +#auto_increment_increment = 2 +#auto_increment_offset = 1 +#log_bin = /var/log/mysql/mariadb-bin +#log_bin_index = /var/log/mysql/mariadb-bin.index +# not fab for performance, but safer +#sync_binlog = 1 +expire_logs_days = 10 +max_binlog_size = 100M +# slaves +#relay_log = /var/log/mysql/relay-bin +#relay_log_index = /var/log/mysql/relay-bin.index +#relay_log_info_file = /var/log/mysql/relay-bin.info +#log_slave_updates +#read_only +# +# If applications support it, this stricter sql_mode prevents some +# mistakes like inserting invalid dates etc. +#sql_mode = NO_ENGINE_SUBSTITUTION,TRADITIONAL +# +# * InnoDB +# +# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. +# Read the manual for more InnoDB related options. There are many! +default_storage_engine = InnoDB +# you can't just change log file size, requires special procedure +#innodb_log_file_size = 50M +innodb_buffer_pool_size = 256M +innodb_log_buffer_size = 8M +innodb_file_per_table = 1 +innodb_open_files = 400 +innodb_io_capacity = 400 +innodb_flush_method = O_DIRECT +# +# * Security Features +# +# Read the manual, too, if you want chroot! +# chroot = /var/lib/mysql/ +# +# For generating SSL certificates I recommend the OpenSSL GUI "tinyca". +# +# ssl-ca=/etc/mysql/cacert.pem +# ssl-cert=/etc/mysql/server-cert.pem +# ssl-key=/etc/mysql/server-key.pem + +# +# * Galera-related settings +# +[galera] +# Mandatory settings +#wsrep_on=ON +#wsrep_provider= +#wsrep_cluster_address= +#binlog_format=row +#default_storage_engine=InnoDB +#innodb_autoinc_lock_mode=2 +# +# Allow server to accept connections on all interfaces. +# +#bind-address=0.0.0.0 +# +# Optional setting +#wsrep_slave_threads=1 +#innodb_flush_log_at_trx_commit=0 + +[mysqldump] +quick +quote-names +max_allowed_packet = 16M + +[mysql] +#no-auto-rehash # faster start of mysql but no tab completion + +[isamchk] +key_buffer = 16M + +# +# * 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/