Beispiel #1
0
    def remove(self, ppa=None, repo_url=None):
        """
        This function used to remove ppa's
        If ppa is provided adds repo file to
            /etc/apt/sources.list.d/
        command.
        """
        if ppa:
            EEShellExec.cmd_exec(self, "add-apt-repository -y "
                                 "--remove '{ppa_name}'"
                                 .format(ppa_name=ppa))
        elif repo_url:
            repo_file_path = ("/etc/apt/sources.list.d/"
                              + EEVariables().ee_repo_file)

            try:
                repofile = open(repo_file_path, "w+")
                repofile.write(repofile.read().replace(repo_url, ""))
                repofile.close()
            except IOError as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "File I/O error.")
            except Exception as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "Unable to remove repo")
Beispiel #2
0
    def reload_service(self, service_name):
        """
            Stop service
            Similar to `service xyz stop`
        """
        try:
            if service_name in ['nginx', 'php5-fpm']:
                service_cmd = ('{0} -t && service {0} reload'
                               .format(service_name))
            else:
                service_cmd = ('service {0} reload'.format(service_name))

            Log.info(self, "Reload : {0:10}".format(service_name), end='')
            retcode = subprocess.getstatusoutput(service_cmd)
            if retcode[0] == 0:
                    Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
                    return True
            else:
                Log.debug(self, "{0}".format(retcode[1]))
                Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
                return False
        except OSError as e:
            Log.debug(self, "{0}".format(e))
            Log.error(self, "\nFailed to reload service {0}"
                      .format(service_name))
Beispiel #3
0
def operateOnPagespeed(self, data):

    ee_domain_name = data['site_name']
    ee_site_webroot = data['webroot']

    if data['pagespeed'] is True:
        if not os.path.isfile("{0}/conf/nginx/pagespeed.conf.disabled"
                              .format(ee_site_webroot)):
            Log.debug(self, 'Writting the Pagespeed common '
                      'configuration to file {0}/conf/nginx/pagespeed.conf'
                      'pagespeed.conf'.format(ee_site_webroot))
            ee_nginx = open('{0}/conf/nginx/pagespeed.conf'
                            .format(ee_site_webroot), encoding='utf-8',
                            mode='w')
            self.app.render((data), 'pagespeed-common.mustache',
                            out=ee_nginx)
            ee_nginx.close()
        else:
            EEFileUtils.mvfile(self, "{0}/conf/nginx/pagespeed.conf.disabled"
                               .format(ee_site_webroot),
                               '{0}/conf/nginx/pagespeed.conf'
                               .format(ee_site_webroot))

    elif data['pagespeed'] is False:
        if os.path.isfile("{0}/conf/nginx/pagespeed.conf"
                          .format(ee_site_webroot)):
            EEFileUtils.mvfile(self, "{0}/conf/nginx/pagespeed.conf"
                               .format(ee_site_webroot),
                               '{0}/conf/nginx/pagespeed.conf.disabled'
                               .format(ee_site_webroot))

    # Add nginx conf folder into GIT
    EEGit.add(self, ["{0}/conf/nginx".format(ee_site_webroot)],
              msg="Adding Pagespeed config of site: {0}"
              .format(ee_domain_name))
Beispiel #4
0
 def add(self, paths, msg="Intializating"):
     """
         Initializes Directory as repository if not already git repo.
         and adds uncommited changes automatically
     """
     for path in paths:
         global git
         git = git.bake("--git-dir={0}/.git".format(path),
                        "--work-tree={0}".format(path))
         if os.path.isdir(path):
             if not os.path.isdir(path+"/.git"):
                 try:
                     Log.debug(self, "EEGit: git init at {0}"
                               .format(path))
                     git.init(path)
                 except ErrorReturnCode as e:
                     Log.debug(self, "{0}".format(e))
                     Log.error(self, "Unable to git init at {0}"
                               .format(path))
             status = git.status("-s")
             if len(status.splitlines()) > 0:
                 try:
                     Log.debug(self, "EEGit: git commit at {0}"
                               .format(path))
                     git.add("--all")
                     git.commit("-am {0}".format(msg))
                 except ErrorReturnCode as e:
                     Log.debug(self, "{0}".format(e))
                     Log.error(self, "Unable to git commit at {0} "
                               .format(path))
         else:
             Log.debug(self, "EEGit: Path {0} not present".format(path))
def setwebrootpermissions(self, webroot):
    Log.debug(self, "Setting up permissions")
    try:
        EEFileUtils.chown(self, webroot, EEVariables.ee_php_user, EEVariables.ee_php_user, recursive=True)
    except Exception as e:
        Log.debug(self, str(e))
        raise SiteError("problem occured while settingup webroot permissions")
Beispiel #6
0
def site_package_check(self, stype):
    apt_packages = []
    packages = []
    stack = EEStackController()
    stack.app = self.app
    if stype in ['html', 'php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for Nginx")
        if not EEAptGet.is_installed(self, 'nginx-common'):
            apt_packages = apt_packages + EEVariables.ee_nginx

    if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for PHP")
        if not EEAptGet.is_installed(self, 'php5-fpm'):
            apt_packages = apt_packages + EEVariables.ee_php

    if stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for MySQL")
        if not EEShellExec.cmd_exec(self, "mysqladmin ping"):
            apt_packages = apt_packages + EEVariables.ee_mysql

    if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for Postfix")
        if not EEAptGet.is_installed(self, 'postfix'):
            apt_packages = apt_packages + EEVariables.ee_postfix

    if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting packages variable for WP-CLI")
        if not EEShellExec.cmd_exec(self, "which wp"):
            packages = packages + [["https://github.com/wp-cli/wp-cli/"
                                    "releases/download/v{0}/"
                                    "wp-cli-{0}.phar"
                                    .format(EEVariables.ee_wp_cli),
                                    "/usr/bin/wp", "WP-CLI"]]
    return(stack.install(apt_packages=apt_packages, packages=packages,
                         disp_msg=False))
def removeNginxConf(self, domain):
    if os.path.isfile("/etc/nginx/sites-available/{0}".format(domain)):
        Log.debug(self, "Removing Nginx configuration")
        EEFileUtils.rm(self, "/etc/nginx/sites-enabled/{0}".format(domain))
        EEFileUtils.rm(self, "/etc/nginx/sites-available/{0}".format(domain))
        EEService.reload_service(self, "nginx")
        EEGit.add(self, ["/etc/nginx"], msg="Deleted {0} ".format(domain))
Beispiel #8
0
def updateSiteInfo(self, site, stype="", cache="", enabled=True, ssl=False, fs="", db=""):
    """updates site record in database"""
    try:
        q = SiteDB.query.filter(SiteDB.sitename == site).first()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to query database for site info")

    if not q:
        Log.error(self, "{0} does not exist in database".format(site))

    # Check if new record matches old if not then only update database
    if stype and q.site_type != stype:
        q.site_type = stype

    if cache and q.cache_type != cache:
        q.cache_type = cache

    if q.is_enabled != enabled:
        q.is_enabled = enabled

    if ssl and q.is_ssl != ssl:
        q.is_ssl = ssl

    try:
        q.created_on = func.now()
        db_session.commit()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to update site info in application database.")
Beispiel #9
0
 def secure_auth(self):
     """This function Secures authentication"""
     passwd = ''.join([random.choice
                      (string.ascii_letters + string.digits)
                      for n in range(6)])
     if not self.app.pargs.user_input:
         username = input("Provide HTTP authentication user "
                          "name [{0}] :".format(EEVariables.ee_user))
         self.app.pargs.user_input = username
         if username == "":
             self.app.pargs.user_input = EEVariables.ee_user
     if not self.app.pargs.user_pass:
         password = getpass.getpass("Provide HTTP authentication "
                                    "password [{0}] :".format(passwd))
         self.app.pargs.user_pass = password
         if password == "":
             self.app.pargs.user_pass = passwd
     Log.debug(self, "printf username:"******"$(openssl passwd -crypt "
               "password 2> /dev/null)\n\""
               "> /etc/nginx/htpasswd-ee 2>/dev/null")
     EEShellExec.cmd_exec(self, "printf \"{username}:"
                          "$(openssl passwd -crypt "
                          "{password} 2> /dev/null)\n\""
                          "> /etc/nginx/htpasswd-ee 2>/dev/null"
                          .format(username=self.app.pargs.user_input,
                                  password=self.app.pargs.user_pass),
                          log=False)
     EEGit.add(self, ["/etc/nginx"],
               msg="Adding changed secure auth into Git")
def updatewpuserpassword(self, ee_domain, ee_site_webroot):

    ee_wp_user = ""
    ee_wp_pass = ""
    EEFileUtils.chdir(self, "{0}/htdocs/".format(ee_site_webroot))

    # Check if ee_domain is wordpress install
    try:
        is_wp = EEShellExec.cmd_exec(self, "wp --allow-root core" " version")
    except CommandExecutionError as e:
        raise SiteError("is wordpress site? check command failed ")

    # Exit if ee_domain is not wordpress install
    if not is_wp:
        Log.error(self, "{0} does not seem to be a WordPress site".format(ee_domain))

    try:
        ee_wp_user = input("Provide WordPress user name [admin]: ")
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "\nCould not update password")

    if ee_wp_user == "?":
        Log.info(self, "Fetching WordPress user list")
        try:
            EEShellExec.cmd_exec(self, "wp --allow-root user list " "--fields=user_login | grep -v user_login")
        except CommandExecutionError as e:
            raise SiteError("fetch wp userlist command failed")

    if not ee_wp_user:
        ee_wp_user = "******"

    try:
        is_user_exist = EEShellExec.cmd_exec(
            self, "wp --allow-root user list " "--fields=user_login | grep {0}$ ".format(ee_wp_user)
        )
    except CommandExecutionError as e:
        raise SiteError("if wp user exists check command failed")

    if is_user_exist:
        try:
            ee_wp_pass = getpass.getpass(prompt="Provide password for " "{0} user: "******"Provide password for " "{0} user: "******"{0}".format(e))
            raise SiteError("failed to read password input ")

        try:
            EEShellExec.cmd_exec(
                self, "wp --allow-root user update {0}" "  --user_pass={1}".format(ee_wp_user, ee_wp_pass)
            )
        except CommandExecutionError as e:
            raise SiteError("wp user password update command failed")
        Log.info(self, "Password updated successfully")

    else:
        Log.error(self, "Invalid WordPress user {0} for {1}.".format(ee_wp_user, ee_domain))
