Browse Source

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

bugfixes
shital.rtcamp 10 years ago
parent
commit
49c4e964bf
  1. 21
      ee/core/apt_repo.py
  2. 24
      ee/core/aptget.py
  3. 3
      ee/core/database.py
  4. 4
      ee/core/domainvalidate.py
  5. 58
      ee/core/fileutils.py
  6. 7
      ee/core/git.py
  7. 16
      ee/core/logging.py
  8. 3
      ee/core/models.py
  9. 161
      ee/core/services.py
  10. 3
      ee/core/shellexec.py
  11. 114
      upgrade

21
ee/core/apt_repo.py

@ -12,6 +12,14 @@ class EERepo():
pass
def add(self, repo_url=None, ppa=None):
"""
This function used to add apt repositories and or ppa's
If repo_url is provided adds repo file to
/etc/apt/sources.list.d/
If ppa is provided add apt-repository using
add-apt-repository
command.
"""
if repo_url is not None:
repo_file_path = ("/etc/apt/sources.list.d/"
@ -43,12 +51,23 @@ class EERepo():
"'{ppa_name}'"
.format(ppa_name=ppa))
def remove(self, repo_url=None):
def remove(self, ppa=None):
"""
This function used to remove ppa's
If ppa is provided adds repo file to
/etc/apt/sources.list.d/
command.
"""
EEShellExec.cmd_exec(self, "add-apt-repository -y "
"--remove '{ppa_name}'"
.format(ppa_name=repo_url))
def add_key(self, keyids, keyserver=None):
"""
This function adds imports repository keys from keyserver.
default keyserver is hkp://keys.gnupg.net
user can provide other keyserver with keyserver="hkp://xyz"
"""
if keyserver is None:
EEShellExec.cmd_exec(self, "gpg --keyserver {serv}"
.format(serv=(keyserver or

24
ee/core/aptget.py

@ -10,6 +10,9 @@ class EEAptGet():
"""Generic apt-get intialisation"""
def update(self):
"""
Similar to `apt-get upgrade`
"""
try:
apt_cache = apt.cache.Cache()
apt_cache.update()
@ -22,6 +25,9 @@ class EEAptGet():
Log.error(self, 'AttributeError: ' + str(e))
def dist_upgrade():
"""
Similar to `apt-get upgrade`
"""
apt_cache = apt.cache.Cache()
apt_cache.update()
apt_cache.open(None)
@ -33,7 +39,9 @@ class EEAptGet():
return success
def install(self, packages):
"""Installation of packages"""
"""
Similar to `apt-get install`
"""
apt_pkg.init()
# #apt_pkg.PkgSystemLock()
global apt_cache
@ -79,6 +87,10 @@ class EEAptGet():
continue
def remove(self, packages, auto=False, purge=False):
"""
Similar to `apt-get remove/purge`
purge packages if purge=True
"""
apt_pkg.init()
# apt_pkg.PkgSystemLock()
global apt_cache
@ -122,6 +134,9 @@ class EEAptGet():
continue
def auto_clean(self):
"""
Similar to `apt-get autoclean`
"""
try:
apt_get.autoclean("-y")
except ErrorReturnCode as e:
@ -129,6 +144,9 @@ class EEAptGet():
Log.error(self, "Unable to apt-get autoclean")
def auto_remove(self):
"""
Similar to `apt-get autoremove`
"""
try:
Log.debug(self, "Running apt-get autoremove")
apt_get.autoremove("-y")
@ -137,6 +155,10 @@ class EEAptGet():
Log.error(self, "Unable to apt-get autoremove")
def is_installed(self, package_name):
"""
Checks if package is available in cache and is installed or not
returns True if installed otherwise returns False
"""
apt_cache = apt.cache.Cache()
apt_cache.open()
if (package_name.strip() in apt_cache and

3
ee/core/database.py

@ -13,6 +13,9 @@ Base.query = db_session.query_property()
def init_db():
"""
Initializes and creates all tables from models into the database
"""
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()

4
ee/core/domainvalidate.py

@ -3,6 +3,10 @@ from urllib.parse import urlparse
def ValidateDomain(url):
"""
This function returns domain name removing http:// and https://
returns domain name only with or without www as user provided.
"""
# Check if http:// or https:// present remove it if present
domain_name = url.split('/')

58
ee/core/fileutils.py

@ -10,11 +10,12 @@ from ee.core.logging import Log
class EEFileUtils():
"""Method to operate on files"""
"""Utilities to operate on files"""
def __init__():
pass
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=' ')
@ -33,6 +34,10 @@ class EEFileUtils():
Log.error(self, 'Unable to Remove file ')
def create_symlink(self, paths, errormsg=''):
"""
Create symbolic links provided in list with first as source
and second as destination
"""
src = paths[0]
dst = paths[1]
if not os.path.islink(dst):
@ -45,6 +50,9 @@ class EEFileUtils():
Log.debug(self, "Destination: {0} exists".format(dst))
def remove_symlink(self, filepath):
"""
Removes symbolic link for the path provided with filepath
"""
try:
os.unlink(filepath)
except Exception as e:
@ -52,6 +60,11 @@ class EEFileUtils():
Log.error(self, "Unable to reomove symbolic link ...\n")
def copyfile(self, src, dest):
"""
Copies files:
src : source path
dest : destination path
"""
try:
shutil.copy2(src, dest)
except shutil.Error as e:
@ -64,6 +77,12 @@ class EEFileUtils():
.fromat(src, dest))
def searchreplace(self, fnm, sstr, rstr):
"""
Search replace strings in file
fnm : filename
sstr: search string
rstr: replace string
"""
try:
for line in fileinput.input(fnm, inplace=True):
print(line.replace(sstr, rstr), end='')
@ -74,6 +93,11 @@ class EEFileUtils():
.format(fnm, sstr, rstr))
def mvfile(self, src, dst):
"""
Moves file from source path to destination path
src : source path
dst : Destination path
"""
try:
Log.debug(self, "Moving file from {0} to {1}".format(src, dst))
shutil.move(src, dst)
@ -83,6 +107,10 @@ class EEFileUtils():
.format(src, dst))
def chdir(self, path):
"""
Change Directory to path specified
Path : path for destination directory
"""
try:
os.chdir(path)
except OSError as e:
@ -90,6 +118,14 @@ class EEFileUtils():
Log.error(self, 'Unable to Change Directory {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:
@ -111,6 +147,12 @@ class EEFileUtils():
Log.error(self, "Unable to change owner : {0} ".format(path))
def chmod(self, path, perm, recursive=False):
"""
Changes Permission for files
path : file path permission to be changed
perm : permissions to be given
recursive: change permission recursively for all files
"""
try:
if recursive:
for root, dirs, files in os.walk(path):
@ -125,6 +167,11 @@ class EEFileUtils():
Log.error(self, "Unable to change owner : {0}".format(path))
def mkdir(self, path):
"""
create directories.
path : path for directory to be created
Similar to `mkdir -p`
"""
try:
os.makedirs(path)
except OSError as e:
@ -132,6 +179,9 @@ class EEFileUtils():
Log.error(self, "Unable to create directory {0} ".format(path))
def isexist(self, path):
"""
Check if file exist on given path
"""
try:
if os.path.exists(path):
return (True)
@ -142,6 +192,9 @@ class EEFileUtils():
Log.error(self, "Unable to check path {0}".format(path))
def grep(self, fnm, sstr):
"""
Searches for string in file and returns the matched line.
"""
try:
for line in open(fnm):
if sstr in line:
@ -152,6 +205,9 @@ class EEFileUtils():
.format(sstr, fnm))
def rm(self, path):
"""
Remove files
"""
if EEFileUtils.isexist(self, path):
try:
if os.path.isdir(path):

