python 2.7 - Custom Django VM Control Panel - LDAP Questions/Insight needed -
i intern @ company summer. have been assigned project must done in django. have rough setup going , need feedback of how should further set things up. people on techops team spend time rebooting vms developers when break something. solution allow users themselves.
project outline
- user logs in ldap credentials
- server gets ldap groups individual in ['techops', 'staff']
- only servers apart of these ldap groups displayed
- user have ability reboot these vms
- server ssh designated vm domain via ssh key , trigger vm reboot
i have ability users sign admin panel via ldap, not on separate authentication page website. there way populate user's ldap groups automatically?
# populate django user ldap directory. auth_ldap_user_attr_map = { "first_name": "givenname", "last_name": "sn", "email": "mail" }
i need understanding how ldap works(with django), , how can retrieve current user logged in, run get_ldapgroups function, compare these groups server ldap group, display servers on /reboot page.
please feel free ask questions or request additional files. appreciate of time , guys may provide me.
here files of right now:
servers models.py
class team(models.model): name = models.charfield(max_length=64) email = models.emailfield(max_length=254, default='@business.com') ldap_group = models.charfield(max_length=64) def __unicode__(self): return '"' + self.name + '" <' + self.email + '>' class site(models.model): name = models.charfield(max_length=254) def __unicode__(self): return self.name class server(models.model): hostname = models.charfield(max_length=254) fqdn = models.charfield(max_length=254) owner = models.foreignkey(team) site = models.foreignkey(site) def __unicode__(self): return self.hostname
reboot views.py lists servers, when click on server let's know if "alive"
from django.template import context, loader django.http import httpresponse servers.models import server import paramiko import socket def index(request): t = loader.get_template('reboot/index.html') servers = server.objects.all() c = context( { 'servers': servers, }) return httpresponse(t.render(c)) def test_ssh_liveness(ssh, name): try: ssh.connect(name, timeout='1') return true except socket.timeout: # server down return false except socket.gaierror: # invalid server name return false except paramiko.sshexception: # unknown host key return true def server(request, name): ssh = paramiko.sshclient() is_alive = test_ssh_liveness(ssh, name) return httpresponse("you selected server "+name+" , "+str(is_alive))
reboot template
{% block title %}server{% endblock %} {% block content %} <h1>server</h1> <ul> {% server in servers %} <li><a href="{% url 'server' server.hostname %}">{{ server.hostname }} {% endfor %} </ul> {% endblock %}
django has pluggable authentication system concept of different backends. django comes 2 backends; 1 uses database , other can use external user system.
taking advantage of flexibility django-auth-ldap, custom backend designed work ldap directory. drop-in replacement.
you set , automatically populate user groups , can other things (see documentation more information).
the example configuration details of configuration , setup details.
as final tip, considering offloading restarting of servers. otherwise should there network issue or other delay, application won't blocked. schedule tasks offline django, use celery.
Comments
Post a Comment