From b3297c0c5e692f3ebc14bcd59974159e8c5e00ed Mon Sep 17 00:00:00 2001 From: gau1991 Date: Thu, 12 Feb 2015 14:26:57 +0530 Subject: [PATCH] Attempt to Fix #409, Python locale issue --- ee/cli/plugins/debug.py | 19 +++--- ee/cli/plugins/site.py | 7 ++- ee/cli/plugins/site_functions.py | 3 +- ee/cli/plugins/stack.py | 104 ++++++++++++++++++++----------- ee/core/addswap.py | 3 +- ee/core/apt_repo.py | 9 ++- ee/core/aptget.py | 24 +++---- ee/core/checkfqdn.py | 4 +- ee/core/fileutils.py | 2 +- ee/core/logwatch.py | 4 +- install | 85 ++++++++++++++++++------- 11 files changed, 174 insertions(+), 90 deletions(-) diff --git a/ee/cli/plugins/debug.py b/ee/cli/plugins/debug.py index 0e384246..9728a563 100644 --- a/ee/cli/plugins/debug.py +++ b/ee/cli/plugins/debug.py @@ -63,7 +63,7 @@ class EEDebugController(CementBaseController): debug_address = ['0.0.0.0/0'] for ip_addr in debug_address: if not ("debug_connection "+ip_addr in open('/etc/nginx/' - 'nginx.conf').read()): + 'nginx.conf', encoding='utf-8').read()): Log.info(self, "Setting up Nginx debug connection" " for "+ip_addr) EEShellExec.cmd_exec(self, "sed -i \"/events {{/a\\ \\ \\ " @@ -79,7 +79,8 @@ class EEDebugController(CementBaseController): # stop global debug elif not self.start and not self.app.pargs.site_name: - if "debug_connection " in open('/etc/nginx/nginx.conf').read(): + if "debug_connection " in open('/etc/nginx/nginx.conf', + encoding='utf-8').read(): Log.info(self, "Disabling Nginx debug connections") EEShellExec.cmd_exec(self, "sed -i \"/debug_connection.*/d\"" " /etc/nginx/nginx.conf") @@ -146,7 +147,8 @@ class EEDebugController(CementBaseController): data = dict(php="9001", debug="9001") Log.info(self, 'Writting the Nginx debug configration to file ' '/etc/nginx/conf.d/upstream.conf ') - ee_nginx = open('/etc/nginx/conf.d/upstream.conf', 'w') + ee_nginx = open('/etc/nginx/conf.d/upstream.conf', + encoding='utf-8', mode='w') self.app.render((data), 'upstream.mustache', out=ee_nginx) ee_nginx.close() self.trigger_php = True @@ -165,7 +167,8 @@ class EEDebugController(CementBaseController): data = dict(php="9000", debug="9001") Log.debug(self, 'Writting the Nginx debug configration to file' ' /etc/nginx/conf.d/upstream.conf ') - ee_nginx = open('/etc/nginx/conf.d/upstream.conf', 'w') + ee_nginx = open('/etc/nginx/conf.d/upstream.conf', + encoding='utf-8', mode='w') self.app.render((data), 'upstream.mustache', out=ee_nginx) ee_nginx.close() self.trigger_php = True @@ -186,7 +189,8 @@ class EEDebugController(CementBaseController): config.remove_option('global', 'include') config['global']['log_level'] = 'debug' config['global']['include'] = '/etc/php5/fpm/pool.d/*.conf' - with open('/etc/php5/fpm/php-fpm.conf', 'w') as configfile: + with open('/etc/php5/fpm/php-fpm.conf', + encoding='utf-8', mode='w') as configfile: Log.debug(self, "Writting php5-FPM configuration into " "/etc/php5/fpm/php-fpm.conf") config.write(configfile) @@ -206,7 +210,8 @@ class EEDebugController(CementBaseController): config.remove_option('global', 'include') config['global']['log_level'] = 'notice' config['global']['include'] = '/etc/php5/fpm/pool.d/*.conf' - with open('/etc/php5/fpm/php-fpm.conf', 'w') as configfile: + with open('/etc/php5/fpm/php-fpm.conf', + encoding='utf-8', mode='w') as configfile: Log.debug(self, "writting php5 configuration into " "/etc/php5/fpm/php-fpm.conf") config.write(configfile) @@ -278,7 +283,7 @@ class EEDebugController(CementBaseController): " grep true".format(wp_config)): Log.info(self, "Starting WordPress debug") open("{0}/htdocs/wp-content/debug.log".format(webroot), - 'a').close() + encoding='utf-8', mode='a').close() EEShellExec.cmd_exec(self, "chown {1}: {0}/htdocs/wp-" "content/debug.log" "".format(webroot, diff --git a/ee/cli/plugins/site.py b/ee/cli/plugins/site.py index f73ea40c..38050a97 100644 --- a/ee/cli/plugins/site.py +++ b/ee/cli/plugins/site.py @@ -152,7 +152,8 @@ class EESiteController(CementBaseController): .format(ee_domain)): Log.info(self, "Display NGINX configuration for {0}" .format(ee_domain)) - f = open('/etc/nginx/sites-available/{0}'.format(ee_domain), "r") + f = open('/etc/nginx/sites-available/{0}'.format(ee_domain), + encoding='utf-8', mode='r') text = f.read() Log.info(self, Log.ENDC + text) f.close() @@ -436,7 +437,7 @@ class EESiteCreateController(CementBaseController): data = setupdatabase(self, data) try: eedbconfig = open("{0}/ee-config.php".format(ee_site_webroot), - 'w') + encoding='utf-8', mode='w') eedbconfig.write("") EEFileUtils.chown(self, "{0}22222" @@ -471,7 +492,8 @@ class EEStackController(CementBaseController): if not os.path.isfile("/etc/mysql/my.cnf"): config = ("[mysqld]\nwait_timeout = 30\n" "interactive_timeout=60\nperformance_schema = 0") - config_file = open("/etc/mysql/my.cnf", "w") + config_file = open("/etc/mysql/my.cnf", + encoding='utf-8', mode='w') config_file.write(config) config_file.close() else: @@ -505,7 +527,8 @@ class EEStackController(CementBaseController): data = dict(email=EEVariables.ee_email) Log.debug(self, "Writting configuration into file" "/etc/dovecot/conf.d/99-ee.conf ") - ee_dovecot = open('/etc/dovecot/conf.d/99-ee.conf', 'w') + ee_dovecot = open('/etc/dovecot/conf.d/99-ee.conf', + encoding='utf-8', mode='w') self.app.render((data), 'dovecot.mustache', out=ee_dovecot) ee_dovecot.close() @@ -595,7 +618,8 @@ class EEStackController(CementBaseController): data = dict() Log.debug(self, "Writting configuration of EasyEngine into " "file /var/lib/dovecot/sieve/default.sieve") - ee_sieve = open('/var/lib/dovecot/sieve/default.sieve', 'w') + ee_sieve = open('/var/lib/dovecot/sieve/default.sieve', + encoding='utf-8', mode='w') self.app.render((data), 'default-sieve.mustache', out=ee_sieve) ee_sieve.close() @@ -619,7 +643,7 @@ class EEStackController(CementBaseController): Log.debug(self, "Configuring file /etc/amavis/conf.d" "/15-content_filter_mode") ee_amavis = open('/etc/amavis/conf.d/15-content_filter_mode', - 'w') + encoding='utf-8', mode='w') self.app.render((data), '15-content_filter_mode.mustache', out=ee_amavis) ee_amavis.close() @@ -634,7 +658,8 @@ class EEStackController(CementBaseController): "$3 }\' | tr -d '\\n'").read() data = dict(host=vm_host, password=vm_pass) - vm_config = open('/etc/amavis/conf.d/50-user', 'w') + vm_config = open('/etc/amavis/conf.d/50-user', + encoding='utf-8', mode='w') self.app.render((data), '50-user.mustache', out=vm_config) vm_config.close() @@ -670,7 +695,8 @@ class EEStackController(CementBaseController): -o smtpd_client_connection_rate_limit=0 -o local_header_rewrite_clients=""") - with open("/etc/postfix/master.cf", "a") as am_config: + with open("/etc/postfix/master.cf", + encoding='utf-8', mode='a') as am_config: am_config.write(amavis_master) # Amavis ClamAV configuration @@ -795,7 +821,8 @@ class EEStackController(CementBaseController): user='anemometer', password=chars) ee_anemometer = open('{0}22222/htdocs/db/anemometer' '/conf/config.inc.php' - .format(EEVariables.ee_webroot), 'w') + .format(EEVariables.ee_webroot), + encoding='utf-8', mode='w') self.app.render((data), 'anemometer.mustache', out=ee_anemometer) ee_anemometer.close() @@ -861,7 +888,8 @@ class EEStackController(CementBaseController): .format(EEVariables.ee_webroot)) ee_vmb = open('{0}22222/htdocs/vimbadmin/application/' 'configs/application.ini' - .format(EEVariables.ee_webroot), 'w') + .format(EEVariables.ee_webroot), + encoding='utf-8', mode='w') self.app.render((data), 'vimbadmin.mustache', out=ee_vmb) ee_vmb.close() @@ -900,7 +928,7 @@ class EEStackController(CementBaseController): host=EEVariables.ee_mysql_host) vm_config = open('/etc/postfix/mysql/virtual_alias_maps.cf', - 'w') + encoding='utf-8', mode='w') self.app.render((data), 'virtual_alias_maps.mustache', out=vm_config) vm_config.close() @@ -909,7 +937,7 @@ class EEStackController(CementBaseController): "/etc/postfix/mysql" "/virtual_domains_maps.cf file") vm_config = open('/etc/postfix/mysql/virtual_domains_maps.cf', - 'w') + encoding='utf-8', mode='w') self.app.render((data), 'virtual_domains_maps.mustache', out=vm_config) vm_config.close() @@ -918,7 +946,7 @@ class EEStackController(CementBaseController): "/etc/postfix/mysql" "/virtual_mailbox_maps.cf file") vm_config = open('/etc/postfix/mysql/virtual_mailbox_maps.cf', - 'w') + encoding='utf-8', mode='w') self.app.render((data), 'virtual_mailbox_maps.mustache', out=vm_config) vm_config.close() @@ -926,7 +954,7 @@ class EEStackController(CementBaseController): Log.debug(self, "Writting configration" " to /etc/dovecot/dovecot-sql.conf.ext file ") vm_config = open('/etc/dovecot/dovecot-sql.conf.ext', - 'w') + encoding='utf-8', mode='w') self.app.render((data), 'dovecot-sql-conf.mustache', out=vm_config) vm_config.close() @@ -935,7 +963,7 @@ class EEStackController(CementBaseController): # Amvis settings if set(EEVariables.ee_mailscanner).issubset(set(apt_packages)): vm_config = open('/etc/amavis/conf.d/50-user', - 'w') + encoding='utf-8', mode='w') self.app.render((data), '50-user.mustache', out=vm_config) vm_config.close() @@ -1024,7 +1052,8 @@ class EEStackController(CementBaseController): Log.debug(self, 'Writting the nginx configuration for ' 'RoundCubemail') - ee_rc = open('/etc/nginx/sites-available/webmail.conf', 'w') + ee_rc = open('/etc/nginx/sites-available/webmail.conf', + encoding='utf-8', mode='w') self.app.render((data), 'virtualconf.mustache', out=ee_rc) ee_rc.close() @@ -1053,8 +1082,7 @@ class EEStackController(CementBaseController): .format(EEVariables.ee_webroot)]) # Remove roundcube installer EEService.reload_service(self, 'nginx') - EEFileUtils.remove(self, ["{0}roundcubemail" - "/htdocs/installer" + EEFileUtils.remove(self, ["{0}roundcubemail/htdocs/installer" .format(EEVariables.ee_webroot)]) EEFileUtils.chown(self, '{0}roundcubemail' .format(EEVariables.ee_webroot), diff --git a/ee/core/addswap.py b/ee/core/addswap.py index 717d67a1..a398ff7c 100644 --- a/ee/core/addswap.py +++ b/ee/core/addswap.py @@ -23,5 +23,6 @@ class EESwap(): EEFileUtils.chown(self, "/ee-swapfile", "root", "root") EEFileUtils.chmod(self, "/ee-swapfile", 0o600) EEShellExec.cmd_exec(self, "swapon /ee-swapfile") - with open("/etc/fstab", "a") as swap_file: + with open("/etc/fstab", + encoding='utf-8', mode='a') as swap_file: swap_file.write("/ee-swapfile\tnone\tswap\tsw\t0 0") diff --git a/ee/core/apt_repo.py b/ee/core/apt_repo.py index d3e82ed9..aea5b2a7 100644 --- a/ee/core/apt_repo.py +++ b/ee/core/apt_repo.py @@ -26,12 +26,15 @@ class EERepo(): + EEVariables().ee_repo_file) try: if not os.path.isfile(repo_file_path): - with open(repo_file_path, "a") as repofile: + with open(repo_file_path, + encoding='utf-8', mode='a') as repofile: repofile.write(repo_url) repofile.write('\n') repofile.close() - elif repo_url not in open(repo_file_path).read(): - with open(repo_file_path, "a") as repofile: + elif repo_url not in open(repo_file_path, + encoding='utf-8').read(): + with open(repo_file_path, + encoding='utf-8', mode='a') as repofile: repofile.write(repo_url) repofile.write('\n') repofile.close() diff --git a/ee/core/aptget.py b/ee/core/aptget.py index b33b3d1d..cd0aa4f5 100644 --- a/ee/core/aptget.py +++ b/ee/core/aptget.py @@ -17,7 +17,8 @@ class EEAptGet(): apt_cache = apt.cache.Cache() import sys orig_out = sys.stdout - sys.stdout = open(self.app.config.get('log.logging', 'file'), 'a') + sys.stdout = open(self.app.config.get('log.logging', 'file'), + encoding='utf-8', mode='a') apt_cache.update(apt.progress.text.AcquireProgress()) sys.stdout = orig_out # success = (apt_cache.commit( @@ -43,7 +44,7 @@ class EEAptGet(): success = (apt_cache.commit( apt.progress.text.AcquireProgress(), apt.progress.base.InstallProgress())) - #apt_cache.close() + # apt_cache.close() return success except AttributeError as e: Log.error(self, 'AttributeError: ' + str(e)) @@ -64,11 +65,11 @@ class EEAptGet(): pkg = apt_cache[package_name.strip()] if package_name.strip() in apt_cache: if pkg.is_installed: - #apt_pkg.PkgSystemUnLock() + # apt_pkg.PkgSystemUnLock() Log.debug(self, 'Trying to install a package that ' 'is already installed (' + package_name.strip() + ')') - #apt_cache.close() + # apt_cache.close() return False else: try: @@ -78,7 +79,7 @@ class EEAptGet(): Log.debug(self, str(e)) Log.error(self, str(e)) else: - #apt_cache.close() + # apt_cache.close() Log.error(self, 'Unknown package selected (' + package_name.strip() + ')') @@ -88,19 +89,19 @@ class EEAptGet(): if apt_cache.install_count > 0: try: - #apt_pkg.PkgSystemUnLock() + # apt_pkg.PkgSystemUnLock() orig_out = sys.stdout sys.stdout = open(self.app.config.get('log.logging', 'file'), - 'a') + encoding='utf-8', mode='a') result = apt_cache.commit(apt.progress.text.AcquireProgress(), apt.progress.base.InstallProgress()) sys.stdout = orig_out - #apt_cache.close() + # apt_cache.close() return result except SystemError as e: Log.debug(self, 'SystemError: ' + str(e)) Log.error(self, 'SystemError: ' + str(e)) - #apt_cache.close() + # apt_cache.close() except Exception as e: Log.debug(self, str(e)) Log.error(self, str(e)) @@ -145,7 +146,7 @@ class EEAptGet(): # apt_pkg.PkgSystemUnLock() orig_out = sys.stdout sys.stdout = open(self.app.config.get('log.logging', 'file'), - 'a') + encoding='utf-8', mode='a') result = apt_cache.commit(apt.progress.text.AcquireProgress(), apt.progress.base.InstallProgress()) sys.stdout = orig_out @@ -165,7 +166,8 @@ class EEAptGet(): """ try: orig_out = sys.stdout - sys.stdout = open(self.app.config.get('log.logging', 'file'), 'a') + 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: diff --git a/ee/core/checkfqdn.py b/ee/core/checkfqdn.py index 915b2e24..4caf9380 100644 --- a/ee/core/checkfqdn.py +++ b/ee/core/checkfqdn.py @@ -5,10 +5,10 @@ import os def check_fqdn(self, ee_host): """FQDN check with EasyEngine, for mail server hostname must be FQDN""" - #ee_host=os.popen("hostname -f | tr -d '\n'").read() + # ee_host=os.popen("hostname -f | tr -d '\n'").read() if '.' in ee_host: EEVariables.ee_fqdn = ee_host - with open('/etc/hostname', 'w') as hostfile: + with open('/etc/hostname', encoding='utf-8', mode='w') as hostfile: hostfile.write(ee_host) EEShellExec.cmd_exec(self, "sed -i \"1i\\127.0.0.1 {0}\" /etc/hosts" diff --git a/ee/core/fileutils.py b/ee/core/fileutils.py index 5e8a4b68..6b53585e 100644 --- a/ee/core/fileutils.py +++ b/ee/core/fileutils.py @@ -196,7 +196,7 @@ class EEFileUtils(): Searches for string in file and returns the matched line. """ try: - for line in open(fnm): + for line in open(fnm, encoding='utf-8'): if sstr in line: return line except OSError as e: diff --git a/ee/core/logwatch.py b/ee/core/logwatch.py index 818f50d3..ec2e210f 100644 --- a/ee/core/logwatch.py +++ b/ee/core/logwatch.py @@ -96,7 +96,7 @@ class LogWatcher(object): def tail(fname, window): """Read last N lines from file fname.""" try: - f = open(fname, 'r') + f = open(fname, encoding='utf-8', mode='r') except IOError as err: if err.errno == errno.ENOENT: return [] @@ -166,7 +166,7 @@ class LogWatcher(object): def watch(self, fname): try: - file = open(fname, "r") + file = open(fname, encoding='utf-8', mode='r') fid = self.get_file_id(os.stat(fname)) except EnvironmentError as err: if err.errno != errno.ENOENT: diff --git a/install b/install index bb2cb291..b7801795 100644 --- a/install +++ b/install @@ -34,22 +34,20 @@ function ee_lib_error() function install_dep() { - # 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 pre depedencies" - apt-get -y install python3 python3-apt python3-setuptools python3-dev sqlite3 git - if [[ $? -ne 0 ]]; then - ee_lib_echo_fail "Unable to install pre depedencies" - exit 1 + if [ "$EE_LINUX_DISTRO" == "Ubuntu" ]; then + apt-get -y install python-software-properties software-properties-common python3 python3-apt python3-setuptools python3-dev sqlite3 git || ee_lib_error "Unable to install pre depedencies, exit status " 1 + elif [ "$EE_LINUX_DISTRO" == "Debian" ]; then + apt-get -y install graphviz python-software-properties python3 python3-apt python3-setuptools python3-dev sqlite3 git || ee_lib_error "Unable to pre depedencies, exit status " 1 fi + } function sync_db() { - mkdir /var/lib/ee + mkdir -p /var/lib/ee # Sqlite query to create table `sites` into ee.db which will be used by ee.3.0 echo "CREATE TABLE sites ( @@ -194,7 +192,7 @@ function update_to_ee3() 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 # Softlink to fix command not found error - ln -s /usr/local/bin/ee /usr/local/sbin/ee + ln -s /usr/local/bin/ee /usr/local/sbin/ee || ee_lib_error "Unable to create softlink to old EasyEngine, exit status " 1 } @@ -217,28 +215,73 @@ if [[ $EUID -ne 0 ]]; then exit 1 fi +# Execute: apt-get update +ee_lib_echo "Executing apt-get update" +apt-get update &>> /dev/null + +# Checking lsb_release package +if [ ! -x /usr/bin/lsb_release ]; then + ee_lib_echo "Installing lsb-release, please wait..." + apt-get -y install lsb-release &>> /dev/null +fi + +# Define variables for later use +readonly EE_LOG_DIR=/var/log/ee/ +readonly EE_INSTALL_LOG=/var/log/ee/install.log +readonly EE_LINUX_DISTRO=$(lsb_release -i | awk '{print $3}') +readonly EE_DEBIAN_VERSION=$(lsb_release -sc) + +# Checking linux distro +if [ "$EE_LINUX_DISTRO" != "Ubuntu" ] && [ "$EE_LINUX_DISTRO" != "Debian" ]; then + ee_lib_echo_fail "EasyEngine (ee) is made for Ubuntu and Debian only as of now" + ee_lib_echo_fail "You are free to fork EasyEngine (ee): https://github.com/rtCamp/easyengine/fork" + ee_lib_echo_fail "EasyEngine (ee) only support Ubuntu 12.04/14.04 and Debian 7.x" + exit 100 +fi + +# EasyEngine (ee) only support all Ubuntu/Debian distro except the distro reached EOL +lsb_release -d | egrep -e "12.04|14.04|wheezy" &>> /dev/null +if [ "$?" -ne "0" ]; then + exit 100 +fi + + +# Pre checks to avoid later screw ups +# Checking EasyEngine (ee) log directory +if [ ! -d $EE_LOG_DIR ]; then + + ee_lib_echo "Creating EasyEngine log directory, please wait..." + mkdir -p $EE_LOG_DIR || ee_lib_error "Creating log directory failed, exit status " 1 + + # Create EasyEngine log files + touch /var/log/ee/{ee.log,install.log} + + # Keep EasyEngine log folder accessible to root only + chmod -R 700 /var/log/ee || ee_lib_error "Unable to setup log directory permissions , exit status " 1 +fi + if [ -f /usr/local/sbin/easyengine ]; then # Check old EasyEngine version ee version | grep ${old_ee_version} &>> /dev/null if [[ $? -ne 0 ]]; then - ee_lib_echo "EasyEngine $old_ee_version not found on your system" - ee_lib_echo "Updating your EasyEngine to $old_ee_version for compability" - wget -q https://raw.githubusercontent.com/rtCamp/easyengine/old-stable/bin/update && bash update + ee_lib_echo "EasyEngine $old_ee_version not found on your system" | tee -ai $EE_INSTALL_LOG + ee_lib_echo "Updating your EasyEngine to $old_ee_version for compability" | tee -ai $EE_INSTALL_LOG + wget -q https://raw.githubusercontent.com/rtCamp/easyengine/old-stable/bin/update && bash update | tee -ai $EE_INSTALL_LOG if [[ $? -ne 0 ]]; then ee_lib_echo_info "Unbale to update EasyEngine2 to $old_ee_version" exit 1 fi fi - install_dep - sync_db - install_ee3 - update_to_ee3 - git_init + install_dep | tee -ai $EE_INSTALL_LOG + sync_db | tee -ai $EE_INSTALL_LOG + install_ee3 | tee -ai $EE_INSTALL_LOG + update_to_ee3 | tee -ai $EE_INSTALL_LOG + git_init | tee -ai $EE_INSTALL_LOG elif [ ! -f /usr/local/bin/ee ]; then - install_dep - install_ee3 - git_init + install_dep | tee -ai $EE_INSTALL_LOG + install_ee3 | tee -ai $EE_INSTALL_LOG + git_init | tee -ai $EE_INSTALL_LOG else - ee_lib_echo_fail "EasyEngine 3 allready installed on your system" + ee_lib_echo_fail "EasyEngine 3 allready installed on your system" | tee -ai $EE_INSTALL_LOG exit 1 fi