7
ee/core/git.py

@ -11,6 +11,10 @@ class EEGit:
pass
def add(self, paths, msg="Intializating"):
"""
Initializes Directory as repository if not already git repo.
and adds uncommited changes automatically
"""
for path in paths:
global git
git = git.bake("--git-dir={0}/.git".format(path),
@ -40,6 +44,9 @@ class EEGit:
Log.debug(self, "EEGit: Path {0} not present".format(path))
def checkfilestatus(self, repo, filepath):
"""
Checks status of file, If its tracked or untracked.
"""
global git
git = git.bake("--git-dir={0}/.git".format(repo),
"--work-tree={0}".format(repo))

16
ee/core/logging.py

@ -2,6 +2,10 @@
class Log:
"""
Logs messages with colors for different messages
according to functions
"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
@ -12,16 +16,28 @@ class Log:
UNDERLINE = '\033[4m'
def error(self, msg):
"""
Logs error into log file
"""
print(Log.FAIL + msg + Log.ENDC)
self.app.log.error(Log.FAIL + msg + Log.ENDC)
self.app.close(1)
def info(self, msg, end='\n'):
"""
Logs info messages into log file
"""
print(Log.OKBLUE + msg + Log.ENDC, end=end)
self.app.log.info(Log.OKBLUE + msg + Log.ENDC)
def warn(self, msg):
"""
Logs warning into log file
"""
self.app.log.warn(Log.BOLD + msg + Log.ENDC)
def debug(self, msg):
"""
Logs debug messages into log file
"""
self.app.log.debug(Log.HEADER + msg + Log.ENDC)

3
ee/core/models.py

@ -3,6 +3,9 @@ from ee.core.database import Base
class SiteDB(Base):
"""
Databse model for site table
"""
__tablename__ = 'sites'
id = Column(Integer, primary_key=True)
sitename = Column(String, unique=True)

161
ee/core/services.py

@ -13,91 +13,106 @@ class EEService():
pass
def start_service(self, service_name):
try:
Log.info(self, "Start : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} start'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to start service {0}"
.format(service_name))
"""
start service
Similar to `service xyz start`
"""
try:
Log.info(self, "Start : {0:10}" .format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} start'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to start service {0}"
.format(service_name))
def stop_service(self, service_name):
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.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to stop service : {0}"
.format(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.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to stop service : {0}"
.format(service_name))
def restart_service(self, service_name):
try:
Log.info(self, "Restart : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} restart'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0} {1}".format(e.errno, e.strerror))
Log.error(self, "\nFailed to restart service : {0}"
.format(service_name))
"""
Restart service
Similar to `service xyz restart`
"""
try:
Log.info(self, "Restart : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} restart'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0} {1}".format(e.errno, e.strerror))
Log.error(self, "\nFailed to restart service : {0}"
.format(service_name))
def reload_service(self, service_name):
try:
if service_name in ['nginx', 'php5-fpm']:
Log.info(self, "Reload : {0:10}".format(service_name),
end='')
retcode = subprocess.getstatusoutput('{0} -t &&'
' service {0} reload'
.format(service_name))
if retcode[0] == 0:
# print(retcode[0])
# subprocess.getstatusoutput('service {0} reload'
# .format(service_name))
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE +
"]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" +
Log.OKBLUE+"]")
return False
Log.info(self, "Reload : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} reload'
"""
Stop service
Similar to `service xyz stop`
"""
try:
if service_name in ['nginx', 'php5-fpm']:
Log.info(self, "Reload : {0:10}".format(service_name),
end='')
retcode = subprocess.getstatusoutput('{0} -t &&'
' service {0} reload'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
# print(retcode[0])
# subprocess.getstatusoutput('service {0} reload'
# .format(service_name))
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE +
"]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
Log.info(self, "[" + Log.FAIL + "Failed" +
Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to reload service {0}"
.format(service_name))
Log.info(self, "Reload : {0:10}".format(service_name), end='')
retcode = subprocess.getstatusoutput('service {0} reload'
.format(service_name))
if retcode[0] == 0:
Log.info(self, "[" + Log.ENDC + "OK" + Log.OKBLUE + "]")
return True
else:
Log.debug(self, "{0}".format(retcode[1]))
Log.info(self, "[" + Log.FAIL + "Failed" + Log.OKBLUE+"]")
return False
except OSError as e:
Log.debug(self, "{0}".format(e))
Log.error(self, "\nFailed to reload service {0}"
.format(service_name))
def get_service_status(self, service_name):
try:

3
ee/core/shellexec.py

@ -36,6 +36,9 @@ class EEShellExec():
.format(command))
def invoke_editor(self, filepath, errormsg=''):
"""
Open files using sensible editor
"""
try:
subprocess.call(['sensible-editor', filepath])
except OSError as e:

114
upgrade

@ -0,0 +1,114 @@
#!/bin/bash
# EasyEngine update script.
# This script is designed to update current EasyEngine from 2.2.2 to 3.x
# Define echo function
old_ee_version="2.2.3"
branch=$1
# Blue color
function ee_lib_echo()
{
echo $(tput setaf 4)$@$(tput sgr0)
}
# White color
function ee_lib_echo_info()
{
echo $(tput setaf 7)$@$(tput sgr0)
}
# Red color
function ee_lib_echo_fail()
{
echo $(tput setaf 1)$@$(tput sgr0)
}
# Checking permissions
if [[ $EUID -ne 0 ]]; then
ee_lib_echo_fail "Sudo privilege required..."
ee_lib_echo_fail "Uses: wget -qO ee rt.cx/ee && sudo bash ee"
exit 1
fi
# Check old EasyEngine is installed or not
if [ ! -f /usr/local/sbin/easyengine ]; then
ee_lib_echo_fail "EasyEngine 2.0 not found"
exit 1
fi
# Check old EasyEngine version
ee version | grep ${old_ee_version} &>> /dev/null
if [[ $? -ne 0 ]]; then
ee_lib_echo_fail "EasyEngine $old_ee_version not found on your system"
ee_lib_echo_fail "Please update is using command: ee update"
exit 1
fi
# Capture errors
function ee_lib_error()
{
echo "[ `date` ] $(tput setaf 1)$@$(tput sgr0)"
exit $2
}
# Execute: apt-get update
ee_lib_echo "Executing apt-get update"
apt-get update &>> /dev/null
# Install Python3 on users system
ee_lib_echo "Installing Python3"
apt-get -y install python3 python3-apt python3-setuptools python3-dev
if [[ $? -ne 0 ]]; then
ee_lib_echo_fail "Unable to install Python3 on system"
exit 1
fi
# Remove old version of EasyEngine (ee)
rm -rf /tmp/easyengine &>> /dev/null
# Clone EE 3.0 Python branch
ee_lib_echo "Cloning EasyEngine 3.0"
if [ "$branch" = "" ]; then
branch=python
fi
git clone -b $branch https://github.com/rtCamp/easyengine.git /tmp/easyengine > /dev/null || ee_lib_error "Unable to clone EasyEngine, exit status" 1
cd /tmp/easyengine
ee_lib_echo "Installing EasyEngine 3.0"
python3 setup.py install || ee_lib_error "Unable to install EasyEngine 3.0, exit status " 1
# Preserve old configuration
ee_lib_echo "Updating EasyEngine 3.0 configuration"
grant_host=$(grep grant-host /etc/easyengine/ee.conf | awk '{ print $3 }')
db_name=$(grep db-name /etc/easyengine/ee.conf | awk '{ print $3 }')
db_user=$(grep db-name /etc/easyengine/ee.conf | awk '{ print $3 }')
wp_prefix=$(grep prefix /etc/easyengine/ee.conf | awk '{ print $3 }')
wp_user=$(grep 'user ' /etc/easyengine/ee.conf | grep -v db-user |awk '{ print $3 }')
wp_pass=$(grep password /etc/easyengine/ee.conf | awk '{ print $3 }')
wp_email=$(grep email /etc/easyengine/ee.conf | awk '{ print $3 }')
ip_addr=$(grep ip-address /etc/easyengine/ee.conf |awk -F'=' '{ print $2 }')
sed -i "s/ip-address.*/ip-address = ${ip_addr}/" /etc/ee/ee.conf && \
sed -i "s/grant-host.*/grant-host = ${grant_host}/" /etc/ee/ee.conf && \
sed -i "s/db-name.*/db-name = ${db-name}/" /etc/ee/ee.conf && \
sed -i "s/db-user.*/db-user = ${db_user}/" /etc/ee/ee.conf && \
sed -i "s/prefix.*/prefix = ${wp_prefix}/" /etc/ee/ee.conf && \
sed -i "s/^user.*/user = ${wp_user}/" /etc/ee/ee.conf && \
sed -i "s/password.*/password = ${wp_password}/" /etc/ee/ee.conf && \
sed -i "s/email.*/email = ${wp_email}/" /etc/ee/ee.conf || ee_lib_error "Unable to update configuration, exit status " 1
# Remove old EasyEngine
ee_lib_echo "Removing EasyEngine 2"
rm -rf /etc/bash_completion.d/ee /etc/easyengine/ /usr/share/easyengine/ /usr/local/lib/easyengine /usr/local/sbin/easyengine /usr/local/sbin/ee /var/log/easyengine
ee_lib_echo "Doing GIT init"
cd /etc/ee
if [ ! -d /etc/ee/.git ]; then
git init > /dev/null
fi
git commit -am "Update EasyEngine 2 to EasyEngine 3" > /dev/null
ee_lib_echo "Successfully update to EasyEngine 3"
Loading…
Cancel
Save