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.OKGREEN + "]"))
         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
    def default(self):
        if ((not self.app.pargs.mariadb)):
            self.app.args.print_help()
        if self.app.pargs.mariadb:
            if SSSVariables.sss_mysql_host is not "localhost":
                Log.error(
                    self, "Remote MySQL found, EasyEngine will not "
                    "install MariaDB")

            if SSSShellExec.cmd_exec(self, "mysqladmin ping") and (
                    not SSSAptGet.is_installed(self, 'mariadb-server')):

                Log.info(
                    self, "If your database size is big, "
                    "migration may take some time.")
                Log.info(
                    self, "During migration non Apache parts of "
                    "your site may remain down")
                start_migrate = input("Type \"mariadb\" to continue:")
                if start_migrate != "mariadb":
                    Log.error(self, "Not starting migration")
                self.migrate_mariadb()
            else:
                Log.error(
                    self, "Your current MySQL is not alive or "
                    "you allready installed MariaDB")
Esempio n. 3
0
    def remove(self, packages, auto=False, purge=False):
        all_packages = ' '.join(packages)
        try:
            with open('/var/log/sss/sss.log', 'a') as f:
                if purge:
                    proc = subprocess.Popen(
                        'apt-get purge -y {0}'.format(all_packages),
                        shell=True,
                        stdin=None,
                        stdout=f,
                        stderr=f,
                        executable="/bin/bash")
                else:
                    proc = subprocess.Popen(
                        'apt-get remove -y {0}'.format(all_packages),
                        shell=True,
                        stdin=None,
                        stdout=f,
                        stderr=f,
                        executable="/bin/bash")
                proc.wait()
            if proc.returncode == 0:
                return True
            else:
                Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
                Log.error(
                    self, "Check logs for reason "
                    "`tail /var/log/sss/sss.log` & Try Again!!!")

        except Exception as e:
            Log.error(
                self, "Error while installing packages, "
                "apt-get exited with error")
    def add(self):
        """Swap addition with SimpleSetupServer"""
        if SSSVariables.sss_ram < 512:
            if SSSVariables.sss_swap < 1000:
                Log.info(self, "Adding SWAP file, please wait...")

                # Install dphys-swapfile
                SSSAptGet.update(self)
                SSSAptGet.install(self, ["dphys-swapfile"])
                # Stop service
                SSSShellExec.cmd_exec(self, "service dphys-swapfile stop")
                # Remove Default swap created
                SSSShellExec.cmd_exec(self, "/sbin/dphys-swapfile uninstall")

                # Modify Swap configuration
                if os.path.isfile("/etc/dphys-swapfile"):
                    SSSFileUtils.searchreplace(self, "/etc/dphys-swapfile",
                                              "#CONF_SWAPFILE=/var/swap",
                                              "CONF_SWAPFILE=/sss-swapfile")
                    SSSFileUtils.searchreplace(self,  "/etc/dphys-swapfile",
                                              "#CONF_MAXSWAP=2048",
                                              "CONF_MAXSWAP=1024")
                    SSSFileUtils.searchreplace(self,  "/etc/dphys-swapfile",
                                              "#CONF_SWAPSIZE=",
                                              "CONF_SWAPSIZE=1024")
                else:
                    with open("/etc/dphys-swapfile", 'w') as conffile:
                        conffile.write("CONF_SWAPFILE=/sss-swapfile\n"
                                       "CONF_SWAPSIZE=1024\n"
                                       "CONF_MAXSWAP=1024\n")
                # Create swap file
                SSSShellExec.cmd_exec(self, "service dphys-swapfile start")
    def add(self):
        """Swap addition with SimpleSetupServer"""
        if SSSVariables.sss_ram < 512:
            if SSSVariables.sss_swap < 1000:
                Log.info(self, "Adding SWAP file, please wait...")

                # Install dphys-swapfile
                SSSAptGet.update(self)
                SSSAptGet.install(self, ["dphys-swapfile"])
                # Stop service
                SSSShellExec.cmd_exec(self, "service dphys-swapfile stop")
                # Remove Default swap created
                SSSShellExec.cmd_exec(self, "/sbin/dphys-swapfile uninstall")

                # Modify Swap configuration
                if os.path.isfile("/etc/dphys-swapfile"):
                    SSSFileUtils.searchreplace(self, "/etc/dphys-swapfile",
                                               "#CONF_SWAPFILE=/var/swap",
                                               "CONF_SWAPFILE=/sss-swapfile")
                    SSSFileUtils.searchreplace(self, "/etc/dphys-swapfile",
                                               "#CONF_MAXSWAP=2048",
                                               "CONF_MAXSWAP=1024")
                    SSSFileUtils.searchreplace(self, "/etc/dphys-swapfile",
                                               "#CONF_SWAPSIZE=",
                                               "CONF_SWAPSIZE=1024")
                else:
                    with open("/etc/dphys-swapfile", 'w') as conffile:
                        conffile.write("CONF_SWAPFILE=/sss-swapfile\n"
                                       "CONF_SWAPSIZE=1024\n"
                                       "CONF_MAXSWAP=1024\n")
                # Create swap file
                SSSShellExec.cmd_exec(self, "service dphys-swapfile start")
Esempio n. 6
0
    def install(self, packages):
        all_packages = " ".join(packages)
        try:
            with open("/var/log/sss/sss.log", "a") as f:
                proc = subprocess.Popen(
                    "DEBIAN_FRONTEND=noninteractive "
                    "apt-get install -o "
                    'Dpkg::Options::="--force-confdef"'
                    " -o "
                    'Dpkg::Options::="--force-confold"'
                    " -y {0}".format(all_packages),
                    shell=True,
                    stdin=None,
                    stdout=f,
                    stderr=f,
                    executable="/bin/bash",
                )
                proc.wait()

            if proc.returncode == 0:
                return True
            else:
                Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
                Log.error(self, "Check logs for reason " "`tail /var/log/sss/sss.log` & Try Again!!!")

        except Exception as e:
            Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
            Log.error(self, "Check logs for reason " "`tail /var/log/sss/sss.log` & Try Again!!!")
Esempio n. 7
0
    def dist_upgrade(self):
        """
        Similar to `apt-get upgrade`
        """
        try:
            with open("/var/log/sss/sss.log", "a") as f:
                proc = subprocess.Popen(
                    "DEBIAN_FRONTEND=noninteractive "
                    "apt-get dist-upgrade -o "
                    'Dpkg::Options::="--force-confdef"'
                    " -o "
                    'Dpkg::Options::="--force-confold"'
                    " -y ",
                    shell=True,
                    stdin=None,
                    stdout=f,
                    stderr=f,
                    executable="/bin/bash",
                )
                proc.wait()

            if proc.returncode == 0:
                return True
            else:
                Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
                Log.error(self, "Check logs for reason " "`tail /var/log/ss/ss.log` & Try Again!!!")
        except Exception as e:
            Log.error(self, "Error while installing packages, " "apt-get exited with error")
Esempio n. 8
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.OKGREEN + "]"))
         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
Esempio n. 9
0
    def dist_upgrade(self):
        """
        Similar to `apt-get upgrade`
        """
        try:
            with open('/var/log/sss/sss.log', 'a') as f:
                proc = subprocess.Popen(
                    "DEBIAN_FRONTEND=noninteractive "
                    "apt-get dist-upgrade -o "
                    "Dpkg::Options::=\"--force-confdef\""
                    " -o "
                    "Dpkg::Options::=\"--force-confold\""
                    " -y ",
                    shell=True,
                    stdin=None,
                    stdout=f,
                    stderr=f,
                    executable="/bin/bash")
                proc.wait()

            if proc.returncode == 0:
                return True
            else:
                Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
                Log.error(
                    self, "Check logs for reason "
                    "`tail /var/log/ss/ss.log` & Try Again!!!")
        except Exception as e:
            Log.error(
                self, "Error while installing packages, "
                "apt-get exited with error")