Beispiel #11
0
def getAllsites(self):

    try:
        q = SiteDB.query.all()
        return q
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to query database")
Beispiel #12
0
def uninstallwp_plugin(self, plugin_name, data):
    ee_site_webroot = data['webroot']
    Log.debug(self, "Uninstalling plugin {0}".format(plugin_name))
    EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
    EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin --allow-root uninstall "
                         "{0}".format(plugin_name),
                         errormsg="Unable to UnInstall plugin {0}"
                         .format(plugin_name))
Beispiel #13
0
 def invoke_editor(self, filepath, errormsg=''):
     """
         Open files using sensible editor
     """
     try:
         subprocess.call(['sensible-editor', filepath])
     except OSError as e:
             Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
             raise CommandExecutionError
Beispiel #14
0
def updateSiteInfo(self, site, stype='', cache='', webroot='',
                   enabled=True, ssl=False, fs='', db='', db_name=None,
                   db_user=None, db_password=None, db_host=None, hhvm=None,
                   pagespeed=None, php_version=''):
    """updates site record in database"""
    try:
        q = SiteDB.query.filter(SiteDB.sitename == site).first()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to query database for site info")

    if not q:
        Log.error(self, "{0} does not exist in database".format(site))

    # Check if new record matches old if not then only update database
    if stype and q.site_type != stype:
        q.site_type = stype

    if cache and q.cache_type != cache:
        q.cache_type = cache

    if q.is_enabled != enabled:
        q.is_enabled = enabled

    if q.is_ssl != ssl:
        q.is_ssl = ssl

    if db_name and q.db_name != db_name:
        q.db_name = db_name

    if db_user and q.db_user != db_user:
        q.db_user = db_user

    if db_user and q.db_password != db_password:
        q.db_password = db_password

    if db_host and q.db_host != db_host:
        q.db_host = db_host

    if webroot and q.site_path != webroot:
        q.site_path = webroot

    if (hhvm is not None) and (q.is_hhvm is not hhvm):
        q.is_hhvm = hhvm

    if (pagespeed is not None) and (q.is_pagespeed is not pagespeed):
        q.is_pagespeed = pagespeed

    if php_version and q.php_version != php_version:
        q.php_version = php_version

    try:
        q.created_on = func.now()
        db_session.commit()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to update site info in application database.")
Beispiel #15
0
 def clean_opcache(self):
     """This function clears opcache"""
     try:
         Log.info(self, "Cleaning opcache")
         wp = urllib.request.urlopen(" https://127.0.0.1:22222/cache"
                                     "/opcache/opgui.php?page=reset").read()
     except Exception as e:
             Log.debug(self, "{0}".format(e))
             Log.error(self, "Unable to clean OpCache")
Beispiel #16
0
 def clean_pagespeed(self):
     """This function clears Pagespeed cache"""
     if(os.path.isdir("/var/ngx_pagespeed_cache")):
         Log.info(self, "Cleaning PageSpeed cache")
         EEShellExec.cmd_exec(self, "rm -rf /var/ngx_pagespeed_cache/*")
     else:
         Log.debug(self, "/var/ngx_pagespeed_cache does not exist," 
                         " so cache not cleared")
         Log.error(self, "Unable to clean pagespeed cache", False)
Beispiel #17
0
def updatewpuserpassword(self, ee_domain, ee_site_webroot):

    ee_wp_user = ''
    ee_wp_pass = ''
    EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))

    # Check if ee_domain is wordpress install
    is_wp = EEShellExec.cmd_exec(self, "wp --allow-root core"
                                 " version",
                                 errormsg="{0} : Unable to check if wp install"
                                 .format(ee_domain))

    # Exit if ee_domain is not wordpress install
    if not is_wp:
        Log.error(self, "{0} does not seem to be a WordPress site"
                  .format(ee_domain))

    try:
        ee_wp_user = input("Provide WordPress user name [admin]: ")
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "\nCould not update password")

    if ee_wp_user == "?":
        Log.info(self, "Fetching WordPress user list")
        EEShellExec.cmd_exec(self, "wp --allow-root user list "
                             "--fields=user_login | grep -v user_login",
                             errormsg="Unable to Fetch users list")

    if not ee_wp_user:
        ee_wp_user = '******'

    is_user_exist = EEShellExec.cmd_exec(self, "wp --allow-root user list "
                                         "--fields=user_login | grep {0}$ "
                                         .format(ee_wp_user))

    if is_user_exist:
        try:
            ee_wp_pass = getpass.getpass(prompt="Provide password for "
                                         "{0} user: "******"{0}".format(e))
            Log.error(self, "Could not update password")

        if len(ee_wp_pass) > 8:
            EEShellExec.cmd_exec(self, "wp --allow-root user update {0}"
                                 "  --user_pass={1}"
                                 .format(ee_wp_user, ee_wp_pass))
            Log.info(self, "Password updated successfully")
        else:
            Log.error(self, "Password Unchanged. Hint : Your password must be "
                      "8 characters long")
    else:
        Log.error(self, "Invalid WordPress user {0} for {1}."
                  .format(ee_wp_user, ee_domain))
Beispiel #18
0
def getAllsites(self):
    """
        1. returns all records from ee database
    """
    try:
        q = SiteDB.query.all()
        return q
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to query database")
Beispiel #19
0
 def auto_remove(self):
     """
     Similar to `apt-get autoremove`
     """
     try:
         Log.debug(self, "Running apt-get autoremove")
         apt_get.autoremove("-y")
     except ErrorReturnCode as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "Unable to apt-get autoremove")
