Browse Source

Merge branch 'python' of github.com:rtCamp/easyengine into python

bugfixes
gau1991 10 years ago
parent
commit
d4924340c7
  1. 163
      ee/cli/plugins/site.py
  2. 70
      ee/cli/plugins/site_functions.py
  3. 5
      ee/core/domainvalidate.py
  4. 5
      ee/core/fileutils.py
  5. 8
      ee/core/services.py
  6. 11
      ee/core/shellexec.py
  7. 2
      ee/core/variables.py

163
ee/cli/plugins/site.py

@ -2,9 +2,10 @@
from cement.core.controller import CementBaseController, expose
from cement.core import handler, hook
from ee.core.variables import EEVariables
from ee.core.domainvalidate import validate_domain
from ee.core.domainvalidate import ValidateDomain
from ee.core.fileutils import EEFileUtils
from ee.cli.plugins.site_functions import *
from ee.core.services import EEService
import sys
import os
@ -113,152 +114,184 @@ class EESiteCreateController(CementBaseController):
# data = dict(foo='EESiteCreateController.default().')
# self.app.render((data), 'default.mustache')
# Check domain name validation
ee_domain_name = validate_domain(self.app.pargs.site_name)
ee_site_webroot = EEVariables.ee_webroot + ee_domain_name
(ee_domain,
ee_www_domain) = ValidateDomain(self.app.pargs.site_name)
ee_site_webroot = EEVariables.ee_webroot + ee_domain
# Check if doain previously exists or not
if os.path.isfile('/etc/nginx/sites-available/{0}.conf'
.format(ee_domain_name)):
.format(ee_domain)):
self.app.log.error("site {0} already exists"
.format(ee_domain_name))
return False
.format(ee_domain))
sys.exit(1)
# setup nginx configuration for site
# HTmL
if (self.app.pargs.html and not (self.app.pargs.php or
self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc or
self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=True, basic=False, wp=False, w3tc=False,
wpfc=False, wpsc=False, multisite=False,
wpsubdir=False, webroot=ee_site_webroot)
#PHP
if (self.app.pargs.php and not (self.app.pargs.html or
self.app.pargs.mysql or self.app.pargs.wp or self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc or
self.app.pargs.wsubdir or self.app.pargs.wsubdomain)):
data = dict(site_name=ee_domain_name,
self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)):
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=True, wp=False, w3tc=False,
wpfc=False, wpsc=False, multisite=False,
wpsubdir=False, webroot=ee_site_webroot)
#ySQL
if (self.app.pargs.mysql and not (self.app.pargs.html or
self.app.pargs.php or self.app.pargs.wp or self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc or
self.app.pargs.wsubdir or self.app.pargs.wsubdomain)):
data = dict(site_name=ee_domain_name,
self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)):
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=True, wp=False, w3tc=False,
wpfc=False, wpsc=False, multisite=False,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
#WP
if ((self.app.pargs.wp or self.app.pargs.w3tc or self.app.pargs.wpfc or
self.app.pargs.wpsc) and not (self.app.pargs.html or
self.app.pargs.php or self.app.pargs.mysql or
self.app.pargs.wpsubdir or self.app.pargs.wpsubdomain)):
if (self.app.pargs.wp and not (self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=True, wp=True, w3tc=False,
wpfc=False, wpsc=False, multisite=False,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.w3tc and not
(self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=True,
wpfc=False, wpsc=False, multisite=False,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.wpfc and not
(self.app.pargs.wpsc or self.app.pargs.w3tc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=False,
wpfc=True, wpsc=False, multisite=False,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.wpsc and not
(self.app.pargs.w3tc or self.app.pargs.wpfc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=False,
wpfc=False, wpsc=True, multisite=False,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
#WPSUBDIR
if (self.app.pargs.wpsubdir and not (self.app.pargs.html or
self.app.pargs.php or self.app.pargs.mysql or
self.app.pargs.wpsubdomain or self.app.pargs.wp)):
print("Inside wpsubdir")
if (self.app.pargs.wpsubdir and not (self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=True, wp=True, w3tc=False,
wpfc=False, wpsc=False, multisite=True,
wpsubdir=True, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.w3tc and not
(self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=True,
wpfc=False, wpsc=False, multisite=True,
wpsubdir=True, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.wpfc and not
(self.app.pargs.wpsc or self.app.pargs.w3tc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=False,
wpfc=True, wpsc=False, multisite=True,
wpsubdir=True, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.wpsc and not
(self.app.pargs.w3tc or self.app.pargs.wpfc)):
data = dict(site_name=ee_domain_name,
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=False,
wpfc=False, wpsc=True, multisite=True,
wpsubdir=True, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
if (self.app.pargs.wpsubdomain and not (self.app.pargs.html or
self.app.pargs.php or self.app.pargs.mysql or
self.app.pargs.wpsubdir or self.app.pargs.wp)):
if (self.app.pargs.wpsubdomain and not (self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain_name,
static=False, basic=True, wp=True, w3tc=False,
wpfc=False, wpsc=False, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
if (self.app.pargs.w3tc and not
(self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain_name,
static=False, basic=False, wp=True, w3tc=True,
wpfc=False, wpsc=False, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
if (self.app.pargs.wpfc and not
(self.app.pargs.wpsc or self.app.pargs.w3tc)):
data = dict(site_name=ee_domain_name,
static=False, basic=False, wp=True, w3tc=False,
wpfc=True, wpsc=False, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
if (self.app.pargs.wpsc and not
(self.app.pargs.w3tc or self.app.pargs.wpfc)):
data = dict(site_name=ee_domain_name,
static=False, basic=False, wp=True, w3tc=False,
wpfc=False, wpsc=True, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='')
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
#WPSUBDOAIN
if (self.app.pargs.wpsubdomain and not (self.app.pargs.html or
self.app.pargs.php or self.app.pargs.mysql or
self.app.pargs.wpsubdir or self.app.pargs.wp)):
if (self.app.pargs.wpsubdomain and not (self.app.pargs.w3tc
or self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=True, wp=True, w3tc=False,
wpfc=False, wpsc=False, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.w3tc and not
(self.app.pargs.wpfc or self.app.pargs.wpsc)):
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=True,
wpfc=False, wpsc=False, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.wpfc and not
(self.app.pargs.wpsc or self.app.pargs.w3tc)):
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=False,
wpfc=True, wpsc=False, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
if (self.app.pargs.wpsc and not
(self.app.pargs.w3tc or self.app.pargs.wpfc)):
data = dict(site_name=ee_domain, www_domain=ee_www_domain,
static=False, basic=False, wp=True, w3tc=False,
wpfc=False, wpsc=True, multisite=True,
wpsubdir=False, webroot=ee_site_webroot,
ee_db_name='', ee_db_user='', ee_db_pass='',
ee_db_host='')
# setup NGINX configuration, and webroot
SetupDomain(self, data)
# Setup database for MySQL site
if 'ee_db_name' in data.keys() and not data['wp']:
data = SetupDatabase(self, data)
eedbconfig = open("{0}/ee-config.php".format(ee_site_webroot), 'w')
eedbconfig.write("<?php \ndefine('DB_NAME', '{0}');"
"\ndefine('DB_USER', '{1}'); "
"\ndefine('DB_PASSWORD', '{2}');"
"\ndefine('DB_HOST', '{3}');\n?>"
.format(data['ee_db_name'], data['ee_db_user'],
data['ee_db_pass'], data['ee_db_host']))
eedbconfig.close()
# Setup WordPress if Wordpress site
if data['wp']:
ee_wp_creds = SetupWordpress(self, data)
# TODO setup Perissions for webroot
# Service Nginx Reload
EEService.reload_service(self, 'nginx')
# Setup Permissions for webroot
SetWebrootPermissions(self, data['webroot'])
if data['wp']:
print("WordPress Admin User : {0}".format(ee_wp_creds['wp_user']))
print("WordPress Admin User Password : {0}"
.format(ee_wp_creds['wp_pass']))
print("Successfully created site http://{0}".format(ee_www_domain))
class EESiteUpdateController(CementBaseController):

70
ee/cli/plugins/site_functions.py

@ -101,11 +101,10 @@ def SetupDatabase(self, data):
ee_db_name = (ee_db_name[0:6] + ee_random10)
# create MySQL database
print("Setting Up Database for {0}...".format(ee_domain_name))
EEMysql.execute(self, "create database {0}"
.format(ee_db_name))
print("create user {0}@{1} identified by '{2}'"
.format(ee_db_username, ee_mysql_host, ee_db_password))
# Create MySQL User
EEMysql.execute(self,
"create user {0}@{1} identified by '{2}'"
@ -114,11 +113,12 @@ def SetupDatabase(self, data):
# Grant permission
EEMysql.execute(self,
"grant all privileges on {0}.* to {1}@{2}"
.format(ee_db_name, ee_db_username, ee_db_password))
.format(ee_db_name, ee_db_username, ee_mysql_host))
data['ee_db_name'] = ee_db_name
data['ee_db_user'] = ee_db_username
data['ee_db_pass'] = ee_db_password
return data
data['ee_db_host'] = ee_mysql_host
return(data)
def SetupWordpress(self, data):
@ -159,24 +159,25 @@ def SetupWordpress(self, data):
# Modify wp-config.php & move outside the webroot
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
print("Setting up WordPress Configuration...")
if not data['multisite']:
print("Generating wp-config")
EEShellExec.cmd_exec(self, "wp --allow-root core config "
+ "--dbname={0} --dbprefix={1} --dbuser={2} "
.format(data['ee_db_name'], ee_wp_prefix,
data['ee_db_user'])
+ "--dbpass={0}".format(data['ee_db_pass']))
else:
EEShellExec.cmd_exec(self, "php /usr/bin/wp --allow-root core config "
+ "--dbname={0} --dbprefix={1} "
.format(data['ee_db_name'], ee_wp_prefix)
+ "--dbuser={0} --dbpass={1} --extra-php<<PHP"
+ "\n define('WP_ALLOW_MULTISITE', true);"
+ "\n define('WPMU_ACCEL_REDIRECT', true);"
+ "\n PHP"
.format(data['ee_db_user'], data['ee_db_pass']))
+ "--dbuser={0} --dbpass={1} "
"--extra-php<<PHP \n {var1} {var2} \nPHP"
.format(data['ee_db_user'], data['ee_db_pass'],
var1=
"\n define('WP_ALLOW_MULTISITE', true);",
var2=
"\n define('WPMU_ACCEL_REDIRECT', true);")
)
EEFileUtils.mvfile(self, './wp-config.php', '../')
@ -199,24 +200,29 @@ def SetupWordpress(self, data):
print("Setting up WordPress, please wait...")
if not data['multisite']:
EEShellExec.cmd_exec(self, "wp --allow-root core install"
"--url=www.{0} --title=www.{0} --admin_name={1}"
.format(ee_domain_name, ee_wp_user)
EEShellExec.cmd_exec(self, "php /usr/bin/wp --allow-root core install "
"--url={0} --title={0} --admin_name={1} "
.format(data['www_domain'], ee_wp_user)
+ "--admin_password={0} --admin_email={1}"
.format(ee_wp_pass, ee_wp_email))
.format(ee_wp_pass, ee_wp_email),
errormsg="Unable to setup WordPress Tables")
else:
EEShellExec.cmd_exec(self, "wp --allow-root core multisite-install"
"--url=www.{0} --title=www.{0} --admin_name={1}"
.format(ee_domain_name, ee_wp_user)
EEShellExec.cmd_exec(self, "php /usr/bin/wp --allow-root "
"core multisite-install "
"--url={0} --title={0} --admin_name={1} "
.format(data['www_domain'], ee_wp_user)
+ "--admin_password={0} --admin_email={1} "
"{subdomains}"
.format(ee_wp_pass, ee_wp_email,
subdomains='--subdomains'
if not data['wpsubdir'] else ''))
if not data['wpsubdir'] else ''),
errormsg="Unable to setup WordPress Tables")
print("Updating WordPress permalink, please wait...")
EEShellExec.cmd_exec(self, "wp --allow-root rewrite structure "
"/%year%/%monthnum%/%day%/%postname%/")
EEShellExec.cmd_exec(self, " php /usr/bin/wp --allow-root "
"rewrite structure "
"/%year%/%monthnum%/%day%/%postname%/",
errormsg="Unable to Update WordPress permalink")
"""Install nginx-helper plugin """
InstallWP_Plugin(self, 'nginx-helper', data)
@ -231,7 +237,8 @@ def SetupWordpress(self, data):
wp_creds = dict(wp_user=ee_wp_user, wp_pass=ee_wp_pass,
wp_email=ee_wp_email)
return wp_creds
return(wp_creds)
def SetupWordpressNetwork(self, data):
@ -245,9 +252,20 @@ def InstallWP_Plugin(self, plugin_name, data):
ee_site_webroot = data['webroot']
print("Installing plugin {0}".format(plugin_name))
EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
EEShellExec.cmd_exec(self, "wp plugin --allow-root install {0}"
EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin --allow-root install "
"{0}".format(plugin_name),
errormsg="Unable to Install plugin {0}"
.format(plugin_name))
EEShellExec.cmd_exec(self, "wp plugin --allow-root activate {0} {na}"
EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin --allow-root activate "
"{0} {na}"
.format(plugin_name,
na='--network' if data['multisite'] else ''))
na='--network' if data['multisite'] else ''),
errormsg="Unable to Activate plugin {0}"
.format(plugin_name))
def SetWebrootPermissions(self, webroot):
print("Setting up Webroot Permissions...")
EEFileUtils.chown(self, webroot, EEVariables.ee_php_user,
EEVariables.ee_php_user, recursive=True)

5
ee/core/domainvalidate.py

@ -1,7 +1,7 @@
from urllib.parse import urlparse
def validate_domain(url):
def ValidateDomain(url):
# Check if http:// or https:// present remove it if present
domain_name = url.split('/')
@ -9,7 +9,6 @@ def validate_domain(url):
domain_name = domain_name[2]
else:
domain_name = domain_name[0]
www_domain_name = domain_name.split('.')
final_domain = ''
if www_domain_name[0] == 'www':
@ -17,4 +16,4 @@ def validate_domain(url):
return final_domain
else:
final_domain = domain_name
return final_domain
return (final_domain, domain_name)

5
ee/core/fileutils.py

@ -92,7 +92,10 @@ class EEFileUtils():
else:
shutil.chown(path, user=user, group=group)
except shutil.Error as e:
self.log.error("Unable to change owner {0}".format(e.strerror))
self.app.log.error("Unable to change owner : {0} ".format(e))
sys.exit(1)
except Exception as e:
self.app.log.error("Unable to change owner {0}".format(e))
sys.exit(1)
def chmod(self, path, perm, recursive=False):

8
ee/core/services.py

@ -20,7 +20,7 @@ class EEService():
else:
self.app.log.error(retcode[1])
except OSError as e:
self.app.log.error("Unable to start service \ {0} {1}"
self.app.log.error("Failed to start service {0} {1}"
.format(e.errno, e.strerror))
return False
@ -34,7 +34,7 @@ class EEService():
else:
return False
except OSError as e:
self.app.log.error("Unable to stop services \ {0}{1}"
self.app.log.error("Failed to stop service : {0}{1}"
.format(e.errno, e.strerror))
return False
@ -43,7 +43,7 @@ class EEService():
EEService.stop_service(self, service_name)
EEService.start_service(self, service_name)
except OSError as e:
self.app.log.error("Unable to restart services \{0} {1}"
self.app.log.error("Failed to restart services \{0} {1}"
.format(e.errno, e.strerror))
def reload_service(self, service_name):
@ -56,7 +56,7 @@ class EEService():
else:
return False
except OSError as e:
self.app.log.error("Execution failed:", e)
self.app.log.error("Failed to reload NGINX:", e)
return False
def get_service_status(self, service_name):

11
ee/core/shellexec.py

@ -10,7 +10,7 @@ class EEShellExec():
def __init__():
pass
def cmd_exec(self, command):
def cmd_exec(self, command, errormsg=''):
try:
retcode = subprocess.getstatusoutput(command)
if retcode[0] == 0:
@ -19,6 +19,11 @@ class EEShellExec():
self.app.log.warn(retcode[1])
return False
except OSError as e:
self.app.log.error("Unable to execute command \ {0}{1}"
if errormsg:
self.app.log.error("{0}", errormsg)
else:
self.app.log.error("Unable to execute command \ {0}{1}"
.format(e.errno, e.strerror))
self.app.log.debug("Unable to execute command \ {0}{1}"
.format(e.errno, e.strerror))
return False
sys.exit(1)

2
ee/core/variables.py

@ -22,6 +22,8 @@ class EEVariables():
ee_webroot = '/var/www/'
ee_php_user = 'www-data'
# Get git user name and EMail
try:
ee_user = config['user']['name']

Loading…
Cancel
Save