diff --git a/service-command.php b/service-command.php index c9004a0..c1de75b 100644 --- a/service-command.php +++ b/service-command.php @@ -28,10 +28,6 @@ define( 'GLOBAL_REDIS_CONTAINER', 'ee-global-redis' ); } -if ( ! defined( 'EE_SERVICE_DIR' ) ) { - define( 'EE_SERVICE_DIR', EE_ROOT_DIR . '/services' ); -} - if ( ! class_exists( 'EE' ) ) { return; } diff --git a/src/helper/service-utils.php b/src/helper/service-utils.php index 753cd0c..7556bdb 100644 --- a/src/helper/service-utils.php +++ b/src/helper/service-utils.php @@ -23,6 +23,7 @@ function nginx_proxy_check() { if ( $config_80_port !== trim( $launch_80_test->stdout ) || $config_443_port !== trim( $launch_443_test->stdout ) ) { EE::error( "Ports of current running nginx-proxy and ports specified in EasyEngine config file don't match." ); } + return; } @@ -39,13 +40,10 @@ function nginx_proxy_check() { $fs = new Filesystem(); - create_global_volumes(); - if ( ! $fs->exists( EE_SERVICE_DIR . '/docker-compose.yml' ) ) { generate_global_docker_compose_yml( $fs ); } - $EE_ROOT_DIR = EE_ROOT_DIR; boot_global_networks(); if ( ! EE::docker()::docker_compose_up( EE_SERVICE_DIR . '', [ 'global-nginx-proxy' ] ) ) { EE::error( "There was some error in starting $proxy_type container. Please check logs." ); @@ -74,12 +72,12 @@ function init_global_container( $service, $container = '' ) { } if ( 'running' !== EE::docker()::container_status( $container ) ) { - chdir( EE_SERVICE_DIR . '' ); - if ( empty( EE::docker()::get_volumes_by_label( $service ) ) ) { - create_global_volumes(); + chdir( EE_SERVICE_DIR . '' ); + $db_conf_file = EE_SERVICE_DIR . '/mariadb/conf/my.cnf'; + if ( IS_DARWIN && GLOBAL_DB === $service && ! $fs->exists( $db_conf_file ) ) { + $fs->copy( SERVICE_TEMPLATE_ROOT . '/my.cnf.mustache', $db_conf_file ); } - EE::docker()::boot_container( $container, 'docker-compose up -d ' . $service ); } else { EE::log( "$service: Service already running" ); @@ -106,38 +104,62 @@ function boot_global_networks() { } /** - * Function to create all necessary volumes for global containers. + * Generates global docker-compose.yml at EE_ROOT_DIR + * + * @param Filesystem $fs Filesystem object to write file. */ -function create_global_volumes() { +function generate_global_docker_compose_yml( Filesystem $fs ) { + + $img_versions = EE\Utils\get_image_versions(); + $config_80_port = \EE\Utils\get_config_value( 'proxy_80_port', 80 ); + $config_443_port = \EE\Utils\get_config_value( 'proxy_443_port', 443 ); - $volumes = [ + $volumes_nginx_proxy = [ [ 'name' => 'certs', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/certs', + 'container_path' => '/etc/nginx/certs', ], [ 'name' => 'dhparam', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/dhparam', + 'container_path' => '/etc/nginx/dhparam', ], [ 'name' => 'confd', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/conf.d', + 'container_path' => '/etc/nginx/conf.d', ], [ 'name' => 'htpasswd', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/htpasswd', + 'container_path' => '/etc/nginx/htpasswd', ], [ 'name' => 'vhostd', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/vhost.d', + 'container_path' => '/etc/nginx/vhost.d', ], [ 'name' => 'html', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/html', + 'container_path' => '/usr/share/nginx/html', ], [ 'name' => 'nginx_proxy_logs', 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/logs', + 'container_path' => '/var/log/nginx', + ], + [ + 'name' => 'nginx_proxy_logs', + 'path_to_symlink' => EE_SERVICE_DIR . '/nginx-proxy/logs', + 'container_path' => '/var/log/nginx', + ], + [ + 'name' => '/var/run/docker.sock', + 'path_to_symlink' => '/var/run/docker.sock', + 'container_path' => '/tmp/docker.sock:ro', + 'skip_volume' => true, ], ]; @@ -145,118 +167,50 @@ function create_global_volumes() { [ 'name' => 'db_data', 'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/data', + 'container_path' => '/var/lib/mysql', ], [ 'name' => 'db_conf', 'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/conf', + 'container_path' => '/etc/mysql', + 'skip_darwin' => true, + ], + [ + 'name' => 'db_conf', + 'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/conf/my.cnf', + 'container_path' => '/etc/mysql/my.cnf', + 'skip_linux' => true, + 'skip_volume' => true, ], [ 'name' => 'db_logs', 'path_to_symlink' => EE_SERVICE_DIR . '/mariadb/logs', + 'container_path' => '/var/log/mysql', ], ]; $volumes_redis = [ [ 'name' => 'redis_data', 'path_to_symlink' => EE_SERVICE_DIR . '/redis/data', + 'container_path' => '/data', + 'skip_darwin' => true, ], [ 'name' => 'redis_conf', 'path_to_symlink' => EE_SERVICE_DIR . '/redis/conf', + 'container_path' => '/usr/local/etc/redis', + 'skip_darwin' => true, ], [ 'name' => 'redis_logs', 'path_to_symlink' => EE_SERVICE_DIR . '/redis/logs', + 'container_path' => '/var/log/redis', ], ]; - if ( empty( EE::docker()::get_volumes_by_label( 'global-nginx-proxy' ) ) ) { - EE::docker()::create_volumes( 'global-nginx-proxy', $volumes, false ); - } - - if ( empty( EE::docker()::get_volumes_by_label( GLOBAL_DB ) ) ) { - EE::docker()::create_volumes( GLOBAL_DB, $volumes_db, false ); - } - - if ( empty( EE::docker()::get_volumes_by_label( GLOBAL_REDIS ) ) ) { - EE::docker()::create_volumes( GLOBAL_REDIS, $volumes_redis, false ); - } -} - -/** - * Generates global docker-compose.yml at EE_ROOT_DIR - * - * @param Filesystem $fs Filesystem object to write file. - */ -function generate_global_docker_compose_yml( Filesystem $fs ) { - - $img_versions = EE\Utils\get_image_versions(); - $config_80_port = \EE\Utils\get_config_value( 'proxy_80_port', 80 ); - $config_443_port = \EE\Utils\get_config_value( 'proxy_443_port', 443 ); + if ( ! IS_DARWIN ) { - $data = [ - 'services' => [ - [ - 'name' => 'global-nginx-proxy', - 'container_name' => EE_PROXY_TYPE, - 'image' => 'easyengine/nginx-proxy:' . $img_versions['easyengine/nginx-proxy'], - 'restart' => 'always', - 'ports' => [ - "$config_80_port:80", - "$config_443_port:443", - ], - 'environment' => [ - 'LOCAL_USER_ID=' . posix_geteuid(), - 'LOCAL_GROUP_ID=' . posix_getegid(), - ], - 'volumes' => [ - 'certs:/etc/nginx/certs', - 'dhparam:/etc/nginx/dhparam', - 'confd:/etc/nginx/conf.d', - 'htpasswd:/etc/nginx/htpasswd', - 'vhostd:/etc/nginx/vhost.d', - 'html:/usr/share/nginx/html', - 'nginx_proxy_logs:/var/log/nginx', - '/var/run/docker.sock:/tmp/docker.sock:ro', - ], - 'networks' => [ - 'global-frontend-network', - ], - ], - [ - 'name' => GLOBAL_DB, - 'container_name' => GLOBAL_DB_CONTAINER, - 'image' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'], - 'restart' => 'always', - 'environment' => [ - 'MYSQL_ROOT_PASSWORD=' . \EE\Utils\random_password(), - ], - 'volumes' => [ - 'db_data:/var/lib/mysql', - 'db_conf:/etc/mysql', - 'db_logs:/var/log/mysql', - ], - 'networks' => [ - 'global-backend-network', - ], - ], - [ - 'name' => GLOBAL_REDIS, - 'container_name' => GLOBAL_REDIS_CONTAINER, - 'image' => 'easyengine/redis:' . $img_versions['easyengine/redis'], - 'restart' => 'always', - 'command' => '["redis-server", "/usr/local/etc/redis/redis.conf"]', - 'volumes' => [ - 'redis_data:/data', - 'redis_conf:/usr/local/etc/redis', - 'redis_logs:/var/log/redis', - ], - 'networks' => [ - 'global-backend-network', - ], - ], - ], - 'created_volumes' => [ + $data['created_volumes'] = [ 'external_vols' => [ [ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'certs' ], [ 'prefix' => 'global-nginx-proxy', 'ext_vol_name' => 'dhparam' ], @@ -272,6 +226,63 @@ function generate_global_docker_compose_yml( Filesystem $fs ) { [ 'prefix' => GLOBAL_REDIS, 'ext_vol_name' => 'redis_conf' ], [ 'prefix' => GLOBAL_REDIS, 'ext_vol_name' => 'redis_logs' ], ], + ]; + + if ( empty( EE::docker()::get_volumes_by_label( 'global-nginx-proxy' ) ) ) { + EE::docker()::create_volumes( 'global-nginx-proxy', $volumes_nginx_proxy, false ); + } + + if ( empty( EE::docker()::get_volumes_by_label( GLOBAL_DB ) ) ) { + EE::docker()::create_volumes( GLOBAL_DB, $volumes_db, false ); + } + + if ( empty( EE::docker()::get_volumes_by_label( GLOBAL_REDIS ) ) ) { + EE::docker()::create_volumes( GLOBAL_REDIS, $volumes_redis, false ); + } + } + + $data['services'] = [ + [ + 'name' => 'global-nginx-proxy', + 'container_name' => EE_PROXY_TYPE, + 'image' => 'easyengine/nginx-proxy:' . $img_versions['easyengine/nginx-proxy'], + 'restart' => 'always', + 'ports' => [ + "$config_80_port:80", + "$config_443_port:443", + ], + 'environment' => [ + 'LOCAL_USER_ID=' . posix_geteuid(), + 'LOCAL_GROUP_ID=' . posix_getegid(), + ], + 'volumes' => \EE_DOCKER::get_mounting_volume_array( $volumes_nginx_proxy ), + 'networks' => [ + 'global-frontend-network', + ], + ], + [ + 'name' => GLOBAL_DB, + 'container_name' => GLOBAL_DB_CONTAINER, + 'image' => 'easyengine/mariadb:' . $img_versions['easyengine/mariadb'], + 'restart' => 'always', + 'environment' => [ + 'MYSQL_ROOT_PASSWORD=' . \EE\Utils\random_password(), + ], + 'volumes' => \EE_DOCKER::get_mounting_volume_array( $volumes_db ), + 'networks' => [ + 'global-backend-network', + ], + ], + [ + 'name' => GLOBAL_REDIS, + 'container_name' => GLOBAL_REDIS_CONTAINER, + 'image' => 'easyengine/redis:' . $img_versions['easyengine/redis'], + 'restart' => 'always', + 'command' => '["redis-server", "/usr/local/etc/redis/redis.conf"]', + 'volumes' => \EE_DOCKER::get_mounting_volume_array( $volumes_redis ), + 'networks' => [ + 'global-backend-network', + ], ], ]; 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/