Beispiel #20
0
 def remove_symlink(self, filepath):
     """
         Removes symbolic link for the path provided with filepath
     """
     try:
         Log.debug(self, "Removing symbolic link: {0}".format(filepath))
         os.unlink(filepath)
     except Exception as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "Unable to reomove symbolic link ...\n")
Beispiel #21
0
 def check_db_exists(self, db_name):
     try:
         if EEMysql.dbConnection(self, db_name):
             return True
     except DatabaseNotExistsError as e:
         Log.debug(self, str(e))
         return False
     except MySQLConnectionError as e:
         Log.debug(self, str(e))
         raise MySQLConnectionError
Beispiel #22
0
def getSiteInfo(self, site):
    """
        Retrieves site record from ee databse
    """
    try:
        q = SiteDB.query.filter(SiteDB.sitename == site).first()
        return q
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to query database for site info")
Beispiel #23
0
 def remove_cron(self,cmd):
     if EEShellExec.cmd_exec(self, "crontab -l | grep -q \'{0}\'".format(cmd)):
         if not EEShellExec.cmd_exec(self, "/bin/bash -c "
                                                 "\"crontab "
                                                 "-l | sed '/{0}/d'"
                                                 "| crontab -\""
                                                 .format(cmd)):
             Log.error(self, "Failed to remove crontab entry",False)
     else:
         Log.debug(self, "Cron not found")
Beispiel #24
0
    def cmd_exec_stdout(self, command, errormsg='', log=True):
        """Run shell command from Python"""
        try:
            log and Log.debug(self, "Running command: {0}".format(command))

            with subprocess.Popen([command], stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE, shell=True) as proc:
                (cmd_stdout_bytes, cmd_stderr_bytes) = proc.communicate()
                (cmd_stdout, cmd_stderr) = (cmd_stdout_bytes.decode('utf-8',
                                            "replace"),
                                            cmd_stderr_bytes.decode('utf-8',
                                            "replace"))

            if proc.returncode == 0:
                Log.debug(self, "Command Output: {0}, \nCommand Error: {1}"
                                .format(cmd_stdout, cmd_stderr))
                return cmd_stdout
            else:
                Log.debug(self, "Command Output: {0}, \nCommand Error: {1}"
                                .format(cmd_stdout, cmd_stderr))
                return cmd_stdout
        except OSError as e:
                Log.debug(self, str(e))
                raise CommandExecutionError
        except Exception as e:
                Log.debug(self, str(e))
                raise CommandExecutionError
Beispiel #25
0
 def install(self, packages):
     global apt_get
     apt_get = apt_get.bake("-y")
     try:
         for line in apt_get.install("-o",
                                     "Dpkg::Options::=--force-confold",
                                     *packages, _iter=True):
             Log.info(self, Log.ENDC+line+Log.OKBLUE, end=' ')
     except ErrorReturnCode as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "Unable to run apt-get install")
Beispiel #26
0
def addNewSite(self, site, stype, cache, path, enabled=True, ssl=False, fs="ext4", db="mysql"):
    """
    Add New Site record information into ee database.
    """
    try:
        newRec = SiteDB(site, stype, cache, path, enabled, ssl, fs, db)
        db_session.add(newRec)
        db_session.commit()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to add site to database")
Beispiel #27
0
    def setcron_weekly(self,cmd,comment='Cron set by EasyEngine',user='******',min=0,hour=12):
        if not EEShellExec.cmd_exec(self, "crontab -l | grep -q \'{0}\'".format(cmd)):

            EEShellExec.cmd_exec(self, "/bin/bash -c \"crontab -l "
                                             "2> /dev/null | {{ cat; echo -e"
                                             " \\\""
                                             "\\n0 0 * * 0 "
                                             "{0}".format(cmd) +
                                             " # {0}".format(comment)+
                                             "\\\"; } | crontab -\"")
            Log.debug(self, "Cron set")
Beispiel #28
0
def uninstallwp_plugin(self, plugin_name, data):
    ee_site_webroot = data['webroot']
    Log.debug(self, "Uninstalling plugin {0}, please wait..."
              .format(plugin_name))
    EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))
    try:
        EEShellExec.cmd_exec(self, "php /usr/bin/wp plugin "
                             "--allow-root uninstall "
                             "{0}".format(plugin_name))
    except CommandExecutionError as e:
        raise SiteError("plugin uninstall failed")
Beispiel #29
0
 def connect(self):
     """Makes connection with MySQL server"""
     try:
         connection = pymysql.connect(read_default_file='~/.my.cnf')
         return connection
     except ValueError as e:
         Log.debug(self, str(e))
         raise MySQLConnectionError
     except pymysql.err.InternalError as e:
         Log.debug(self, str(e))
         raise MySQLConnectionError
Beispiel #30
0
 def clean_memcache(self):
     """This function Clears memcache """
     try:
         if(EEAptGet.is_installed(self, "memcached")):
             EEService.restart_service(self, "memcached")
             Log.info(self, "Cleaning MemCache")
         else:
             Log.info(self, "Memcache not installed")
     except Exception as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "Unable to restart Memcached", False)
Beispiel #31
0
    def execute(self, statement, errormsg='', log=True):
        """Get login details from /etc/mysql/conf.d/my.cnf & Execute MySQL query"""
        connection = EEMysql.connect(self)
        log and Log.debug(self,
                          "Exceuting MySQL Statement : {0}".format(statement))
        try:
            cursor = connection.cursor()
            sql = statement
            cursor.execute(sql)

            # connection is not autocommit by default.
            # So you must commit to save your changes.
            connection.commit()
        except AttributeError as e:
            Log.debug(self, str(e))
            raise StatementExcecutionError
        except Error as e:
            Log.debug(self, str(e))
            raise StatementExcecutionError
        finally:
            connection.close()
Beispiel #32
0
    def dbConnection(self, db_name):
        try:
            if os.path.exists('/etc/mysql/conf.d/my.cnf'):
                connection = pymysql.connect(
                    db=db_name, read_default_file='/etc/mysql/conf.d/my.cnf')
            else:
                connection = pymysql.connect(db=db_name,
                                             read_default_file='~/.my.cnf')

            return connection
        except DatabaseError as e:
            if e.args[1] == '#42000Unknown database \'{0}\''.format(db_name):
                raise DatabaseNotExistsError
            else:
                raise MySQLConnectionError
        except pymysql.err.InternalError as e:
            Log.debug(self, str(e))
            raise MySQLConnectionError
        except Exception as e:
            Log.debug(self, "[Error]Setting up database: \'" + str(e) + "\'")
            raise MySQLConnectionError
Beispiel #33
0
 def chmod(self, path, perm, recursive=False):
     """
         Changes Permission for files
         path : file path permission to be changed
         perm : permissions to be given
         recursive: change permission recursively for all files
     """
     try:
         Log.debug(self, "Changing permission of {0}, Perm:{1}"
                   .format(path, perm))
         if recursive:
             for root, dirs, files in os.walk(path):
                 for d in dirs:
                     os.chmod(os.path.join(root, d), perm)
                 for f in files:
                     os.chmod(os.path.join(root, f), perm)
         else:
             os.chmod(path, perm)
     except OSError as e:
         Log.debug(self, "{0}".format(e.strerror))
         Log.error(self, "Unable to change owner : {0}".format(path))
Beispiel #34
0
 def install_package(self, package_name):
     pkg = apt_cache[package_name.strip()]
     if package_name.strip() in apt_cache:
         if pkg.is_installed:
             # apt_pkg.PkgSystemUnLock()
             Log.debug(self, 'Trying to install a package that '
                       'is already installed (' +
                       package_name.strip() + ')')
             # apt_cache.close()
             return False
         else:
             try:
                 # print(pkg.name)
                 pkg.mark_install()
             except Exception as e:
                 Log.debug(self, str(e))
                 Log.error(self, str(e))
     else:
         # apt_cache.close()
         Log.error(self, 'Unknown package selected (' +
                   package_name.strip() + ')')
