convert port to an int before passing it to httplib.HTTPSConnection(). It
[bootmanager.git] / source / steps / WriteNetworkConfig.py
index 2e6867d..9d5fd81 100644 (file)
@@ -11,8 +11,11 @@ import os, string
 
 from Exceptions import *
 import utils
-import BootAPI
+import BootServerRequest
 import ModelOptions
+import urlparse
+import httplib
+import BootAPI
 
 def Run( vars, log ):
     """
@@ -22,9 +25,6 @@ def Run( vars, log ):
     /etc/resolv.conf (if applicable)
     /etc/sysconfig/network
 
-    It is assumed the caller mounted the root partition and the vserver partition
-    starting on SYSIMG_PATH - it is not checked here.
-
     The values to be used for the network settings are to be set in vars
     in the variable 'NETWORK_SETTINGS', which is a dictionary
     with keys:
@@ -50,6 +50,7 @@ def Run( vars, log ):
                             (always starts with TEMP_PATH)
     NETWORK_SETTINGS  A dictionary of the values from the network
                                 configuration file
+    NODE_NETWORKS           All the network associated with this node
     Sets the following variables:
     None
     """
@@ -91,7 +92,39 @@ def Run( vars, log ):
     except KeyError, e:
         pass
 
-        
+
+    # Node Manager needs at least PLC_API_HOST and PLC_BOOT_HOST
+    log.write("Writing /etc/planetlab/plc_config\n")
+    utils.makedirs("%s/etc/planetlab" % SYSIMG_PATH)
+    plc_config = file("%s/etc/planetlab/plc_config" % SYSIMG_PATH, "w")
+
+    api_url = vars['BOOT_API_SERVER']
+    (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
+    parts = netloc.split(':')
+    host = parts[0]
+    if len(parts) > 1:
+        port = parts[1]
+    else:
+        port = '80'
+    try:
+        log.write("getting via https://%s/PlanetLabConf/get_plc_config.php" % host)
+        bootserver = httplib.HTTPSConnection(host, int(port))
+        bootserver.connect()
+        bootserver.request("GET","https://%s/PlanetLabConf/get_plc_config.php" % host)
+        plc_config.write("%s" % bootserver.getresponse().read())
+        bootserver.close()
+    except:
+        log.write("Failed.  Using old method.")
+        bs= BootServerRequest.BootServerRequest()
+        if bs.BOOTSERVER_CERTS:
+            print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
+        print >> plc_config, "PLC_API_HOST='%s'" % host
+        print >> plc_config, "PLC_API_PORT='%s'" % port
+        print >> plc_config, "PLC_API_PATH='%s'" % path
+
+    plc_config.close()
+
+
     log.write( "Writing /etc/hosts\n" )
     hosts_file= file("%s/etc/hosts" % SYSIMG_PATH, "w" )    
     hosts_file.write( "127.0.0.1       localhost\n" )
@@ -101,25 +134,6 @@ def Run( vars, log ):
     hosts_file= None
     
 
-    log.write( "Writing /etc/sysconfig/network-scripts/ifcfg-eth0\n" )
-    eth0_file= file("%s/etc/sysconfig/network-scripts/ifcfg-eth0" %
-                    SYSIMG_PATH, "w" )
-    eth0_file.write( "DEVICE=eth0\n" )
-    if method == "static":
-        eth0_file.write( "BOOTPROTO=static\n" )
-        eth0_file.write( "IPADDR=%s\n" % ip )
-        eth0_file.write( "NETMASK=%s\n" % netmask )
-        eth0_file.write( "GATEWAY=%s\n" % gateway )
-    else:
-        eth0_file.write( "BOOTPROTO=dhcp\n" )
-        eth0_file.write( "DHCP_HOSTNAME=%s\n" % hostname )
-    if mac != "":
-        eth0_file.write( "HWADDR=%s\n" % mac )
-    eth0_file.write( "ONBOOT=yes\n" )
-    eth0_file.write( "USERCTL=no\n" )
-    eth0_file.close()
-    eth0_file= None
-
     if method == "static":
         log.write( "Writing /etc/resolv.conf\n" )
         resolv_file= file("%s/etc/resolv.conf" % SYSIMG_PATH, "w" )
@@ -139,3 +153,90 @@ def Run( vars, log ):
         network_file.write( "GATEWAY=%s\n" % gateway )
     network_file.close()
     network_file= None
+
+    interfaces = {}
+    interface = 1
+    for network in vars['NODE_NETWORKS']:
+        if method == "static" or method == "dhcp":
+            if network['is_primary'] == 1:
+                ifnum = 0
+            else:
+                ifnum = interface
+                interface += 1
+
+            inter = {}
+            if network['mac']:
+                inter['HWADDR'] = network['mac']
+
+            if network['method'] == "static":
+                inter['BOOTPROTO'] = "static"
+                inter['IPADDR'] = network['ip']
+                inter['NETMASK'] = network['netmask']
+
+            elif network['method'] == "dhcp":
+                inter['BOOTPROTO'] = "dhcp"
+                if network['hostname']:
+                    inter['DHCP_HOSTNAME'] = network['hostname']
+                else:
+                    inter['DHCP_HOSTNAME'] = hostname
+                if not network['is_primary']:
+                    inter['DHCLIENTARGS'] = "-R subnet-mask"
+
+            alias = ""
+            ifname=None
+            if len(network['nodenetwork_setting_ids']) > 0:
+                settings = BootAPI.call_api_function(vars, "GetNodeNetworkSettings",
+                    ({'nodenetwork_setting_id': network['nodenetwork_setting_ids']},))
+                for setting in settings:
+                    # to explicitly set interface name
+                    if   setting['name'].upper() == "IFNAME":
+                        ifname=setting['value']
+                    elif setting['name'].upper() == "DRIVER":
+                        # xxx not sure how to do that yet - probably add a line in modprobe.conf
+                        pass
+                    elif setting['name'].upper() == "ALIAS":
+                        alias = ":" + setting['value']
+
+                    # a hack for testing before a new setting is hardcoded here
+                    # use the backdoor setting and put as a value 'var=value'
+                    elif setting['name'].upper() == "BACKDOOR":
+                        [var,value]=setting['value'].split('=',1)
+                        inter[var]=value
+
+                    elif setting['name'].lower() in \
+                            [  "mode", "essid", "nw", "freq", "channel", "sens", "rate",
+                               "key", "key1", "key2", "key3", "key4", "securitymode", 
+                               "iwconfig", "iwpriv" ] :
+                        inter [setting['name'].upper()] = setting['value']
+                        inter ['TYPE']='Wireless'
+                    else:
+                        log.write("Warning - ignored setting named %s\n"%setting['name'])
+
+            if alias and 'HWADDR' in inter:
+                for (dev, i) in interfaces.iteritems():
+                    if i['HWADDR'] == inter['HWADDR']:
+                        break
+                del inter['HWADDR']
+                interfaces[dev + alias] =inter 
+                interface -= 1
+            else:
+                if not ifname:
+                    ifname="eth%d" % ifnum
+                else:
+                    interface -= 1
+                interfaces[ifname] =inter 
+
+    for (dev, inter) in interfaces.iteritems():
+        path = "%s/etc/sysconfig/network-scripts/ifcfg-%s" % (
+               SYSIMG_PATH, dev)
+        f = file(path, "w")
+        log.write("Writing %s\n" % path.replace(SYSIMG_PATH, ""))
+
+        f.write("DEVICE=%s\n" % dev)
+        f.write("ONBOOT=yes\n")
+        f.write("USERCTL=no\n")
+        for (key, val) in inter.iteritems():
+            f.write('%s="%s"\n' % (key, val))
+
+        f.close()
+