Example #1
0
def add(cmd):
    if misc.arg_range(cmd):
        exit(' Error! Usage: onyx-api --webadd user domain')

    user = cmd[0]
    domain = cmd[1]

    if misc.user_chk(user):
        base = '/home/' + user + '/web/' + domain

        # Create domain dirs if missing
        if not os.path.exists(base):
            os.makedirs(base + '/logs/')

            misc.copy_to('/opt/onyx/etc/web/usr/public_html/', base + '/public_html/')
            misc.copy_to('/opt/onyx/etc/web/usr/nginx/', base + '/nginx/')

            # Update user nginx template with correct values
            for line in fileinput.input([base + '/nginx/default.conf', base + '/nginx/default_s.conf'], inplace=True):
                line = line.replace('%web_port%', '80')
                line = line.replace('%web_ssl_port%', '443')
                line = line.replace('%proxy_port%', '8080')
                line = line.replace('%proxy_ssl_port%', '8443')
                line = line.replace('%user%', user)
                line = line.replace('%domain_idn%', domain)
                line = line.replace('%alias_idn%', 'www.' + domain)
                line = line.replace('%docroot%', base + '/public_html/')
                line = line.replace('%sdocroot%', base + '/public_html/')
                sys.stdout.write(line)

            # Update user html template with correct values
            for line in fileinput.input([base + '/public_html/50x.html',
                                         base + '/public_html/403.html',
                                         base + '/public_html/404.html',
                                         base + '/public_html/index.html'], inplace=True):
                line = line.replace('%domain%', domain)
                sys.stdout.write(line)

            # Set user as owner of dirs and files
            misc.cmd(['chown', '-R', user + ':' + user, '/home/' + user])

            exit(' %s was added successfully.' % domain)

        else:
            exit(' %s already exists.' % domain)
Example #2
0
def rmv(cmd):
    if misc.arg_range(cmd):
        exit(' Error! Usage: onyx-api --webdel user domain')

    user = cmd[0]
    domain = cmd[1]

    if misc.user_chk(user):
        path = '/home/' + user + '/web/'

        # Remove the domain's dir recursively
        if os.path.exists(path + domain):
            shutil.rmtree(path + domain)

            # Remove empty web folder
            if not os.listdir(path):
                os.rmdir(path)

            exit(" %s was removed." % domain)

        else:
            exit(' %s doesn\'t exist.' % domain)