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 setwebrootpermissions(self, webroot): Log.debug(self, "Setting up permissions") try: SSSFileUtils.chown(self, webroot, SSSVariables.sss_php_user, SSSVariables.sss_php_user, recursive=True) except Exception as e: Log.debug(self, str(e)) raise SiteError("problem occured while setting up webroot permissions")
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 addNewSite(self, site, stype, cache, path, enabled=True, ssl=False, fs='ext4', db='mysql', db_name=None, db_user=None, db_password=None, db_host='localhost', hhvm=0, pagespeed=0): """ Add New Site record information into sss database. """ try: newRec = SiteDB(site, stype, cache, path, enabled, ssl, fs, db, db_name, db_user, db_password, db_host, hhvm, pagespeed) 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")
def removeApacheConf(self, domain): if os.path.isfile("/etc/apache2/sites-available/{0}.conf".format(domain)): Log.debug(self, "Removing Apache configuration") SSSFileUtils.rm(self, "/etc/apache2/sites-enabled/{0}.conf".format(domain)) SSSFileUtils.rm(self, "/etc/apache2/sites-available/{0}.conf".format(domain)) SSSService.reload_service(self, "apache2") SSSGit.add(self, ["/etc/apache2"], msg="Deleted {0} ".format(domain))
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(SSSVariables.sss_user)) self.app.pargs.user_input = username if username == "": self.app.pargs.user_input = SSSVariables.sss_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/apache2/htpasswd-sss 2>/dev/null") SSSShellExec.cmd_exec(self, "printf \"{username}:" "$(openssl passwd -crypt " "{password} 2> /dev/null)\n\"" "> /etc/apache2/htpasswd-sss 2>/dev/null" .format(username=self.app.pargs.user_input, password=self.app.pargs.user_pass), log=False) SSSGit.add(self, ["/etc/apache2"], msg="Adding changed secure auth into Git")
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))
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))
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(SSSVariables.sss_user)) self.app.pargs.user_input = username if username == "": self.app.pargs.user_input = SSSVariables.sss_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/apache2/htpasswd-sss 2>/dev/null") SSSShellExec.cmd_exec(self, "printf \"{username}:" "$(openssl passwd -crypt " "{password} 2> /dev/null)\n\"" "> /etc/apache2/htpasswd-sss 2>/dev/null".format( username=self.app.pargs.user_input, password=self.app.pargs.user_pass), log=False) SSSGit.add(self, ["/etc/apache2"], msg="Adding changed secure auth into Git")
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
def removeApacheConf(self, domain): if os.path.isfile('/etc/apache2/sites-available/{0}.conf'.format(domain)): Log.debug(self, "Removing Apache configuration") SSSFileUtils.rm(self, '/etc/apache2/sites-enabled/{0}.conf'.format(domain)) SSSFileUtils.rm(self, '/etc/apache2/sites-available/{0}.conf'.format(domain)) SSSService.reload_service(self, 'apache2') SSSGit.add(self, ["/etc/apache2"], msg="Deleted {0} ".format(domain))
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)): 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 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 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)): 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 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 check_db_exists(self, db_name): try: if SSSMysql.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
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)
def getAllsites(self): """ 1. returns all records from sss 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")
def getSiteInfo(self, site): """ Retrieves site record from sss 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")
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): """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 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 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.")
def extract(self, file, path): """Function to extract tar.gz file""" try: tar = tarfile.open(file) tar.extractall(path=path) tar.close() os.remove(file) return True except tarfile.TarError as e: Log.debug(self, "{0}".format(e)) Log.error(self, 'Unable to extract file \{0}'.format(file)) return False
def extract(self, file, path): """Function to extract tar.gz file""" try: tar = tarfile.open(file) tar.extractall(path=path) tar.close() os.remove(file) return True except tarfile.TarError as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to extract file \{0}".format(file)) return False
def isexist(self, path): """ Check if file exist on given path """ try: if os.path.exists(path): return (True) else: return (False) except OSError as e: Log.debug(self, "{0}".format(e.strerror)) Log.error(self, "Unable to check path {0}".format(path))
def auto_clean(self): """ Similar to `apt-get autoclean` """ try: orig_out = sys.stdout sys.stdout = open(self.app.config.get("log.logging", "file"), encoding="utf-8", mode="a") apt_get.autoclean("-y") sys.stdout = orig_out except ErrorReturnCode as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to apt-get autoclean")
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!!!")
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: SSSShellExec.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/" + SSSVariables().sss_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")
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!!!")
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: SSSShellExec.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/" + SSSVariables().sss_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")
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)
def connect(self): """Makes connection with MySQL server""" try: if os.path.exists('/etc/mysql/conf.d/my.cnf'): connection = pymysql.connect(read_default_file='/etc/mysql/conf.d/my.cnf') else: 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
def auto_clean(self): """ Similar to `apt-get autoclean` """ try: orig_out = sys.stdout sys.stdout = open(self.app.config.get('log.logging', 'file'), encoding='utf-8', mode='a') apt_get.autoclean("-y") sys.stdout = orig_out except ErrorReturnCode as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to apt-get autoclean")
def connect(self): """Makes connection with MySQL server""" try: if os.path.exists('/etc/mysql/conf.d/my.cnf'): connection = pymysql.connect( read_default_file='/etc/mysql/conf.d/my.cnf') else: 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
def deleteSiteInfo(self, site): """Delete 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") if not q: Log.error(self, "{0} does not exist in database".format(site)) try: db_session.delete(q) db_session.commit() except Exception as e: Log.debug(self, "{0}".format(e)) Log.error(self, "Unable to delete site from application database.")
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 cmd_exec(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: return True else: Log.debug( self, "Command Output: {0}, \nCommand Error: {1}".format( cmd_stdout, cmd_stderr)) return False except OSError as e: Log.debug(self, str(e)) raise CommandExecutionError except Exception as e: Log.debug(self, str(e)) raise CommandExecutionError
def cmd_exec(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: return True else: Log.debug(self, "Command Output: {0}, \nCommand Error: {1}" .format(cmd_stdout, cmd_stderr)) return False except OSError as e: Log.debug(self, str(e)) raise CommandExecutionError except Exception as e: Log.debug(self, str(e)) raise CommandExecutionError
def rm(self, path): """ Remove files """ Log.debug(self, "Removing {0}".format(path)) if SSSFileUtils.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))
def copyfile(self, src, dest): """ Copies files: 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, "{e}".format(e.strerror)) Log.error(self, "Unable to copy file from {0} to {1}".format(src, dest))
def copyfile(self, src, dest): """ Copies files: 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, "{e}".format(e.strerror)) Log.error(self, "Unable to copy file from {0} to {1}" .format(src, dest))
def rm(self, path): """ Remove files """ Log.debug(self, "Removing {0}".format(path)) if SSSFileUtils.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))
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
def execute(self, statement, errormsg='', log=True): """Get login details from /etc/mysql/conf.d/my.cnf & Execute MySQL query""" connection = SSSMysql.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()
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
def execute(self, statement, errormsg='', log=True): """Get login details from /etc/mysql/conf.d/my.cnf & Execute MySQL query""" connection = SSSMysql.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()
def site_package_check(self, stype): apt_packages = [] packages = [] stack = SSSStackController() stack.app = self.app if stype in ["html", "php", "mysql"]: Log.debug(self, "Setting apt_packages variables for Apache") # check if server has apache if not SSSAptGet.is_installed(self, "apache2"): apt_packages = apt_packages + SSSVariables.sss_apache if stype in ["php", "mysql"]: Log.debug(self, "Setting apt_packages variables for PHP") # check if server has php if not SSSAptGet.is_installed(self, "php7.0-fpm"): apt_packages = apt_packages + SSSVariables.sss_php if stype in ["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", ] ] return stack.install(apt_packages=apt_packages, packages=packages, disp_msg=False)
def default(self): """default function for info""" if (not self.app.pargs.apache2 and not self.app.pargs.php and not self.app.pargs.mysql): self.app.pargs.apache2 = False self.app.pargs.php = True self.app.pargs.mysql = True if self.app.pargs.apache2: if SSSAptGet.is_installed(self, 'apache2'): self.info_apache2() else: Log.error(self, "Apache is not installed") if self.app.pargs.php: if SSSAptGet.is_installed(self, 'php7.0-fpm'): self.info_php() else: Log.error(self, "PHP5 is not installed") if self.app.pargs.mysql: if SSSShellExec.cmd_exec(self, "mysqladmin ping"): self.info_mysql() else: Log.error(self, "MySQL is not installed")
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 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")
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 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")
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_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")