Friday, 23 August 2013

Custom Django VM Control Panel - LDAP Questions/Insight needed

Custom Django VM Control Panel - LDAP Questions/Insight needed

I am an intern at a company for the summer. I have been assigned a project
that must be done in Django. I have a rough setup going and I need some
feedback of how I should further set things up. People on the techops team
spend too much time rebooting VMs for developers when they break
something. The solution is to allow users to do this themselves.
Project Outline
User logs in with ldap credentials
Server gets ldap groups that the individual is in ['techops', 'staff']
Only servers that are apart of these ldap groups will be displayed
User will then have the ability to reboot these VMs
Server will ssh into designated VM domain via SSH key and trigger a vm reboot
I need help understanding how LDAP works(with Django), and how I can
retrieve the current user that is logged in, run my get_Ldapgroups
function, compare these groups to the server ldap group, then only display
those servers on the /reboot page.
Please feel free to ask questions or request any additional files. I
appreciate all of your time and help that you guys may provide me.
Here are my files as 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 the servers, when you click on a server it let's you
know if it is "alive"
from django.template import Context, loader
from django.http import HttpResponse
from 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 is 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+" and it is
"+str(is_alive))
reboot template
{% block title %}Server{% endblock %}
{% block content %}
<h1>Server</h1>
<ul>
{% for server in servers %}
<li><a href="{% url 'server' server.hostname %}">{{ server.hostname }}
{% endfor %}
</ul>
{% endblock %}

No comments:

Post a Comment