def create(self, validated_data): validated_domain = validated_data['domain'] validated_ip = validated_data['ip'] validated_port = validated_data['port'] validated_user = validated_data['user'] path_config = f"{NginxPath.sites_dir()}{validated_domain}.{validated_port}.conf" path_config_enabled = f"{NginxPath.sites_enabled_dir()}{validated_domain}.{validated_port}.conf" ip = (str(validated_ip) if validated_ip.version == 4 else f"[{validated_ip}]") content = render_to_string(f"nginx/virtualhost_{validated_port}.tmpl") \ .replace('[WEB-DOMAIN]', validated_domain) \ .replace('[WEB-VHOST]', WebPath.www_dir(validated_user)) \ .replace('[WEB-VHOST-SSL]', WebPath.ssl_dir(validated_user)) \ .replace('[SYSTEM-IPADDRESS]', ip) handle = open(path_config, 'w') handle.write(content) handle.close() os.symlink(path_config, path_config_enabled) path_sites_conf = f"{NginxPath.sites_conf_dir()}{validated_domain}" if not os.path.exists(path_sites_conf): os.makedirs(path_sites_conf, 0o755) shutil.chown(path_sites_conf, user='******', group='root') return validated_data
def create(self, validated_data): validated_domain = validated_data['domain'] validated_user = validated_data['user'] # If path does not exist, create it if not os.path.exists(WebPath.ssl_dir(validated_user)): os.makedirs(WebPath.ssl_dir(validated_user), 0o755) rsa = f"{WebPath.ssl_dir(validated_user)}{validated_domain}.rsa" # Remove Private Key if os.path.exists(rsa): os.remove(rsa) crt = f"{WebPath.ssl_dir(validated_user)}{validated_domain}.crt" # Remove Certificate if os.path.exists(crt): os.remove(crt) try: result = models.DomainSsl.objects.get( domain__name=validated_domain) except models.DomainSsl.DoesNotExist: raise ValueError(f"Domain '{validated_domain}' does not exist.") ssl = render_to_string('web/ssl.tmpl') # Dedicated and Self-Signed if result.ssl_type in ['dedicated', 'self']: # Private Key handle_rsa = open(rsa, 'w') handle_rsa.write(ssl.replace('[SSL]', result.decrypt_rsa())) handle_rsa.close() # Certificate handle_crt = open(crt, 'w') handle_crt.write(ssl.replace('[SSL]', result.decrypt_crt())) handle_crt.close() # TODO Let's Encrypt elif result.ssl_type == 'letsencrypt': pass return validated_data
def create(self, validated_data): validated_domain = validated_data['domain'] validated_user = validated_data['user'] # If path does not exist, create it if not os.path.exists(WebPath.ssl_dir(validated_user)): os.makedirs(WebPath.ssl_dir(validated_user), 0o755) rsa = f"{WebPath.ssl_dir(validated_user)}{validated_domain}.rsa" # Remove Private Key if os.path.exists(rsa): os.remove(rsa) crt = f"{WebPath.ssl_dir(validated_user)}{validated_domain}.crt" # Remove Certificate if os.path.exists(crt): os.remove(crt) return validated_data
def create(self, validated_data): validated_domain = validated_data['domain'] validated_ip = validated_data['ip'] validated_port = validated_data['port'] validated_user = validated_data['user'] validated_group = validated_data['group'] # Config File path_config = f"{ApachePath.sites_dir()}{validated_group}.{validated_port}.conf" # Sym Link path_config_enabled = f"{ApachePath.sites_enabled_dir()}{validated_group}.{validated_port}.conf" ip = (str(validated_ip) if validated_ip.version == 4 else f"[{validated_ip}]") # Virtual Host Config 80/443 content_config = render_to_string(f"apache/virtualhost_{validated_port}.tmpl") \ .replace('[SYSTEM-IPADDRESS]', ip) \ .replace('[SYSTEM-USERNAME]', validated_user) \ .replace('[SYSTEM-GROUP]', validated_group) \ .replace('[WEB-DOMAIN]', validated_domain) \ .replace('[WEB-VHOST]', WebPath.www_dir(validated_user)) \ .replace('[WEB-VHOST-SSL]', WebPath.ssl_dir(validated_user)) handle = open(path_config, 'w') handle.write(content_config) handle.close() # Symlink os.symlink(path_config, path_config_enabled) return validated_data