# Path: sshkm/models.py
# class Host(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     superuser = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     status = models.CharField(max_length=10, null=True, blank=True)
#     last_status = models.DateTimeField(null=True, blank=True)
# 
# class Osuser(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     home = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
# 
# class Key(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     firstname = models.CharField(max_length=191, null=True, blank=True)
#     lastname = models.CharField(max_length=191, null=True, blank=True)
#     email = models.EmailField(max_length=191, null=True, blank=True)
#     publickey = models.TextField(null=True, blank=True)
#     member_of = models.ManyToManyField('Group', through='KeyGroup', blank=True)
# 
# class KeyGroup(models.Model):
#     key = models.ForeignKey('Key', on_delete=models.CASCADE)
#     group = models.ForeignKey('Group', on_delete=models.CASCADE)
# 
# class Permission(models.Model):
#     group = models.ForeignKey(Group, on_delete=models.CASCADE)
#     host = models.ForeignKey(Host, on_delete=models.CASCADE)
#     osuser = models.ForeignKey(Osuser, on_delete=models.CASCADE)
# 
#     class Meta:
#         unique_together = ("group", "host", "osuser")
# 
# class Setting(models.Model):
#     name = models.CharField(max_length=100, unique=True)
#     value = models.TextField(null=True, blank=True)


from django.utils import timezone
    from StringIO import StringIO
    from io import StringIO
from sshkm.models import Host, Osuser, Key, KeyGroup, Permission, Setting
import paramiko
        last_home = keys[0][0]
        last_osuser = keys[0][2]
        masterkey = ''
        keyfile = ''
        counter = 0

        for key in keys:
            home = key[0]
            publickey = key[1]
            osuser = key[2]

            counter = counter + 1

            if counter == 1 and osuser == superuser:
                masterkey = config_masterkey + '\n'

            if home != last_home:
                if osuser == superuser:
                    masterkey = config_masterkey + '\n'
                CopyKeyfile(host.name, keyfile, last_osuser, last_home, superuser)
                keyfile = masterkey + publickey + '\n'
            else:
                keyfile += masterkey + publickey + '\n'
            last_home = home
            last_osuser = osuser
            masterkey = ''

        CopyKeyfile(host.name, keyfile, last_osuser, last_home, superuser)

def GetHome(osuser_id):

# Path: sshkm/models.py
# class Host(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     superuser = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     status = models.CharField(max_length=10, null=True, blank=True)
#     last_status = models.DateTimeField(null=True, blank=True)
# 
# class Osuser(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     home = models.CharField(max_length=191, null=True, blank=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
# 
# class Key(models.Model):
#     name = models.CharField(max_length=191, unique=True)
#     description = models.CharField(max_length=191, null=True, blank=True)
#     firstname = models.CharField(max_length=191, null=True, blank=True)
#     lastname = models.CharField(max_length=191, null=True, blank=True)
#     email = models.EmailField(max_length=191, null=True, blank=True)
#     publickey = models.TextField(null=True, blank=True)
#     member_of = models.ManyToManyField('Group', through='KeyGroup', blank=True)
# 
# class KeyGroup(models.Model):
#     key = models.ForeignKey('Key', on_delete=models.CASCADE)
#     group = models.ForeignKey('Group', on_delete=models.CASCADE)
# 
# class Permission(models.Model):
#     group = models.ForeignKey(Group, on_delete=models.CASCADE)
#     host = models.ForeignKey(Host, on_delete=models.CASCADE)
#     osuser = models.ForeignKey(Osuser, on_delete=models.CASCADE)
# 
#     class Meta:
#         unique_together = ("group", "host", "osuser")
# 
# class Setting(models.Model):
#     name = models.CharField(max_length=100, unique=True)
#     value = models.TextField(null=True, blank=True)


# Path: sshkm/views/deploy.py
from django.utils import timezone
    from StringIO import StringIO
    from io import StringIO
from sshkm.models import Host, Osuser, Key, KeyGroup, Permission, Setting
import paramiko

        last_home = keys[0][0]
        last_osuser = keys[0][2]
        masterkey = ''
        keyfile = ''
        counter = 0

        for key in keys:
            home = key[0]
            publickey = key[1]
            osuser = key[2]

            counter = counter + 1

            if counter == 1 and osuser == superuser:
                masterkey = config_masterkey + '\n'

            if home != last_home:
                if osuser == superuser:
                    masterkey = config_masterkey + '\n'
                CopyKeyfile(host.name, keyfile, last_osuser, last_home, superuser)
                keyfile = masterkey + publickey + '\n'
            else:
                keyfile += masterkey + publickey + '\n'
            last_home = home
            last_osuser = osuser
            masterkey = ''

        CopyKeyfile(host.name, keyfile, last_osuser, last_home, superuser)

def GetHome(osuser_id):

    osuser = Osuser.objects.get(id=osuser_id)