2020-03-08 17:21:12 +00:00
#!/bin/sh
2020-03-12 19:31:36 +00:00
set -e
2020-03-08 23:14:07 +00:00
_docker_dir = " $_docker_base_dir / $_remote_host "
2020-03-08 17:21:12 +00:00
2020-03-08 23:14:07 +00:00
_ssh_cmd = " ssh root@ $_remote_host . $_domain "
_scp_to_base = " root@ $_remote_host . $_domain : $_docker_dir "
2020-03-08 17:21:12 +00:00
2020-03-09 20:45:35 +00:00
echo " HOST= $_remote_host " > .env
echo " DOMAIN= $_domain " >> .env
echo " MYSQL_ROOT_PASSWD= $_mysql_root_passwd " >> .env
2020-03-09 20:52:10 +00:00
echo "MYSQL_DATABASE=wordpress" >> .env
2020-03-09 20:45:35 +00:00
echo "MYSQL_USER=wordpress" >> .env
echo "MYSQL_PASSWORD=wordpress" >> .env
2020-03-08 23:14:07 +00:00
$_ssh_cmd " mkdir -p $_docker_dir /wordpress "
2020-03-08 17:21:12 +00:00
2020-03-09 18:44:33 +00:00
echo "Copying files ..."
2020-03-08 23:14:07 +00:00
scp -r production/config " $_scp_to_base " /config
2020-03-10 19:36:39 +00:00
scp production/docker/Dockerfile " $_scp_to_base " /Dockerfile
scp production/docker/entrypoint.sh " $_scp_to_base " /entrypoint.sh
2020-03-09 20:45:35 +00:00
scp .env " $_scp_to_base " /.env
2020-03-10 19:36:39 +00:00
scp production/docker/docker-compose.yml " $_scp_to_base " /docker-compose.yml
2020-03-08 17:21:12 +00:00
2020-03-08 23:14:07 +00:00
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 "
2020-03-08 17:21:12 +00:00
2020-03-09 18:44:33 +00:00
echo "Configuring files ..."
2020-03-08 23:14:07 +00:00
$_ssh_cmd " sed -i 's/__HOST__/ $_remote_host /g' $_docker_dir /config/nginx.conf "
2020-03-09 18:44:33 +00:00
# TODO make this more robust than just substituting localhost
2020-03-08 23:14:07 +00:00
$_ssh_cmd " sed -i 's/localhost/ $_remote_host -mysql/g' $_docker_dir /wordpress/wp-config.php "
2020-03-09 20:14:10 +00:00
$_ssh_cmd " cat $_docker_dir /config/wp-config-forward-headers.php $_docker_dir /wordpress/wp-config.php >/tmp/out.tmp && mv /tmp/out.tmp $_docker_dir /wordpress/wp-config.php "
2020-03-08 17:21:12 +00:00
$_ssh_cmd " chown -R $_docker_user : $_docker_dir "
2020-03-09 18:44:33 +00:00
# The 82 comes from the Dockerfile addgroup and adduser commands
$_ssh_cmd " chown -R 82:82 $_docker_dir /wordpress "
2020-03-08 17:21:12 +00:00
2020-03-09 18:44:33 +00:00
echo "Starting containers ..."
2020-03-08 23:14:07 +00:00
$_ssh_cmd " cd $_docker_dir && docker-compose up -d --build "
# TODO - need to wait for mysql to initialise
2020-03-09 18:44:33 +00:00
# temporary solution
2020-03-08 23:14:07 +00:00
echo "waiting for mysql to initialise ..."
sleep 10
2020-03-09 20:52:10 +00:00
echo "still waiting for mysql ..."
2020-03-08 23:14:07 +00:00
sleep 10
2020-03-09 20:52:10 +00:00
echo "and still waiting for mysql ..."
2020-03-08 23:14:07 +00:00
sleep 5
2020-03-09 18:44:33 +00:00
echo "Configuring WordPress ..."
2020-03-08 23:14:07 +00:00
# TODO does docker always assign ips in range 172.% ????
2020-03-09 18:44:33 +00:00
# TODO - sort this mess out - use functions or something
2020-03-12 19:31:36 +00:00
_ssh_shell_cmd = 'echo "GRANT ALL PRIVILEGES ON wordpress."*" TO \"wordpress\"@\"172.%\" IDENTIFIED BY \"' " $_wp_db_passwd " '\";" | mysql -uroot -p' " $_mysql_root_passwd "
2020-03-08 23:14:07 +00:00
_cmd_docker_exec = " cd $_docker_dir && docker exec -t $_remote_host -mysql /bin/sh -c ' $_ssh_shell_cmd ' "
2020-03-09 18:44:33 +00:00
echo " $_ssh_cmd $_cmd_docker_exec "
2020-03-08 23:14:07 +00:00
$_ssh_cmd $_cmd_docker_exec
2020-03-12 19:31:36 +00:00
_cmd_wp = " wp db import --dbuser=wordpress --dbpass= $_wp_db_passwd data.sql && rm data.sql "
2020-03-09 18:44:33 +00:00
_cmd_docker_exec = " cd $_docker_dir && docker exec -t $_remote_host -wordpress /bin/sh -c ' $_cmd_wp ' "
echo " $_ssh_cmd $_cmd_docker_exec "
$_ssh_cmd " $_cmd_docker_exec "
2020-03-09 20:45:35 +00:00
_cmd_wp = "wp rewrite structure /%postname%/"
_cmd_docker_exec = " cd $_docker_dir && docker exec -t $_remote_host -wordpress /bin/sh -c ' $_cmd_wp ' "
echo " $_ssh_cmd $_cmd_docker_exec "
$_ssh_cmd " $_cmd_docker_exec "
2020-03-09 20:14:10 +00:00
# _cmd_wp="wp user create $_wp_admin_user"
# _cmd_docker_exec="cd $_docker_dir && docker exec -t $_remote_host-wordpress /bin/sh -c '$_cmd_wp'"
# echo "$_ssh_cmd $_cmd_docker_exec"
# $_ssh_cmd "$_cmd_docker_exec"
2020-03-09 18:44:33 +00:00
# TODO move this into helper script
# wp search-replace 'example.dev' 'example.com' --skip-columns=guid
# Or, if you only want to change the option, you can do:
# wp option update home 'http://example.com'
# wp option update siteurl 'http://example.com';
2020-03-09 20:14:10 +00:00
_cmd_wp = " wp search-replace 'http:// $_local_hostname . $_local_domain ' 'https:// $_local_hostname . $_local_domain ' --skip-columns=guid "
2020-03-09 18:44:33 +00:00
_cmd_docker_exec = " cd $_docker_dir && docker exec -t $_remote_host -wordpress /bin/sh -c ' $_cmd_wp ' "
echo " $_ssh_cmd $_cmd_docker_exec "
$_ssh_cmd " $_cmd_docker_exec "
2020-03-09 20:14:10 +00:00
_cmd_wp = " wp search-replace ' $_local_hostname . $_local_domain ' ' $_remote_host . $_domain ' --skip-columns=guid "
_cmd_docker_exec = " cd $_docker_dir && docker exec -t $_remote_host -wordpress /bin/sh -c ' $_cmd_wp ' "
echo " $_ssh_cmd $_cmd_docker_exec "
$_ssh_cmd " $_cmd_docker_exec "
2020-03-09 18:44:33 +00:00
2020-03-09 20:45:35 +00:00
rm .env