Beispiel #35
0
 def download(self, packages):
     """Download packages, packges must be list in format of
     [url, path, package name]"""
     for package in packages:
         url = package[0]
         filename = package[1]
         pkg_name = package[2]
         try:
             directory = os.path.dirname(filename)
             if not os.path.exists(directory):
                 os.makedirs(directory)
             Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ')
             req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
             with urllib.request.urlopen(req) as response, open(filename, 'wb') as out_file:
                 out_file.write(response.read())
             Log.info(self, "{0}".format("[" + Log.ENDC + "Done"
                                         + Log.OKBLUE + "]"))
         except urllib.error.URLError as e:
             Log.debug(self, "[{err}]".format(err=str(e.reason)))
             Log.error(self, "Unable to download file, {0}"
                       .format(filename))
             return False
         except urllib.error.HTTPError as e:
             Log.error(self, "Package download failed. {0}"
                       .format(pkg_name))
             Log.debug(self, "[{err}]".format(err=str(e.reason)))
             return False
         except urllib.error.ContentTooShortError as e:
             Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
             Log.error(self, "Package download failed. The amount of the"
                       " downloaded data is less than "
                       "the expected amount \{0} ".format(pkg_name))
             return False
Beispiel #36
0
 def chown(self, path, user, group, recursive=False):
     """
         Change Owner for files
         change owner for file with path specified
         user: username of owner
         group: group of owner
         recursive: if recursive is True change owner for all
                    files in directory
     """
     userid = pwd.getpwnam(user)[2]
     groupid = pwd.getpwnam(user)[3]
     try:
         Log.debug(
             self,
             "Changing ownership of {0}, Userid:{1},Groupid:{2}".format(
                 path, userid, groupid))
         # Change inside files/directory permissions only if recursive flag
         # is set
         if recursive:
             for root, dirs, files in os.walk(path):
                 for d in dirs:
                     os.chown(os.path.join(root, d), userid, groupid)
                 for f in files:
                     os.chown(os.path.join(root, f), userid, groupid)
         os.chown(path, userid, groupid)
     except shutil.Error as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "Unable to change owner : {0}".format(path))
     except Exception as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "Unable to change owner : {0} ".format(path))
Beispiel #37
0
    def debug_php(self):
        """Start/Stop PHP debug"""
        # PHP global debug start
        if self.start:
            if not (EEShellExec.cmd_exec(self, "sed -n \"/upstream php"
                                               "{/,/}/p \" /etc/nginx/"
                                               "conf.d/upstream.conf "
                                               "| grep 9001")):
                Log.info(self, "Enabling PHP debug")
                data = dict(php="9001", debug="9001")
                Log.info(self, 'Writting the Nginx debug configration to file '
                         '/etc/nginx/conf.d/upstream.conf ')
                ee_nginx = open('/etc/nginx/conf.d/upstream.conf',
                                encoding='utf-8', mode='w')
                self.app.render((data), 'upstream.mustache', out=ee_nginx)
                ee_nginx.close()
                self.trigger_php = True
                self.trigger_nginx = True
            else:
                Log.info(self, "PHP debug is already enabled")

            self.msg = self.msg + ['/var/log/php5/slow.log']

        # PHP global debug stop
        else:
            if EEShellExec.cmd_exec(self, "sed -n \"/upstream php {/,/}/p\" "
                                          "/etc/nginx/conf.d/upstream.conf "
                                          "| grep 9001"):
                Log.info(self, "Disabling PHP debug")
                data = dict(php="9000", debug="9001")
                Log.debug(self, 'Writting the Nginx debug configration to file'
                          ' /etc/nginx/conf.d/upstream.conf ')
                ee_nginx = open('/etc/nginx/conf.d/upstream.conf',
                                encoding='utf-8', mode='w')
                self.app.render((data), 'upstream.mustache', out=ee_nginx)
                ee_nginx.close()
                self.trigger_php = True
                self.trigger_nginx = True
            else:
                Log.info(self, "PHP debug is already disabled")
Beispiel #38
0
 def download(self, packages):
     """Download packages, packges must be list in format of
     [url, path, package name]"""
     for package in packages:
         url = package[0]
         filename = package[1]
         pkg_name = package[2]
         try:
             directory = os.path.dirname(filename)
             if not os.path.exists(directory):
                 os.makedirs(directory)
             Log.info(self, "Downloading {0:20}".format(pkg_name), end=' ')
             urllib.request.urlretrieve(url, filename)
             Log.info(
                 self,
                 "{0}".format("[" + Log.ENDC + "Done" + Log.OKBLUE + "]"))
         except urllib.error.URLError as e:
             Log.debug(self, "[{err}]".format(err=str(e.reason)))
             Log.error(self,
                       "Unable to download file, {0}".format(filename))
             return False
         except urllib.error.HTTPError as e:
             Log.error(self,
                       "Package download failed. {0}".format(pkg_name))
             Log.debug(self, "[{err}]".format(err=str(e.reason)))
             return False
         except urllib.error.ContentTooShortError as e:
             Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
             Log.error(
                 self, "Package download failed. The amount of the"
                 " downloaded data is less than "
                 "the expected amount \{0} ".format(pkg_name))
             return False
Beispiel #39
0
    def execute(self, statement, errormsg='', log=True):
        """Get login details from ~/.my.cnf & Execute MySQL query"""
        config = configparser.RawConfigParser()
        cnfpath = expanduser("~") + "/.my.cnf"
        if [cnfpath] == config.read(cnfpath):
            user = config.get('client', 'user')
            passwd = config.get('client', 'password')
            try:
                host = config.get('client', 'host')
            except configparser.NoOptionError as e:
                host = 'localhost'

            try:
                port = config.get('client', 'port')
            except configparser.NoOptionError as e:
                port = '3306'

            try:
                conn = pymysql.connect(host=host,
                                       port=int(port),
                                       user=user,
                                       passwd=passwd)
                cur = conn.cursor()
            except Exception as e:
                if errormsg:
                    Log.debug(self, '{0}'.format(e))
                    Log.error(self, '{0}'.format(errormsg))
                else:
                    Log.debug(self, '{0}'.format(e))
                    Log.error(self,
                              'Unable to connect to database: {0}'.format(e))

            try:
                if log:
                    Log.debug(
                        self,
                        "Executing MySQL statement: {0}".format(statement))

                cur.execute(statement)
                cur.close()
                conn.close()

            except Exception as e:
                cur.close()
                conn.close()
                Log.debug(self, "{0}".format(e))
                if not errormsg:
                    Log.error(self, 'Unable to execute statement')
                else:
                    Log.error(self, '{0}'.format(errormsg))
Beispiel #40
0
 def update(self):
     """
     Similar to `apt-get upgrade`
     """
     try:
         apt_cache = apt.cache.Cache()
         import sys
         orig_out = sys.stdout
         sys.stdout = open(self.app.config.get('log.logging', 'file'),
                           encoding='utf-8', mode='a')
         apt_cache.update(apt.progress.text.AcquireProgress())
         sys.stdout = orig_out
         # success = (apt_cache.commit(
         #            apt.progress.text.AcquireProgress(),
         #            apt.progress.base.InstallProgress()))
         # #apt_cache.close()
         # return success
     except AttributeError as e:
         Log.error(self, 'AttributeError: ' + str(e))
     except Exception as e:
         Log.debug(self, 'SystemError:  ' + str(e))
         Log.error(self, 'Unable to Fetch update')
