Skip to content

Commit

Permalink
Merge pull request WordOps#598 from doofusdavid/create-subsite
Browse files Browse the repository at this point in the history
Create subsite
  • Loading branch information
VirtuBox authored Dec 16, 2023
2 parents 6f214eb + 847614e commit 301b146
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 10 deletions.
4 changes: 2 additions & 2 deletions config/bash_completion.d/wo_auto.rc
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ _wo_complete()

"create")
COMPREPLY=( $(compgen \
-W "--user --pass --email --html --php --php72 --php73 --php74 --php80 --php81 --php82 --php83 --mysql --wp --wpsubdir --wpsubdomain --wpfc --wpsc --proxy= --alias --wpredis --wprocket --wpce -le --letsencrypt --letsencrypt=wildcard -le=wildcard --dns --dns=dns_cf --dns=dns_dgon" \
-W "--user --pass --email --html --php --php72 --php73 --php74 --php80 --php81 --php82 --php83 --mysql --wp --wpsubdir --wpsubdomain --wpfc --wpsc --proxy= --alias --subsiteof --wpredis --wprocket --wpce -le --letsencrypt --letsencrypt=wildcard -le=wildcard --dns --dns=dns_cf --dns=dns_dgon" \
-- $cur) )
;;

"update")
COMPREPLY=( $(compgen \
-W "--password --php --php72 --php73 --php74 --php80 --php81 --php82 --php83 --mysql --wp --wpsubdir --wpsubdomain --wpfc --wpsc --wpredis --wprocket --wpce --alias -le -le=off --letsencrypt --letsencrypt=off --letsencrypt=clean -le=wildcard -le=clean --dns --dns=dns_cf --dns=dns_dgon --ngxblocker --ngxblocker=off" \
-W "--password --php --php72 --php73 --php74 --php80 --php81 --php82 --php83 --mysql --wp --wpsubdir --wpsubdomain --wpfc --wpsc --wpredis --wprocket --wpce --alias --subsiteof -le -le=off --letsencrypt --letsencrypt=off --letsencrypt=clean -le=wildcard -le=clean --dns --dns=dns_cf --dns=dns_dgon --ngxblocker --ngxblocker=off" \
-- $cur) )
;;
"delete")
Expand Down
67 changes: 64 additions & 3 deletions wo/cli/plugins/site_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
doCleanupAction, setupdatabase, setupwordpress, setwebrootpermissions,
display_cache_settings, copyWildcardCert)
from wo.cli.plugins.sitedb import (addNewSite, deleteSiteInfo,
updateSiteInfo)
updateSiteInfo, getSiteInfo)
from wo.core.acme import WOAcme
from wo.core.domainvalidate import WODomain
from wo.core.git import WOGit
Expand Down Expand Up @@ -64,6 +64,9 @@ class Meta:
(['--alias'],
dict(help="domain name to redirect to",
action='store', nargs='?')),
(['--subsiteof'],
dict(help="create a subsite of a multisite install",
action='store', nargs='?')),
(['-le', '--letsencrypt'],
dict(help="configure letsencrypt ssl for the site",
action='store' or 'store_const',
Expand Down Expand Up @@ -124,17 +127,24 @@ def default(self):
proxyinfo = proxyinfo.split(':')
host = proxyinfo[0].strip()
port = '80' if len(proxyinfo) < 2 else proxyinfo[1].strip()
elif stype is None and not pargs.proxy and not pargs.alias:
elif stype is None and not pargs.proxy and not pargs.alias and not pargs.subsiteof:
stype, cache = 'html', 'basic'
elif stype is None and pargs.alias:
stype, cache = 'alias', ''
alias_name = pargs.alias.strip()
if not alias_name:
Log.error(self, "Please provide alias name")
elif stype is None and pargs.subsiteof:
stype, cache = 'subsite', ''
subsiteof_name = pargs.subsiteof.strip()
if not subsiteof_name:
Log.error(self, "Please provide multisite parent name")
elif stype and pargs.proxy:
Log.error(self, "proxy should not be used with other site types")
elif stype and pargs.alias:
Log.error(self, "alias should not be used with other site types")
elif stype and pargs.subsiteof:
Log.error(self, "subsiteof should not be used with other site types")

if not pargs.site_name:
try:
Expand Down Expand Up @@ -185,6 +195,36 @@ def default(self):
data['alias_name'] = alias_name
data['basic'] = True

if stype == 'subsite':
# Get parent site data
parent_site_info = getSiteInfo(self, subsiteof_name)
if not parent_site_info:
Log.error(self, "Parent site {0} does not exist"
.format(subsiteof_name))
if not parent_site_info.is_enabled:
Log.error(self, "Parent site {0} is not enabled"
.format(subsiteof_name))
if parent_site_info.site_type not in ['wpsubdomain', 'wpsubdir']:
Log.error(self, "Parent site {0} is not WordPress multisite"
.format(subsiteof_name))

data = dict(
site_name=wo_domain, www_domain=wo_www_domain,
static=False, basic=False, multisite=False, webroot=wo_site_webroot)

data["wp"] = parent_site_info.site_type == 'wp'
data["wpfc"] = parent_site_info.cache_type == 'wpfc'
data["wpsc"] = parent_site_info.cache_type == 'wpsc'
data["wprocket"] = parent_site_info.cache_type == 'wprocket'
data["wpce"] = parent_site_info.cache_type == 'wpce'
data["wpredis"] = parent_site_info.cache_type == 'wpredis'
data["wpsubdir"] = parent_site_info.site_type == 'wpsubdir'
data["wo_php"] = ("php" + parent_site_info.php_version).replace(".", "")
data['subsite'] = True
data['subsiteof_name'] = subsiteof_name
data['subsiteof_webroot'] = parent_site_info.site_path


if (pargs.php72 or pargs.php73 or pargs.php74 or
pargs.php80 or pargs.php81 or pargs.php82 or pargs.php83):
data = dict(
Expand Down Expand Up @@ -255,7 +295,8 @@ def default(self):
if ((not pargs.wpfc) and (not pargs.wpsc) and
(not pargs.wprocket) and
(not pargs.wpce) and
(not pargs.wpredis)):
(not pargs.wpredis) and
(not pargs.subsiteof)):
data['basic'] = True

if (cache == 'wpredis'):
Expand Down Expand Up @@ -332,6 +373,26 @@ def default(self):
Log.info(self, "Successfully created site"
" http://{0}".format(wo_domain))

elif 'subsite' in data.keys() and data['subsite']:
addNewSite(self, wo_domain, stype, cache, wo_site_webroot)
# Service Nginx Reload
if not WOService.reload_service(self, 'nginx'):
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain)
deleteSiteInfo(self, wo_domain)
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
Log.error(self, "Check the log for details: "
"`tail /var/log/wo/wordops.log` "
"and please try again")
if wo_auth and len(wo_auth):
for msg in wo_auth:
Log.info(self, Log.ENDC + msg, log=False)
Log.info(self, "Successfully created site"
" http://{0}".format(wo_domain))

else:
addNewSite(self, wo_domain, stype, cache, wo_site_webroot,
php_version=php_version)
Expand Down
2 changes: 1 addition & 1 deletion wo/cli/plugins/site_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def site_package_check(self, stype):
stack.app = self.app
pargs = self.app.pargs
if stype in ['html', 'proxy', 'php', 'php72', 'mysql', 'wp', 'wpsubdir',
'wpsubdomain', 'php73', 'php74', 'php80', 'php81', 'php82', 'php83', 'alias']:
'wpsubdomain', 'php73', 'php74', 'php80', 'php81', 'php82', 'php83', 'alias', 'subsite']:
Log.debug(self, "Setting apt_packages variable for Nginx")