Esempio n. 10
0
    def remove(self, packages, auto=False, purge=False):
        all_packages = " ".join(packages)
        try:
            with open("/var/log/sss/sss.log", "a") as f:
                if purge:
                    proc = subprocess.Popen(
                        "apt-get purge -y {0}".format(all_packages),
                        shell=True,
                        stdin=None,
                        stdout=f,
                        stderr=f,
                        executable="/bin/bash",
                    )
                else:
                    proc = subprocess.Popen(
                        "apt-get remove -y {0}".format(all_packages),
                        shell=True,
                        stdin=None,
                        stdout=f,
                        stderr=f,
                        executable="/bin/bash",
                    )
                proc.wait()
            if proc.returncode == 0:
                return True
            else:
                Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
                Log.error(self, "Check logs for reason " "`tail /var/log/sss/sss.log` & Try Again!!!")

        except Exception as e:
            Log.error(self, "Error while installing packages, " "apt-get exited with error")
Esempio n. 11
0
    def install(self, packages):
        all_packages = ' '.join(packages)
        try:
            with open('/var/log/sss/sss.log', 'a') as f:
                proc = subprocess.Popen("DEBIAN_FRONTEND=noninteractive "
                                        "apt-get install -o "
                                        "Dpkg::Options::=\"--force-confdef\""
                                        " -o "
                                        "Dpkg::Options::=\"--force-confold\""
                                        " -y {0}".format(all_packages),
                                        shell=True,
                                        stdin=None,
                                        stdout=f,
                                        stderr=f,
                                        executable="/bin/bash")
                proc.wait()

            if proc.returncode == 0:
                return True
            else:
                Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
                Log.error(
                    self, "Check logs for reason "
                    "`tail /var/log/sss/sss.log` & Try Again!!!")

        except Exception as e:
            Log.info(self, Log.FAIL + "Oops Something went " "wrong!!")
            Log.error(
                self, "Check logs for reason "
                "`tail /var/log/sss/sss.log` & Try Again!!!")
Esempio n. 12
0
    def remove(self):
        """Start removal of packages"""
        apt_packages = []
        packages = []

        # Default action for stack remove
        if ((not self.app.pargs.web) and (not self.app.pargs.apache2)
                and (not self.app.pargs.php) and (not self.app.pargs.mysql)):
            self.app.pargs.web = True
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.all:
            self.app.pargs.web = True
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.web:
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True
            #self.app.pargs.wpcli = True
            #self.app.pargs.postfix = True

        if self.app.pargs.apache2:
            Log.debug(self, "Removing apt_packages variable of Apache")
            apt_packages = apt_packages + SSSVariables.sss_apache
        if self.app.pargs.php:
            Log.debug(self, "Removing apt_packages variable of PHP")
            apt_packages = apt_packages + SSSVariables.sss_php
        if self.app.pargs.mysql:
            Log.debug(self, "Removing apt_packages variable of PHP")
            apt_packages = apt_packages + SSSVariables.sss_mysql
            packages = packages + ['/usr/bin/tuning-primer']

        if len(packages) or len(apt_packages):
            sss_prompt = input('Are you sure you to want to'
                               ' remove from server.'
                               '\nPackage configuration will remain'
                               ' on server after this operation.\n'
                               'Any answer other than '
                               '"yes" will be stop this'
                               ' operation :  ')

        if sss_prompt == 'YES' or sss_prompt == 'yes':
            if len(packages):
                SSSFileUtils.remove(self, packages)
                SSSAptGet.auto_remove(self)

            if len(apt_packages):
                Log.debug(self, "Removing apt_packages")
                Log.info(self, "Removing packages, please wait...")
                SSSAptGet.remove(self, apt_packages)
                SSSAptGet.auto_remove(self)

            Log.info(self, "Successfully removed packages")
    def remove(self):
        """Start removal of packages"""
        apt_packages = []
        packages = []

        # Default action for stack remove
        if ((not self.app.pargs.web) and (not self.app.pargs.apache2) and
            (not self.app.pargs.php) and (not self.app.pargs.mysql)):
                self.app.pargs.web = True
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                self.app.pargs.mysql = True

        if self.app.pargs.all:
            self.app.pargs.web = True
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.web:
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True
            #self.app.pargs.wpcli = True
            #self.app.pargs.postfix = True

        if self.app.pargs.apache2:
            Log.debug(self,"Removing apt_packages variable of Apache")
            apt_packages = apt_packages + SSSVariables.sss_apache
        if self.app.pargs.php:
            Log.debug(self,"Removing apt_packages variable of PHP")
            apt_packages = apt_packages + SSSVariables.sss_php
        if self.app.pargs.mysql:
            Log.debug(self,"Removing apt_packages variable of PHP")
            apt_packages = apt_packages + SSSVariables.sss_mysql
            packages = packages + ['/usr/bin/tuning-primer']

        if len(packages) or len(apt_packages):
            sss_prompt = input('Are you sure you to want to'
                          ' remove from server.'
                          '\nPackage configuration will remain'
                          ' on server after this operation.\n'
                          'Any answer other than '
                          '"yes" will be stop this'
                          ' operation :  ')

        if sss_prompt == 'YES' or sss_prompt == 'yes':
            if len(packages):
                SSSFileUtils.remove(self, packages)
                SSSAptGet.auto_remove(self)

            if len(apt_packages):
                Log.debug(self, "Removing apt_packages")
                Log.info(self, "Removing packages, please wait...")
                SSSAptGet.remove(self, apt_packages)
                SSSAptGet.auto_remove(self)

            Log.info(self, "Successfully removed packages")
Esempio n. 14
0
 def callback(filename, lines):
     for line in lines:
         if line.find(":::") == -1:
             print(line)
         else:
             data = line.split(":::")
             try:
                 print(data[0], data[1], zlib.decompress(base64.decodestring(data[2])))
             except Exception as e:
                 Log.info(time.time(), "caught exception rendering a new log line in %s" % filename)
Esempio n. 15
0
def pre_run_checks(self):

    # Check Apache configuration
    Log.info(self, "Running pre-update checks, please wait...")
    try:
        Log.debug(self, "checking Apache configuration ...")
        FNULL = open("/dev/null", "w")
        ret = subprocess.check_call(["apachectl", "configtest"], stdout=FNULL, stderr=subprocess.STDOUT)
    except CalledProcessError as e:
        Log.debug(self, "{0}".format(str(e)))
        raise SiteError("Apache configuration check failed.")
def pre_run_checks(self):

    # Check Apache configuration
    Log.info(self, "Running pre-update checks, please wait...")
    try:
        Log.debug(self, "checking Apache configuration ...")
        FNULL = open('/dev/null', 'w')
        ret = subprocess.check_call(["apachectl", "configtest"],
                                    stdout=FNULL,
                                    stderr=subprocess.STDOUT)
    except CalledProcessError as e:
        Log.debug(self, "{0}".format(str(e)))
        raise SiteError("Apache configuration check failed.")
 def callback(filename, lines):
     for line in lines:
         if line.find(':::') == -1:
             print(line)
         else:
             data = line.split(':::')
             try:
                 print(data[0], data[1],
                       zlib.decompress(base64.decodestring(data[2])))
             except Exception as e:
                 Log.info(
                     time.time(),
                     'caught exception rendering a new log line in %s' %
                     filename)
