From 715497d3b1d2af6069d27291ea4010a80b2b3255 Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Tue, 1 Oct 2019 04:08:54 +0200 Subject: [PATCH] Simplify several functions --- wo/cli/plugins/secure.py | 2 +- wo/cli/plugins/site.py | 40 +++++++++++++++----------------- wo/cli/plugins/site_functions.py | 2 +- wo/core/acme.py | 1 + wo/core/domainvalidate.py | 4 ++-- wo/core/random.py | 11 +++------ 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/wo/cli/plugins/secure.py b/wo/cli/plugins/secure.py index 0c8a968..e2b21ef 100644 --- a/wo/cli/plugins/secure.py +++ b/wo/cli/plugins/secure.py @@ -70,7 +70,7 @@ class WOSecureController(CementBaseController): WOGit.add(self, ["/etc/nginx"], msg="Add Nginx to into Git") pargs = self.app.pargs - passwd = RANDOM.long(self) + passwd = RANDOM.gen(self, length='24') if not pargs.user_input: username = input("Provide HTTP authentication user " "name [{0}] :".format(WOVariables.wo_user)) diff --git a/wo/cli/plugins/site.py b/wo/cli/plugins/site.py index 50ee089..6a36d27 100644 --- a/wo/cli/plugins/site.py +++ b/wo/cli/plugins/site.py @@ -57,7 +57,7 @@ class WOSiteController(CementBaseController): pargs.site_name = pargs.site_name.strip() # validate domain name (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) # check if site exists if not check_domain_exists(self, wo_domain): @@ -94,8 +94,8 @@ class WOSiteController(CementBaseController): Log.debug(self, str(e)) Log.error(self, 'could not input site name') pargs.site_name = pargs.site_name.strip() - (wo_domain, wo_www_domain) = WODomain.validatedomain(self, - pargs.site_name) + (wo_domain, wo_www_domain) = WODomain.validate(self, + pargs.site_name) # check if site exists if not check_domain_exists(self, wo_domain): Log.error(self, "site {0} does not exist".format(wo_domain)) @@ -136,8 +136,8 @@ class WOSiteController(CementBaseController): Log.error(self, 'could not input site name') pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) - (wo_domain_type, wo_root_domain) = WODomain.getdomainlevel( + wo_www_domain) = WODomain.validate(self, pargs.site_name) + (wo_domain_type, wo_root_domain) = WODomain.getlevel( self, wo_domain) wo_db_name = '' wo_db_user = '' @@ -190,7 +190,7 @@ class WOSiteController(CementBaseController): pargs = self.app.pargs pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) wo_site_webroot = getSiteInfo(self, wo_domain).site_path if not check_domain_exists(self, wo_domain): @@ -213,7 +213,7 @@ class WOSiteController(CementBaseController): # TODO Write code for wo site edit command here pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) if not check_domain_exists(self, wo_domain): Log.error(self, "site {0} does not exist".format(wo_domain)) @@ -245,7 +245,7 @@ class WOSiteController(CementBaseController): pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) if not check_domain_exists(self, wo_domain): Log.error(self, "site {0} does not exist".format(wo_domain)) @@ -286,7 +286,7 @@ class WOSiteEditController(CementBaseController): pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) if not check_domain_exists(self, wo_domain): Log.error(self, "site {0} does not exist".format(wo_domain)) @@ -437,7 +437,7 @@ class WOSiteCreateController(CementBaseController): pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) if not wo_domain.strip(): Log.error(self, "Invalid domain name, " "Provide valid domain name") @@ -731,11 +731,14 @@ class WOSiteCreateController(CementBaseController): if pargs.letsencrypt: acme_domains = [] - (wo_domain_type, wo_root_domain) = WODomain.getdomainlevel( + (wo_domain_type, wo_root_domain) = WODomain.getlevel( self, wo_domain) data['letsencrypt'] = True letsencrypt = True - if data['letsencrypt'] is True: + if os.path.isfile('/etc/letsencrypt/live/{0}/fullchain.pem' + .format(wo_domain)): + archivedCertificateHandle(self, wo_domain) + else: Log.debug(self, "Going to issue Let's Encrypt certificate") acmedata = dict(acme_domains, dns=False, acme_dns='dns_cf', dnsalias=False, acme_alias='', keylength='') @@ -788,9 +791,7 @@ class WOSiteCreateController(CementBaseController): # check if a wildcard cert for the root domain exist Log.debug(self, "checkWildcardExist on *.{0}" .format(wo_root_domain)) - iswildcard = SSL.checkwildcardexist(self, wo_root_domain) - Log.debug(self, "iswildcard = {0}".format(iswildcard)) - if iswildcard: + if SSL.checkwildcardexist(self, wo_root_domain): Log.info(self, "Using existing Wildcard SSL " "certificate from {0} to secure {1}" .format(wo_root_domain, wo_domain)) @@ -839,9 +840,6 @@ class WOSiteCreateController(CementBaseController): msg="Adding letsencrypts config of site: {0}" .format(wo_domain)) updateSiteInfo(self, wo_domain, ssl=letsencrypt) - elif data['letsencrypt'] is False: - Log.info(self, "Not using Let\'s encrypt for Site " - " http://{0}".format(wo_domain)) class WOSiteUpdateController(CementBaseController): @@ -995,7 +993,7 @@ class WOSiteUpdateController(CementBaseController): pargs.site_name = pargs.site_name.strip() (wo_domain, - wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + wo_www_domain) = WODomain.validate(self, pargs.site_name) wo_site_webroot = WOVariables.wo_webroot + wo_domain check_site = getSiteInfo(self, wo_domain) @@ -1243,7 +1241,7 @@ class WOSiteUpdateController(CementBaseController): acme_domains = [] acmedata = dict(acme_domains, dns=False, acme_dns='dns_cf', dnsalias=False, acme_alias='', keylength='') - (wo_domain_type, wo_root_domain) = WODomain.getdomainlevel( + (wo_domain_type, wo_root_domain) = WODomain.getlevel( self, wo_domain) acmedata['keylength'] = self.app.config.get('letsencrypt', 'keylength') @@ -2035,7 +2033,7 @@ class WOSiteDeleteController(CementBaseController): Log.error(self, 'could not input site name') pargs.site_name = pargs.site_name.strip() - (wo_domain, wo_www_domain) = WODomain.validatedomain(self, pargs.site_name) + (wo_domain, wo_www_domain) = WODomain.validate(self, pargs.site_name) wo_db_name = '' wo_prompt = '' wo_nginx_prompt = '' diff --git a/wo/cli/plugins/site_functions.py b/wo/cli/plugins/site_functions.py index 77ce9dc..62320cc 100644 --- a/wo/cli/plugins/site_functions.py +++ b/wo/cli/plugins/site_functions.py @@ -848,7 +848,7 @@ def site_package_check(self, stype): apt = ["nginx"] + WOVariables.wo_nginx # apt_packages = apt_packages + WOVariables.wo_nginx post_pref(self, apt, packages) - elif os.file.isfile('/usr/sbin/nginx'): + elif os.path.isfile('/usr/sbin/nginx'): post_pref(self, WOVariables.wo_nginx, []) else: apt_packages = apt_packages + WOVariables.wo_nginx diff --git a/wo/core/acme.py b/wo/core/acme.py index 6945a38..a034793 100644 --- a/wo/core/acme.py +++ b/wo/core/acme.py @@ -112,6 +112,7 @@ class WOAcme: Log.debug(self, str(e)) Log.debug(self, "Error occured while generating " "ssl.conf") + return 0 def check_dns(self, acme_domains): """Check if a list of domains point to the server IP""" diff --git a/wo/core/domainvalidate.py b/wo/core/domainvalidate.py index d198adf..0e0b912 100644 --- a/wo/core/domainvalidate.py +++ b/wo/core/domainvalidate.py @@ -5,7 +5,7 @@ import os class WODomain(): """WordOps domain validation utilities""" - def validatedomain(self, url): + def validate(self, url): """ This function returns domain name removing http:// and https:// returns domain name only with or without www as user provided. @@ -26,7 +26,7 @@ class WODomain(): return (final_domain, domain_name) - def getdomainlevel(self, domain): + def getlevel(self, domain): """ Returns the domain type : domain, subdomain and the root domain """ diff --git a/wo/core/random.py b/wo/core/random.py index cb7482d..f6ff3b0 100644 --- a/wo/core/random.py +++ b/wo/core/random.py @@ -3,15 +3,10 @@ import string class RANDOM: + """Random strings generator""" - def short(self): + def gen(self, length='24'): short_random = ''.join([random.choice (string.ascii_letters + string.digits) - for n in range(8)]) + for n in range(length)]) return short_random - - def long(self): - long_random = ''.join([random.choice - (string.ascii_letters + string.digits) - for n in range(24)]) - return long_random