# Check if server has nginx-custom package
Expand Down
55 changes: 51 additions & 4 deletions wo/cli/plugins/site_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class Meta:
(['--alias'],
dict(help="domain name to redirect to",
action='store', nargs='?')),
(['--subsiteof'],
dict(help="create a subsite of a multisite install",
action='store', nargs='?')),
(['-le', '--letsencrypt'],
dict(help="configure letsencrypt ssl for the site",
action='store' or 'store_const',
Expand Down Expand Up @@ -155,11 +158,18 @@ def doupdatesite(self, pargs):
alias_name = pargs.alias.strip()
if not alias_name:
Log.error(self, "Please provide alias name")
elif stype is None and pargs.subsiteof:
stype, cache = 'subsite', ''
subsiteof_name = pargs.subsiteof.strip()
if not subsiteof_name:
Log.error(self, "Please provide multisite parent name")
elif stype is None and not (pargs.proxy or
pargs.letsencrypt or pargs.alias):
pargs.letsencrypt or
pargs.alias or
pargs.subsiteof):
stype, cache = 'html', 'basic'
elif stype and (pargs.proxy or pargs.alias):
Log.error(self, "--proxy/alias can not be used with other site types")
elif stype and (pargs.proxy or pargs.alias or pargs.subsiteof):
Log.error(self, "--proxy/alias/subsiteof can not be used with other site types")

if not pargs.site_name:
try:
Expand Down Expand Up @@ -293,6 +303,35 @@ def doupdatesite(self, pargs):
data['alias'] = True
data['alias_name'] = alias_name

if stype == 'subsite':
# Get parent site data
parent_site_info = getSiteInfo(self, subsiteof_name)
if not parent_site_info:
Log.error(self, "Parent site {0} does not exist"
.format(subsiteof_name))
if not parent_site_info.is_enabled:
Log.error(self, "Parent site {0} is not enabled"
.format(subsiteof_name))
if parent_site_info.site_type not in ['wpsubdomain', 'wpsubdir']:
Log.error(self, "Parent site {0} is not WordPress multisite"
.format(subsiteof_name))

data = dict(
site_name=wo_domain, www_domain=wo_www_domain,
static=False, basic=False, multisite=False, webroot=wo_site_webroot)

data["wp"] = parent_site_info.site_type == 'wp'
data["wpfc"] = parent_site_info.cache_type == 'wpfc'
data["wpsc"] = parent_site_info.cache_type == 'wpsc'
data["wprocket"] = parent_site_info.cache_type == 'wprocket'
data["wpce"] = parent_site_info.cache_type == 'wpce'
data["wpredis"] = parent_site_info.cache_type == 'wpredis'
data["wpsubdir"] = parent_site_info.site_type == 'wpsubdir'
data["wo_php"] = ("php" + parent_site_info.php_version).replace(".", "")
data['subsite'] = True
data['subsiteof_name'] = subsiteof_name
data['subsiteof_webroot'] = parent_site_info.site_path

if stype == 'php':
data = dict(
site_name=wo_domain, www_domain=wo_www_domain,
Expand Down Expand Up @@ -483,7 +522,8 @@ def doupdatesite(self, pargs):
version in WOVar.wo_php_versions.items()) and (stype == oldsitetype
and cache == oldcachetype
and stype != 'alias'
and stype != 'proxy'):
and stype != 'proxy'
and stype != 'subsite'):
Log.debug(self, "Nothing to update")
return 1

Expand Down Expand Up @@ -548,6 +588,13 @@ def doupdatesite(self, pargs):
" http://{0}".format(wo_domain))
return 0

if 'subsite' in data.keys() and data['subsite']:
updateSiteInfo(self, wo_domain, stype=stype, cache=cache,
ssl=(bool(check_site.is_ssl)))
Log.info(self, "Successfully updated site"
" http://{0}".format(wo_domain))
return 0

if pargs.letsencrypt:
if data['letsencrypt'] is True:
# DNS API configuration
Expand Down
7 changes: 7 additions & 0 deletions wo/cli/templates/virtualconf.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ server {

{{^proxy}}
{{^alias}}

{{^subsite}}
root {{webroot}}/htdocs;
{{/subsite}}

{{#subsite}}
root {{subsiteof_webroot}}/htdocs;
{{/subsite}}

index {{^static}}index.php{{/static}} index.html index.htm;

Expand Down

0 comments on commit 301b146

Please sign in to comment.