Beispiel #41
0
    def backupAll(self):
        import subprocess
        try:
            Log.info(self, "Backing up database at location: "
                     "/var/ee-mysqlbackup")
            # Setup Nginx common directory
            if not os.path.exists('/var/ee-mysqlbackup'):
                Log.debug(self, 'Creating directory' '/var/ee-mysqlbackup')
                os.makedirs('/var/ee-mysqlbackup')

            db = subprocess.check_output(["mysql -Bse \'show databases\'"],
                                         universal_newlines=True,
                                         shell=True).split('\n')
            for dbs in db:
                if dbs == "":
                    continue
                Log.info(self, "Backing up {0} database".format(dbs))
                p1 = subprocess.Popen("mysqldump {0}"
                                      " --max_allowed_packet=1024M"
                                      " --single-transaction".format(dbs),
                                      stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE,
                                      shell=True)
                p2 = subprocess.Popen("gzip -c > /var/ee-mysqlbackup/{0}{1}.s"
                                      "ql.gz".format(dbs, EEVariables.ee_date),
                                      stdin=p1.stdout,
                                      shell=True)

                # Allow p1 to receive a SIGPIPE if p2 exits
                p1.stdout.close()
                output = p1.stderr.read()
                p1.wait()
                if p1.returncode == 0:
                    Log.debug(self, "done")
                else:
                    Log.error(self, output.decode("utf-8"))
        except Exception as e:
            Log.error(self, "Error: process exited with status %s" % e)
Beispiel #42
0
    def get_service_status(self, service_name):

        try:
            is_exist = subprocess.getstatusoutput(
                'which {0}'.format(service_name))
            if is_exist[0] == 0 or service_name in [
                    'php7.2-fpm', 'php5.6-fpm'
            ]:
                retcode = subprocess.getstatusoutput(
                    'service {0} status'.format(service_name))
                if retcode[0] == 0:
                    return True
                else:
                    Log.debug(self, "{0}".format(retcode[1]))
                    return False
            else:
                return False
        except OSError as e:
            Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
            Log.error(
                self,
                "Unable to get services status of {0}".format(service_name))
            return False
Beispiel #43
0
    def add(self, repo_url=None, ppa=None):
        """
        This function used to add apt repositories and or ppa's
        If repo_url is provided adds repo file to
            /etc/apt/sources.list.d/
        If ppa is provided add apt-repository using
            add-apt-repository
        command.
        """

        if repo_url is not None:
            repo_file_path = ("/etc/apt/sources.list.d/"
                              + EEVariables().ee_repo_file)
            try:
                if not os.path.isfile(repo_file_path):
                    with open(repo_file_path,
                              encoding='utf-8', mode='a') as repofile:
                        repofile.write(repo_url)
                        repofile.write('\n')
                        repofile.close()
                elif repo_url not in open(repo_file_path,
                                          encoding='utf-8').read():
                    with open(repo_file_path,
                              encoding='utf-8', mode='a') as repofile:
                        repofile.write(repo_url)
                        repofile.write('\n')
                        repofile.close()
                return True
            except IOError as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "File I/O error.")
            except Exception as e:
                Log.debug(self, "{0}".format(e))
                Log.error(self, "Unable to add repo")
        if ppa is not None:
            EEShellExec.cmd_exec(self, "add-apt-repository -y '{ppa_name}'"
                                 .format(ppa_name=ppa))
Beispiel #44
0
def site_package_check(self, stype):
    apt_packages = []
    packages = []
    stack = EEStackController()
    stack.app = self.app
    if stype in ['html', 'php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for Nginx")
        if not EEAptGet.is_installed(self, 'nginx-common'):
            apt_packages = apt_packages + EEVariables.ee_nginx

    if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for PHP")
        if not EEAptGet.is_installed(self, 'php5-fpm'):
            apt_packages = apt_packages + EEVariables.ee_php

    if stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for MySQL")
        if not EEShellExec.cmd_exec(self, "mysqladmin ping"):
            apt_packages = apt_packages + EEVariables.ee_mysql

    if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for Postfix")
        if not EEAptGet.is_installed(self, 'postfix'):
            apt_packages = apt_packages + EEVariables.ee_postfix

    if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting packages variable for WP-CLI")
        if not EEShellExec.cmd_exec(self, "which wp"):
            packages = packages + [[
                "https://github.com/wp-cli/wp-cli/"
                "releases/download/v{0}/"
                "wp-cli-{0}.phar".format(EEVariables.ee_wp_cli), "/usr/bin/wp",
                "WP-CLI"
            ]]
    return (stack.install(apt_packages=apt_packages,
                          packages=packages,
                          disp_msg=False))
Beispiel #45
0
def updateSiteInfo(self,
                   site,
                   stype='',
                   cache='',
                   enabled=True,
                   ssl=False,
                   fs='',
                   db=''):
    """updates site record in database"""
    try:
        q = SiteDB.query.filter(SiteDB.sitename == site).first()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to query database for site info")

    if not q:
        Log.error(self, "{0} does not exist in database".format(site))

    # Check if new record matches old if not then only update database
    if stype and q.site_type != stype:
        q.site_type = stype

    if cache and q.cache_type != cache:
        q.cache_type = cache

    if q.is_enabled != enabled:
        q.is_enabled = enabled

    if ssl and q.is_ssl != ssl:
        q.is_ssl = ssl

    try:
        q.created_on = func.now()
        db_session.commit()
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "Unable to update site info in application database.")
Beispiel #46
0
    def sync(self):
        """
        1. reads database information from wp/ee-config.php
        2. updates records into ee database accordingly.
        """
        Log.info(self, "Synchronizing ee database, please wait...")
        sites = getAllsites(self)
        if not sites:
            pass
        for site in sites:
            if site.site_type in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
                ee_site_webroot = site.site_path
                # Read config files
                configfiles = glob.glob(ee_site_webroot + '/*-config.php')
                if configfiles:
                    if EEFileUtils.isexist(self, configfiles[0]):
                        ee_db_name = (EEFileUtils.grep(self, configfiles[0],
                                                       'DB_NAME').split(',')
                                      [1].split(')')[0].strip().replace(
                                          '\'', ''))
                        ee_db_user = (EEFileUtils.grep(self, configfiles[0],
                                                       'DB_USER').split(',')
                                      [1].split(')')[0].strip().replace(
                                          '\'', ''))
                        ee_db_pass = (EEFileUtils.grep(
                            self, configfiles[0],
                            'DB_PASSWORD').split(',')[1].split(')')
                                      [0].strip().replace('\'', ''))
                        ee_db_host = (EEFileUtils.grep(self, configfiles[0],
                                                       'DB_HOST').split(',')
                                      [1].split(')')[0].strip().replace(
                                          '\'', ''))

                        # Check if database really exist
                        try:
                            if not EEMysql.check_db_exists(self, ee_db_name):
                                # Mark it as deleted if not exist
                                ee_db_name = 'deleted'
                                ee_db_user = '******'
                                ee_db_pass = '******'
                        except StatementExcecutionError as e:
                            Log.debug(self, str(e))
                        except Exception as e:
                            Log.debug(self, str(e))

                        if site.db_name != ee_db_name:
                            # update records if any mismatch found
                            Log.debug(
                                self, "Updating ee db record for {0}".format(
                                    site.sitename))
                            updateSiteInfo(self,
                                           site.sitename,
                                           db_name=ee_db_name,
                                           db_user=ee_db_user,
                                           db_password=ee_db_pass,
                                           db_host=ee_db_host)
Beispiel #47
0
def operateOnPagespeed(self, data):

    ee_domain_name = data['site_name']
    ee_site_webroot = data['webroot']

    if data['pagespeed'] is True:
        if not os.path.isfile("{0}/conf/nginx/pagespeed.conf.disabled".format(
                ee_site_webroot)):
            Log.debug(
                self, 'Writting the Pagespeed common '
                'configuration to file {0}/conf/nginx/pagespeed.conf'
                'pagespeed.conf'.format(ee_site_webroot))
            ee_nginx = open(
                '{0}/conf/nginx/pagespeed.conf'.format(ee_site_webroot),
                encoding='utf-8',
                mode='w')
            self.app.render((data), 'pagespeed-common.mustache', out=ee_nginx)
            ee_nginx.close()
        else:
            EEFileUtils.mvfile(
                self, "{0}/conf/nginx/pagespeed.conf.disabled".format(
                    ee_site_webroot),
                '{0}/conf/nginx/pagespeed.conf'.format(ee_site_webroot))

    elif data['pagespeed'] is False:
        if os.path.isfile(
                "{0}/conf/nginx/pagespeed.conf".format(ee_site_webroot)):
            EEFileUtils.mvfile(
                self, "{0}/conf/nginx/pagespeed.conf".format(ee_site_webroot),
                '{0}/conf/nginx/pagespeed.conf.disabled'.format(
                    ee_site_webroot))

    # Add nginx conf folder into GIT
    EEGit.add(
        self, ["{0}/conf/nginx".format(ee_site_webroot)],
        msg="Adding Pagespeed config of site: {0}".format(ee_domain_name))
