diff --git a/config-example b/config-example new file mode 100644 index 0000000..f23200e --- /dev/null +++ b/config-example @@ -0,0 +1,31 @@ +# rename to `config` and assign suitable values. + +# local configuration + +export _local_hostname="wp-now" +export _local_domain="home" + +export _user_www='www-data' +export _passwd_www='www-data' + +export _wp_title="WP Test" +export _wp_admin_user="admin" +export _wp_password="admin" +export _wp_email="$_local_hostname@isnet.uk" +export _wp_db_passwd="wordpress" + +export _wp_theme_active="twentytwenty" +export _wp_themes_additional="twentynineteen twentyseventeen" + +export _wp_plugins="better-wp-security wp-fastest-cache autodescription google-analytics-for-wordpress" +export _wp_plugins_active="elementor wp-mail-smtp gdpr-cookie-compliance regenerate-thumbnails" + + +# remote configuration + +export _docker_user="ray" + +export _remote_host="wp-stack-four" +export _domain="wptest.isnet.uk" +export _docker_base_dir="/var/docker" +export _mysql_root_passwd="db" diff --git a/dev/config b/dev/config deleted file mode 120000 index 288d285..0000000 --- a/dev/config +++ /dev/null @@ -1 +0,0 @@ -config-example \ No newline at end of file diff --git a/dev/config-example b/dev/config-example deleted file mode 100644 index 08160f0..0000000 --- a/dev/config-example +++ /dev/null @@ -1,16 +0,0 @@ -# rename to `config` and assign suitable values. - -_user_www='www-data' -_passwd_www='www-data' - -_wp_title="WP Test" -_wp_admin_user="admin" -_wp_password="admin" -_wp_email="admin@$_host" -_wp_db_passwd="wordpress" - -_wp_theme_active="twentytwenty" -_wp_themes_additional="twentynineteen twentyseventeen" - -_wp_plugins="better-wp-security wp-fastest-cache autodescription google-analytics-for-wordpress" -_wp_plugins_active="elementor wp-mail-smtp gdpr-cookie-compliance regenerate-thumbnails" diff --git a/dev/container-create.sh b/dev/container-create.sh index e1f565c..f6f9341 100755 --- a/dev/container-create.sh +++ b/dev/container-create.sh @@ -1,13 +1,6 @@ #!/bin/sh -if [ -z "$1" ] ; then - echo "Must supply container name as argument" - exit 1 -fi - -. ./config - -_host="$1.home" +_host="$_local_hostname.$_local_domain" _user_root='root' _ssh_cmd_root="ssh $_user_root@$_host" diff --git a/dev/download-data.sh b/dev/download-data.sh index 9f643ee..1a0e8cb 100755 --- a/dev/download-data.sh +++ b/dev/download-data.sh @@ -1,13 +1,13 @@ #!/bin/sh -if [ -z "$1" ] ; then - echo "Must supply container name as argument" - exit 1 +if [ -z "$1" ] ; then + _target_filename='.' +else + _target_filename="$1" fi -. ./config - -_host="$1.home" +_host="$_local_hostname.$_local_domain" +echo "$_host" _cmd_wp_export="wp db export --dbuser=wordpress --dbpass=$_wp_db_passwd --add-drop-table data.sql" _ssh_cmd="sshpass -p$_passwd_www ssh $_user_www@$_host" @@ -16,5 +16,5 @@ _scp_cmd="sshpass -p$_passwd_www scp $_user_www@$_host:/tmp/wordpress.tar.gz" $_ssh_cmd "cd /var/www/html/wordpress &&" $_cmd_wp_export $_ssh_cmd 'cd /var/www/html/wordpress && tar czf /tmp/wordpress.tar.gz .' $_ssh_cmd "rm /var/www/html/wordpress/data.sql" -$_scp_cmd "." +$_scp_cmd "$_target_filename" $_ssh_cmd 'rm /tmp/wordpress.tar.gz' diff --git a/production/deploy-to-container.sh b/production/deploy-to-container.sh new file mode 100755 index 0000000..59426b3 --- /dev/null +++ b/production/deploy-to-container.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +_docker_service_name="blog" +_mysql_root_passwd="dklfm904mg-uiojn" +_wordpress_tag="latest" +_mariadb_tag="10.5" +_site_domain="blog.wptest.isnet.uk" + +echo " +version: \"3\" + +networks: + web: + external: true + internal: + external: false + +services: +# TODO this needs to be just an apache or php container or whatever with a persistent +# html volume + $_docker_service_name: + image: wordpress:$_wordpress_tag + container_name: $_docker_service_name + environment: + WORDPRESS_DB_PASSWORD: $_mysql_root_passwd + labels: + - \"traefik.enable=true\" + - \"traefik.http.routers.$_docker_service_name.entrypoints=http\" + - \"traefik.http.routers.$_docker_service_name.rule=Host(\`$_site_domain\`)\" + - \"traefik.http.middlewares.$_docker_service_name.redirectscheme.scheme=https\" + - \"traefik.http.routers.$_docker_service_name.middlewares=$_docker_service_name\" + - \"traefik.http.routers.$_docker_service_name.entrypoints=https\" + - \"traefik.http.routers.$_docker_service_name.rule=Host(\`$_site_domain\`)\" + - \"traefik.http.routers.$_docker_service_name.tls=true\" + - \"traefik.http.routers.$_docker_service_name.tls.certresolver=http\" + - \"traefik.http.services.$_docker_service_name.loadbalancer.server.port=80\" + - \"traefik.http.routers.$_docker_service_name.service=$_docker_service_name\" + - \"traefik.docker.network=web\" + networks: + - internal + - web + depends_on: + - mysql + mariadb: + image: mariadb:$_mariadb_tag + environment: + MYSQL_ROOT_PASSWORD: $_mysql_root_passwd + networks: + - internal + labels: + - traefik.enable=false +" > docker-compose.yml diff --git a/production/docker-compose.yml b/production/docker-compose.yml index 7fbe9bf..fd25b01 100644 --- a/production/docker-compose.yml +++ b/production/docker-compose.yml @@ -17,8 +17,6 @@ services: restart: unless-stopped environment: - ALLOW_EMPTY_PASSWORD=yes - ports: - - '127.0.0.1:6379:6379' # volumes: # - 'redis-data:/bitnami/redis/data' labels: @@ -36,10 +34,11 @@ services: volumes: # - ./wp-db:/var/lib/mysql - ./config/my.cnf:/etc/mysql/conf.d/zzz_my.cnf - ports: - - "127.0.0.1:3306:3306" environment: - - MYSQL_ROOT_PASSWORD=db + - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWD} + - MYSQL_USER=${MYSQL_USER} + - MYSQL_PASSWORD=${MYSQL_PASSWORD} + - MYSQL_DATABASE=${MYSQL_DATABASE} labels: - "traefik.enable=false" @@ -63,15 +62,13 @@ services: wordpress: depends_on: - db + - redis networks: - backend container_name: ${HOST}-wordpress # env_file: # - .env build: ./ - ports: - - "127.0.0.1:8081:80" - - "127.0.0.1:9000:9000" volumes: - ./wordpress:/var/www/wordpress labels: diff --git a/production/export-to-production.sh b/production/export-to-production.sh index 3de77a3..3a06301 100755 --- a/production/export-to-production.sh +++ b/production/export-to-production.sh @@ -1,37 +1,61 @@ #!/bin/sh -_docker_user="ray" +_docker_dir="$_docker_base_dir/$_remote_host" -_host="wp-stack-two" -_domain="wptest.isnet.uk" -_docker_base_dir="/var/docker" -_docker_dir="$_docker_base_dir/$_host" +_ssh_cmd="ssh root@$_remote_host.$_domain" +_scp_to_base="root@$_remote_host.$_domain:$_docker_dir" -# _cmd_wp_export="wp db export --dbuser=wordpress --dbpass=$_wp_db_passwd --add-drop-table data.sql" -_ssh_cmd="ssh root@$_host.$_domain" -_scp_to_base="root@$_host.$_domain:$_docker_dir" - -echo "HOST=$_host" > .env +echo "HOST=$_remote_host" > .env echo "DOMAIN=$_domain" >> .env +echo "MYSQL_ROOT_PASSWD=$_mysql_root_passwd" >> .env +echo "MYSQL_USER=wordpress" >> .env +echo "MYSQL_PASSWORD=wordpress" >> .env +echo "MYSQL_DATABASE=wordpress" >> .env -$_ssh_cmd "mkdir -p $_docker_dir" +$_ssh_cmd "mkdir -p $_docker_dir/wordpress" -scp -r config "$_scp_to_base"/config -scp Dockerfile "$_scp_to_base"/Dockerfile -scp entrypoint.sh "$_scp_to_base"/entrypoint.sh -scp .env "$_scp_to_base"/.env -scp docker-compose.yml "$_scp_to_base"/docker-compose.yml +scp -r production/config "$_scp_to_base"/config +scp production/Dockerfile "$_scp_to_base"/Dockerfile +scp production/entrypoint.sh "$_scp_to_base"/entrypoint.sh +scp ./.env "$_scp_to_base"/.env +scp production/docker-compose.yml "$_scp_to_base"/docker-compose.yml -$_ssh_cmd "sed -i 's/__HOST__/$_host/g' $_docker_dir/config/nginx.conf" +scp wordpress.tar.gz "$_scp_to_base"/wordpress.tar.gz +$_ssh_cmd "tar -xf $_docker_dir/wordpress.tar.gz -C $_docker_dir/wordpress && rm $_docker_dir/wordpress.tar.gz" + +$_ssh_cmd "sed -i 's/__HOST__/$_remote_host/g' $_docker_dir/config/nginx.conf" +# TODO make this more robust than just changing any old localhost +$_ssh_cmd "sed -i 's/localhost/$_remote_host-mysql/g' $_docker_dir/wordpress/wp-config.php" $_ssh_cmd "chown -R $_docker_user: $_docker_dir" -# export wordpress directory +$_ssh_cmd "cd $_docker_dir && docker-compose up -d --build" -# import database +# TODO - need to wait for mysql to initialise +# temporary +echo "waiting for mysql to initialise ..." +sleep 10 +echo "still waiting for mysql to initialise ..." +sleep 10 +echo "waiting a little bit longer for mysql to initialise ..." +sleep 5 + +# TODO does docker always assign ips in range 172.% ???? +_ssh_shell_cmd='echo "GRANT ALL PRIVILEGES ON wordpress."*" TO \"wordpress\"@\"172.%\" IDENTIFIED BY \"wordpress\";" | mysql -uroot -pdb' +# _ssh_shell_cmd='echo "'"$_mysql_cmd"'" | mysql -uroot -pdb' +echo "$_ssh_shell_cmd" +_cmd_docker_exec="cd $_docker_dir && docker exec -t $_remote_host-mysql /bin/sh -c '$_ssh_shell_cmd'" + +echo "$_ssh_cmd -- $_cmd_docker_exec" +$_ssh_cmd $_cmd_docker_exec + + +_cmd_wp_import="wp db import --dbuser=wordpress --dbpass=$_wp_db_passwd data.sql" +_cmd_docker_exec="cd $_docker_dir && docker exec -t $_remote_host-wordpress /bin/sh -c '$_cmd_wp_import'" +$_ssh_cmd "echo '$_wp_db_passwd' && $_cmd_docker_exec" # set correct URLS # ensure HTTP_X_FORWARDED_PROTO header is set in wp-config.php -rm .env +# rm .env diff --git a/publish.sh b/publish.sh new file mode 100755 index 0000000..0c673e3 --- /dev/null +++ b/publish.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +. ./config + +# ./dev/download-data.sh + +./production/export-to-production.sh + + +