示例#1
0
    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
示例#2
0
    def create(self, validated_data):
        validated_domain = validated_data['domain']

        validated_user = validated_data['user']

        validated_group = validated_data['group']

        # Remove Domain Configuration
        domain_conf = f"{PhpPath.conf_dir()}{validated_user}.conf"

        if os.path.exists(domain_conf):
            os.remove(domain_conf)

        content = render_to_string('php/fpm.conf.tmpl') \
            .replace('[SYSTEM-USERNAME]', validated_user) \
            .replace('[SYSTEM-GROUP]', validated_group) \
            .replace('[PHP-RUN]', PhpPath.run_dir()) \
            .replace('[WEB-DOMAIN]', validated_domain) \
            .replace('[WEB-VHOST]', WebPath.www_dir(validated_user)) \
            .replace('[POSTFIX-SENDMAIL]', PostfixPath.sendmail_cmd())

        handle = open(domain_conf, 'w')
        handle.write(content)
        handle.close()

        return validated_data
示例#3
0
    def create(self, validated_data):
        validated_domain = validated_data['domain']

        validated_user = validated_data['user']

        validated_ip = validated_data['ip']

        validated_ip_type = validated_data['ip_type']

        # Create Data Directory
        data_dir = f"{AwstatsPath.sites_dir()}{validated_domain}/data"

        if not os.path.exists(data_dir):
            os.makedirs(data_dir, 0o755)

        if validated_ip_type == 'dedicated':
            alias = f"{validated_domain} www.{validated_domain} {validated_ip}"
        else:
            alias = f"{validated_domain} www.{validated_domain}"

        content = render_to_string('awstats/config.tmpl') \
            .replace('[AWSTATS-ALIASES]', alias) \
            .replace('[AWSTATS-SITES]', AwstatsPath.sites_dir()) \
            .replace('[AWSTATS-CGI]', AwstatsPath.cgi_bin_dir()) \
            .replace('[WEB-DOMAIN]', validated_domain) \
            .replace('[WEB-VHOST]', WebPath.www_dir(validated_user))

        handle = open(
            f"{AwstatsPath.sites_dir()}awstats.{validated_domain}.conf", 'w')
        handle.write(content)
        handle.close()

        return validated_data
示例#4
0
    def create(self, validated_data):
        validated_domain = validated_data['domain']

        validated_user = validated_data['user']

        domain = f"{WebPath.www_dir(validated_user)}{validated_domain}"

        path_config = f"{NginxPath.sites_conf_dir()}{validated_domain}/logs.conf"

        content = render_to_string('nginx/logs.conf.tmpl') \
            .replace('[WEB-DOMAIN]', validated_domain) \
            .replace('[WEB-VHOST]', WebPath.www_dir(validated_user))

        handle = open(path_config, 'w')
        handle.write(content)
        handle.close()

        path = f"{domain}/logs"

        if not os.path.exists(path):
            # Domain/logs
            os.makedirs(path, 0o755)
            shutil.chown(path, user='******', group='root')

            # Empty log files
            os.mknod(f"{path}/access.log", 0o644)
            os.mknod(f"{path}/error.log", 0o644)

        return validated_data
示例#5
0
    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
示例#6
0
    def validate_user(self, value):
        try:
            pwd.getpwnam(value).pw_uid
        except KeyError:
            raise serializers.ValidationError(
                f"System User '{value}' does not exist.", code='invalid')

        if not os.path.exists(WebPath.www_dir(value)):
            raise serializers.ValidationError(
                f"Web User '{value}' does not exist.", code='not_found')

        return value
示例#7
0
    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
示例#8
0
    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
示例#9
0
    def create(self, validated_data):
        validated_domain = validated_data['domain']

        validated_user = validated_data['user']

        path_config = f"{NginxPath.sites_conf_dir()}{validated_domain}/uwsgi_python3.conf"

        content = render_to_string('nginx/uwsgi_python3.conf.tmpl') \
            .replace('[SYSTEM-USERNAME]', validated_user) \
            .replace('[UWSGI-RUN]', UwsgiPath.run_dir()) \
            .replace('[WEB-DOMAIN]', validated_domain) \
            .replace('[WEB-VHOST]', WebPath.www_dir(validated_user))

        handle = open(path_config, 'w')
        handle.write(content)
        handle.close()

        return validated_data