Beispiel #48
0
def setupdomain(self, data):

    ee_domain_name = data['site_name']
    ee_site_webroot = data['webroot']
    Log.info(self, "Setting up NGINX configuration \t", end='')
    # write nginx config for file
    try:
        ee_site_nginx_conf = open(
            '/etc/nginx/sites-available/{0}'.format(ee_domain_name),
            encoding='utf-8',
            mode='w')

        self.app.render((data), 'virtualconf.mustache', out=ee_site_nginx_conf)
        ee_site_nginx_conf.close()
    except IOError as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "\nUnable to create NGINX configuration")
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "\nUnable to create NGINX configuration")
    Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")

    # create symbolic link for
    EEFileUtils.create_symlink(self, [
        '/etc/nginx/sites-available/{0}'.format(ee_domain_name),
        '/etc/nginx/sites-enabled/{0}'.format(ee_domain_name)
    ])

    # Creating htdocs & logs directory
    Log.info(self, "Setting up webroot \t\t", end='')
    try:
        if not os.path.exists('{0}/htdocs'.format(ee_site_webroot)):
            os.makedirs('{0}/htdocs'.format(ee_site_webroot))
        if not os.path.exists('{0}/logs'.format(ee_site_webroot)):
            os.makedirs('{0}/logs'.format(ee_site_webroot))
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "\nUnable to setup webroot")

    EEFileUtils.create_symlink(self, [
        '/var/log/nginx/{0}.access.log'.format(ee_domain_name),
        '{0}/logs/access.log'.format(ee_site_webroot)
    ])
    EEFileUtils.create_symlink(self, [
        '/var/log/nginx/{0}.error.log'.format(ee_domain_name),
        '{0}/logs/error.log'.format(ee_site_webroot)
    ])
    Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
Beispiel #49
0
def deleteWebRoot(self, webroot):
    # do some preprocessing before proceeding
    webroot = webroot.strip()
    if (webroot == "/var/www/" or webroot == "/var/www"
       or webroot == "/var/www/.." or webroot == "/var/www/."):
        Log.debug(self, "Tried to remove {0}, but didn't remove it"
                  .format(webroot))
        return False

    if os.path.isdir(webroot):
        Log.debug(self, "Removing {0}".format(webroot))
        EEFileUtils.rm(self, webroot)
        return True
    else:
        Log.debug(self, "{0} does not exist".format(webroot))
        return False
Beispiel #50
0
 def clean_opcache(self):
     """This function clears opcache"""
     try:
         Log.info(self, "Cleaning opcache")
         wp = urllib.request.urlopen(
             " https://127.0.0.1:22222/cache"
             "/opcache/opgui.php?page=reset").read()
     except Exception as e:
         Log.debug(self, "{0}".format(e))
         Log.debug(
             self, "Unable hit url, "
             " https://127.0.0.1:22222/cache/opcache/opgui.php?page=reset,"
             " please check you have admin tools installed")
         Log.debug(
             self, "please check you have admin tools installed,"
             " or install them with `ee stack install --admin`")
         Log.error(self, "Unable to clean opcache", False)
Beispiel #51
0
 def create_symlink(self, paths, errormsg=''):
     """
     Create symbolic links provided in list with first as source
     and second as destination
     """
     src = paths[0]
     dst = paths[1]
     if not os.path.islink(dst):
         try:
             Log.debug(self, "Creating Symbolic link, Source:{0}, Dest:{1}"
                       .format(src, dst))
             os.symlink(src, dst)
         except Exception as e:
             Log.debug(self, "{0}{1}".format(e.errno, e.strerror))
             Log.error(self, "Unable to create symbolic link ...\n ")
     else:
         Log.debug(self, "Destination: {0} exists".format(dst))
Beispiel #52
0
 def rm(self, path):
     """
         Remove files
     """
     Log.debug(self, "Removing {0}".format(path))
     if EEFileUtils.isexist(self, path):
         try:
             if os.path.isdir(path):
                 shutil.rmtree(path)
             else:
                 os.remove(path)
         except shutil.Error as e:
             Log.debug(self, "{0}".format(e))
             Log.error(self,
                       "Unable to remove directory : {0} ".format(path))
         except OSError as e:
             Log.debug(self, "{0}".format(e))
             Log.error(self, "Unable to remove file  : {0} ".format(path))
Beispiel #53
0
 def copyfile(self, src, dest):
     """
     Copy file:
         src : source path
         dest : destination path
     """
     try:
         Log.debug(self,
                   "Copying file, Source:{0}, Dest:{1}".format(src, dest))
         shutil.copy2(src, dest)
     except shutil.Error as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self,
                   'Unable to copy file from {0} to {1}'.format(src, dest))
     except IOError as e:
         Log.debug(self, "{0}".format(e.strerror))
         Log.error(self,
                   "Unable to copy file from {0} to {1}".format(src, dest))
Beispiel #54
0
    def copyfiles(self, src, dest):
        """
        Copies files:
            src : source path
            dest : destination path

            Recursively copy an entire directory tree rooted at src.
            The destination directory, named by dst, must not already exist;
            it will be created as well as missing parent directories.
        """
        try:
            Log.debug(self,
                      "Copying files, Source:{0}, Dest:{1}".format(src, dest))
            shutil.copytree(src, dest)
        except shutil.Error as e:
            Log.debug(self, "{0}".format(e))
            Log.error(self,
                      'Unable to copy files from {0} to {1}'.format(src, dest))
        except IOError as e:
            Log.debug(self, "{0}".format(e.strerror))
            Log.error(self,
                      "Unable to copy files from {0} to {1}".format(src, dest))
