import os
import shutil
import stat
import time
from subprocess import Popen, check_output, PIPE

from .models import Site
from ..users.models import User, Group

from django.template.loader import render_to_string


def create_site_users(site):
    try:
        user = User.objects.get(username="site_{}".format(site.name))
    except User.DoesNotExist:
        user = User.objects.create(id=get_next_id(), service=True, username="site_{}".format(site.name), email="site_{}@tjhsst.edu".format(site.name))
    try:
        group = Group.objects.get(name="site_{}".format(site.name))
    except Group.DoesNotExist:
        group = Group.objects.create(id=user.id, service=True, name="site_{}".format(site.name))
        group.users.add(user)
        group.save()
    site.user = user
    site.group = group
    site.save()
    return (user, group)


def get_next_id():
    if User.objects.filter(service=True).count() == 0:
        return 10000
    return list(User.objects.filter(service=True).order_by('id'))[-1].id + 1


def make_site_dirs(site):
    for i in ["{}", "{}public", "{}private"]:
        path = i.format(site.path)
        if not os.path.exists(path):
            os.makedirs(path)
        os.chown(path, site.user.id, site.group.id)
        os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
                 | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
                 | stat.S_ISGID)
    Popen("/usr/bin/setfacl -m u:www-data:rx {}".format(site.path).split())
    Popen("/usr/bin/setfacl -m u:www-data:rx {}public".format(site.path).split())


def create_config_files(site):
    if not site.custom_nginx:
        with open("/etc/nginx/director.d/{}.conf".format(site.name), "w+") as f:
            f.write(render_to_string("config/nginx.conf", {"site": site}))
    if site.category == "php":
        with open("/etc/php5/fpm/pool.d/{}.conf".format(site.name), "w+") as f:
            f.write(render_to_string("config/phpfpm.conf", {"site": site}))


def write_new_index_file(site):
    with open("{}public/index.html".format(site.path), "w+") as f:
        f.write(render_to_string("config/index.html", {"site": site}))


def delete_site_files(site):
    files = ["/etc/nginx/director.d/{}.conf", "/etc/php5/fpm/pool.d/{}.conf", "/etc/supervisor/director.d/{}.conf"]
    files = [x.format(site.name) for x in files]
    for f in files:
        if os.path.isfile(f):
            os.remove(f)
    shutil.rmtree(site.path)


def create_process_config(process):
    with open("/etc/supervisor/director.d/{}.conf".format(process.site.name), "w+") as f:
        f.write(render_to_string("config/supervisor.conf", {"process": process}))


def delete_process_config(process):
    filename = "/etc/supervisor/director.d/{}.conf".format(process.site.name)
    if os.path.isfile(filename):
        os.remove(filename)


def restart_supervisor(site):
    try:
        site.process
        Popen("supervisorctl restart {}".format(site.name).split())
    except Site.process.RelatedObjectDoesNotExist:
        pass


def get_supervisor_status(site):
    try:
        site.process
        return check_output("supervisorctl status {}".format(site.name).split()).decode()
    except Site.process.RelatedObjectDoesNotExist:
        return "No Process"


def reload_services():
    Popen("systemctl reload nginx.service".split())
    Popen("systemctl restart php5-fpm.service".split())
    Popen("supervisorctl update".split())


def flush_permissions():
    with open("/proc/net/rpc/auth.unix.gid/flush", "w") as f:
        f.write(str(int(time.time())))
    Popen("/usr/sbin/nscd -i group".split())
    Popen("/usr/sbin/nscd -i passwd".split())


def run_as_site(site, cmd, cwd=None, env=None):
    proc = Popen(cmd.split(), preexec_fn=demote(site.user.id, site.group.id), cwd=cwd or site.path, env=env, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    return (proc.returncode, out, err)


def demote(uid, gid):
    def result():
        os.setgid(gid)
        os.setuid(uid)
    return result


def generate_ssh_key(site):
    keypath = os.path.join(site.private_path, "rsa.key")

    if os.path.isfile(keypath):
        os.remove(keypath)
    if os.path.isfile(keypath + ".pub"):
        os.remove(keypath + ".pub")

    proc = Popen("/usr/bin/ssh-keygen -t rsa -b 4096 -N '' -f {}".format(keypath).split(), stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    if proc.returncode != 0:
        raise IOError("Error generating ssh keys: ({}) {} {}".format(proc.returncode, out, err))

    os.chown(keypath, site.user.id, site.group.id)
    os.chown(keypath + ".pub", site.user.id, site.group.id)

@@ -4240,30 +4240,24 @@
 b%22)%0A%0A   
- proc =
  Popen(
+%5B
 %22/usr/bi
@@ -4272,246 +4272,69 @@
 ygen
- -t rsa -b 4096 -N '' -f %7B%7D%22.format(keypath).split(), stdout=PIPE, stderr=PIPE)%0A    out, err = proc.communicate()%0A    if proc.returncode != 0:%0A        raise IOError(%22Error generating ssh keys: (%7B%7D) %7B%7D %7B%7D%22.format(proc.returncode, out, err)
+%22, %22-t%22, %22rsa%22, %22-b%22, %224096%22, %22-N%22, %22%22, %22-f%22, keypath%5D).wait(
 )%0A%0A 

fix keygen command