Esempio n. 18
0
    def update(self):
        """
        Similar to `apt-get update`
        """
        try:
            with open('/var/log/sss/sss.log', 'a') as f:
                proc = subprocess.Popen('apt-get update',
                                        shell=True,
                                        stdin=None,
                                        stdout=f,
                                        stderr=subprocess.PIPE,
                                        executable="/bin/bash")
                proc.wait()
                output, error_output = proc.communicate()

                # Check what is error in error_output
                if "NO_PUBKEY" in str(error_output):
                    # Split the output
                    Log.info(self, "Fixing missing GPG keys, please wait...")
                    error_list = str(error_output).split("\\n")

                    # Use a loop to add misising keys
                    for single_error in error_list:
                        if "NO_PUBKEY" in single_error:
                            key = single_error.rsplit(None, 1)[-1]
                            SSSRepo.add_key(self,
                                            key,
                                            keyserver="hkp://pgp.mit.edu")

                    proc = subprocess.Popen('apt-get update',
                                            shell=True,
                                            stdin=None,
                                            stdout=f,
                                            stderr=f,
                                            executable="/bin/bash")
                    proc.wait()

                if proc.returncode == 0:
                    return True
                else:
                    Log.info(self, Log.FAIL + "Oops Something went wrong!!")
                    Log.error(
                        self, "Check logs for reason "
                        "`tail /var/log/sss/sss.log` & Try Again!!!")

        except Exception as e:
            Log.error(self, "apt-get update exited with error")
Esempio n. 19
0
    def check_upgrade(self):
        """
        Similar to `apt-get upgrade`
        """
        try:
            check_update = subprocess.Popen(
                ["apt-get upgrade -s | grep " '"^Inst" | wc -l'], stdout=subprocess.PIPE, shell=True
            ).communicate()[0]
            if check_update == b"0\n":
                Log.error(self, "No package updates available")
            Log.info(self, "Following package updates are available:")
            subprocess.Popen(
                'apt-get -s dist-upgrade | grep "^Inst"', shell=True, executable="/bin/bash", stdout=sys.stdout
            ).communicate()

        except Exception as e:
            Log.error(self, "Unable to check for packages upgrades")
Esempio n. 20
0
    def default(self):
        if not self.app.pargs.mariadb:
            self.app.args.print_help()
        if self.app.pargs.mariadb:
            if SSSVariables.sss_mysql_host is not "localhost":
                Log.error(self, "Remote MySQL found, EasyEngine will not " "install MariaDB")

            if SSSShellExec.cmd_exec(self, "mysqladmin ping") and (not SSSAptGet.is_installed(self, "mariadb-server")):

                Log.info(self, "If your database size is big, " "migration may take some time.")
                Log.info(self, "During migration non Apache parts of " "your site may remain down")
                start_migrate = input('Type "mariadb" to continue:')
                if start_migrate != "mariadb":
                    Log.error(self, "Not starting migration")
                self.migrate_mariadb()
            else:
                Log.error(self, "Your current MySQL is not alive or " "you allready installed MariaDB")
    def status(self):
        """Status of services"""
        services = []
        if not (self.app.pargs.apache2 or self.app.pargs.php
                or self.app.pargs.mysql or self.app.pargs.memcache):
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.apache2:
            if SSSAptGet.is_installed(self, 'apache2'):
                services = services + ['apache2']
            else:
                Log.info(self, "Apache is not installed")

        if self.app.pargs.php:
            if SSSAptGet.is_installed(self, 'php7.0-fpm'):
                services = services + ['php7.0-fpm']
            else:
                Log.info(self, "PHP7-FPM is not installed")

        if self.app.pargs.mysql:
            if ((SSSVariables.sss_mysql_host is "localhost")
                    or (SSSVariables.sss_mysql_host is "127.0.0.1")):
                if (SSSAptGet.is_installed(self, 'mysql-server')
                        or SSSAptGet.is_installed(self,
                                                  'percona-server-server-5.6')
                        or SSSAptGet.is_installed(self, 'mariadb-server')):
                    services = services + ['mysql']
                else:
                    Log.info(self, "MySQL is not installed")
            else:
                Log.warn(
                    self, "Remote MySQL found, "
                    "Unable to check MySQL service status")

        if self.app.pargs.memcache:
            if SSSAptGet.is_installed(self, 'memcached'):
                services = services + ['memcached']
            else:
                Log.info(self, "Memcache is not installed")

        for service in services:
            if SSSService.get_service_status(self, service):
                Log.info(self, "{0:10}:  {1}".format(service, "Running"))
    def status(self):
        """Status of services"""
        services = []
        if not (self.app.pargs.apache2 or self.app.pargs.php
                or self.app.pargs.mysql or self.app.pargs.memcache):
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.apache2:
            if SSSAptGet.is_installed(self,'apache2'):
                services = services + ['apache2']
            else:
                Log.info(self,"Apache is not installed")

        if self.app.pargs.php:
            if SSSAptGet.is_installed(self, 'php7.0-fpm'):
                services = services + ['php7.0-fpm']
            else:
                Log.info(self, "PHP7-FPM is not installed")

        if self.app.pargs.mysql:
            if ((SSSVariables.sss_mysql_host is "localhost") or
               (SSSVariables.sss_mysql_host is "127.0.0.1")):
                if (SSSAptGet.is_installed(self, 'mysql-server') or
                   SSSAptGet.is_installed(self, 'percona-server-server-5.6') or
                   SSSAptGet.is_installed(self, 'mariadb-server')):
                    services = services + ['mysql']
                else:
                    Log.info(self, "MySQL is not installed")
            else:
                Log.warn(self, "Remote MySQL found, "
                         "Unable to check MySQL service status")

        if self.app.pargs.memcache:
            if SSSAptGet.is_installed(self, 'memcached'):
                services = services + ['memcached']
            else:
                Log.info(self, "Memcache is not installed")

        for service in services:
            if SSSService.get_service_status(self, service):
                Log.info(self, "{0:10}:  {1}".format(service, "Running"))
Esempio n. 23
0
    def check_upgrade(self):
        """
        Similar to `apt-get upgrade`
        """
        try:
            check_update = subprocess.Popen(
                ['apt-get upgrade -s | grep '
                 '\"^Inst\" | wc -l'],
                stdout=subprocess.PIPE,
                shell=True).communicate()[0]
            if check_update == b'0\n':
                Log.error(self, "No package updates available")
            Log.info(self, "Following package updates are available:")
            subprocess.Popen("apt-get -s dist-upgrade | grep \"^Inst\"",
                             shell=True,
                             executable="/bin/bash",
                             stdout=sys.stdout).communicate()

        except Exception as e:
            Log.error(self, "Unable to check for packages upgrades")
def deleteDB(self, dbname, dbuser, dbhost, exit=True):
    try:
        # Check if Database exists
        try:
            if SSSMysql.check_db_exists(self, dbname):
                # Drop database if exists
                Log.debug(self, "dropping database `{0}`".format(dbname))
                SSSMysql.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:
                SSSMysql.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:
                SSSMysql.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", exit)
