summaryrefslogtreecommitdiffstats
path: root/scripts/register_mac.cgi
blob: 5507525bb21ebea1af2b9a5a17bed8c914ff02a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python

# Michael DeHaan <mdehaan@redhat.com>
# (C) 2008 Red Hat Inc
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# what is this?  This is a 
# script to auto add systems who make a wget into cobbler.
# right now it requires "kssendmac" in kernel options and takes only 1 arg
# ex: wget http://cobbler.example.org/cgi-bin/regsister_mac?profile=foo
# suitable to be called from kickstart,etc

import cgi
#import cgitb
import time
import os
import sys
import socket
import xmlrpclib

# FIXME: edit these two variables to match your webui configuration
USERNAME = "cobbler"
PASSWORD = "cobbler"

COBBLER_BASE = "/var/www/cobbler"
XMLRPC_SERVER = "http://127.0.0.1/cobbler_api_rw"
DEFAULT_PROFILE = "default"

#----------------------------------------------------------------------

class ServerProxy(xmlrpclib.ServerProxy):

    def __init__(self, url=None):
        xmlrpclib.ServerProxy.__init__(self, url, allow_none=True)

#----------------------------------------------------------------------

def parse_query():

    form = cgi.parse()

    if form.has_key("profile"):
        profile = form["profile"][0]
    else:
        profile = DEFAULT_PROFILE
    mac = autodetect()
    print "# incoming profile = %s" % profile
    return (mac,profile)

#----------------------------------------------------------------------

def autodetect():

    # connect to cobblerd and get the list of systems
    
    try:
        xmlrpc_server = ServerProxy(XMLRPC_SERVER)
        systems = xmlrpc_server.get_systems()
    except:
        print "# could not contact cobblerd at %s" % XMLRPC_SERVER
        sys.exit(1)

    # if kssendmac was in the kernel options line, see
    # if a system can be found matching the MAC address.  This
    # is more specific than an IP match.

    if os.environ.has_key("HTTP_X_RHN_PROVISIONING_MAC_0"):
        # FIXME: will not key off other NICs
        devicepair = os.environ["HTTP_X_RHN_PROVISIONING_MAC_0"]
        mac = devicepair.split()[1].strip()
        print "# discovered MAC: %s" % mac.lower()
        return mac.lower()
    else:
        print "# missing kssendmac in the kernel args?  Can't continue."
        return "BB:EE:EE:EE:EE:FF"

#----------------------------------------------------------------------
    

def make_change(server,mac,profile,token):
    server.register_mac(mac,profile)

#----------------------------------------------------------------------

def header():
    print "Content-type: text/plain"
    print

#----------------------------------------------------------------------

if __name__ == "__main__":
    #cgitb.enable(format='text')
    header()
    server = ServerProxy(XMLRPC_SERVER)
    token = server.login(USERNAME,PASSWORD)
    (mac, profile) = parse_query()
    print "# running for %s %s" % (mac,profile)
    make_change(server,mac,profile,token)