remove svn keywords and use %{SCMURL} in spec file
[bootmanager.git] / source / steps / WriteNetworkConfig.py
index f92985c..bfc29b4 100644 (file)
@@ -1,5 +1,5 @@
-#!/usr/bin/python2
-
+#!/usr/bin/python
+#
 # Copyright (c) 2003 Intel Corporation
 # All rights reserved.
 #
@@ -8,23 +8,44 @@
 # expected /proc/partitions format
 
 import os, string
+import traceback
 
-from Exceptions import *
 import utils
+import urlparse
+import httplib
+
+from Exceptions import *
 import BootServerRequest
 import ModelOptions
-import urlparse
+import BootAPI
+import plnet
+
+class BootAPIWrap:
+    def __init__(self, vars):
+        self.vars = vars
+    def call(self, func, *args):
+        return BootAPI.call_api_function(self.vars, func, args)
+    def __getattr__(self, func):
+        return lambda *args: self.call(func, *args)
+
+class logger:
+    def __init__(self, log):
+        self._log = log
+    def log(self, msg, level=3):
+        self._log.write(msg + "\n")
+    def verbose(self, msg):
+        self.log(msg, 0)
 
 def Run( vars, log ):
     """
     Write out the network configuration for this machine:
     /etc/hosts
-    /etc/sysconfig/network-scripts/ifcfg-eth0
+    /etc/sysconfig/network-scripts/ifcfg-<ifname>
     /etc/resolv.conf (if applicable)
     /etc/sysconfig/network
 
     The values to be used for the network settings are to be set in vars
-    in the variable 'NETWORK_SETTINGS', which is a dictionary
+    in the variable 'INTERFACE_SETTINGS', which is a dictionary
     with keys:
 
      Key               Used by this function
@@ -45,9 +66,9 @@ def Run( vars, log ):
 
     Expect the following variables from the store:
     SYSIMG_PATH             the path where the system image will be mounted
-                            (always starts with TEMP_PATH)
-    NETWORK_SETTINGS  A dictionary of the values from the network
-                                configuration file
+                                (always starts with TEMP_PATH)
+    INTERFACES              All the interfaces associated with this node
+    INTERFACE_SETTINGS      dictionary 
     Sets the following variables:
     None
     """
@@ -66,39 +87,31 @@ def Run( vars, log ):
 
 
     try:
-        network_settings= vars['NETWORK_SETTINGS']
+        INTERFACE_SETTINGS= vars['INTERFACE_SETTINGS']
     except KeyError, e:
-        raise BootManagerException, "No network settings found in vars."
+        raise BootManagerException, "No interface settings found in vars."
 
     try:
-        hostname= network_settings['hostname']
-        domainname= network_settings['domainname']
-        method= network_settings['method']
-        ip= network_settings['ip']
-        gateway= network_settings['gateway']
-        network= network_settings['network']
-        netmask= network_settings['netmask']
-        dns1= network_settings['dns1']
-        mac= network_settings['mac']
+        hostname= INTERFACE_SETTINGS['hostname']
+        domainname= INTERFACE_SETTINGS['domainname']
+        method= INTERFACE_SETTINGS['method']
+        ip= INTERFACE_SETTINGS['ip']
+        gateway= INTERFACE_SETTINGS['gateway']
+        network= INTERFACE_SETTINGS['network']
+        netmask= INTERFACE_SETTINGS['netmask']
+        dns1= INTERFACE_SETTINGS['dns1']
+        mac= INTERFACE_SETTINGS['mac']
     except KeyError, e:
-        raise BootManagerException, "Missing value %s in network settings." % str(e)
-
-    try:
-        dns2= ''
-        dns2= network_settings['dns2']
-    except KeyError, e:
-        pass
+        raise BootManagerException, "Missing value %s in interface settings." % str(e)
 
+    # dns2 is not required to be set
+    dns2 = INTERFACE_SETTINGS.get('dns2','')
 
     # 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")
 
-    bs= BootServerRequest.BootServerRequest()
-    if bs.BOOTSERVER_CERTS:
-        print >> plc_config, "PLC_BOOT_HOST='%s'" % bs.BOOTSERVER_CERTS.keys()[0]
-
     api_url = vars['BOOT_API_SERVER']
     (scheme, netloc, path, params, query, fragment) = urlparse.urlparse(api_url)
     parts = netloc.split(':')
@@ -107,9 +120,23 @@ def Run( vars, log ):
         port = parts[1]
     else:
         port = '80'
-    print >> plc_config, "PLC_API_HOST='%s'" % host
-    print >> plc_config, "PLC_API_PORT='%s'" % port
-    print >> plc_config, "PLC_API_PATH='%s'" % path
+    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()
+        log.write("Done\n")
+    except :
+        log.write(" .. Failed.  Using old method. -- stack trace follows\n")
+        traceback.print_exc(file=log.OutputFile)
+        bs= BootServerRequest.BootServerRequest(vars)
+        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()
 
@@ -122,42 +149,8 @@ def Run( vars, log ):
     hosts_file.close()
     hosts_file= None
     
+    data =  {'hostname': '%s.%s' % (hostname, domainname),
+             'networks': vars['INTERFACES']}
+    plnet.InitInterfaces(logger(log), BootAPIWrap(vars), data, SYSIMG_PATH,
+                         True, "BootManager")
 
-    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" )
-        if dns1 != "":
-            resolv_file.write( "nameserver %s\n" % dns1 )
-        if dns2 != "":
-            resolv_file.write( "nameserver %s\n" % dns2 )
-        resolv_file.write( "search %s\n" % domainname )
-        resolv_file.close()
-        resolv_file= None
-
-    log.write( "Writing /etc/sysconfig/network\n" )
-    network_file= file("%s/etc/sysconfig/network" % SYSIMG_PATH, "w" )
-    network_file.write( "NETWORKING=yes\n" )
-    network_file.write( "HOSTNAME=%s.%s\n" % (hostname, domainname) )
-    if method == "static":
-        network_file.write( "GATEWAY=%s\n" % gateway )
-    network_file.close()
-    network_file= None