Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

[PoC] Lando multi instance support #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .env → .env.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ DB_NAME_TESTS=test
WP_DIR=/app/wp
WP_TESTS_DIR=/tmp/wp-tests
WP_CLI_CONFIG_PATH=/app/configs/wp-cli.yml
DOMAIN=%LANDO_NAME%.vipdev.lndo.site
TITLE="%TITLE%"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/wp/
/wp-content/
/wp-tests/
/site-*

# Composer
vendor
Expand Down
24 changes: 20 additions & 4 deletions .lando.yml → .lando.yml.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: vipwpdevenv
name: vipdev%LANDO_NAME%
recipe: wordpress
env_file:
- .env
Expand All @@ -11,15 +11,18 @@ config:
# https://docs.devwithlando.io/tutorials/php.html#toggling-xdebug
xdebug: true
config:
vhosts: configs/nginx-wordpress.conf
php: configs/php.ini
vhosts: ../configs/nginx-wordpress.conf
php: ../configs/php.ini
proxy:
appserver_nginx:
- vip-go-dev.lndo.site
- '%LANDO_NAME%.vipdev.lndo.site'
- '*.%LANDO_NAME%.vipdev.lndo.site'
services:
appserver:
overrides:
volumes:
- ../bin:/app/bin
- ../configs:/app/configs
- ./mu-plugins:/app/wp/wp-content/mu-plugins
- ./wp-content/client-mu-plugins:/app/wp/wp-content/client-mu-plugins
- ./wp-content/images:/app/wp/wp-content/images
Expand All @@ -30,6 +33,7 @@ services:
- ./wp-content/vip-config:/app/wp/wp-content/vip-config
run:
- bash /app/bin/lando/setup.sh
%MULTISITE%- bash /app/bin/lando/setup-multisite.sh
run_as_root:
- bash /app/bin/lando/setup-as-root.sh
build_as_root:
Expand Down Expand Up @@ -75,3 +79,15 @@ tooling:
description: "Swap your wp-content with a client repo or the vip-go-skeleton"
cmd:
- bash /app/bin/lando/vip-switch.sh

setup-multisite:
service: appserver
description: "Setup WordPress network to enable multisite mode"
cmd:
- bash /app/bin/lando/setup-multisite.sh

add-site:
service: appserver
description: "Add site to a multisite installation"
cmd:
- bash /app/bin/lando/add-site.sh
41 changes: 41 additions & 0 deletions bin/lando/add-site.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

syntax() {
echo "Syntax: lando add-site --slug=<slug> --title=\"<title>\""
exit 1
}

# Parse title and slug arguments
arguments=`getopt -o '' -l slug:,title: -- "$@"`
eval set -- "$arguments"
while true; do
case "$1" in
--slug) slug=$2; shift 2;;
--title) title=$2; shift 2;;
--) shift; break;;
esac
done
[ -z "$slug" ] && echo "ERROR: Missing or empty slug argument" && syntax
[ -z "$title" ] && echo "ERROR: Missing or empty title argument" && syntax

site_domain=$slug.$DOMAIN

echo "Checking if this is a multisite installation..."
wp core is-installed --network
[ $? -ne 0 ] && echo "ERROR: Not a multisite, please run 'lando setup-multisite' first" && exit 1

echo "Checking if $site_domain already belongs to another site..."
wp --path=$LANDO_WEBROOT site list --field=domain | grep -q "^$site_domain$"
[ $? -eq 0 ] && echo "ERROR: site with domain $site_domain already exists" && exit 1

echo "Creating the new site..."
wp --path=$LANDO_WEBROOT site create --title="$title" --slug="$slug"

echo
echo "======================================================================"
echo "Site '$title' added correctly"
echo
echo "You can access it using these URLs:"
echo " http://$site_domain/"
echo " http://$site_domain/wp-admin/"
echo "======================================================================"
18 changes: 18 additions & 0 deletions bin/lando/setup-multisite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# Check if this is already a multisite installation
wp core is-installed --network
[ $? -eq 0 ] && echo "WARNING: Multisite already setup, no need to do anything" && exit 1

echo "Converting installation to multisite..."
wp core multisite-convert --path=$LANDO_WEBROOT --title="VIP Go Dev Network" --subdomains