示例#10
0
    def create(self, validated_data):
        validated_domain = validated_data['domain']

        # service
        path_service = f"{SystemPath.initd_dir()}worker.service"

        if os.path.exists(path_service):
            os.remove(path_service)

        content_service = render_to_string('daemon/worker.service.tmpl') \
            .replace('[SYSTEM-CONFD]', SystemPath.confd_dir()) \
            .replace('[SYSTEM-SH]', SystemPath.sh_cmd()) \
            .replace('[SYSTEM-KILL]', SystemPath.kill_cmd())

        handle = open(path_service, 'w')
        handle.write(content_service)
        handle.close()

        # conf.d
        path_confd = f"{SystemPath.confd_dir()}worker"

        if os.path.exists(path_confd):
            os.remove(path_confd)

        content_confd = render_to_string('daemon/worker.tmpl') \
            .replace('[DAEMON-WORKER]', WebPath.www_dir('gwhcp')) \
            .replace('[WEB-DOMAIN]', validated_domain)

        handle2 = open(path_confd, 'w')
        handle2.write(content_confd)
        handle2.close()

        os.system(
            f"{SystemPath.systemctl_cmd()}"
            f" enable worker"
        )

        os.system(
            f"{SystemPath.systemctl_cmd()}"
            f" start worker"
        )

        return validated_data
示例#11
0
    def create(self, validated_data):
        validated_domain = validated_data['domain']

        validated_ip = validated_data['ip']

        # Celery Run / Log
        celery_path = [
            f"{SystemPath.log_dir()}celery",
            f"{SystemPath.run_dir()}celery",
        ]

        for celery_item in celery_path:
            if not os.path.exists(celery_item):
                os.makedirs(celery_item, 0o755)

            shutil.chown(celery_item, user='******', group='gwhcp')

        # service
        path_service = f"{SystemPath.initd_dir()}celery.service"

        if os.path.exists(path_service):
            os.remove(path_service)

        content_service = render_to_string('daemon/celery.service.tmpl') \
            .replace('[DAEMON-WORKER]', f"{WebPath.www_dir('gwhcp')}{validated_domain}/public/gwhcp_api") \
            .replace('[WEB-DOMAIN]', validated_domain) \
            .replace('[SYSTEM-CONFD]', SystemPath.confd_dir()) \
            .replace('[SYSTEM-SH]', SystemPath.sh_cmd()) \
            .replace('[SYSTEM-SUDO]', SystemPath.sudo_cmd())

        handle = open(path_service, 'w')
        handle.write(content_service)
        handle.close()

        # conf.d
        path_confd = f"{SystemPath.confd_dir()}celery"

        if os.path.exists(path_confd):
            os.remove(path_confd)

        content_confd = render_to_string('daemon/celery.tmpl') \
            .replace('[DAEMON-WORKER]', WebPath.www_dir('gwhcp')) \
            .replace('[WEB-DOMAIN]', validated_domain) \
            .replace('[SYSTEM-IPADDRESS]', validated_ip)

        handle2 = open(path_confd, 'w')
        handle2.write(content_confd)
        handle2.close()

        os.system(
            f"{SystemPath.systemctl_cmd()}"
            f" enable celery"
        )

        os.system(
            f"{SystemPath.systemctl_cmd()}"
            f" start celery"
        )

        # tmpfilesd
        path_tmpfilesd = f"{SystemPath.tmpfilesd_dir()}celery.conf"

        content_tmpfilesd = render_to_string('daemon/tmpfilesd.tmpl') \
            .replace('[DAEMON-SERVICE]', 'celery') \
            .replace('[SYSTEM-USER]', 'gwhcp') \
            .replace('[SYSTEM-GROUP]', 'gwhcp')

        handle3 = open(path_tmpfilesd, 'w')
        handle3.write(content_tmpfilesd)
        handle3.close()

        return validated_data