Beispiel #55
0
 def reload_service(self, service_name):
     """
         Stop service
         Similar to `service xyz stop`
     """
     try:
         if service_name in ['nginx', 'php5-fpm']:
             Log.info(self, "Reload : {0:10}".format(service_name), end='')
             retcode = subprocess.getstatusoutput(
                 '{0} -t &&'
                 ' service {0} reload'.format(service_name))
             if retcode[0] == 0:
                 # print(retcode[0])
                 # subprocess.getstatusoutput('service {0} reload'
                 #                            .format(service_name))
                 Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
                 return True
             else:
                 Log.debug(self, "{0}".format(retcode[1]))
                 Log.info(self,
                          "[" + Log.FAIL + "Failed" + Log.OKBLUE + "]")
                 return False
         Log.info(self, "Reload : {0:10}".format(service_name), end='')
         retcode = subprocess.getstatusoutput(
             'service {0} reload'.format(service_name))
         if retcode[0] == 0:
             Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
             return True
         else:
             Log.debug(self, "{0}".format(retcode[1]))
             Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE + "]")
             return False
     except OSError as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self,
                   "\nFailed to reload service {0}".format(service_name))
    def migrate_mariadb(self):
        # Backup all database
        EEMysql.backupAll(self)

        # Add MariaDB repo
        Log.info(self, "Adding repository for MariaDB, please wait...")

        mysql_pref = ("Package: *\nPin: origin sfo1.mirrors.digitalocean.com"
                      "\nPin-Priority: 1000\n")
        with open('/etc/apt/preferences.d/'
                  'MariaDB.pref', 'w') as mysql_pref_file:
            mysql_pref_file.write(mysql_pref)

        EERepo.add(self, repo_url=EEVariables.ee_mysql_repo)
        Log.debug(self, 'Adding key for {0}'.format(EEVariables.ee_mysql_repo))
        EERepo.add_key(self,
                       '0xF1656F24C74CD1D8',
                       keyserver="keyserver.ubuntu.com")

        config = configparser.ConfigParser()
        if os.path.exists('/etc/mysql/conf.d/my.cnf'):
            config.read('/etc/mysql/conf.d/my.cnf')
        else:
            config.read(os.path.expanduser("~") + '/.my.cnf')

        try:
            chars = config['client']['password']
        except Exception as e:
            Log.error(self, "Error: process exited with error %s" % e)

        Log.debug(self, "Pre-seeding MariaDB")
        Log.debug(
            self, "echo \"mariadb-server-10.0 "
            "mysql-server/root_password "
            "password \" | "
            "debconf-set-selections")
        EEShellExec.cmd_exec(self,
                             "echo \"mariadb-server-10.0 "
                             "mysql-server/root_password "
                             "password {chars}\" | "
                             "debconf-set-selections".format(chars=chars),
                             log=False)
        Log.debug(
            self, "echo \"mariadb-server-10.0 "
            "mysql-server/root_password_again "
            "password \" | "
            "debconf-set-selections")
        EEShellExec.cmd_exec(self,
                             "echo \"mariadb-server-10.0 "
                             "mysql-server/root_password_again "
                             "password {chars}\" | "
                             "debconf-set-selections".format(chars=chars),
                             log=False)

        # Install MariaDB
        apt_packages = EEVariables.ee_mysql

        # If PHP is installed then install php5-mysql
        if EEAptGet.is_installed(self, "php5-fpm"):
            apt_packages = apt_packages + ["php5-mysql"]

        # If mail server is installed then install dovecot-sql and postfix-sql
        if EEAptGet.is_installed(self, "dovecot-core"):
            apt_packages = apt_packages + [
                "dovecot-mysql", "postfix-mysql", "libclass-dbi-mysql-perl"
            ]

        Log.info(self, "Updating apt-cache, please wait...")
        EEAptGet.update(self)
        Log.info(self, "Installing MariaDB, please wait...")
        EEAptGet.remove(self, ["mysql-common", "libmysqlclient18"])
        EEAptGet.auto_remove(self)
        EEAptGet.install(self, apt_packages)

        # Restart  dovecot and postfix if installed
        if EEAptGet.is_installed(self, "dovecot-core"):
            EEService.restart_service(self, 'dovecot')
            EEService.restart_service(self, 'postfix')
Beispiel #57
0
def deleteDB(self, dbname, dbuser, dbhost):
    try:
        # Check if Database exists
        try:
            if EEMysql.check_db_exists(self, dbname):
                # Drop database if exists
                Log.debug(self, "dropping database `{0}`".format(dbname))
                EEMysql.execute(self,
                                "drop database `{0}`".format(dbname),
                                errormsg='Unable to drop database {0}'
                                .format(dbname))
        except StatementExcecutionError as e:
            Log.debug(self, "drop database failed")
            Log.info(self, "Database {0} not dropped".format(dbname))

        except MySQLConnectionError as e:
            Log.debug(self, "Mysql Connection problem occured")

        if dbuser != 'root':
            Log.debug(self, "dropping user `{0}`".format(dbuser))
            try:
                EEMysql.execute(self,
                                "drop user `{0}`@`{1}`"
                                .format(dbuser, dbhost))
            except StatementExcecutionError as e:
                Log.debug(self, "drop database user failed")
                Log.info(self, "Database {0} not dropped".format(dbuser))
            try:
                EEMysql.execute(self, "flush privileges")
            except StatementExcecutionError as e:
                Log.debug(self, "drop database failed")
                Log.info(self, "Database {0} not dropped".format(dbname))
    except Exception as e:
        Log.error(self, "Error occured while deleting database")
Beispiel #58
0
def updatewpuserpassword(self, ee_domain, ee_site_webroot):

    ee_wp_user = ''
    ee_wp_pass = ''
    EEFileUtils.chdir(self, '{0}/htdocs/'.format(ee_site_webroot))

    # Check if ee_domain is wordpress install
    try:
        is_wp = EEShellExec.cmd_exec(self, "wp --allow-root core"
                                     " version")
    except CommandExecutionError as e:
        raise SiteError("is wordpress site? check command failed ")

    # Exit if ee_domain is not wordpress install
    if not is_wp:
        Log.error(self, "{0} does not seem to be a WordPress site"
                  .format(ee_domain))

    try:
        ee_wp_user = input("Provide WordPress user name [admin]: ")
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        Log.error(self, "\nCould not update password")

    if ee_wp_user == "?":
        Log.info(self, "Fetching WordPress user list")
        try:
            EEShellExec.cmd_exec(self, "wp --allow-root user list "
                                 "--fields=user_login | grep -v user_login")
        except CommandExecutionError as e:
            raise SiteError("fetch wp userlist command failed")

    if not ee_wp_user:
        ee_wp_user = '******'

    try:
        is_user_exist = EEShellExec.cmd_exec(self, "wp --allow-root user list "
                                             "--fields=user_login | grep {0}$ "
                                             .format(ee_wp_user))
    except CommandExecutionError as e:
        raise SiteError("if wp user exists check command failed")

    if is_user_exist:
        try:
            ee_wp_pass = getpass.getpass(prompt="Provide password for "
                                         "{0} user: "******"{0}".format(e))
            raise SiteError("failed to read password input ")

        try:
            EEShellExec.cmd_exec(self, "wp --allow-root user update {0}"
                                 "  --user_pass={1}"
                                 .format(ee_wp_user, ee_wp_pass))
        except CommandExecutionError as e:
            raise SiteError("wp user password update command failed")
        Log.info(self, "Password updated successfully")

    else:
        Log.error(self, "Invalid WordPress user {0} for {1}."
                  .format(ee_wp_user, ee_domain))