echo "=========================================================================="
echo "WordPress installation converted to multisite"
echo
echo "You can now add sites with:"
echo " lando add-site --slug=<slug> --title=\"<title>\""
echo
echo "You can also add sites manually from wp-admin. In both cases, sites"
echo "will follow this url schema: http://<slug>.$DOMAIN/"
echo "=========================================================================="
4 changes: 2 additions & 2 deletions bin/lando/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ echo_heading "Installing WordPress $WP_VERSION"

wp core install \
--path=$LANDO_WEBROOT \
--url="http://vip-go-dev.lndo.site" \
'--title="VIP Go Dev"' \
--url="http://$DOMAIN" \
--title="$TITLE" \
--admin_user="vipgo" \
--admin_password="password" \
--admin_email="vip@localhost.local" \
Expand Down
83 changes: 83 additions & 0 deletions vip-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

syntax() {
echo "Syntax: vip-create.sh -s <slug> [-t \"<title>\"] [-m] [-w <wordpress_version>]"
echo " [-r <client_repo>] [-b <client_branch>] [-u <mu_branch>]"
echo
echo " -s <slug> Short name to be used for the lando project and the internal domain"
echo " -t <title> Title for the WordPress site (default: \"VIP Go Dev\")"
echo " -m Enable multisite WordPress install"
echo " -w <wp_version> Use a specific WordPress version (default: last stable version)"
echo " -r <client_repo> Clone a specific client code repository (default: vip-go skeleton)"
echo " -b <client_branch> Use this branch from the client code repository (default: master)"
echo " -u <mu_branch> Use this branch for the mu-plugins repository (default: master)"
}

# Parse arguments
while getopts "hs:t:mw:r:b:u:" opt; do
case "$opt" in
h) syntax; exit;;
s) slug=$OPTARG;;
t) title=$OPTARG;;
m) multisite=true;;
w) wp_version=$OPTARG;;
r) client_repo=$OPTARG;;
b) client_branch=$OPTARG;;
u) mu_branch=$OPTARG;;
esac
done

[ -z "$slug" ] && echo "ERROR: Missing or empty slug argument" && syntax && exit 1

instance=site-$slug

[ -d "$instance" ] && echo "ERROR: Site already exists. Remove $instance and try again" && exit 1

mkdir $instance
cd $instance

# TODO: could we reuse some of the WordPress clone across multiple instances?
[ -z "$wp_version" ] && wp_version=`curl -Ls http://api.wordpress.org/core/version-check/1.7/ | perl -ne '/"version":\s*"([\d\.]+)"/; print $1;'`
echo "Cloning WordPress $wp_version"
git clone --branch $wp_version --depth 1 git@github.com:WordPress/WordPress.git wp

# TODO: should we reuse mu-plugins+branch clone across multiple instances?
[ -z "$mu_branch" ] && mu_branch="master"
echo "Cloning mu-plugins on branch $mu_branch"
git clone git@github.com:Automattic/vip-go-mu-plugins.git mu-plugins -b $mu_branch
cd mu-plugins
echo "git submodules"
git submodule update --init --recursive --jobs 8
echo "npm + composer install"
npm install
composer install
cd ..

echo "VIP client code"
if [ -n "$client_repo" ]; then
[ -z "$client_branch" ] && client_branch="master"
git clone $client_repo wp-content -b $client_branch
# TODO: change the theme
# TODO: enable client plugins?
else
# TODO: reuse vip-go-skeleton across multiple instances
svn export https://github.com/Automattic/vip-go-skeleton/trunk wp-content
fi

if [ "$multisite" = "true" ]; then
multisite_comment=''
else
multisite_comment='#'
fi

[ -z "$title" ] && title="VIP Go Dev"

echo "Creating .env"
cat ../.env.tpl | sed -e "s/%LANDO_NAME%/$slug/g" -e "s/%TITLE%/$title/" > .env

echo "Creating .lando.yml"
cat ../.lando.yml.tpl | sed -e "s/%LANDO_NAME%/$slug/g" -e "s/%MULTISITE%/$multisite_comment/" > .lando.yml

echo
echo "Site created in $instance. To start it:"
echo "$ cd $instance; lando start"