Files
WPIQ/wo/cli/plugins/site.py

2125 lines
94 KiB
Python
Raw Normal View History

import glob
import json
import os
import subprocess
2018-11-13 21:55:59 +01:00
from cement.core import handler, hook
from cement.core.controller import CementBaseController, expose
2018-11-13 21:55:59 +01:00
from wo.cli.plugins.site_functions import *
from wo.cli.plugins.sitedb import (addNewSite, deleteSiteInfo, getAllsites,
getSiteInfo, updateSiteInfo)
2019-09-22 19:30:39 +02:00
from wo.core.acme import WOAcme
2019-09-19 14:07:34 +02:00
from wo.core.domainvalidate import WODomain
from wo.core.fileutils import WOFileUtils
2018-11-13 21:55:59 +01:00
from wo.core.git import WOGit
2019-08-18 00:03:11 +02:00
from wo.core.logging import Log
2018-11-13 21:55:59 +01:00
from wo.core.nginxhashbucket import hashbucket
from wo.core.services import WOService
from wo.core.shellexec import WOShellExec
from wo.core.sslutils import SSL
from wo.core.variables import WOVariables
2018-11-13 21:55:59 +01:00
def wo_site_hook(app):
from wo.core.database import init_db
import wo.cli.plugins.models
init_db(app)
class WOSiteController(CementBaseController):
class Meta:
label = 'site'
stacked_on = 'base'
stacked_type = 'nested'
description = ('Performs website specific operations')
arguments = [
(['site_name'],
dict(help='Website name', nargs='?')),
]
2018-11-13 21:55:59 +01:00
usage = "wo site (command) <site_name> [options]"
@expose(hide=True)
def default(self):
self.app.args.print_help()
@expose(help="Enable site example.com")
def enable(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'could not input site name')
pargs.site_name = pargs.site_name.strip()
2018-11-13 21:55:59 +01:00
# validate domain name
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
# check if site exists
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(wo_domain)):
Log.info(self, "Enable domain {0:10} \t".format(wo_domain), end='')
WOFileUtils.create_symlink(self,
['/etc/nginx/sites-available/{0}'
.format(wo_domain),
'/etc/nginx/sites-enabled/{0}'
.format(wo_domain)])
WOGit.add(self, ["/etc/nginx"],
msg="Enabled {0} "
.format(wo_domain))
updateSiteInfo(self, wo_domain, enabled=True)
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
else:
2019-09-02 22:24:46 +02:00
Log.error(self, "nginx configuration file does not exist")
2018-11-13 21:55:59 +01:00
@expose(help="Disable site example.com")
def disable(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'could not input site name')
pargs.site_name = pargs.site_name.strip()
2019-09-19 15:59:25 +02:00
(wo_domain, wo_www_domain) = WODomain.validatedomain(self,
pargs.site_name)
2018-11-13 21:55:59 +01:00
# check if site exists
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(wo_domain)):
Log.info(self, "Disable domain {0:10} \t"
.format(wo_domain), end='')
if not os.path.isfile('/etc/nginx/sites-enabled/{0}'
.format(wo_domain)):
Log.debug(self, "Site {0} already disabled".format(wo_domain))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
else:
WOFileUtils.remove_symlink(self,
'/etc/nginx/sites-enabled/{0}'
.format(wo_domain))
WOGit.add(self, ["/etc/nginx"],
msg="Disabled {0} "
.format(wo_domain))
updateSiteInfo(self, wo_domain, enabled=False)
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
else:
2019-09-02 22:24:46 +02:00
Log.error(self, "nginx configuration file does not exist")
2018-11-13 21:55:59 +01:00
@expose(help="Get example.com information")
def info(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'could not input site name')
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
(wo_domain_type,
wo_root_domain) = WODomain.getdomainlevel(self, wo_domain)
2018-11-13 21:55:59 +01:00
wo_db_name = ''
wo_db_user = ''
wo_db_pass = ''
2019-03-13 11:55:18 +01:00
2018-11-13 21:55:59 +01:00
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(wo_domain)):
siteinfo = getSiteInfo(self, wo_domain)
sitetype = siteinfo.site_type
cachetype = siteinfo.cache_type
wo_site_webroot = siteinfo.site_path
access_log = (wo_site_webroot + '/logs/access.log')
error_log = (wo_site_webroot + '/logs/error.log')
wo_db_name = siteinfo.db_name
wo_db_user = siteinfo.db_user
wo_db_pass = siteinfo.db_password
wo_db_host = siteinfo.db_host
php_version = siteinfo.php_version
ssl = ("enabled" if siteinfo.is_ssl else "disabled")
if (ssl == "enabled"):
sslprovider = "Lets Encrypt"
2019-08-30 08:51:43 +02:00
if os.path.islink("{0}/conf/nginx/ssl.conf"
.format(wo_site_webroot)):
sslexpiry = str(
2019-09-04 03:07:24 +02:00
SSL.getexpirationdays(self, wo_root_domain))
2019-08-30 08:51:43 +02:00
else:
2019-09-04 03:07:24 +02:00
sslexpiry = str(SSL.getexpirationdays(self, wo_domain))
2018-11-13 21:55:59 +01:00
else:
sslprovider = ''
sslexpiry = ''
data = dict(domain=wo_domain, webroot=wo_site_webroot,
accesslog=access_log, errorlog=error_log,
2019-04-07 22:10:08 +02:00
dbname=wo_db_name, dbuser=wo_db_user,
php_version=php_version,
2019-03-13 11:55:18 +01:00
dbpass=wo_db_pass,
ssl=ssl, sslprovider=sslprovider, sslexpiry=sslexpiry,
2018-11-13 21:55:59 +01:00
type=sitetype + " " + cachetype + " ({0})"
.format("enabled" if siteinfo.is_enabled else
"disabled"))
self.app.render((data), 'siteinfo.mustache')
else:
2019-09-02 22:24:46 +02:00
Log.error(self, "nginx configuration file does not exist")
2018-11-13 21:55:59 +01:00
@expose(help="Monitor example.com logs")
def log(self):
pargs = self.app.pargs
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
wo_site_webroot = getSiteInfo(self, wo_domain).site_path
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
logfiles = glob.glob(wo_site_webroot + '/logs/*.log')
if logfiles:
logwatch(self, logfiles)
@expose(help="Display Nginx configuration of example.com")
def show(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'could not input site name')
# TODO Write code for wo site edit command here
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(wo_domain)):
Log.info(self, "Display NGINX configuration for {0}"
.format(wo_domain))
f = open('/etc/nginx/sites-available/{0}'.format(wo_domain),
encoding='utf-8', mode='r')
text = f.read()
Log.info(self, Log.ENDC + text)
f.close()
else:
Log.error(self, "nginx configuration file does not exists"
.format(wo_domain))
@expose(help="Change directory to site webroot")
def cd(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'Unable to read input, please try again')
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
wo_site_webroot = getSiteInfo(self, wo_domain).site_path
WOFileUtils.chdir(self, wo_site_webroot)
try:
2019-08-01 11:09:49 +02:00
subprocess.call(['/bin/bash'])
2018-11-13 21:55:59 +01:00
except OSError as e:
Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
Log.error(self, "unable to change directory")
class WOSiteEditController(CementBaseController):
class Meta:
label = 'edit'
stacked_on = 'site'
stacked_type = 'nested'
description = ('Edit Nginx configuration of site')
arguments = [
(['site_name'],
dict(help='domain name for the site',
nargs='?')),
]
2018-11-13 21:55:59 +01:00
@expose(hide=True)
def default(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'Unable to read input, Please try again')
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
wo_site_webroot = WOVariables.wo_webroot + wo_domain
if os.path.isfile('/etc/nginx/sites-available/{0}'
.format(wo_domain)):
try:
WOShellExec.invoke_editor(self, '/etc/nginx/sites-availa'
'ble/{0}'.format(wo_domain))
except CommandExecutionError as e:
2019-07-29 04:23:37 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, "Failed invoke editor")
if (WOGit.checkfilestatus(self, "/etc/nginx",
2019-04-07 22:10:08 +02:00
'/etc/nginx/sites-available/{0}'
.format(wo_domain))):
2018-11-13 21:55:59 +01:00
WOGit.add(self, ["/etc/nginx"], msg="Edit website: {0}"
.format(wo_domain))
# Reload NGINX
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
else:
Log.error(self, "nginx configuration file does not exists"
.format(wo_domain))
class WOSiteCreateController(CementBaseController):
class Meta:
label = 'create'
stacked_on = 'site'
stacked_type = 'nested'
description = ('this commands set up configuration and installs '
'required files as options are provided')
arguments = [
(['site_name'],
dict(help='domain name for the site to be created.',
nargs='?')),
(['--html'],
dict(help="create html site", action='store_true')),
2019-08-04 16:40:34 +02:00
(['--php'],
dict(help="create php 7.2 site", action='store_true')),
(['--php72'],
2019-03-03 00:16:20 +01:00
dict(help="create php 7.2 site", action='store_true')),
(['--php73'],
dict(help="create php 7.3 site", action='store_true')),
2018-11-13 21:55:59 +01:00
(['--mysql'],
dict(help="create mysql site", action='store_true')),
(['--wp'],
2019-03-18 00:10:51 +01:00
dict(help="create WordPress single site",
2018-11-13 21:55:59 +01:00
action='store_true')),
(['--wpsubdir'],
2019-03-18 00:10:51 +01:00
dict(help="create WordPress multisite with subdirectory setup",
2018-11-13 21:55:59 +01:00
action='store_true')),
(['--wpsubdomain'],
2019-03-18 00:10:51 +01:00
dict(help="create WordPress multisite with subdomain setup",
2018-11-13 21:55:59 +01:00
action='store_true')),
(['--wpfc'],
dict(help="create WordPress single/multi site with "
"Nginx fastcgi_cache",
2018-11-13 21:55:59 +01:00
action='store_true')),
(['--wpsc'],
2019-03-18 00:10:51 +01:00
dict(help="create WordPress single/multi site with wpsc cache",
2018-11-13 21:55:59 +01:00
action='store_true')),
(['--wprocket'],
dict(help="create WordPress single/multi site with WP-Rocket",
action='store_true')),
2019-08-15 19:59:23 +02:00
(['--wpce'],
dict(help="create WordPress single/multi site with Cache-Enabler",
action='store_true')),
2018-11-13 21:55:59 +01:00
(['--wpredis'],
2019-03-20 02:29:40 +01:00
dict(help="create WordPress single/multi site "
"with redis cache",
2018-11-13 21:55:59 +01:00
action='store_true')),
2019-07-15 14:00:15 +02:00
(['-le', '--letsencrypt'],
2019-03-16 01:12:32 +01:00
dict(help="configure letsencrypt ssl for the site",
action='store' or 'store_const',
2019-03-16 10:30:52 +01:00
choices=('on', 'subdomain', 'wildcard'),
2019-03-16 01:12:32 +01:00
const='on', nargs='?')),
2019-09-25 22:41:13 +02:00
(['--force'],
dict(help="force Let's Encrypt certificate issuance",
action='store_true')),
2019-07-13 19:56:01 +02:00
(['--dns'],
dict(help="choose dns provider api for letsencrypt",
action='store' or 'store_const',
const='dns_cf', nargs='?')),
2019-09-24 02:36:46 +02:00
(['--dnsalias'],
dict(help="set domain used for acme dns alias validation",
action='store', nargs='?')),
2019-04-07 22:10:08 +02:00
(['--hsts'],
2019-04-08 01:38:14 +02:00
dict(help="enable HSTS for site secured with letsencrypt",
action='store_true')),
2019-09-27 01:19:45 +02:00
(['--ngxblocker'],
dict(help="enable HSTS for site secured with letsencrypt",
action='store_true')),
2018-11-13 21:55:59 +01:00
(['--user'],
2019-03-18 00:10:51 +01:00
dict(help="provide user for WordPress site")),
2018-11-13 21:55:59 +01:00
(['--email'],
2019-03-18 00:10:51 +01:00
dict(help="provide email address for WordPress site")),
2018-11-13 21:55:59 +01:00
(['--pass'],
2019-03-18 00:10:51 +01:00
dict(help="provide password for WordPress user",
2018-11-13 21:55:59 +01:00
dest='wppass')),
(['--proxy'],
dict(help="create proxy for site", nargs='+')),
2019-03-20 02:29:40 +01:00
(['--vhostonly'], dict(help="only create vhost and database "
"without installing WordPress",
action='store_true')),
]
2018-11-13 21:55:59 +01:00
@expose(hide=True)
def default(self):
2019-07-16 17:15:17 +02:00
pargs = self.app.pargs
2019-08-04 16:40:34 +02:00
if pargs.php72:
pargs.php = True
2018-11-13 21:55:59 +01:00
# self.app.render((data), 'default.mustache')
# Check domain name validation
data = dict()
host, port = None, None
try:
stype, cache = detSitePar(vars(pargs))
2018-11-13 21:55:59 +01:00
except RuntimeError as e:
Log.debug(self, str(e))
Log.error(self, "Please provide valid options to creating site")
if stype is None and pargs.proxy:
2018-11-13 21:55:59 +01:00
stype, cache = 'proxy', ''
proxyinfo = pargs.proxy[0].strip()
2018-11-13 21:55:59 +01:00
if not proxyinfo:
Log.error(self, "Please provide proxy server host information")
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:
2018-11-13 21:55:59 +01:00
stype, cache = 'html', 'basic'
elif stype and pargs.proxy:
2018-11-13 21:55:59 +01:00
Log.error(self, "proxy should not be used with other site types")
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
2018-11-13 21:55:59 +01:00
# preprocessing before finalize site name
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
Log.debug(self, str(e))
Log.error(self, "Unable to input site name, Please try again!")
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
if not wo_domain.strip():
2019-09-02 18:56:34 +02:00
Log.error(self, "Invalid domain name, "
2018-11-13 21:55:59 +01:00
"Provide valid domain name")
wo_site_webroot = WOVariables.wo_webroot + wo_domain
if check_domain_exists(self, wo_domain):
Log.error(self, "site {0} already exists".format(wo_domain))
elif os.path.isfile('/etc/nginx/sites-available/{0}'
.format(wo_domain)):
Log.error(self, "Nginx configuration /etc/nginx/sites-available/"
"{0} already exists".format(wo_domain))
if stype == 'proxy':
2019-05-01 13:46:39 +02:00
data = dict(site_name=wo_domain, www_domain=wo_www_domain,
static=True, basic=False, php73=False, wp=False,
2019-08-15 19:59:23 +02:00
wpfc=False, wpsc=False, wprocket=False, wpce=False,
multisite=False,
2019-05-01 13:46:39 +02:00
wpsubdir=False, webroot=wo_site_webroot)
2018-11-13 21:55:59 +01:00
data['proxy'] = True
data['host'] = host
data['port'] = port
2019-05-01 13:46:39 +02:00
data['basic'] = True
2018-11-13 21:55:59 +01:00
if pargs.php73:
2018-11-13 21:55:59 +01:00
data = dict(site_name=wo_domain, www_domain=wo_www_domain,
static=False, basic=False, php73=True, wp=False,
2019-08-15 19:59:23 +02:00
wpfc=False, wpsc=False, wprocket=False, wpce=False,
multisite=False,
2018-11-13 21:55:59 +01:00
wpsubdir=False, webroot=wo_site_webroot)
data['basic'] = True
if stype in ['html', 'php']:
2018-11-13 21:55:59 +01:00
data = dict(site_name=wo_domain, www_domain=wo_www_domain,
static=True, basic=False, php73=False, wp=False,
2019-08-15 19:59:23 +02:00
wpfc=False, wpsc=False, wprocket=False, wpce=False,
multisite=False,
2018-11-13 21:55:59 +01:00
wpsubdir=False, webroot=wo_site_webroot)
if stype == 'php':
data['static'] = False
data['basic'] = True
elif stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
data = dict(site_name=wo_domain, www_domain=wo_www_domain,
static=False, basic=True, wp=False, wpfc=False,
2019-08-15 19:59:23 +02:00
wpsc=False, wpredis=False, wprocket=False, wpce=False,
multisite=False,
2018-11-13 21:55:59 +01:00
wpsubdir=False, webroot=wo_site_webroot,
wo_db_name='', wo_db_user='', wo_db_pass='',
wo_db_host='')
if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
data['wp'] = True
data['basic'] = False
data[cache] = True
data['wp-user'] = pargs.user
data['wp-email'] = pargs.email
data['wp-pass'] = pargs.wppass
2018-11-13 21:55:59 +01:00
if stype in ['wpsubdir', 'wpsubdomain']:
data['multisite'] = True
if stype == 'wpsubdir':
data['wpsubdir'] = True
else:
pass
if data and pargs.php73:
2019-03-18 00:10:51 +01:00
data['php73'] = True
2019-03-03 00:08:51 +01:00
elif data:
data['php73'] = False
2018-11-13 21:55:59 +01:00
if ((not pargs.wpfc) and
(not pargs.wpsc) and
(not pargs.wprocket) and
(not pargs.wpce) and
(not pargs.wpredis)):
2018-11-13 21:55:59 +01:00
data['basic'] = True
2019-03-18 00:30:54 +01:00
if (cache == 'wpredis'):
2019-03-20 02:29:40 +01:00
cache = 'wpredis'
data['wpredis'] = True
data['basic'] = False
pargs.wpredis = True
2018-11-13 21:55:59 +01:00
# Check rerequired packages are installed or not
wo_auth = site_package_check(self, stype)
try:
pre_run_checks(self)
except SiteError as e:
Log.debug(self, str(e))
Log.error(self, "NGINX configuration check failed.")
try:
try:
# setup NGINX configuration, and webroot
setupdomain(self, data)
# Fix Nginx Hashbucket size error
hashbucket(self)
except SiteError as e:
# call cleanup actions on failure
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
2018-11-13 21:55:59 +01:00
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain,
webroot=data['webroot'])
Log.debug(self, str(e))
Log.error(self, "Check the log for details: "
2019-04-07 22:10:08 +02:00
"`tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
if 'proxy' in data.keys() and data['proxy']:
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...")
2018-11-13 21:55:59 +01:00
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: "
2019-04-07 22:10:08 +02:00
"`tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
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))
return
if data['php73']:
php_version = "7.3"
2019-09-01 23:29:23 +02:00
php73 = 1
2018-11-13 21:55:59 +01:00
else:
2018-11-30 18:09:31 +01:00
php_version = "7.2"
2019-09-01 23:29:23 +02:00
php73 = 0
2018-11-13 21:55:59 +01:00
addNewSite(self, wo_domain, stype, cache, wo_site_webroot,
2019-03-13 11:55:18 +01:00
php_version=php_version)
2018-11-13 21:55:59 +01:00
# Setup database for MySQL site
if 'wo_db_name' in data.keys() and not data['wp']:
try:
data = setupdatabase(self, data)
# Add database information for site into database
updateSiteInfo(self, wo_domain, db_name=data['wo_db_name'],
db_user=data['wo_db_user'],
db_password=data['wo_db_pass'],
db_host=data['wo_db_host'])
except SiteError as e:
# call cleanup actions on failure
Log.debug(self, str(e))
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
2018-11-13 21:55:59 +01:00
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain,
webroot=data['webroot'],
dbname=data['wo_db_name'],
dbuser=data['wo_db_user'],
dbhost=data['wo_db_host'])
deleteSiteInfo(self, wo_domain)
Log.error(self, "Check the log for details: "
2019-04-07 22:10:08 +02:00
"`tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
try:
wodbconfig = open("{0}/wo-config.php"
.format(wo_site_webroot),
encoding='utf-8', mode='w')
wodbconfig.write("<?php \ndefine('DB_NAME', '{0}');"
"\ndefine('DB_USER', '{1}'); "
"\ndefine('DB_PASSWORD', '{2}');"
"\ndefine('DB_HOST', '{3}');\n?>"
.format(data['wo_db_name'],
data['wo_db_user'],
data['wo_db_pass'],
data['wo_db_host']))
wodbconfig.close()
stype = 'mysql'
except IOError as e:
Log.debug(self, str(e))
Log.debug(self, "Error occured while generating "
"wo-config.php")
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
2018-11-13 21:55:59 +01:00
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain,
webroot=data['webroot'],
dbname=data['wo_db_name'],
dbuser=data['wo_db_user'],
dbhost=data['wo_db_host'])
deleteSiteInfo(self, wo_domain)
Log.error(self, "Check the log for details: "
2019-04-07 22:10:08 +02:00
"`tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
# Setup WordPress if Wordpress site
if data['wp']:
2019-09-03 19:02:00 +02:00
if pargs.vhostonly:
vhostonly = True
else:
2019-09-03 19:02:00 +02:00
vhostonly = False
try:
wo_wp_creds = setupwordpress(self, data, vhostonly)
# Add database information for site into database
updateSiteInfo(self, wo_domain,
db_name=data['wo_db_name'],
db_user=data['wo_db_user'],
db_password=data['wo_db_pass'],
db_host=data['wo_db_host'])
except SiteError as e:
# call cleanup actions on failure
Log.debug(self, str(e))
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain,
webroot=data['webroot'],
dbname=data['wo_db_name'],
dbuser=data['wo_db_user'],
dbhost=data['wo_mysql_grant_host'])
deleteSiteInfo(self, wo_domain)
Log.error(self, "Check the log for details: "
"`tail /var/log/wo/wordops.log` "
"and please try again")
2019-05-02 12:28:44 +02:00
2018-11-13 21:55:59 +01:00
# Service Nginx Reload call cleanup if failed to reload nginx
if not WOService.reload_service(self, 'nginx'):
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
2018-11-13 21:55:59 +01:00
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain,
webroot=data['webroot'])
if 'wo_db_name' in data.keys():
doCleanupAction(self, domain=wo_domain,
dbname=data['wo_db_name'],
dbuser=data['wo_db_user'],
dbhost=data['wo_mysql_grant_host'])
deleteSiteInfo(self, wo_domain)
Log.info(self, Log.FAIL + "service nginx reload failed."
" check issues with `nginx -t` command.")
Log.error(self, "Check the log for details: "
2019-04-07 22:10:08 +02:00
"`tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
WOGit.add(self, ["/etc/nginx"],
msg="{0} created with {1} {2}"
.format(wo_www_domain, stype, cache))
# Setup Permissions for webroot
try:
setwebrootpermissions(self, data['webroot'])
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL +
"There was a serious error encountered...")
2018-11-13 21:55:59 +01:00
Log.info(self, Log.FAIL + "Cleaning up afterwards...")
doCleanupAction(self, domain=wo_domain,
webroot=data['webroot'])
if 'wo_db_name' in data.keys():
print("Inside db cleanup")
doCleanupAction(self, domain=wo_domain,
dbname=data['wo_db_name'],
dbuser=data['wo_db_user'],
dbhost=data['wo_mysql_grant_host'])
deleteSiteInfo(self, wo_domain)
Log.error(self, "Check the log for details: "
2019-04-07 22:10:08 +02:00
"`tail /var/log/wo/wordops.log` and "
"please try again")
2018-11-13 21:55:59 +01:00
if wo_auth and len(wo_auth):
for msg in wo_auth:
Log.info(self, Log.ENDC + msg, log=False)
if data['wp'] and (not pargs.vhostonly):
2018-11-13 21:55:59 +01:00
Log.info(self, Log.ENDC + "WordPress admin user :"
" {0}".format(wo_wp_creds['wp_user']), log=False)
2019-08-27 22:49:54 +02:00
Log.info(self, Log.ENDC + "WordPress admin password : {0}"
2018-11-13 21:55:59 +01:00
.format(wo_wp_creds['wp_pass']), log=False)
display_cache_settings(self, data)
Log.info(self, "Successfully created site"
" http://{0}".format(wo_domain))
except SiteError as e:
Log.error(self, "Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
if pargs.letsencrypt:
2019-09-22 19:30:39 +02:00
acme_domains = []
(wo_domain_type,
wo_root_domain) = WODomain.getdomainlevel(self,
wo_domain)
2019-04-23 19:03:42 +02:00
data['letsencrypt'] = True
letsencrypt = True
2018-11-13 21:55:59 +01:00
if data['letsencrypt'] is True:
2019-09-22 19:30:39 +02:00
Log.debug(self, "Going to issue Let's Encrypt certificate")
2019-09-24 02:36:46 +02:00
acmedata = dict(acme_domains, dns=False, acme_dns='dns_cf',
dnsalias=False, acme_alias='')
2019-08-27 23:25:39 +02:00
if pargs.dns:
2019-09-22 19:30:39 +02:00
Log.debug(self, "DNS validation enabled")
acmedata['dns'] = True
if not pargs.dns == 'dns_cf':
Log.debug(self, "DNS API : {0}".format(pargs.dns))
acmedata['acme_dns'] = pargs.dns
2019-09-24 02:36:46 +02:00
if pargs.dnsalias:
Log.debug(self, "DNS Alias enabled")
acmedata['dnsalias'] = True
acmedata['acme_alias'] = pargs.dnsalias
2019-09-22 19:30:39 +02:00
# detect subdomain and set subdomain variable
if pargs.letsencrypt == "subdomain":
2019-09-22 19:30:39 +02:00
Log.warn(
self, 'Flag --letsencrypt=subdomain is '
'deprecated and not required anymore.')
acme_subdomain = True
acme_wildcard = False
elif pargs.letsencrypt == "wildcard":
2019-09-22 19:30:39 +02:00
acme_wildcard = True
acme_subdomain = False
acmedata['dns'] = True
else:
if ((wo_domain_type == 'subdomain')):
Log.debug(self, "Domain type = {0}"
.format(wo_domain_type))
acme_subdomain = True
else:
acme_subdomain = False
acme_wildcard = False
if acme_subdomain is True:
2019-09-23 01:40:26 +02:00
Log.info(self, "Certificate type : subdomain")
2019-09-22 19:30:39 +02:00
acme_domains = acme_domains + ['{0}'.format(wo_domain)]
elif acme_wildcard is True:
2019-09-23 01:40:26 +02:00
Log.info(self, "Certificate type : wildcard")
2019-09-22 19:30:39 +02:00
acme_domains = acme_domains + ['{0}'.format(wo_domain),
'*.{0}'.format(wo_domain)]
2019-07-15 14:00:15 +02:00
else:
2019-09-23 01:40:26 +02:00
Log.info(self, "Certificate type : domain")
2019-09-22 19:30:39 +02:00
acme_domains = acme_domains + ['{0}'.format(wo_domain),
'www.{0}'.format(wo_domain)]
if acme_subdomain is True:
2019-08-30 07:58:00 +02:00
# check if a wildcard cert for the root domain exist
2019-08-30 08:17:18 +02:00
Log.debug(self, "checkWildcardExist on *.{0}"
.format(wo_root_domain))
2019-09-04 03:07:24 +02:00
iswildcard = SSL.checkwildcardexist(self, wo_root_domain)
Log.debug(self, "iswildcard = {0}".format(iswildcard))
if iswildcard:
2019-08-30 08:55:23 +02:00
Log.info(self, "Using existing Wildcard SSL "
"certificate from {0} to secure {1}"
.format(wo_root_domain, wo_domain))
2019-08-30 08:17:18 +02:00
Log.debug(self, "symlink wildcard "
"cert between {0} & {1}"
.format(wo_domain, wo_root_domain))
2019-08-30 07:58:00 +02:00
# copy the cert from the root domain
copyWildcardCert(self, wo_domain, wo_root_domain)
else:
# check DNS records before issuing cert
if not acmedata['dns'] is True:
2019-09-25 22:41:13 +02:00
if not pargs.force:
if not WOAcme.check_dns(self, acme_domains):
Log.error(self,
"Aborting SSL "
"certificate issuance")
2019-08-30 08:17:18 +02:00
Log.debug(self, "Setup Cert with acme.sh for {0}"
.format(wo_domain))
2019-09-22 19:30:39 +02:00
if WOAcme.setupletsencrypt(
self, acme_domains, acmedata):
WOAcme.deploycert(self, wo_domain)
else:
if not acmedata['dns'] is True:
2019-09-25 22:41:13 +02:00
if not pargs.force:
if not WOAcme.check_dns(self, acme_domains):
Log.error(self,
"Aborting SSL certificate issuance")
2019-09-22 19:30:39 +02:00
if WOAcme.setupletsencrypt(
self, acme_domains, acmedata):
2019-09-22 19:30:39 +02:00
WOAcme.deploycert(self, wo_domain)
httpsRedirect(self, wo_domain, True, acme_wildcard)
2019-04-07 22:10:08 +02:00
if pargs.hsts:
2019-09-06 22:21:16 +02:00
SSL.setuphsts(self, wo_domain)
2019-04-07 22:10:08 +02:00
2019-09-04 03:07:24 +02:00
SSL.siteurlhttps(self, wo_domain)
2019-03-08 00:31:23 +01:00
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
2019-04-07 22:10:08 +02:00
Log.info(self, "Congratulations! Successfully Configured "
"SSl for Site "
2019-03-08 00:31:23 +01:00
" https://{0}".format(wo_domain))
2018-11-13 21:55:59 +01:00
# Add nginx conf folder into GIT
WOGit.add(self, ["{0}/conf/nginx".format(wo_site_webroot)],
2018-11-13 21:55:59 +01:00
msg="Adding letsencrypts config of site: {0}"
.format(wo_domain))
updateSiteInfo(self, wo_domain, ssl=letsencrypt)
2018-11-13 21:55:59 +01:00
elif data['letsencrypt'] is False:
Log.info(self, "Not using Let\'s encrypt for Site "
" http://{0}".format(wo_domain))
class WOSiteUpdateController(CementBaseController):
class Meta:
label = 'update'
stacked_on = 'site'
stacked_type = 'nested'
description = ('This command updates websites configuration to '
'another as per the options are provided')
arguments = [
(['site_name'],
dict(help='domain name for the site to be updated',
nargs='?')),
(['--password'],
dict(help="update to password for wordpress site user",
action='store_true')),
(['--html'],
dict(help="update to html site", action='store_true')),
2019-08-04 16:40:34 +02:00
(['--php72'],
2018-11-13 21:55:59 +01:00
dict(help="update to php site", action='store_true')),
2019-08-04 16:40:34 +02:00
(['--php'],
dict(help="update to php site", action='store_true')),
(['--php73'],
2019-03-03 00:16:20 +01:00
dict(help="update to php73 site",
2018-11-13 21:55:59 +01:00
action='store' or 'store_const',
choices=('on', 'off'), const='on', nargs='?')),
(['--mysql'],
dict(help="update to mysql site", action='store_true')),
(['--wp'],
dict(help="update to wordpress single site",
action='store_true')),
(['--wpsubdir'],
dict(help="update to wpsubdir site", action='store_true')),
(['--wpsubdomain'],
dict(help="update to wpsubdomain site", action='store_true')),
(['--wpfc'],
dict(help="update to wpfc cache", action='store_true')),
(['--wpsc'],
dict(help="update to wpsc cache", action='store_true')),
(['--wprocket'],
dict(help="update to WP-Rocket cache", action='store_true')),
2019-08-15 19:59:23 +02:00
(['--wpce'],
2019-08-30 20:33:33 +02:00
dict(help="update to Cache-Enabler cache",
action='store_true')),
2018-11-13 21:55:59 +01:00
(['--wpredis'],
dict(help="update to redis cache", action='store_true')),
2019-07-15 14:00:15 +02:00
(['-le', '--letsencrypt'],
2018-11-13 21:55:59 +01:00
dict(help="configure letsencrypt ssl for the site",
action='store' or 'store_const',
2019-07-15 15:39:45 +02:00
choices=('on', 'off', 'renew', 'subdomain',
'wildcard', 'clean', 'purge'),
const='on', nargs='?')),
2019-09-25 22:41:13 +02:00
(['--force'],
2019-09-25 23:12:56 +02:00
dict(help="force LetsEncrypt certificate issuance/renewal",
2019-09-25 22:41:13 +02:00
action='store_true')),
2019-07-13 20:42:11 +02:00
(['--dns'],
dict(help="choose dns provider api for letsencrypt",
action='store' or 'store_const',
2019-07-14 22:53:48 +02:00
const='dns_cf', nargs='?')),
2019-09-24 02:51:36 +02:00
(['--dnsalias'],
dict(help="set domain used for acme dns alias validation",
action='store', nargs='?')),
2019-04-15 23:54:05 +02:00
(['--hsts'],
2019-04-15 15:31:19 +02:00
dict(help="configure hsts for the site",
action='store' or 'store_const',
choices=('on', 'off'),
const='on', nargs='?')),
2019-09-27 01:19:45 +02:00
(['--ngxblocker'],
dict(help="enable HSTS for site secured with letsencrypt",
action='store' or 'store_const',
const='on', nargs='?')),
2018-11-13 21:55:59 +01:00
(['--proxy'],
dict(help="update to proxy site", nargs='+')),
(['--all'],
dict(help="update all sites", action='store_true')),
]
2018-11-13 21:55:59 +01:00
@expose(help="Update site type or cache")
def default(self):
pargs = self.app.pargs
2019-08-04 16:40:34 +02:00
if pargs.php72:
2019-08-07 03:05:32 +02:00
pargs.php = True
2019-08-04 16:40:34 +02:00
2018-11-13 21:55:59 +01:00
if pargs.all:
if pargs.site_name:
Log.error(self, "`--all` option cannot be used with site name"
" provided")
if pargs.html:
Log.error(self, "No site can be updated to html")
if not (pargs.php or pargs.php73 or
2018-11-13 21:55:59 +01:00
pargs.mysql or pargs.wp or pargs.wpsubdir or
pargs.wpsubdomain or pargs.wpfc or pargs.wpsc or
2019-08-15 19:59:23 +02:00
pargs.wprocket or pargs.wpce or
pargs.wpredis or pargs.letsencrypt or pargs.hsts or
2019-07-19 22:29:18 +02:00
pargs.dns or pargs.force):
2018-11-13 21:55:59 +01:00
Log.error(self, "Please provide options to update sites.")
if pargs.all:
if pargs.site_name:
Log.error(self, "`--all` option cannot be used with site name"
" provided")
sites = getAllsites(self)
if not sites:
pass
else:
for site in sites:
pargs.site_name = site.sitename
Log.info(self, Log.ENDC + Log.BOLD + "Updating site {0},"
" please wait..."
.format(pargs.site_name))
self.doupdatesite(pargs)
print("\n")
else:
self.doupdatesite(pargs)
def doupdatesite(self, pargs):
pargs = self.app.pargs
2018-11-13 21:55:59 +01:00
letsencrypt = False
php73 = None
2018-11-13 21:55:59 +01:00
data = dict()
try:
stype, cache = detSitePar(vars(pargs))
except RuntimeError as e:
Log.debug(self, str(e))
Log.error(self, "Please provide valid options combination for"
" site update")
if stype is None and pargs.proxy:
stype, cache = 'proxy', ''
proxyinfo = pargs.proxy[0].strip()
if not proxyinfo:
Log.error(self, "Please provide proxy server host information")
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 or pargs.letsencrypt):
stype, cache = 'html', 'basic'
elif stype and pargs.proxy:
Log.error(self, "--proxy can not be used with other site types")
if not pargs.site_name:
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ').strip())
2019-08-27 23:25:39 +02:00
except IOError:
2018-11-13 21:55:59 +01:00
Log.error(self, 'Unable to input site name, Please try again!')
pargs.site_name = pargs.site_name.strip()
2019-09-22 19:30:39 +02:00
(wo_domain,
wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
wo_site_webroot = WOVariables.wo_webroot + wo_domain
check_site = getSiteInfo(self, wo_domain)
if check_site is None:
Log.error(self, " Site {0} does not exist.".format(wo_domain))
else:
oldsitetype = check_site.site_type
oldcachetype = check_site.cache_type
check_ssl = check_site.is_ssl
check_php_version = check_site.php_version
if check_php_version == "7.3":
old_php73 = True
2018-11-13 21:55:59 +01:00
else:
old_php73 = False
2018-11-13 21:55:59 +01:00
if (pargs.password and not (pargs.html or
pargs.php or pargs.php73 or pargs.mysql or
pargs.wp or pargs.wpfc or pargs.wpsc or
2019-08-15 19:59:23 +02:00
pargs.wprocket or pargs.wpce or
2019-04-15 15:09:10 +02:00
pargs.wpsubdir or pargs.wpsubdomain or
2019-09-27 01:19:45 +02:00
pargs.hsts or pargs.ngxblocker)):
2018-11-13 21:55:59 +01:00
try:
updatewpuserpassword(self, wo_domain, wo_site_webroot)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, "\nPassword Unchanged.")
return 0
2019-03-13 11:59:13 +01:00
if ((stype == 'php' and
2019-03-13 11:59:28 +01:00
oldsitetype not in ['html', 'proxy', 'php73']) or
2018-11-13 21:55:59 +01:00
(stype == 'mysql' and oldsitetype not in ['html', 'php',
'proxy', 'php73']) or
2018-11-13 21:55:59 +01:00
(stype == 'wp' and oldsitetype not in ['html', 'php', 'mysql',
'proxy', 'wp', 'php73']) or
2018-11-13 21:55:59 +01:00
(stype == 'wpsubdir' and oldsitetype in ['wpsubdomain']) or
(stype == 'wpsubdomain' and oldsitetype in ['wpsubdir']) or
2019-04-23 19:03:42 +02:00
(stype == oldsitetype and cache == oldcachetype) and not
2019-07-19 22:29:18 +02:00
pargs.php73):
2018-11-13 21:55:59 +01:00
Log.info(self, Log.FAIL + "can not update {0} {1} to {2} {3}".
format(oldsitetype, oldcachetype, stype, cache))
return 1
if stype == 'proxy':
data['site_name'] = wo_domain
data['www_domain'] = wo_www_domain
data['proxy'] = True
data['host'] = host
data['port'] = port
data['webroot'] = wo_site_webroot
data['currsitetype'] = oldsitetype
data['currcachetype'] = oldcachetype
if stype == 'php':
2019-09-04 04:26:09 +02:00
data = dict(
site_name=wo_domain, www_domain=wo_www_domain,
static=False, basic=True, wp=False, wpfc=False,
wpsc=False, wpredis=False, wprocket=False, wpce=False,
multisite=False, wpsubdir=False, webroot=wo_site_webroot,
currsitetype=oldsitetype, currcachetype=oldcachetype)
2018-11-13 21:55:59 +01:00
elif stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
2019-09-04 04:26:09 +02:00
data = dict(
site_name=wo_domain, www_domain=wo_www_domain,
static=False, basic=True, wp=False, wpfc=False,
wpsc=False, wpredis=False, wprocket=False, wpce=False,
multisite=False, wpsubdir=False, webroot=wo_site_webroot,
wo_db_name='', wo_db_user='', wo_db_pass='',
wo_db_host='',
currsitetype=oldsitetype, currcachetype=oldcachetype)
2018-11-13 21:55:59 +01:00
if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
data['wp'] = True
data['basic'] = False
data[cache] = True
if stype in ['wpsubdir', 'wpsubdomain']:
data['multisite'] = True
if stype == 'wpsubdir':
data['wpsubdir'] = True
2019-03-13 05:02:53 +01:00
if pargs.php73:
2018-11-13 21:55:59 +01:00
if not data:
2019-09-04 04:26:09 +02:00
data = dict(
site_name=wo_domain, www_domain=wo_www_domain,
currsitetype=oldsitetype,
currcachetype=oldcachetype,
webroot=wo_site_webroot)
2018-11-13 21:55:59 +01:00
stype = oldsitetype
cache = oldcachetype
if oldsitetype == 'html' or oldsitetype == 'proxy':
data['static'] = True
data['wp'] = False
data['multisite'] = False
data['wpsubdir'] = False
elif oldsitetype == 'php' or oldsitetype == 'mysql':
data['static'] = False
data['wp'] = False
data['multisite'] = False
data['wpsubdir'] = False
elif oldsitetype == 'wp':
data['static'] = False
data['wp'] = True
data['multisite'] = False
data['wpsubdir'] = False
elif oldsitetype == 'wpsubdir':
data['static'] = False
data['wp'] = True
data['multisite'] = True
data['wpsubdir'] = True
elif oldsitetype == 'wpsubdomain':
data['static'] = False
data['wp'] = True
data['multisite'] = True
data['wpsubdir'] = False
if oldcachetype == 'basic':
data['basic'] = True
data['wpfc'] = False
data['wpsc'] = False
data['wpredis'] = False
data['wprocket'] = False
2019-08-15 19:59:23 +02:00
data['wpce'] = False
2018-11-13 21:55:59 +01:00
elif oldcachetype == 'wpfc':
data['basic'] = False
data['wpfc'] = True
data['wpsc'] = False
data['wpredis'] = False
data['wprocket'] = False
2019-08-15 19:59:23 +02:00
data['wpce'] = False
2018-11-13 21:55:59 +01:00
elif oldcachetype == 'wpsc':
data['basic'] = False
data['wpfc'] = False
data['wpsc'] = True
data['wpredis'] = False
data['wprocket'] = False
2019-08-15 19:59:23 +02:00
data['wpce'] = False
2018-11-13 21:55:59 +01:00
elif oldcachetype == 'wpredis':
data['basic'] = False
data['wpfc'] = False
data['wpsc'] = False
data['wpredis'] = True
data['wprocket'] = False
2019-08-15 19:59:23 +02:00
data['wpce'] = False
elif oldcachetype == 'wprocket':
data['basic'] = False
data['wpfc'] = False
data['wpsc'] = False
data['wpredis'] = False
data['wprocket'] = True
2019-08-15 19:59:23 +02:00
data['wpce'] = False
elif oldcachetype == 'wpce':
data['basic'] = False
data['wpfc'] = False
data['wpsc'] = False
data['wpredis'] = False
data['wprocket'] = False
data['wpce'] = True
2018-11-13 21:55:59 +01:00
if pargs.php73 == 'on':
data['php73'] = True
php73 = True
check_php_version = '7.3'
elif pargs.php73 == 'off':
data['php73'] = False
php73 = False
2019-03-03 00:16:20 +01:00
check_php_version = '7.2'
if pargs.php73:
if php73 is old_php73:
if php73 is False:
2019-03-03 00:16:20 +01:00
Log.info(self, "PHP 7.3 is already disabled for given "
2018-11-13 21:55:59 +01:00
"site")
elif php73 is True:
2019-03-03 00:16:20 +01:00
Log.info(self, "PHP 7.3 is already enabled for given "
2018-11-13 21:55:59 +01:00
"site")
pargs.php73 = False
2018-11-13 21:55:59 +01:00
2019-09-01 23:29:23 +02:00
if pargs.letsencrypt:
2019-09-22 19:30:39 +02:00
acme_domains = []
2019-09-24 02:36:46 +02:00
acmedata = dict(acme_domains, dns=False, acme_dns='dns_cf',
dnsalias=False, acme_alias='')
2019-09-22 19:30:39 +02:00
(wo_domain_type,
wo_root_domain) = WODomain.getdomainlevel(self, wo_domain)
2019-09-01 23:29:23 +02:00
if pargs.letsencrypt == 'on':
data['letsencrypt'] = True
letsencrypt = True
2019-09-22 19:30:39 +02:00
if (wo_domain_type == 'subdomain'):
acme_subdomain = True
2019-09-01 23:29:23 +02:00
else:
2019-09-22 19:30:39 +02:00
acme_subdomain = False
acme_wildcard = False
2019-09-01 23:29:23 +02:00
elif pargs.letsencrypt == 'subdomain':
data['letsencrypt'] = True
letsencrypt = True
2019-09-22 19:30:39 +02:00
acme_subdomain = True
acme_wildcard = False
2019-09-01 23:29:23 +02:00
elif pargs.letsencrypt == 'wildcard':
data['letsencrypt'] = True
letsencrypt = True
2019-09-22 19:30:39 +02:00
acme_wildcard = True
acme_subdomain = False
acmedata['dns'] = True
2019-09-01 23:29:23 +02:00
elif pargs.letsencrypt == 'off':
data['letsencrypt'] = False
letsencrypt = False
2019-09-22 19:30:39 +02:00
acme_subdomain = False
acme_wildcard = False
2019-09-01 23:29:23 +02:00
elif pargs.letsencrypt == 'clean':
data['letsencrypt'] = False
letsencrypt = False
2019-09-22 19:30:39 +02:00
acme_subdomain = False
acme_wildcard = False
2019-09-01 23:29:23 +02:00
elif pargs.letsencrypt == 'purge':
data['letsencrypt'] = False
letsencrypt = False
2019-09-22 19:30:39 +02:00
acme_subdomain = False
acme_wildcard = False
2019-09-01 23:29:23 +02:00
2019-09-22 19:30:39 +02:00
if not (acme_subdomain is True):
2019-09-01 23:29:23 +02:00
if letsencrypt is check_ssl:
if letsencrypt is False:
Log.error(self, "SSl is not configured for given "
"site")
elif letsencrypt is True:
Log.error(self, "SSl is already configured for given "
"site")
pargs.letsencrypt = False
# --letsencrypt=renew code goes here
2018-11-13 21:55:59 +01:00
if pargs.letsencrypt == "renew" and not pargs.all:
2019-09-04 03:07:24 +02:00
expiry_days = SSL.getexpirationdays(self, wo_domain)
2019-07-13 04:16:36 +02:00
min_expiry_days = 45
2018-11-13 21:55:59 +01:00
if check_ssl:
2019-07-13 04:16:36 +02:00
if (expiry_days <= min_expiry_days):
renewLetsEncrypt(self, wo_domain)
elif pargs.force:
renewLetsEncrypt(self, wo_domain)
2018-11-13 21:55:59 +01:00
else:
Log.error(
2019-03-25 17:42:11 +01:00
self, "You have more than 30 days with the current "
"certificate - refusing to run.")
2018-11-13 21:55:59 +01:00
else:
Log.error(
2019-03-25 17:42:11 +01:00
self, "Cannot renew - HTTPS is not configured for "
"the given site. Install LE first...")
2018-11-13 21:55:59 +01:00
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
2018-11-13 21:55:59 +01:00
Log.info(self, "SUCCESS: Certificate was successfully renewed For"
" https://{0}".format(wo_domain))
2019-09-04 03:07:24 +02:00
if (SSL.getexpirationdays(self, wo_domain) > 0):
Log.info(self, "Your cert will expire within " +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdays(self, wo_domain)) +
2019-04-07 22:10:08 +02:00
" days.")
Log.info(self, "Expiration date: " +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdate(self, wo_domain)))
2018-11-13 21:55:59 +01:00
else:
Log.warn(
2019-03-25 17:42:11 +01:00
self, "The certificate seems to be already expired. "
"Please renew it as soon as possible...")
2018-11-13 21:55:59 +01:00
return 0
if pargs.all and pargs.letsencrypt == "renew":
if check_ssl:
expiry_days = SSL.getExpirationDays(self, wo_domain, True)
2018-11-13 21:55:59 +01:00
if expiry_days < 0:
return 0
2019-07-13 04:16:36 +02:00
min_expiry_days = 45
if (expiry_days <= min_expiry_days):
2019-08-30 00:56:33 +02:00
renewLetsEncrypt(self, wo_domain)
2019-07-13 04:16:36 +02:00
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
Log.info(self, "SUCCESS: Certificate was successfully "
"renewed For https://{0}".format(wo_domain))
elif pargs.force:
2019-08-30 00:56:33 +02:00
renewLetsEncrypt(self, wo_domain)
2019-03-25 17:42:11 +01:00
Log.info(self, "Certificate was successfully renewed")
2018-11-13 21:55:59 +01:00
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
2019-03-25 17:42:11 +01:00
Log.info(self, "SUCCESS: Certificate was successfully "
"renewed For https://{0}".format(wo_domain))
2018-11-13 21:55:59 +01:00
else:
Log.info(
2019-07-13 04:16:36 +02:00
self, "You have more than 45 days with the current "
2019-03-25 17:42:11 +01:00
"certificate - refusing to run.\n")
2019-09-04 03:07:24 +02:00
if (SSL.getexpirationdays(self, wo_domain) > 0):
Log.info(self, "Your cert will expire within " +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdays(self, wo_domain)) +
2019-04-07 22:10:08 +02:00
" days.")
Log.info(self, "Expiration date: \n\n" +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdate(self, wo_domain)))
2018-11-13 21:55:59 +01:00
return 0
# else:
2019-04-07 22:10:08 +02:00
# Log.warn(self, "Your cert already EXPIRED !
# .PLEASE renew soon . ")
2018-11-13 21:55:59 +01:00
else:
Log.info(
2019-03-25 17:42:11 +01:00
self, "SSL not configured for "
"site http://{0}".format(wo_domain))
2018-11-13 21:55:59 +01:00
return 0
if pargs.all and pargs.letsencrypt == "off":
if letsencrypt is check_ssl:
if letsencrypt is False:
2018-12-03 23:06:09 +01:00
Log.error(self, "HTTPS is not configured for given "
"site", False)
2018-11-13 21:55:59 +01:00
return 0
if data and (not pargs.php73):
if old_php73 is True:
data['php73'] = True
php73 = True
2018-11-13 21:55:59 +01:00
else:
data['php73'] = False
php73 = False
2019-09-22 19:30:39 +02:00
if pargs.php73 == "on":
data['php73'] = True
php73 = True
else:
data['php73'] = False
php73 = False
2018-11-13 21:55:59 +01:00
if pargs.wpredis and data['currcachetype'] != 'wpredis':
2019-03-18 00:10:51 +01:00
data['wpredis'] = True
data['basic'] = False
cache = 'wpredis'
2019-03-03 00:08:51 +01:00
if pargs.wprocket and data['currcachetype'] != 'wprocket':
data['wprocket'] = True
data['basic'] = False
cache = 'wprocket'
2019-08-15 19:59:23 +02:00
if pargs.wpce and data['currcachetype'] != 'wpce':
data['wpce'] = True
data['basic'] = False
cache = 'wpce'
2019-03-18 00:10:51 +01:00
if (php73 is old_php73) and (stype == oldsitetype and
cache == oldcachetype):
2018-11-13 21:55:59 +01:00
return 1
2019-04-15 15:51:17 +02:00
if pargs.hsts:
2019-04-15 16:00:26 +02:00
if pargs.hsts == "on":
data['hsts'] = True
elif pargs.hsts == "off":
data['hsts'] = False
2019-04-15 15:51:17 +02:00
2019-09-27 01:19:45 +02:00
if pargs.ngxblocker:
if pargs.ngxblocker == 'on':
ngxblocker = True
elif pargs.ngxblocker == 'off':
ngxblocker = False
2018-11-13 21:55:59 +01:00
if not data:
Log.error(self, "Cannot update {0}, Invalid Options"
.format(wo_domain))
wo_auth = site_package_check(self, stype)
data['wo_db_name'] = check_site.db_name
data['wo_db_user'] = check_site.db_user
data['wo_db_pass'] = check_site.db_password
data['wo_db_host'] = check_site.db_host
2019-09-27 01:19:45 +02:00
if not (pargs.letsencrypt or pargs.hsts or pargs.ngxblocker):
2018-11-13 21:55:59 +01:00
try:
pre_run_checks(self)
except SiteError as e:
Log.debug(self, str(e))
Log.error(self, "NGINX configuration check failed.")
try:
sitebackup(self, data)
except Exception as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
2018-11-13 21:55:59 +01:00
return 1
# setup NGINX configuration, and webroot
try:
setupdomain(self, data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details:"
"`tail /var/log/wo/wordops.log` and please try again")
2018-11-13 21:55:59 +01:00
return 1
if 'proxy' in data.keys() and data['proxy']:
2019-03-13 11:59:28 +01:00
updateSiteInfo(self, wo_domain, stype=stype, cache=cache,
ssl=True if check_site.is_ssl else False)
2018-11-13 21:55:59 +01:00
Log.info(self, "Successfully updated site"
" http://{0}".format(wo_domain))
return 0
if pargs.letsencrypt:
2018-11-13 21:55:59 +01:00
if data['letsencrypt'] is True:
2019-09-22 19:30:39 +02:00
# DNS API configuration
2019-08-27 23:25:39 +02:00
if pargs.dns:
2019-09-22 19:30:39 +02:00
Log.debug(self, "DNS validation enabled")
acmedata['dns'] = True
if not pargs.dns == 'dns_cf':
Log.debug(self, "DNS API : {0}".format(pargs.dns))
acmedata['acme_dns'] = pargs.dns
2019-09-24 02:36:46 +02:00
if pargs.dnsalias:
Log.debug(self, "DNS Alias enabled")
acmedata['dnsalias'] = True
acmedata['acme_alias'] = pargs.dnsalias
2019-09-22 19:30:39 +02:00
# Set list of domains to secure
if acme_subdomain is True:
2019-09-23 01:40:26 +02:00
Log.info(self, "Certificate type : subdomain")
2019-09-22 19:30:39 +02:00
acme_domains = acme_domains + ['{0}'.format(wo_domain)]
elif acme_wildcard is True:
2019-09-23 01:40:26 +02:00
Log.info(self, "Certificate type : wildcard")
2019-09-22 19:30:39 +02:00
acme_domains = acme_domains + ['{0}'.format(wo_domain),
'*.{0}'.format(wo_domain)]
2019-08-27 23:25:39 +02:00
else:
2019-09-23 01:40:26 +02:00
Log.info(self, "Certificate type : domain")
2019-09-22 19:30:39 +02:00
acme_domains = acme_domains + ['{0}'.format(wo_domain),
'www.{0}'.format(wo_domain)]
if acme_subdomain:
2019-09-02 02:29:45 +02:00
# check if a wildcard cert for the root domain exist
Log.debug(self, "checkWildcardExist on *.{0}"
2019-09-03 19:02:00 +02:00
.format(wo_root_domain))
2019-09-04 03:07:24 +02:00
iswildcard = SSL.checkwildcardexist(self, wo_root_domain)
Log.debug(self, "iswildcard = {0}".format(iswildcard))
2019-08-27 23:25:39 +02:00
if not os.path.isfile("{0}/conf/nginx/ssl.conf.disabled"):
2019-09-22 19:30:39 +02:00
if acme_subdomain:
2019-09-04 03:07:24 +02:00
if iswildcard:
2019-08-30 08:55:23 +02:00
Log.info(self, "Using existing Wildcard SSL "
2019-08-30 08:31:33 +02:00
"certificate from {0} to secure {1}"
.format(wo_root_domain, wo_domain))
Log.debug(self, "symlink wildcard "
"cert between {0} & {1}"
.format(wo_domain, wo_root_domain))
# copy the cert from the root domain
copyWildcardCert(self, wo_domain, wo_root_domain)
else:
# check DNS records before issuing cert
if not acmedata['dns'] is True:
2019-09-25 22:41:13 +02:00
if not pargs.force:
if not WOAcme.check_dns(self,
acme_domains):
Log.error(
self,
"Aborting SSL certificate "
"issuance")
Log.debug(self, "Setup Cert with acme.sh for {0}"
.format(wo_domain))
2019-09-23 01:06:44 +02:00
if WOAcme.setupletsencrypt(
self, acme_domains, acmedata):
2019-09-23 01:06:44 +02:00
WOAcme.deploycert(self, wo_domain)
2019-09-23 01:11:27 +02:00
else:
Log.error(self, "Unable to issue certificate")
else:
# check DNS records before issuing cert
if not acmedata['dns'] is True:
2019-09-25 22:41:13 +02:00
if not pargs.force:
if not WOAcme.check_dns(self, acme_domains):
Log.error(
self,
"Aborting SSL certificate issuance")
2019-09-23 01:06:44 +02:00
if WOAcme.setupletsencrypt(
self, acme_domains, acmedata):
2019-09-23 01:06:44 +02:00
WOAcme.deploycert(self, wo_domain)
2019-09-23 01:11:27 +02:00
else:
Log.error(self, "Unable to issue certificate")
2018-11-13 21:55:59 +01:00
else:
WOFileUtils.mvfile(self, "{0}/conf/nginx/ssl.conf.disabled"
.format(wo_site_webroot),
'{0}/conf/nginx/ssl.conf'
.format(wo_site_webroot))
2019-08-27 23:25:39 +02:00
WOFileUtils.mvfile(self, "/etc/nginx/conf.d/"
"force-ssl-{0}.conf.disabled"
.format(wo_domain),
'/etc/nginx/conf.d/force-ssl-{0}.conf'
.format(wo_domain))
2019-09-22 19:30:39 +02:00
httpsRedirect(self, wo_domain, True, acme_wildcard)
2019-09-04 04:21:07 +02:00
SSL.siteurlhttps(self, wo_domain)
2018-11-13 21:55:59 +01:00
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
2019-04-07 12:57:35 +02:00
Log.info(self, "Congratulations! Successfully "
2019-08-30 08:55:23 +02:00
"Configured SSL for Site "
2018-11-13 21:55:59 +01:00
" https://{0}".format(wo_domain))
2019-09-22 19:30:39 +02:00
if acme_subdomain and iswildcard:
2019-09-04 03:07:24 +02:00
if (SSL.getexpirationdays(self, wo_root_domain) > 0):
2019-09-03 19:02:00 +02:00
Log.info(
self, "Your cert will expire within " +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdays(self, wo_root_domain)) +
2019-09-03 19:02:00 +02:00
" days.")
2019-08-30 08:35:06 +02:00
else:
Log.warn(
self, "Your cert already EXPIRED ! "
".PLEASE renew soon . ")
2018-11-13 21:55:59 +01:00
else:
2019-09-04 03:07:24 +02:00
if (SSL.getexpirationdays(self, wo_domain) > 0):
2019-08-30 08:35:06 +02:00
Log.info(self, "Your cert will expire within " +
2019-09-04 03:07:24 +02:00
str(SSL.getexpirationdays(self, wo_domain)) +
2019-08-30 08:35:06 +02:00
" days.")
else:
Log.warn(
self, "Your cert already EXPIRED ! "
".PLEASE renew soon . ")
2018-11-13 21:55:59 +01:00
elif data['letsencrypt'] is False:
if pargs.letsencrypt == "off":
2019-08-30 08:40:35 +02:00
if os.path.islink("{0}/conf/nginx/ssl.conf"
2019-04-07 12:57:35 +02:00
.format(wo_site_webroot)):
2019-08-30 08:40:35 +02:00
WOFileUtils.remove_symlink(self,
"{0}/conf/nginx/ssl.conf"
.format(wo_site_webroot))
elif os.path.isfile("{0}/conf/nginx/ssl.conf"
.format(wo_site_webroot)):
2019-07-15 15:39:45 +02:00
Log.info(self, 'Setting Nginx configuration')
WOFileUtils.mvfile(self, "{0}/conf/nginx/ssl.conf"
2019-04-07 12:57:35 +02:00
.format(wo_site_webroot),
2019-07-15 15:39:45 +02:00
'{0}/conf/nginx/ssl.conf.disabled'
2019-04-07 12:57:35 +02:00
.format(wo_site_webroot))
2019-07-15 15:39:45 +02:00
httpsRedirect(self, wo_domain, False)
if os.path.isfile("{0}/conf/nginx/hsts.conf"
.format(wo_site_webroot)):
WOFileUtils.mvfile(self, "{0}/conf/nginx/hsts.conf"
.format(wo_site_webroot),
'{0}/conf/nginx/'
'hsts.conf.disabled'
.format(wo_site_webroot))
# find all broken symlinks
sympath = "/var/www"
2019-08-30 10:17:31 +02:00
WOFileUtils.findBrokenSymlink(self, sympath)
elif (pargs.letsencrypt == "clean" or
pargs.letsencrypt == "purge"):
removeAcmeConf(self, wo_domain)
2019-09-01 23:29:23 +02:00
# find all broken symlinks
sympath = "/var/www"
WOFileUtils.findBrokenSymlink(self, sympath)
2019-07-15 15:39:45 +02:00
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
# Log.info(self,"Removing Cron Job set for cert
# auto-renewal") WOCron.remove_cron(self,'wo site
# update {0} --le=renew --min_expiry_limit 30
# 2> \/dev\/null'.format(wo_domain))
Log.info(self, "Successfully Disabled SSl for Site "
" http://{0}".format(wo_domain))
2018-11-13 21:55:59 +01:00
# Add nginx conf folder into GIT
WOGit.add(self, ["{0}/conf/nginx".format(wo_site_webroot)],
msg="Adding letsencrypts config of site: {0}"
.format(wo_domain))
2018-11-13 21:55:59 +01:00
updateSiteInfo(self, wo_domain, ssl=letsencrypt)
return 0
2019-04-08 09:14:23 +02:00
if pargs.hsts:
2019-04-15 16:00:26 +02:00
if data['hsts'] is True:
2019-04-15 15:31:19 +02:00
if os.path.isfile(("{0}/conf/nginx/ssl.conf")
.format(wo_site_webroot)):
2019-04-15 23:54:05 +02:00
if not os.path.isfile("{0}/conf/nginx/hsts.conf"
.format(wo_site_webroot)):
2019-09-06 22:21:16 +02:00
SSL.setuphsts(self, wo_domain)
2019-04-15 15:31:19 +02:00
else:
Log.error(self, "HSTS is already configured for given "
"site")
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
else:
Log.error(self, "HTTPS is not configured for given "
"site")
2019-04-15 14:44:06 +02:00
2019-04-15 16:00:26 +02:00
elif data['hsts'] is False:
2019-04-15 15:31:19 +02:00
if os.path.isfile(("{0}/conf/nginx/hsts.conf")
.format(wo_site_webroot)):
WOFileUtils.mvfile(self, "{0}/conf/nginx/hsts.conf"
.format(wo_site_webroot),
'{0}/conf/nginx/hsts.conf.disabled'
.format(wo_site_webroot))
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
2019-04-08 01:38:14 +02:00
else:
2019-04-15 15:31:19 +02:00
Log.error(self, "HSTS is not configured for given "
2019-04-15 15:09:10 +02:00
"site")
2019-09-27 01:19:45 +02:00
if pargs.ngxblocker:
if ngxblocker is True:
if not os.path.isfile("{0}/conf/nginx/ngxblocker.conf.disabled"
.format(wo_site_webroot)):
setupngxblocker(self, wo_domain)
else:
WOFileUtils.mvfile(
self,
"{0}/conf/nginx/ngxblocker.conf.disabled"
.format(wo_site_webroot),
"{0}/conf/nginx/ngxblocker.conf"
.format(wo_site_webroot))
elif ngxblocker is False:
if os.path.isfile("{0}/conf/nginx/ngxblocker.conf"
.format(wo_site_webroot)):
WOFileUtils.mvfile(
self,
"{0}/conf/nginx/ngxblocker.conf"
.format(wo_site_webroot),
"{0}/conf/nginx/ngxblocker.conf.disabled"
.format(wo_site_webroot))
# Service Nginx Reload
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
2019-04-08 01:38:14 +02:00
2018-11-13 21:55:59 +01:00
if stype == oldsitetype and cache == oldcachetype:
# Service Nginx Reload
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
updateSiteInfo(self, wo_domain, stype=stype, cache=cache,
2019-04-07 12:57:35 +02:00
ssl=True if check_site.is_ssl else False,
php_version=check_php_version)
2018-11-13 21:55:59 +01:00
Log.info(self, "Successfully updated site"
" http://{0}".format(wo_domain))
return 0
# if data['wo_db_name'] and not data['wp']:
2018-11-13 21:55:59 +01:00
if 'wo_db_name' in data.keys() and not data['wp']:
try:
data = setupdatabase(self, data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details:"
"`tail /var/log/wo/wordops.log` and please try again")
return 1
try:
wodbconfig = open("{0}/wo-config.php".format(wo_site_webroot),
encoding='utf-8', mode='w')
wodbconfig.write("<?php \ndefine('DB_NAME', '{0}');"
"\ndefine('DB_USER', '{1}'); "
"\ndefine('DB_PASSWORD', '{2}');"
"\ndefine('DB_HOST', '{3}');\n?>"
.format(data['wo_db_name'],
data['wo_db_user'],
data['wo_db_pass'],
data['wo_db_host']))
wodbconfig.close()
except IOError as e:
Log.debug(self, str(e))
Log.debug(self, "creating wo-config.php failed.")
Log.info(self, Log.FAIL + "Update site failed. "
"Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
return 1
# Setup WordPress if old sites are html/php/mysql sites
2019-07-19 22:29:18 +02:00
if data['wp'] and oldsitetype in ['html', 'proxy', 'php',
'mysql', 'php73']:
2018-11-13 21:55:59 +01:00
try:
wo_wp_creds = setupwordpress(self, data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
return 1
# Uninstall unnecessary plugins
if oldsitetype in ['wp', 'wpsubdir', 'wpsubdomain']:
# Setup WordPress Network if update option is multisite
# and oldsite is WordPress single site
if data['multisite'] and oldsitetype == 'wp':
try:
setupwordpressnetwork(self, data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed. "
"Check the log for details:"
2019-04-07 12:57:35 +02:00
" `tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
return 1
if ((oldcachetype in ['wpsc', 'basic', 'wpredis', 'wprocket',
'wpce'] and
2019-04-07 22:10:08 +02:00
(data['wpfc'])) or (oldsitetype == 'wp' and
data['multisite'] and data['wpfc'])):
2018-11-13 21:55:59 +01:00
try:
2019-09-04 04:26:09 +02:00
plugin_data_object = {
"log_level": "INFO",
"log_filesize": 5,
"enable_purge": 1,
"enable_map": "0",
"enable_log": 0,
"enable_stamp": 1,
"purge_homepage_on_new": 1,
"purge_homepage_on_edit": 1,
"purge_homepage_on_del": 1,
"purge_archive_on_new": 1,
"purge_archive_on_edit": 0,
"purge_archive_on_del": 0,
"purge_archive_on_new_comment": 0,
"purge_archive_on_deleted_comment": 0,
"purge_page_on_mod": 1,
"purge_page_on_new_comment": 1,
"purge_page_on_deleted_comment": 1,
"cache_method": "enable_fastcgi",
"purge_method": "get_request",
"redis_hostname": "127.0.0.1",
2019-08-04 16:40:34 +02:00
"redis_port": "6379",
"redis_prefix": "nginx-cache:"}
plugin_data = json.dumps(plugin_data_object)
setupwp_plugin(self, 'nginx-helper',
'rt_wp_nginx_helper_options',
plugin_data, data)
2018-11-13 21:55:59 +01:00
except SiteError as e:
Log.debug(self, str(e))
2019-04-07 12:57:35 +02:00
Log.info(self, Log.FAIL + "Update nginx-helper "
"settings failed. "
2018-11-13 21:55:59 +01:00
"Check the log for details:"
2019-04-07 12:57:35 +02:00
" `tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
return 1
elif ((oldcachetype in ['wpsc', 'basic', 'wpfc',
'wprocket', 'wpce'] and
2019-04-07 12:57:35 +02:00
(data['wpredis'])) or (oldsitetype == 'wp' and
2019-04-07 22:10:08 +02:00
data['multisite'] and
data['wpredis'])):
2018-11-13 21:55:59 +01:00
try:
2019-09-04 04:26:09 +02:00
plugin_data_object = {
"log_level": "INFO",
"log_filesize": 5,
"enable_purge": 1,
"enable_map": "0",
"enable_log": 0,
"enable_stamp": 1,
"purge_homepage_on_new": 1,
"purge_homepage_on_edit": 1,
"purge_homepage_on_del": 1,
"purge_archive_on_new": 1,
"purge_archive_on_edit": 0,
"purge_archive_on_del": 0,
"purge_archive_on_new_comment": 0,
"purge_archive_on_deleted_comment": 0,
"purge_page_on_mod": 1,
"purge_page_on_new_comment": 1,
"purge_page_on_deleted_comment": 1,
"cache_method": "enable_redis",
"purge_method": "get_request",
"redis_hostname": "127.0.0.1",
2019-08-04 16:40:34 +02:00
"redis_port": "6379",
"redis_prefix": "nginx-cache:"}
plugin_data = json.dumps(plugin_data_object)
setupwp_plugin(self, 'nginx-helper',
'rt_wp_nginx_helper_options',
plugin_data, data)
2018-11-13 21:55:59 +01:00
except SiteError as e:
Log.debug(self, str(e))
2019-04-07 12:57:35 +02:00
Log.info(self, Log.FAIL + "Update nginx-helper "
"settings failed. "
2018-11-13 21:55:59 +01:00
"Check the log for details:"
2019-04-07 12:57:35 +02:00
" `tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
return 1
else:
try:
# disable nginx-helper
2019-09-04 04:26:09 +02:00
plugin_data_object = {
"log_level": "INFO",
"log_filesize": 5,
"enable_purge": 0,
"enable_map": 0,
"enable_log": 0,
"enable_stamp": 0,
"purge_homepage_on_new": 1,
"purge_homepage_on_edit": 1,
"purge_homepage_on_del": 1,
"purge_archive_on_new": 1,
"purge_archive_on_edit": 0,
"purge_archive_on_del": 0,
"purge_archive_on_new_comment": 0,
"purge_archive_on_deleted_comment": 0,
"purge_page_on_mod": 1,
"purge_page_on_new_comment": 1,
"purge_page_on_deleted_comment": 1,
"cache_method": "enable_redis",
"purge_method": "get_request",
"redis_hostname": "127.0.0.1",
2019-08-04 16:40:34 +02:00
"redis_port": "6379",
"redis_prefix": "nginx-cache:"}
plugin_data = json.dumps(plugin_data_object)
setupwp_plugin(
2019-04-07 12:57:35 +02:00
self, 'nginx-helper',
'rt_wp_nginx_helper_options', plugin_data, data)
2018-11-13 21:55:59 +01:00
except SiteError as e:
Log.debug(self, str(e))
2019-04-07 12:57:35 +02:00
Log.info(self, Log.FAIL + "Update nginx-helper "
"settings failed. "
2018-11-13 21:55:59 +01:00
"Check the log for details:"
2019-04-07 12:57:35 +02:00
" `tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
return 1
2019-09-04 04:26:09 +02:00
if ((oldcachetype in ['wpsc', 'basic',
'wpfc', 'wprocket', 'wpredis'] and
(data['wpce'])) or (oldsitetype == 'wp' and
data['multisite'] and
data['wpce'])):
try:
installwp_plugin(self, 'cache-enabler', data)
# setup cache-enabler
2019-09-04 04:26:09 +02:00
plugin_data_object = {
"expires": 24,
"new_post": 1,
"new_comment": 0,
"webp": 0,
"clear_on_upgrade": 1,
"compress": 0,
"excl_ids": "",
"excl_regexp": "",
"excl_cookies": "",
"incl_attributes": "",
"minify_html": 1}
plugin_data = json.dumps(plugin_data_object)
setupwp_plugin(self, 'cache-enabler',
'cache-enabler', plugin_data, data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update cache-enabler "
"settings failed. "
"Check the log for details:"
" `tail /var/log/wo/wordops.log` "
"and please try again")
return 1
2018-11-13 21:55:59 +01:00
if oldcachetype == 'wpsc' and not data['wpsc']:
try:
uninstallwp_plugin(self, 'wp-super-cache', data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details:"
2019-04-07 12:57:35 +02:00
" `tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
return 1
if oldcachetype == 'wpredis' and not data['wpredis']:
try:
uninstallwp_plugin(self, 'redis-cache', data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details:"
2019-04-07 12:57:35 +02:00
" `tail /var/log/wo/wordops.log` "
"and please try again")
2018-11-13 21:55:59 +01:00
return 1
if oldcachetype != 'wpsc' and data['wpsc']:
try:
installwp_plugin(self, 'wp-super-cache', data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
return 1
if oldcachetype == 'wprocket' and not data['wprocket']:
try:
uninstallwp_plugin(self, 'wp-rocket', data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
return 1
2019-08-15 19:59:23 +02:00
if oldcachetype == 'wpce' and not data['wpce']:
try:
uninstallwp_plugin(self, 'cache-enabler', data)
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
return 1
2018-11-13 21:55:59 +01:00
# Service Nginx Reload
if not WOService.reload_service(self, 'nginx'):
Log.error(self, "service nginx reload failed. "
"check issues with `nginx -t` command")
WOGit.add(self, ["/etc/nginx"],
msg="{0} updated with {1} {2}"
.format(wo_www_domain, stype, cache))
# Setup Permissions for webroot
try:
setwebrootpermissions(self, data['webroot'])
except SiteError as e:
Log.debug(self, str(e))
Log.info(self, Log.FAIL + "Update site failed."
"Check the log for details: "
"`tail /var/log/wo/wordops.log` and please try again")
return 1
if wo_auth and len(wo_auth):
for msg in wo_auth:
Log.info(self, Log.ENDC + msg)
display_cache_settings(self, data)
if data['wp'] and oldsitetype in ['html', 'php', 'mysql']:
Log.info(self, "\n\n" + Log.ENDC + "WordPress admin user :"
" {0}".format(wo_wp_creds['wp_user']))
Log.info(self, Log.ENDC + "WordPress admin password : {0}"
.format(wo_wp_creds['wp_pass']) + "\n\n")
if oldsitetype in ['html', 'php'] and stype != 'php':
updateSiteInfo(self, wo_domain, stype=stype, cache=cache,
db_name=data['wo_db_name'],
db_user=data['wo_db_user'],
db_password=data['wo_db_pass'],
2019-03-13 11:55:18 +01:00
db_host=data['wo_db_host'],
2019-04-07 22:10:08 +02:00
ssl=True if check_site.is_ssl else False,
php_version=check_php_version)
2018-11-13 21:55:59 +01:00
else:
updateSiteInfo(self, wo_domain, stype=stype, cache=cache,
2019-04-07 22:10:08 +02:00
ssl=True if check_site.is_ssl else False,
php_version=check_php_version)
2018-11-13 21:55:59 +01:00
Log.info(self, "Successfully updated site"
" http://{0}".format(wo_domain))
return 0
class WOSiteDeleteController(CementBaseController):
class Meta:
label = 'delete'
stacked_on = 'site'
stacked_type = 'nested'
description = 'delete an existing website'
arguments = [
(['site_name'],
dict(help='domain name to be deleted', nargs='?')),
(['--no-prompt'],
dict(help="doesnt ask permission for delete",
action='store_true')),
(['-f', '--force'],
2018-11-13 21:55:59 +01:00
dict(help="forcefully delete site and configuration",
action='store_true')),
(['--all'],
dict(help="delete all", action='store_true')),
(['--db'],
dict(help="delete db only", action='store_true')),
(['--files'],
dict(help="delete webroot only", action='store_true')),
]
2018-11-13 21:55:59 +01:00
@expose(help="Delete website configuration and files")
@expose(hide=True)
def default(self):
pargs = self.app.pargs
if not pargs.site_name:
2018-11-13 21:55:59 +01:00
try:
while not pargs.site_name:
pargs.site_name = (input('Enter site name : ')
.strip())
2018-11-13 21:55:59 +01:00
except IOError as e:
2019-08-30 20:33:33 +02:00
Log.debug(self, str(e))
2018-11-13 21:55:59 +01:00
Log.error(self, 'could not input site name')
pargs.site_name = pargs.site_name.strip()
2019-09-19 14:07:34 +02:00
(wo_domain, wo_www_domain) = WODomain.validatedomain(self, pargs.site_name)
2018-11-13 21:55:59 +01:00
wo_db_name = ''
wo_prompt = ''
wo_nginx_prompt = ''
mark_db_delete_prompt = False
mark_webroot_delete_prompt = False
mark_db_deleted = False
mark_webroot_deleted = False
if not check_domain_exists(self, wo_domain):
Log.error(self, "site {0} does not exist".format(wo_domain))
if ((not pargs.db) and (not pargs.files) and
(not pargs.all)):
pargs.all = True
2018-11-13 21:55:59 +01:00
# Gather information from wo-db for wo_domain
check_site = getSiteInfo(self, wo_domain)
wo_site_type = check_site.site_type
wo_site_webroot = check_site.site_path
if wo_site_webroot == 'deleted':
mark_webroot_deleted = True
if wo_site_type in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
wo_db_name = check_site.db_name
wo_db_user = check_site.db_user
wo_mysql_grant_host = self.app.config.get('mysql', 'grant-host')
if wo_db_name == 'deleted':
mark_db_deleted = True
if pargs.all:
pargs.db = True
pargs.files = True
2018-11-13 21:55:59 +01:00
else:
if pargs.all:
2018-11-13 21:55:59 +01:00
mark_db_deleted = True
pargs.files = True
2018-11-13 21:55:59 +01:00
# Delete website database
if pargs.db:
2018-11-13 21:55:59 +01:00
if wo_db_name != 'deleted' and wo_db_name != '':
if not pargs.no_prompt:
2018-11-13 21:55:59 +01:00
wo_db_prompt = input('Are you sure, you want to delete'
' database [y/N]: ')
else:
wo_db_prompt = 'Y'
mark_db_delete_prompt = True
if wo_db_prompt == 'Y' or wo_db_prompt == 'y':
mark_db_delete_prompt = True
Log.info(self, "Deleting Database, {0}, user {1}"
.format(wo_db_name, wo_db_user))
deleteDB(self, wo_db_name, wo_db_user,
wo_mysql_grant_host, False)
2018-11-13 21:55:59 +01:00
updateSiteInfo(self, wo_domain,
db_name='deleted',
db_user='deleted',
db_password='deleted')
mark_db_deleted = True
Log.info(self, "Deleted Database successfully.")
else:
mark_db_deleted = True
Log.info(self, "Does not seems to have database for this site."
)
# Delete webroot
if pargs.files:
2018-11-13 21:55:59 +01:00
if wo_site_webroot != 'deleted':
if not pargs.no_prompt:
2018-11-13 21:55:59 +01:00
wo_web_prompt = input('Are you sure, you want to delete '
'webroot [y/N]: ')
else:
wo_web_prompt = 'Y'
mark_webroot_delete_prompt = True
if wo_web_prompt == 'Y' or wo_web_prompt == 'y':
mark_webroot_delete_prompt = True
Log.info(self, "Deleting Webroot, {0}"
.format(wo_site_webroot))
deleteWebRoot(self, wo_site_webroot)
updateSiteInfo(self, wo_domain, webroot='deleted')
mark_webroot_deleted = True
Log.info(self, "Deleted webroot successfully")
else:
mark_webroot_deleted = True
Log.info(self, "Webroot seems to be already deleted")
if not pargs.force:
2018-11-13 21:55:59 +01:00
if (mark_webroot_deleted and mark_db_deleted):
# TODO Delete nginx conf
removeNginxConf(self, wo_domain)
deleteSiteInfo(self, wo_domain)
Log.info(self, "Deleted site {0}".format(wo_domain))
# else:
2019-04-07 22:10:08 +02:00
# Log.error(self, " site {0} does
# not exists".format(wo_domain))
2018-11-13 21:55:59 +01:00
else:
2019-04-07 22:10:08 +02:00
if (mark_db_delete_prompt or mark_webroot_delete_prompt or
(mark_webroot_deleted and mark_db_deleted)):
2018-11-13 21:55:59 +01:00
# TODO Delete nginx conf
removeNginxConf(self, wo_domain)
deleteSiteInfo(self, wo_domain)
Log.info(self, "Deleted site {0}".format(wo_domain))
class WOSiteListController(CementBaseController):
class Meta:
label = 'list'
stacked_on = 'site'
stacked_type = 'nested'
description = 'List websites'
arguments = [
(['--enabled'],
dict(help='List enabled websites', action='store_true')),
(['--disabled'],
dict(help="List disabled websites", action='store_true')),
]
2018-11-13 21:55:59 +01:00
@expose(help="Lists websites")
def default(self):
pargs = self.app.pargs
sites = getAllsites(self)
if not sites:
pass
2018-11-13 21:55:59 +01:00
if pargs.enabled:
for site in sites:
if site.is_enabled:
Log.info(self, "{0}".format(site.sitename))
elif pargs.disabled:
for site in sites:
if not site.is_enabled:
Log.info(self, "{0}".format(site.sitename))
else:
for site in sites:
Log.info(self, "{0}".format(site.sitename))
2018-11-13 21:55:59 +01:00
def load(app):
# register the plugin class.. this only happens if the plugin is enabled
2019-09-24 00:01:20 +02:00
app.handler.register(WOSiteController)
app.handler.register(WOSiteCreateController)
app.handler.register(WOSiteUpdateController)
app.handler.register(WOSiteDeleteController)
app.handler.register(WOSiteListController)
app.handler.register(WOSiteEditController)
2018-11-13 21:55:59 +01:00
# register a hook (function) to run after arguments are parsed.
2019-09-24 00:04:32 +02:00
app.hook.register('post_argument_parsing', wo_site_hook)