Beispiel #59
0
def site_package_check(self, stype):
    apt_packages = []
    packages = []
    stack = EEStackController()
    stack.app = self.app
    if stype in ['html', 'proxy', 'php', 'mysql', 'wp', 'wpsubdir',
                 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for Nginx")

        if not EEAptGet.is_installed(self, 'nginx-custom'):
            apt_packages = apt_packages + EEVariables.ee_nginx
        else:
            # Fix for Nginx white screen death
            if not EEFileUtils.grep(self, '/etc/nginx/fastcgi_params',
                                    'SCRIPT_FILENAME'):
                with open('/etc/nginx/fastcgi_params', encoding='utf-8',
                          mode='a') as ee_nginx:
                    ee_nginx.write('fastcgi_param \tSCRIPT_FILENAME '
                                   '\t$request_filename;\n')

    if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for PHP")
        if not EEAptGet.is_installed(self, 'php5-fpm'):
            apt_packages = apt_packages + EEVariables.ee_php

    if stype in ['mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for MySQL")
        if not EEShellExec.cmd_exec(self, "mysqladmin ping"):
            apt_packages = apt_packages + EEVariables.ee_mysql
            packages = packages + [["https://raw.githubusercontent.com/"
                                    "major/MySQLTuner-perl/master/"
                                    "mysqltuner.pl", "/usr/bin/mysqltuner",
                                    "MySQLTuner"]]

    if stype in ['php', 'mysql', 'wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting apt_packages variable for Postfix")
        if not EEAptGet.is_installed(self, 'postfix'):
            apt_packages = apt_packages + EEVariables.ee_postfix

    if stype in ['wp', 'wpsubdir', 'wpsubdomain']:
        Log.debug(self, "Setting packages variable for WP-CLI")
        if not EEShellExec.cmd_exec(self, "which wp"):
            packages = packages + [["https://github.com/wp-cli/wp-cli/"
                                    "releases/download/v{0}/"
                                    "wp-cli-{0}.phar"
                                    .format(EEVariables.ee_wp_cli),
                                    "/usr/bin/wp", "WP-CLI"]]
    if self.app.pargs.wpredis:
        Log.debug(self, "Setting apt_packages variable for redis")
        if not EEAptGet.is_installed(self, 'redis-server'):
            apt_packages = apt_packages + EEVariables.ee_redis

        if os.path.isfile("/etc/nginx/nginx.conf") and (not
           os.path.isfile("/etc/nginx/common/redis.conf")):

            data = dict()
            Log.debug(self, 'Writting the nginx configuration to '
                      'file /etc/nginx/common/redis.conf')
            ee_nginx = open('/etc/nginx/common/redis.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'redis.mustache',
                            out=ee_nginx)
            ee_nginx.close()

        if os.path.isfile("/etc/nginx/nginx.conf") and (not
           os.path.isfile("/etc/nginx/common/redis-hhvm.conf")):

            data = dict()
            Log.debug(self, 'Writting the nginx configuration to '
                      'file /etc/nginx/common/redis-hhvm.conf')
            ee_nginx = open('/etc/nginx/common/redis-hhvm.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'redis-hhvm.mustache',
                            out=ee_nginx)
            ee_nginx.close()

        if os.path.isfile("/etc/nginx/conf.d/upstream.conf"):
            if not EEFileUtils.grep(self, "/etc/nginx/conf.d/"
                                    "upstream.conf",
                                    "redis"):
                with open("/etc/nginx/conf.d/upstream.conf",
                          "a") as redis_file:
                    redis_file.write("upstream redis {\n"
                                     "    server 127.0.0.1:6379;\n"
                                     "    keepalive 10;\n}")

        if os.path.isfile("/etc/nginx/nginx.conf") and (not
           os.path.isfile("/etc/nginx/conf.d/redis.conf")):
            with open("/etc/nginx/conf.d/redis.conf", "a") as redis_file:
                redis_file.write("# Log format Settings\n"
                                 "log_format rt_cache_redis '$remote_addr $upstream_response_time $srcache_fetch_status [$time_local] '\n"
                                 "'$http_host \"$request\" $status $body_bytes_sent '\n"
                                 "'\"$http_referer\" \"$http_user_agent\"';\n")

    if self.app.pargs.hhvm:
        if platform.architecture()[0] is '32bit':
            Log.error(self, "HHVM is not supported by 32bit system")
        Log.debug(self, "Setting apt_packages variable for HHVM")
        if not EEAptGet.is_installed(self, 'hhvm'):
            apt_packages = apt_packages + EEVariables.ee_hhvm

        if os.path.isdir("/etc/nginx/common") and (not
           os.path.isfile("/etc/nginx/common/php-hhvm.conf")):
            data = dict()
            Log.debug(self, 'Writting the nginx configuration to '
                      'file /etc/nginx/common/php-hhvm.conf')
            ee_nginx = open('/etc/nginx/common/php-hhvm.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'php-hhvm.mustache',
                            out=ee_nginx)
            ee_nginx.close()

            Log.debug(self, 'Writting the nginx configuration to '
                      'file /etc/nginx/common/w3tc-hhvm.conf')
            ee_nginx = open('/etc/nginx/common/w3tc-hhvm.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'w3tc-hhvm.mustache', out=ee_nginx)
            ee_nginx.close()

            Log.debug(self, 'Writting the nginx configuration to '
                      'file /etc/nginx/common/wpfc-hhvm.conf')
            ee_nginx = open('/etc/nginx/common/wpfc-hhvm.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'wpfc-hhvm.mustache',
                            out=ee_nginx)
            ee_nginx.close()

            Log.debug(self, 'Writting the nginx configuration to '
                      'file /etc/nginx/common/wpsc-hhvm.conf')
            ee_nginx = open('/etc/nginx/common/wpsc-hhvm.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'wpsc-hhvm.mustache',
                            out=ee_nginx)
            ee_nginx.close()

        if os.path.isfile("/etc/nginx/conf.d/upstream.conf"):
            if not EEFileUtils.grep(self, "/etc/nginx/conf.d/upstream.conf",
                                          "hhvm"):
                with open("/etc/nginx/conf.d/upstream.conf", "a") as hhvm_file:
                    hhvm_file.write("upstream hhvm {\nserver 127.0.0.1:8000;\n"
                                    "server 127.0.0.1:9000 backup;\n}\n")

    # Check if Nginx is allready installed and Pagespeed config there or not
    # If not then copy pagespeed config
    if self.app.pargs.pagespeed:
        if (os.path.isfile('/etc/nginx/nginx.conf') and
           (not os.path.isfile('/etc/nginx/conf.d/pagespeed.conf'))):
            # Pagespeed configuration
            data = dict()
            Log.debug(self, 'Writting the Pagespeed Global '
                      'configuration to file /etc/nginx/conf.d/'
                      'pagespeed.conf')
            ee_nginx = open('/etc/nginx/conf.d/pagespeed.conf',
                            encoding='utf-8', mode='w')
            self.app.render((data), 'pagespeed-global.mustache',
                            out=ee_nginx)
            ee_nginx.close()

    return(stack.install(apt_packages=apt_packages, packages=packages,
                         disp_msg=False))
Beispiel #60
0
def setupdomain(self, data):

    ee_domain_name = data['site_name']
    ee_site_webroot = data['webroot'] if 'webroot' in data.keys() else ''

    # Check if nginx configuration already exists
    # if os.path.isfile('/etc/nginx/sites-available/{0}'
    #                   .format(ee_domain_name)):
    #     raise SiteError("nginx configuration already exists for site")

    Log.info(self, "Setting up NGINX configuration \t", end='')
    # write nginx config for file
    try:
        ee_site_nginx_conf = open('/etc/nginx/sites-available/{0}'
                                  .format(ee_domain_name), encoding='utf-8',
                                  mode='w')

        self.app.render((data), 'virtualconf.mustache',
                        out=ee_site_nginx_conf)
        ee_site_nginx_conf.close()
    except IOError as e:
        Log.debug(self, "{0}".format(e))
        raise SiteError("create nginx configuration failed for site")
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        raise SiteError("create nginx configuration failed for site")
    finally:
        # Check nginx -t and return status over it
        try:
            Log.debug(self, "Checking generated nginx conf, please wait...")
            FNULL = open('/dev/null', 'w')
            ret = subprocess.check_call(["nginx", "-t"], stdout=FNULL,
                                        stderr=subprocess.STDOUT)
            Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
        except CalledProcessError as e:
            Log.debug(self, "{0}".format(str(e)))
            Log.info(self, "[" + Log.ENDC + Log.FAIL + "Fail"
                     + Log.OKBLUE + "]")
            raise SiteError("created nginx configuration failed for site."
                            " check with `nginx -t`")


    # create symbolic link for
    EEFileUtils.create_symlink(self, ['/etc/nginx/sites-available/{0}'
                                      .format(ee_domain_name),
                                      '/etc/nginx/sites-enabled/{0}'
                                      .format(ee_domain_name)])

    if 'proxy' in data.keys() and data['proxy']:
        return

    # Creating htdocs & logs directory
    Log.info(self, "Setting up webroot \t\t", end='')
    try:
        if not os.path.exists('{0}/htdocs'.format(ee_site_webroot)):
            os.makedirs('{0}/htdocs'.format(ee_site_webroot))
        if not os.path.exists('{0}/logs'.format(ee_site_webroot)):
            os.makedirs('{0}/logs'.format(ee_site_webroot))
        if not os.path.exists('{0}/conf/nginx'.format(ee_site_webroot)):
            os.makedirs('{0}/conf/nginx'.format(ee_site_webroot))

        EEFileUtils.create_symlink(self, ['/var/log/nginx/{0}.access.log'
                                          .format(ee_domain_name),
                                          '{0}/logs/access.log'
                                          .format(ee_site_webroot)])
        EEFileUtils.create_symlink(self, ['/var/log/nginx/{0}.error.log'
                                          .format(ee_domain_name),
                                          '{0}/logs/error.log'
                                          .format(ee_site_webroot)])
    except Exception as e:
        Log.debug(self, "{0}".format(e))
        raise SiteError("setup webroot failed for site")
    finally:
        # TODO Check if directories are setup
        if (os.path.exists('{0}/htdocs'.format(ee_site_webroot)) and
           os.path.exists('{0}/logs'.format(ee_site_webroot))):
            Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")
        else:
            Log.info(self, "[" + Log.ENDC + "Fail" + Log.OKBLUE + "]")
            raise SiteError("setup webroot failed for site")