def deleteDB(self, dbname, dbuser, dbhost, exit=True):
    try:
        # Check if Database exists
        try:
            if SSSMysql.check_db_exists(self, dbname):
                # Drop database if exists
                Log.debug(self, "dropping database `{0}`".format(dbname))
                SSSMysql.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:
                SSSMysql.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:
                SSSMysql.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", exit)
    def backupAll(self):
        import subprocess
        try:
            Log.info(
                self, "Backing up database at location: "
                "/var/sss-mysqlbackup")
            # Setup MySql backup directory
            if not os.path.exists('/var/sss-mysqlbackup'):
                Log.debug(self, 'Creating directory' '/var/sss-mysqlbackup')
                os.makedirs('/var/sss-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/sss-mysqlbackup/{0}{1}.s"
                                      "ql.gz".format(dbs,
                                                     SSSVariables.sss_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)
    def reload_service(self, service_name):
        """
            Stop service
            Similar to `service xyz stop`
        """
        try:
            if service_name in ['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.OKGREEN + "]")
                return True
            else:
                Log.debug(self, "{0}".format(retcode[1]))
                Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKGREEN + "]")
                return False
        except OSError as e:
            Log.debug(self, "{0}".format(e))
            Log.error(self,
                      "\nFailed to reload service {0}".format(service_name))
    def reload_service(self, service_name):
        """
            Stop service
            Similar to `service xyz stop`
        """
        try:
            if service_name in ['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.OKGREEN + "]")
                    return True
            else:
                Log.debug(self, "{0}".format(retcode[1]))
                Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKGREEN+"]")
                return False
        except OSError as e:
            Log.debug(self, "{0}".format(e))
            Log.error(self, "\nFailed to reload service {0}"
                      .format(service_name))
    def backupAll(self):
        import subprocess
        try:
            Log.info(self, "Backing up database at location: "
                     "/var/sss-mysqlbackup")
            # Setup MySql backup directory
            if not os.path.exists('/var/sss-mysqlbackup'):
                Log.debug(self, 'Creating directory'
                          '/var/sss-mysqlbackup')
                os.makedirs('/var/sss-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/sss-mysqlbackup/{0}{1}.s"
                                      "ql.gz".format(dbs, SSSVariables.sss_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)
Esempio n. 30
0
    def update(self):
        """
        Similar to `apt-get update`
        """
        try:
            with open("/var/log/sss/sss.log", "a") as f:
                proc = subprocess.Popen(
                    "apt-get update", shell=True, stdin=None, stdout=f, stderr=subprocess.PIPE, executable="/bin/bash"
                )
                proc.wait()
                output, error_output = proc.communicate()

                # Check what is error in error_output
                if "NO_PUBKEY" in str(error_output):
                    # Split the output
                    Log.info(self, "Fixing missing GPG keys, please wait...")
                    error_list = str(error_output).split("\\n")

                    # Use a loop to add misising keys
                    for single_error in error_list:
                        if "NO_PUBKEY" in single_error:
                            key = single_error.rsplit(None, 1)[-1]
                            SSSRepo.add_key(self, key, keyserver="hkp://pgp.mit.edu")

                    proc = subprocess.Popen(
                        "apt-get update", shell=True, stdin=None, stdout=f, stderr=f, executable="/bin/bash"
                    )
                    proc.wait()

                if proc.returncode == 0:
                    return True
                else:
                    Log.info(self, Log.FAIL + "Oops Something went wrong!!")
                    Log.error(self, "Check logs for reason " "`tail /var/log/sss/sss.log` & Try Again!!!")

        except Exception as e:
            Log.error(self, "apt-get update exited with error")
 def remove(self, filelist):
     """remove files from given path"""
     for file in filelist:
         if os.path.isfile(file):
             Log.info(self, "Removing {0:65}".format(file), end=' ')
             os.remove(file)
             Log.info(self, "{0}".format("[" + Log.ENDC + "Done" +
                      Log.OKGREEN + "]"))
             Log.debug(self, 'file Removed')
         if os.path.isdir(file):
             try:
                 Log.info(self, "Removing {0:65}".format(file), end=' ')
                 shutil.rmtree(file)
                 Log.info(self, "{0}".format("[" + Log.ENDC + "Done" +
                          Log.OKGREEN + "]"))
             except shutil.Error as e:
                 Log.debug(self, "{err}".format(err=str(e.reason)))
                 Log.error(self, 'Unable to Remove file ')
 def remove(self, filelist):
     """remove files from given path"""
     for file in filelist:
         if os.path.isfile(file):
             Log.info(self, "Removing {0:65}".format(file), end=' ')
             os.remove(file)
             Log.info(
                 self,
                 "{0}".format("[" + Log.ENDC + "Done" + Log.OKGREEN + "]"))
             Log.debug(self, 'file Removed')
         if os.path.isdir(file):
             try:
                 Log.info(self, "Removing {0:65}".format(file), end=' ')
                 shutil.rmtree(file)
                 Log.info(
                     self, "{0}".format("[" + Log.ENDC + "Done" +
                                        Log.OKGREEN + "]"))
             except shutil.Error as e:
                 Log.debug(self, "{err}".format(err=str(e.reason)))
                 Log.error(self, 'Unable to Remove file ')
 def stop_service(self, service_name):
     """
         Stop service
         Similar to `service xyz stop`
     """
     try:
         Log.info(self, "Stop : {0:10}".format(service_name), end='')
         retcode = subprocess.getstatusoutput(
             'service {0} stop'.format(service_name))
         if retcode[0] == 0:
             Log.info(self, "[" + Log.ENDC + "OK" + Log.OKGREEN + "]")
             return True
         else:
             Log.debug(self, "{0}".format(retcode[1]))
             Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKGREEN + "]")
             return False
     except OSError as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self,
                   "\nFailed to stop service : {0}".format(service_name))
 def stop_service(self, service_name):
     """
         Stop service
         Similar to `service xyz stop`
     """
     try:
         Log.info(self, "Stop : {0:10}" .format(service_name), end='')
         retcode = subprocess.getstatusoutput('service {0} stop'
                                              .format(service_name))
         if retcode[0] == 0:
             Log.info(self, "[" + Log.ENDC + "OK" + Log.OKGREEN + "]")
             return True
         else:
             Log.debug(self, "{0}".format(retcode[1]))
             Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKGREEN+"]")
             return False
     except OSError as e:
         Log.debug(self, "{0}".format(e))
         Log.error(self, "\nFailed to stop service : {0}"
                   .format(service_name))
    def migrate_mariadb(self):
        # Backup all database
        SSSMysql.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)

        SSSRepo.add(self, repo_url=SSSVariables.sss_mysql_repo)
        Log.debug(self,
                  'Adding key for {0}'.format(SSSVariables.sss_mysql_repo))
        SSSRepo.add_key(self,
                        '0xcbcb082a1bb943db',
                        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")
        SSSShellExec.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")
        SSSShellExec.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 = SSSVariables.sss_mysql

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

        # If mail server is installed then install dovecot-sql and postfix-sql
        if SSSAptGet.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...")
        SSSAptGet.update(self)
        Log.info(self, "Installing MariaDB, please wait...")
        SSSAptGet.remove(self, ["mysql-common", "libmysqlclient18"])
        SSSAptGet.auto_remove(self)
        SSSAptGet.install(self, apt_packages)

        # Restart  dovecot and postfix if installed
        if SSSAptGet.is_installed(self, "dovecot-core"):
            SSSService.restart_service(self, 'dovecot')
            SSSService.restart_service(self, 'postfix')
def setupdatabase(self, data):
    sss_domain_name = data['site_name']
    sss_random = (''.join(
        random.sample(
            string.ascii_uppercase + string.ascii_lowercase + string.digits,
            15)))
    sss_replace_dot = sss_domain_name.replace('.', '_')
    prompt_dbname = self.app.config.get('mysql', 'db-name')
    prompt_dbuser = self.app.config.get('mysql', 'db-user')
    sss_mysql_grant_host = self.app.config.get('mysql', 'grant-host')
    sss_db_name = ''
    sss_db_username = ''
    sss_db_password = ''

    if prompt_dbname == 'True' or prompt_dbname == 'true':
        try:
            sss_db_name = input('Enter the MySQL database name [{0}]: '.format(
                sss_replace_dot))
        except EOFError as e:
            Log.debug(self, "{0}".format(e))
            raise SiteError("Unable to input database name")

    if not sss_db_name:
        sss_db_name = sss_replace_dot

    if prompt_dbuser == 'True' or prompt_dbuser == 'true':
        try:
            sss_db_username = input(
                'Enter the MySQL database user name [{0}]: '.format(
                    sss_replace_dot))
            sss_db_password = getpass.getpass(
                prompt='Enter the MySQL database'
                ' password [{0}]: '.format(sss_random))
        except EOFError as e:
            Log.debug(self, "{0}".format(e))
            raise SiteError("Unable to input database credentials")

    if not sss_db_username:
        sss_db_username = sss_replace_dot
    if not sss_db_password:
        sss_db_password = sss_random

    if len(sss_db_username) > 16:
        Log.debug(
            self, 'Autofix MySQL username (ERROR 1470 (HY000)),'
            ' please wait')
        sss_db_username = (sss_db_name[0:6] + generate_random())

    # create MySQL database

    Log.info(self, "Setting up database\t\t", end='')
    Log.debug(self, "Creating database {0}".format(sss_db_name))
    try:
        if SSSMysql.check_db_exists(self, sss_db_name):
            Log.debug(self, "Database already exists, Updating DB_NAME .. ")
            sss_db_name = (sss_db_name[0:6] + generate_random())
            sss_db_username = (sss_db_name[0:6] + generate_random())
    except MySQLConnectionError as e:
        raise SiteError("MySQL Connectivity problem occured")

    try:
        SSSMysql.execute(self, "create database `{0}`".format(sss_db_name))
    except StatementExcecutionError as e:
        Log.info(self,
                 "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKGREEN + "]")
        raise SiteError("create database execution failed")

    # Create MySQL User
    Log.debug(self, "Creating user {0}".format(sss_db_username))
    Log.debug(
        self, "create user `{0}`@`{1}` identified by ''".format(
            sss_db_username, sss_mysql_grant_host))

    try:
        SSSMysql.execute(self,
                         "create user `{0}`@`{1}` identified by '{2}'".format(
                             sss_db_username, sss_mysql_grant_host,
                             sss_db_password),
                         log=False)
    except StatementExcecutionError as e:
        Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
        raise SiteError("creating user failed for database")

    # Grant permission
    Log.debug(self, "Setting up user privileges")

    try:
        SSSMysql.execute(
            self, "grant all privileges on `{0}`.* to `{1}`@`{2}`".format(
                sss_db_name, sss_db_username, sss_mysql_grant_host))
    except StatementExcecutionError as e:
        Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
        SiteError("grant privileges to user failed for database ")

    Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")

    data['sss_db_name'] = sss_db_name
    data['sss_db_user'] = sss_db_username
    data['sss_db_pass'] = sss_db_password
    data['sss_db_host'] = SSSVariables.sss_mysql_host
    data['sss_mysql_grant_host'] = sss_mysql_grant_host
    return (data)
Esempio n. 37
0
    def migrate_mariadb(self):
        # Backup all database
        SSSMysql.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)

        SSSRepo.add(self, repo_url=SSSVariables.sss_mysql_repo)
        Log.debug(self, "Adding key for {0}".format(SSSVariables.sss_mysql_repo))
        SSSRepo.add_key(self, "0xcbcb082a1bb943db", 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"
        )
        SSSShellExec.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",
        )
        SSSShellExec.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 = SSSVariables.sss_mysql

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

        # If mail server is installed then install dovecot-sql and postfix-sql
        if SSSAptGet.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...")
        SSSAptGet.update(self)
        Log.info(self, "Installing MariaDB, please wait...")
        SSSAptGet.remove(self, ["mysql-common", "libmysqlclient18"])
        SSSAptGet.auto_remove(self)
        SSSAptGet.install(self, apt_packages)

        # Restart  dovecot and postfix if installed
        if SSSAptGet.is_installed(self, "dovecot-core"):
            SSSService.restart_service(self, "dovecot")
            SSSService.restart_service(self, "postfix")
