Esempio n. 1
0
    def _setConsoleGateway(self,
                           newGateway,
                           vswifName='vswif0',
                           setConfFileOnly=True):
        '''Set the default network gateway for the Console OS.
        Arguments:
         vswifName: name of the vswif. ie, 'vswif0'.  If the named vswif
         hasn't actually been created, ie, there's no vNic with that name,
         then setConfFileOnly should be set to True.  Using an empty string
         results in no GATEWAYDEV being set in /etc/sysconfig/network

         setConfFileOnly: setting to False will result in vmkctl
         trying to bring up the new gateway, which results in a call
         to `/sbin/ip route replace ...`
        '''
        # TODO: I am trusting here that it has been previously sanity-checked
        #       perhaps I should be less trusting
        routeInfo = vmkctl.RoutingInfoImpl()

        oldGateway = self._getConsoleGateway()
        if oldGateway and oldGateway not in [self.DEFAULT_GATEWAY, newGateway]:
            log.info('Changing gateway from %s to %s' %
                     (oldGateway, newGateway))
        else:
            log.info('Setting gateway to %s' % newGateway)

        self._desiredGateway = newGateway
        if setConfFileOnly:
            self._vmkctlKnowsDesiredGateway = False
        vmkctlGateway = vmkctl.Ipv4Address(newGateway)
        routeInfo.SetConsoleDefaultGateway(vmkctlGateway, vswifName,
                                           setConfFileOnly)
        self._verifyConsoleGatewaySaved(newGateway)
Esempio n. 2
0
    def _verifyConsoleGatewaySaved(self, gateway):
        '''Weasel is reliant on vmkctl to do the right thing.  Sometimes
        vmkctl is finicky and doesn't do what we expect, like not writing
        the gateway to the conf files.  Furthermore, it may not complain.
        We want to know about those errors earlier rather than later,
        so check that files get written properly.
        '''
        # This function is not critical, so if it borks, don't take
        # Weasel down with it.
        try:
            fp = open('/etc/sysconfig/network', 'r')
            etcSysconfigNetworkContent = fp.read()
            fp.close()
            if ('GATEWAY=%s' % gateway) not in etcSysconfigNetworkContent:
                log.error('Gateway (%s) did not get saved in'
                          ' /etc/sysconfig/network' % gateway)
            if self._vmkctlKnowsDesiredGateway:
                rInfo = vmkctl.RoutingInfoImpl()
                vGateway = rInfo.GetConsoleDefaultGateway().GetStringAddress()
                if gateway != vGateway:
                    log.error('Gateway (%s) did not get saved in'
                              ' RoutingInfoImpl (%s)' % (gateway, vGateway))

        except Exception, ex:
            log.error('Exception while verifying the saving of the gateway'
                      ' (%s). Exception: %s' % (gateway, str(ex)))
Esempio n. 3
0
    def _setVmkernelGateway(self, newGateway):
        # TODO: I am trusting here that it has been previously sanity-checked
        #       perhaps I should be less trusting
        routeInfo = vmkctl.RoutingInfoImpl()

        oldGateway = self._getVmkernelGateway()
        if oldGateway and oldGateway not in [self.DEFAULT_GATEWAY, newGateway]:
            log.info('Changing vmkernel gateway from %s to %s' %
                     (oldGateway, newGateway))
        else:
            log.info('Setting vmkernel gateway to %s' % newGateway)

        vmkctlGateway = vmkctl.Ipv4Address(newGateway)
        routeInfo.SetVmKernelDefaultGateway(vmkctlGateway)
Esempio n. 4
0
    else:
        ipConfig.SetUseDhcp(False)
        ipConfig.SetIpv4Address(vmkctl.Ipv4Address(ip))
        ipConfig.SetIpv4Netmask(vmkctl.Ipv4Address(netmask))

    newNic = None
    try:
        newNic = vmknicInfo.AddVmKernelNic(PORTGROUP_NAME, ipConfig,
                                           macAddress, 0, 0)
    except vmkctl.HostCtlException, ex:
        log.debug("AddVmKernelNic(): " + str(ex.GetMessage()))
        raise

    if not dhcp:
        try:
            vmkctl.RoutingInfoImpl().SetVmKernelDefaultGateway(
                vmkctl.Ipv4Address(gateway))
        except vmkctl.HostCtlException, ex:
            log.debug("SetVmKernelDefaultGateway(): " + str(ex.GetMessage()))
            raise

    for pnic in vmkctl.NetworkInfoImpl().GetPnics():
        if pnic.GetMacAddress().GetStringAddress() == \
        macAddress.GetStringAddress():
            try:
                vswitch.AddUplink(pnic.GetName())
            except vmkctl.HostCtlException, ex:
                log.debug("vswitch.addUplink("+pnic.GetName()+"): " + \
                          str(ex.GetMessage()))
                raise

    try:
Esempio n. 5
0
 def _getVmkernelGateway(self):
     routeInfo = vmkctl.RoutingInfoImpl()
     return routeInfo.GetVmKernelDefaultGateway().GetStringAddress()
Esempio n. 6
0
 def _getConsoleGateway(self):
     if self._vmkctlKnowsDesiredGateway:
         routeInfo = vmkctl.RoutingInfoImpl()
         return routeInfo.GetConsoleDefaultGateway().GetStringAddress()
     else:
         return self._desiredGateway
Esempio n. 7
0
    more python friendly WrappedVmkctlException
    '''
    def newFn(*args, **kwargs):
        try:
            return fn(*args, **kwargs)
        except vmkctl.HostCtlException, ex:
            raise WrappedVmkctlException(ex)
    newFn.__name__ = fn.__name__ #to make the stacktrace look better
    return newFn
    

# ==========================================================================
# aliases to vmkctl singletons
# ==========================================================================
_netInfo = vmkctl.NetworkInfoImpl()
_routingInfo = vmkctl.RoutingInfoImpl()
_vswitchInfo = vmkctl.VirtualSwitchInfoImpl()
_consoleNicInfo = vmkctl.ConsoleNicInfoImpl()
_vmkernelNicInfo = vmkctl.VmKernelNicInfoImpl()

# ==========================================================================
# module-internal helper functions
# ==========================================================================

def _checkInit():
    '''To be called from any function that assumes init() has been called
    and can not be expected to work otherwise.  Throws an exception if
    init() has not been called
    '''
    if not _networkDriverIsLoaded:
        init()