def setupdomain(self, data):

    sss_domain_name = data['site_name']
    sss_site_webroot = data['webroot'] if 'webroot' in data.keys() else ''

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

    Log.info(self, "Setting up Apache configuration \t", end='')
    # write apache config for file
    try:
        sss_site_apache_conf = open(
            '/etc/apache2/sites-available/{0}.conf'.format(sss_domain_name),
            encoding='utf-8',
            mode='w')

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

    # create Symbolic link
    SSSFileUtils.create_symlink(self, [
        '/etc/apache2/sites-available/{0}.conf'.format(sss_domain_name),
        '/etc/apache2/sites-enabled/{0}.conf'.format(sss_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(sss_site_webroot)):
            os.makedirs('{0}/htdocs'.format(sss_site_webroot))
        if not os.path.exists('{0}/logs'.format(sss_site_webroot)):
            os.makedirs('{0}/logs'.format(sss_site_webroot))

        # Create log file if not exists
        if not os.path.isfile(
                '/var/log/apache2/{0}.access.log'.format(sss_domain_name)):
            with open(
                    '/var/log/apache2/{0}.access.log'.format(sss_domain_name),
                    encoding='utf-8',
                    mode='a') as logfile:
                logfile.close()
        if not os.path.isfile(
                '/var/log/apache2/{0}.error.log'.format(sss_domain_name)):
            with open('/var/log/apache2/{0}.error.log'.format(sss_domain_name),
                      encoding='utf-8',
                      mode='a') as logfile:
                logfile.close()

        SSSFileUtils.create_symlink(self, [
            '/var/log/apache2/{0}.access.log'.format(sss_domain_name),
            '{0}/logs/access.log'.format(sss_site_webroot)
        ])
        SSSFileUtils.create_symlink(self, [
            '/var/log/apache2/{0}.error.log'.format(sss_domain_name),
            '{0}/logs/error.log'.format(sss_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(sss_site_webroot))
                and os.path.exists('{0}/logs'.format(sss_site_webroot))):
            Log.info(self, "[" + Log.ENDC + "Done" + Log.OKGREEN + "]")
        else:
            Log.info(self, "[" + Log.ENDC + "Fail" + Log.OKGREEN + "]")
            raise SiteError("setup webroot failed for site")
 def default(self):
     Log.info(
         self, "This command is deprecated."
         " You can use this command instead, " + Log.ENDC + Log.BOLD +
         "\n`sss debug --import-slow-log`" + Log.ENDC)
    def default(self):
        # All package update
        apt_packages = []
        packages = []

        if ((not self.app.pargs.web) and (not self.app.pargs.apache2) and
           (not self.app.pargs.php) and (not self.app.pargs.mysql) and
            (not self.app.pargs.all)):
            self.app.pargs.web = True

        if self.app.pargs.all:
            self.app.pargs.web = True

        if self.app.pargs.web:
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.apache2:
            if SSSAptGet.is_installed(self, 'apache2'):
                apt_packages = apt_packages + SSSVariables.sss_apache
            else:
                Log.info(self, "Apache is not already installed")

        if self.app.pargs.php:
            if SSSAptGet.is_installed(self, 'php7.0-fpm'):
                apt_packages = apt_packages + SSSVariables.sss_php
            else:
                Log.info(self, "PHP is not installed")

        if self.app.pargs.mysql:
            if SSSAptGet.is_installed(self, 'mariadb-server'):
                apt_packages = apt_packages + SSSVariables.sss_mysql
            else:
                Log.info(self, "MariaDB is not installed")

        if len(packages) or len(apt_packages):

            Log.info(self, "During package update process non Apache"
                     " parts of your site may remain down")
            # Check prompt
            if (not self.app.pargs.no_prompt):
                start_upgrade = input("Do you want to continue:[y/N]")
                if start_upgrade != "Y" and start_upgrade != "y":
                    Log.error(self, "Not starting package update")

            Log.info(self, "Updating packages, please wait...")
            if len(apt_packages):
                # apt-get update
                SSSAptGet.update(self)
                # Update packages
                SSSAptGet.install(self, apt_packages)

                # Post Actions after package updates
                if set(SSSVariables.sss_apache).issubset(set(apt_packages)):
                    SSSService.restart_service(self, 'apache2')
                if set(SSSVariables.sss_php).issubset(set(apt_packages)):
                    SSSService.restart_service(self, 'php7.0-fpm')
                if set(SSSVariables.sss_mysql).issubset(set(apt_packages)):
                    SSSService.restart_service(self, 'mysql')

            if len(packages):
                pass
            Log.info(self, "Successfully updated packages")
Esempio n. 41
0
    def install(self, packages=[], apt_packages=[], disp_msg=True):
        """Start installation of packages"""
        self.msg = []
        try:
            # Default action for stack installation
            if ((not self.app.pargs.web) and (not self.app.pargs.apache2)
                    and (not self.app.pargs.php)
                    and (not self.app.pargs.mysql)):
                self.app.pargs.web = True
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                self.app.pargs.mysql = True
                self.app.pargs.phpmyadmin = True

            if self.app.pargs.all:
                self.app.pargs.web = True
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                self.app.pargs.mysql = True
                self.app.pargs.phpmyadmin = True

            if self.app.pargs.web:
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                self.app.pargs.mysql = True
                #self.app.pargs.wpcli = True
                #self.app.pargs.postfix = True

            if self.app.pargs.apache2:
                Log.debug(self, "Setting apt_packages variable for Apache2")
                if not SSSAptGet.is_installed(self, 'apache2'):
                    apt_packages = apt_packages + SSSVariables.sss_apache

                else:
                    Log.debug(self, "Apache2 already installed")
                    Log.info(self, "Apache2 already installed")

            if self.app.pargs.php:
                Log.debug(self, "Setting apt_packages variable for PHP")
                if not SSSAptGet.is_installed(self, 'php7.0-fpm'):
                    apt_packages = apt_packages + SSSVariables.sss_php
                else:
                    Log.debug(self, "PHP already installed")
                    Log.info(self, "PHP already installed")

            if self.app.pargs.phpmyadmin:
                Log.debug(self, "Setting apt_packages variable for PhpMyadmin")
                if not SSSAptGet.is_installed(self, 'phpmyadmin'):
                    apt_packages = apt_packages + SSSVariables.sss_pma
                else:
                    Log.debug(self, "PhpMyadmin already installed")
                    Log.info(self, "PhpMyadmin already installed")

            if self.app.pargs.mysql:
                Log.debug(self, "Setting apt_packages variable for MySQL")
                if not SSSShellExec.cmd_exec(self, "mysqladmin ping"):
                    apt_packages = apt_packages + SSSVariables.sss_mysql
                    packages = packages + [[
                        "https://raw."
                        "githubusercontent.com"
                        "/serversetup/tuning-primer/"
                        "master/tuning-primer.sh", "/usr/bin/tuning-primer",
                        "Tuning-Primer"
                    ]]
                else:
                    Log.debug(self, "MySQL connection is already alive")
                    Log.info(self, "MySQL connection is already alive")

        except Exception as e:
            pass

        if len(apt_packages) or len(packages):
            Log.debug(self, "Calling pre_pref")
            self.pre_pref(apt_packages)
            if len(apt_packages):
                SSSSwap.add(self)
                Log.info(self, "Updating Apt-cache, please wait...")
                SSSAptGet.update(self)
                Log.info(self, "Installing packages, please wait...")
                SSSAptGet.install(self, apt_packages)
                SSSShellExec.cmd_exec(
                    self,
                    "a2enmod proxy_fcgi proxy proxy_http http2 ssl expires headers rewrite"
                )
            if len(packages):
                Log.debug(self, "Downloading following: {0}".format(packages))
                SSSDownload.download(self, packages)
            Log.debug(self, "Calling post_pref")
            self.post_pref(apt_packages, packages)

            if disp_msg:
                if len(self.msg):
                    for msg in self.msg:
                        Log.info(self, Log.ENDC + msg)
                Log.info(self, "Successfully installed packages")
            else:
                return self.msg
Esempio n. 42
0
    def pre_pref(self, apt_packages):
        """Pre settings to do before installation packages"""

        if set(SSSVariables.sss_pma).issubset(set(apt_packages)):
            Log.info(self, "Adding repository for phpMyAdmin ,please wait...")
            """pma_pref = ("def origin http://ppa.launchpad.net/nijel/phpmyadmin/ubuntu trusty main")

            with open('/etc/apt/sources.list.d/', 'w') as pma_pref_file:
                pma_pref_file.write(pma_pref)"""
            SSSRepo.add(self, repo_url=SSSVariables.sss_pma_repo_url)

            Log.debug(self,
                      'Adding key for {0}'.format(SSSVariables.sss_pma_repo))
            SSSRepo.add_key(self, '06ED541C', keyserver="keyserver.ubuntu.com")
            chars = ''.join(random.sample(string.ascii_letters, 8))

            Log.debug(self, "Adding ppa for phpMyAdmin")
            SSSRepo.add(self, ppa=SSSVariables.sss_pma_repo)

        if set(SSSVariables.sss_mysql).issubset(set(apt_packages)):
            Log.info(self, "Adding repository for MySQL, 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)

            SSSRepo.add(self, repo_url=SSSVariables.sss_mysql_repo)
            Log.debug(self,
                      'Adding key for {0}'.format(SSSVariables.sss_mysql_repo))
            SSSRepo.add_key(self,
                            '0xcbcb082a1bb943db',
                            keyserver="keyserver.ubuntu.com")
            chars = ''.join(random.sample(string.ascii_letters, 8))
            Log.debug(self, "Pre-seeding MySQL")
            Log.debug(
                self, "echo \"mariadb-server-10.1 "
                "mysql-server/root_password "
                "password \" | "
                "debconf-set-selections")

            try:
                SSSShellExec.cmd_exec(
                    self,
                    "echo \"mariadb-server-10.1 "
                    "mysql-server/root_password "
                    "password {chars}\" | "
                    "debconf-set-selections".format(chars=chars),
                    log=False)
            except CommandExecutionError as e:
                Log.error("Failed to initialize MySQL package")

            Log.debug(
                self, "echo \"mariadb-server-10.1 "
                "mysql-server/root_password_again "
                "password \" | "
                "debconf-set-selections")

            try:
                SSSShellExec.cmd_exec(
                    self,
                    "echo \"mariadb-server-10.1 "
                    "mysql-server/root_password_again "
                    "password {chars}\" | "
                    "debconf-set-selections".format(chars=chars),
                    log=False)
            except CommandExecutionError as e:
                Log.error("Failed to initialize MySQL package")

            mysql_config = """
            [client]
            user = root
            password = {chars}
            """.format(chars=chars)
            config = configparser.ConfigParser()
            config.read_string(mysql_config)
            Log.debug(self, 'Writting configuration into MySQL file')
            conf_path = "/etc/mysql/conf.d/my.cnf"
            os.makedirs(os.path.dirname(conf_path), exist_ok=True)
            with open(conf_path, encoding='utf-8', mode='w') as configfile:
                config.write(configfile)
            Log.debug(self, 'Setting my.cnf permission')
            SSSFileUtils.chmod(self, "/etc/mysql/conf.d/my.cnf", 0o600)

        if set(SSSVariables.sss_apache).issubset(set(apt_packages)):
            Log.info(self, "Adding repository for Apache, please wait...")
            SSSRepo.add(self, ppa=SSSVariables.sss_apache_repo)

        if set(SSSVariables.sss_php).issubset(set(apt_packages)):
            Log.info(self, "Adding repository for PHP, please wait...")
            Log.debug(self, 'Adding ppa for PHP')
            SSSRepo.add(self, ppa=SSSVariables.sss_php_repo)
Esempio n. 43
0
    def default(self):
        # All package update
        apt_packages = []
        packages = []

        if ((not self.app.pargs.web) and (not self.app.pargs.apache2)
                and (not self.app.pargs.php) and (not self.app.pargs.mysql)
                and (not self.app.pargs.all)):
            self.app.pargs.web = True

        if self.app.pargs.all:
            self.app.pargs.web = True

        if self.app.pargs.web:
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.apache2:
            if SSSAptGet.is_installed(self, 'apache2'):
                apt_packages = apt_packages + SSSVariables.sss_apache
            else:
                Log.info(self, "Apache is not already installed")

        if self.app.pargs.php:
            if SSSAptGet.is_installed(self, 'php7.0-fpm'):
                apt_packages = apt_packages + SSSVariables.sss_php
            else:
                Log.info(self, "PHP is not installed")

        if self.app.pargs.mysql:
            if SSSAptGet.is_installed(self, 'mariadb-server'):
                apt_packages = apt_packages + SSSVariables.sss_mysql
            else:
                Log.info(self, "MariaDB is not installed")

        if len(packages) or len(apt_packages):

            Log.info(
                self, "During package update process non Apache"
                " parts of your site may remain down")
            # Check prompt
            if (not self.app.pargs.no_prompt):
                start_upgrade = input("Do you want to continue:[y/N]")
                if start_upgrade != "Y" and start_upgrade != "y":
                    Log.error(self, "Not starting package update")

            Log.info(self, "Updating packages, please wait...")
            if len(apt_packages):
                # apt-get update
                SSSAptGet.update(self)
                # Update packages
                SSSAptGet.install(self, apt_packages)

                # Post Actions after package updates
                if set(SSSVariables.sss_apache).issubset(set(apt_packages)):
                    SSSService.restart_service(self, 'apache2')
                if set(SSSVariables.sss_php).issubset(set(apt_packages)):
                    SSSService.restart_service(self, 'php7.0-fpm')
                if set(SSSVariables.sss_mysql).issubset(set(apt_packages)):
                    SSSService.restart_service(self, 'mysql')

            if len(packages):
                pass
            Log.info(self, "Successfully updated packages")
def setupdatabase(self, data):
    sss_domain_name = data['site_name']
    sss_random = (''.join(random.sample(string.ascii_uppercase +
                 string.ascii_lowercase + string.digits, 15)))
    sss_replace_dot = sss_domain_name.replace('.', '_')
    prompt_dbname = self.app.config.get('mysql', 'db-name')
    prompt_dbuser = self.app.config.get('mysql', 'db-user')
    sss_mysql_grant_host = self.app.config.get('mysql', 'grant-host')
    sss_db_name = ''
    sss_db_username = ''
    sss_db_password = ''

    if prompt_dbname == 'True' or prompt_dbname == 'true':
        try:
            sss_db_name = input('Enter the MySQL database name [{0}]: '
                               .format(sss_replace_dot))
        except EOFError as e:
            Log.debug(self, "{0}".format(e))
            raise SiteError("Unable to input database name")

    if not sss_db_name:
        sss_db_name = sss_replace_dot

    if prompt_dbuser == 'True' or prompt_dbuser == 'true':
        try:
            sss_db_username = input('Enter the MySQL database user name [{0}]: '
                                   .format(sss_replace_dot))
            sss_db_password = getpass.getpass(prompt='Enter the MySQL database'
                                             ' password [{0}]: '
                                             .format(sss_random))
        except EOFError as e:
            Log.debug(self, "{0}".format(e))
            raise SiteError("Unable to input database credentials")

    if not sss_db_username:
        sss_db_username = sss_replace_dot
    if not sss_db_password:
        sss_db_password = sss_random

    if len(sss_db_username) > 16:
        Log.debug(self, 'Autofix MySQL username (ERROR 1470 (HY000)),'
                  ' please wait')
        sss_db_username = (sss_db_name[0:6] + generate_random())

    # create MySQL database

    Log.info(self, "Setting up database\t\t", end='')
    Log.debug(self, "Creating database {0}".format(sss_db_name))
    try:
        if SSSMysql.check_db_exists(self, sss_db_name):
            Log.debug(self, "Database already exists, Updating DB_NAME .. ")
            sss_db_name = (sss_db_name[0:6] + generate_random())
            sss_db_username = (sss_db_name[0:6] + generate_random())
    except MySQLConnectionError as e:
        raise SiteError("MySQL Connectivity problem occured")

    try:
        SSSMysql.execute(self, "create database `{0}`"
                        .format(sss_db_name))
    except StatementExcecutionError as e:
        Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKGREEN + "]")
        raise SiteError("create database execution failed")

    # Create MySQL User
    Log.debug(self, "Creating user {0}".format(sss_db_username))
    Log.debug(self, "create user `{0}`@`{1}` identified by ''"
              .format(sss_db_username, sss_mysql_grant_host))

    try:
        SSSMysql.execute(self,
                        "create user `{0}`@`{1}` identified by '{2}'"
                        .format(sss_db_username, sss_mysql_grant_host,
                                sss_db_password), log=False)
    except StatementExcecutionError as e:
        Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
        raise SiteError("creating user failed for database")

    # Grant permission
    Log.debug(self, "Setting up user privileges")

    try:
        SSSMysql.execute(self,
                        "grant all privileges on `{0}`.* to `{1}`@`{2}`"
                        .format(sss_db_name,
                                sss_db_username, sss_mysql_grant_host))
    except StatementExcecutionError as e:
        Log.info(self, "[" + Log.ENDC + Log.FAIL + "Failed" + Log.OKBLUE + "]")
        SiteError("grant privileges to user failed for database ")

    Log.info(self, "[" + Log.ENDC + "Done" + Log.OKBLUE + "]")

    data['sss_db_name'] = sss_db_name
    data['sss_db_user'] = sss_db_username
    data['sss_db_pass'] = sss_db_password
    data['sss_db_host'] = SSSVariables.sss_mysql_host
    data['sss_mysql_grant_host'] = sss_mysql_grant_host
    return(data)
Esempio n. 45
0
    def install(self, packages=[], apt_packages=[], disp_msg=True):
        """Start installation of packages"""
        self.msg = []
        try:
            # Default action for stack installation
            if ((not self.app.pargs.web) and (not self.app.pargs.apache2) and
                (not self.app.pargs.php) and (not self.app.pargs.mysql) and 
                (not self.app.pargs.phpmyadmin)):
                self.app.pargs.web = True
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                self.app.pargs.mysql = True

            if self.app.pargs.all:
                self.app.pargs.web = True
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                #self.app.pargs.mysql = True

            if self.app.pargs.web:
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                #self.app.pargs.mysql = True
                #self.app.pargs.wpcli = True
                #self.app.pargs.postfix = True

            if self.app.pargs.apache2:
                Log.debug(self, "Setting apt_packages variable for Apache2")
                if not SSSAptGet.is_installed(self,'apache2'):
                    apt_packages = apt_packages + SSSVariables.sss_apache

                else :
                    Log.debug(self, "Apache2 already installed")
                    Log.info(self, "Apache2 already installed")

            if self.app.pargs.php:
                Log.debug(self,"Setting apt_packages variable for PHP")
                if not SSSAptGet.is_installed(self,'php7.0-fpm'):
                    apt_packages = apt_packages + SSSVariables.sss_php
                else:
                    Log.debug(self, "PHP already installed")
                    Log.info(self, "PHP already installed")

            if self.app.pargs.mysql:
                Log.debug(self,"Setting apt_packages variable for MySQL")
                if not SSSShellExec.cmd_exec(self,"mysqladmin ping"):
                    apt_packages = apt_packages + SSSVariables.sss_mysql
                    packages = packages + [["https://raw."
                                            "githubusercontent.com"
                                            "/serversetup/tuning-primer/"
                                            "master/tuning-primer.sh",
                                            "/usr/bin/tuning-primer",
                                            "Tuning-Primer"]]
                else:
                    Log.debug(self, "MySQL connection is already alive")
                    Log.info(self, "MySQL connection is already alive")

            if self.app.pargs.phpmyadmin:
                Log.debug(self, "Setting packages varible for phpMyAdmin ")
                packages = packages + [["https://github.com/phpmyadmin/"
                                        "phpmyadmin/archive/STABLE.tar.gz",
                                        "/tmp/pma.tar.gz", "phpMyAdmin"]]


        except Exception as e:
            pass

        if len(apt_packages) or len(packages):
            Log.debug(self,"Calling pre_pref")
            self.pre_pref(apt_packages)
            if len(apt_packages):
                SSSSwap.add(self)
                Log.info(self, "Updating Apt-cache, please wait...")
                SSSAptGet.update(self)
                Log.info(self, "Installing packages, please wait...")
                SSSAptGet.install(self, apt_packages)
                SSSShellExec.cmd_exec(self, "a2enmod proxy_fcgi proxy proxy_http http2 ssl expires headers rewrite")
            if len(packages):
                Log.debug(self, "Downloading following: {0}".format(packages))
                SSSDownload.download(self, packages)
            Log.debug(self, "Calling post_pref")
            self.post_pref(apt_packages, packages)

            if disp_msg:
                if len(self.msg):
                    for msg in self.msg:
                        Log.info(self, Log.ENDC + msg)
                Log.info(self, "Successfully installed packages")
            else:
                return self.msg
def setupdomain(self, data):

    sss_domain_name = data['site_name']
    sss_site_webroot = data['webroot'] if 'webroot' in data.keys() else ''

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

    Log.info(self, "Setting up Apache configuration \t", end='')
    # write apache config for file
    try:
        sss_site_apache_conf = open('/etc/apache2/sites-available/{0}.conf'
                                  .format(sss_domain_name), encoding='utf-8',
                                  mode='w')

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


    # create Symbolic link
    SSSFileUtils.create_symlink(self, ['/etc/apache2/sites-available/{0}.conf'
                                      .format(sss_domain_name),
                                      '/etc/apache2/sites-enabled/{0}.conf'
                                      .format(sss_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(sss_site_webroot)):
            os.makedirs('{0}/htdocs'.format(sss_site_webroot))
        if not os.path.exists('{0}/logs'.format(sss_site_webroot)):
            os.makedirs('{0}/logs'.format(sss_site_webroot))

        # Create log file if not exists
        if not os.path.isfile('/var/log/apache2/{0}.access.log'
                                          .format(sss_domain_name)):
                    with open('/var/log/apache2/{0}.access.log'
                                          .format(sss_domain_name),
                              encoding='utf-8', mode='a') as logfile:
                        logfile.close()
        if not os.path.isfile('/var/log/apache2/{0}.error.log'
                                          .format(sss_domain_name)):
                    with open('/var/log/apache2/{0}.error.log'
                                          .format(sss_domain_name),
                              encoding='utf-8', mode='a') as logfile:
                        logfile.close()

        SSSFileUtils.create_symlink(self, ['/var/log/apache2/{0}.access.log'
                                          .format(sss_domain_name),
                                          '{0}/logs/access.log'
                                          .format(sss_site_webroot)])
        SSSFileUtils.create_symlink(self, ['/var/log/apache2/{0}.error.log'
                                          .format(sss_domain_name),
                                          '{0}/logs/error.log'
                                          .format(sss_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(sss_site_webroot)) and
           os.path.exists('{0}/logs'.format(sss_site_webroot))):
            Log.info(self, "[" + Log.ENDC + "Done" + Log.OKGREEN + "]")
        else:
            Log.info(self, "[" + Log.ENDC + "Fail" + Log.OKGREEN + "]")
            raise SiteError("setup webroot failed for site")
Esempio n. 47
0
    def purge(self):
        """Start purging of packages"""
        apt_packages = []
        packages = []

        # Default action for stack remove
        if ((not self.app.pargs.web) and (not self.app.pargs.apache2) and
            (not self.app.pargs.php) and (not self.app.pargs.mysql) and
            (not self.app.pargs.phpmyadmin)):
                self.app.pargs.web = True
                self.app.pargs.apache2 = True
                self.app.pargs.php = True
                self.app.pargs.mysql = True

        if self.app.pargs.all:
            self.app.pargs.web = True
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True

        if self.app.pargs.web:
            self.app.pargs.apache2 = True
            self.app.pargs.php = True
            self.app.pargs.mysql = True
            #self.app.pargs.wpcli = True
            #self.app.pargs.postfix = True

        if self.app.pargs.apache2:
            Log.debug(self, "Purge apt_packages variable of Apache")
            apt_packages = apt_packages + SSSVariables.sss_apache
        if self.app.pargs.php:
            Log.debug(self, "Purge apt_packages variable PHP")
            apt_packages = apt_packages + SSSVariables.sss_php
        if self.app.pargs.mysql:
            Log.debug(self,"Removing apt_packages variable of PHP")
            apt_packages = apt_packages + SSSVariables.sss_mysql
            packages = packages + ['/usr/bin/tuning-primer']
        if self.app.pargs.phpmyadmin:
            packages = packages + ['{0}22222/htdocs/db/pma'.
                                   format(SSSVariables.sss_webroot)]
            Log.debug(self, "Purge package variable phpMyAdmin")

        if len(packages) or len(apt_packages):
            sss_prompt = input('Are you sure you to want to purge '
                              'from server '
                              'along with their configuration'
                              ' packages,\nAny answer other than '
                              '"yes" will be stop this '
                              'operation :')

            if sss_prompt == 'YES' or sss_prompt == 'yes':
                if len(apt_packages):
                    Log.info(self, "Purging packages, please wait...")
                    SSSAptGet.remove(self, apt_packages, purge=True)
                    SSSAptGet.auto_remove(self)

                if len(packages):
                    SSSFileUtils.remove(self, packages)
                    SSSAptGet.auto_remove(self)

                Log.info(self, "Successfully purged packages")
 def default(self):
     Log.info(self, "This command is deprecated."
              " You can use this command instead, " +
              Log.ENDC + Log.BOLD + "\n`sss debug --import-slow-log`" +
              Log.ENDC)
Esempio n. 49
0
    def pre_pref(self,apt_packages):
        """Pre settings to do before installation packages"""

        if set(SSSVariables.sss_mysql).issubset(set(apt_packages)):
            Log.info(self,"Adding repository for MySQL, 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)

            SSSRepo.add(self, repo_url=SSSVariables.sss_mysql_repo)
            Log.debug(self, 'Adding key for {0}'
                        .format(SSSVariables.sss_mysql_repo))
            SSSRepo.add_key(self, '0xcbcb082a1bb943db',
                               keyserver="keyserver.ubuntu.com")
            chars = ''.join(random.sample(string.ascii_letters, 8))
            Log.debug(self, "Pre-seeding MySQL")
            Log.debug(self, "echo \"mariadb-server-10.1 "
                      "mysql-server/root_password "
                      "password \" | "
                      "debconf-set-selections")

            try:
                SSSShellExec.cmd_exec(self, "echo \"mariadb-server-10.1 "
                                     "mysql-server/root_password "
                                     "password {chars}\" | "
                                     "debconf-set-selections"
                                     .format(chars=chars),
                                     log=False)
            except CommandExecutionError as e:
                Log.error("Failed to initialize MySQL package")

            Log.debug(self, "echo \"mariadb-server-10.1 "
                      "mysql-server/root_password_again "
                      "password \" | "
                      "debconf-set-selections")

            try:
                SSSShellExec.cmd_exec(self, "echo \"mariadb-server-10.1 "
                                     "mysql-server/root_password_again "
                                     "password {chars}\" | "
                                     "debconf-set-selections"
                                     .format(chars=chars),
                                     log=False)
            except CommandExecutionError as e:
                Log.error("Failed to initialize MySQL package")

            mysql_config = """
            [client]
            user = root
            password = {chars}
            """.format(chars=chars)
            config = configparser.ConfigParser()
            config.read_string(mysql_config)
            Log.debug(self, 'Writting configuration into MySQL file')
            conf_path = "/etc/mysql/conf.d/my.cnf"
            os.makedirs(os.path.dirname(conf_path), exist_ok=True)
            with open(conf_path, encoding='utf-8',
                      mode='w') as configfile:
                config.write(configfile)
            Log.debug(self, 'Setting my.cnf permission')
            SSSFileUtils.chmod(self, "/etc/mysql/conf.d/my.cnf", 0o600)

        if set(SSSVariables.sss_apache).issubset(set(apt_packages)):
            Log.info(self, "Adding repository for Apache, please wait...")
            SSSRepo.add(self, ppa=SSSVariables.sss_apache_repo)

        if set(SSSVariables.sss_php).issubset(set(apt_packages)):
            Log.info(self, "Adding repository for PHP, please wait...")
            Log.debug(self, 'Adding ppa for PHP')
            SSSRepo.add(self, ppa=SSSVariables.sss_php_repo)