Example #1
0
    def setup(self):
        log.debug('Setting network options for media downloads')
        nicChoices = userchoices.getDownloadNic()
        nic = nicChoices.get('device', None)

        if nic and not nic.isLinkUp:
            failWithLog(('The specified network interface card (Name: %s'
                         ' MAC Address: %s) is not plugged in.'
                         ' Installation cannot continue as requested') %\
                        (nic.name, nic.macAddress))
        elif not nic and not networking.getPluggedInAvailableNIC(None):
            # Check for an available NIC before we go further.
            # It's best to fail early and provide a descriptive error
            failWithLog('This system does not have a network interface'
                        ' card that is plugged in, or all network'
                        ' interface cards are already claimed. '
                        ' Installation cannot continue as requested')

        # Create a netmask if it was left out
        ip = nicChoices.get('ip', None)
        netmask = nicChoices.get('netmask', None)

        if netmask and not ip:
            failWithLog('Netmask specified, but no IP given.')
        if ip and not netmask:
            log.warn('IP specified, but no netmask given.  Guessing netmask.')
            try:
                netmask = networking.utils.calculateNetmask(ip)
            except ValueError, ex:
                msg = ((genericErr + ' A netmask could not be created.')
                       % ('IP Address', str(ex)))
                failWithLog(msg)
            nicChoices.update(netmask=netmask)
            userchoices.setDownloadNic(**nicChoices)
Example #2
0
 def updateNicChoices(self, **kwargs):
     self.needed = True
     nicChoices = userchoices.getDownloadNic()
     if not nicChoices:
         # was empty - this is the first time populating it.
         newArgs = dict(device='', vlanID='') #set defaults
     else:
         newArgs = nicChoices
     newArgs.update(kwargs)
     userchoices.setDownloadNic(**newArgs)
Example #3
0
    def checkExpected():
        expected = {
            'gateway' : '192.168.2.254',
            'nameserver1' : '',
            'nameserver2' : '',
            'hostname' : 'localhost'
            }
        actual = userchoices.getDownloadNetwork()
        assert actual == expected, actual

        actual = userchoices.getDownloadNic()
        assert actual['device'].name == 'vmnic32'
        assert actual['vlanID'] == '5'
        assert actual['bootProto'] == userchoices.NIC_BOOT_STATIC
        assert actual['ip'] == '192.168.2.1'
        assert actual['netmask'] == '255.255.255.0'
    def checkExpected():
        expected = {
            'gateway': '192.168.2.254',
            'nameserver1': '',
            'nameserver2': '',
            'hostname': 'localhost'
        }
        actual = userchoices.getDownloadNetwork()
        assert actual == expected, actual

        actual = userchoices.getDownloadNic()
        assert actual['device'].name == 'vmnic32'
        assert actual['vlanID'] == '5'
        assert actual['bootProto'] == userchoices.NIC_BOOT_STATIC
        assert actual['ip'] == '192.168.2.1'
        assert actual['netmask'] == '255.255.255.0'
Example #5
0
def cosConnectForInstaller(failOnWarnings=True, onlyConfiguredNics=True):
    '''Like cosConnect, but it uses userchoices and if you use this
    function exclusively, it will keep only one connected Vnic at a time.
    Tries to make a connection in the following order, stopping after the
    first successful connection is made:
     1. try to use the config in userchoices.*DownloadNic and *DownloadNetwork
     2. try to use the config in userchoices.*CosNic and *CosNetwork
     3. try to use DHCP connections on any remaining NICs

    Arguments:
    failOnWarnings: if True, raise an exception on otherwise survivable
               warnings
    onlyConfiguredNics: if True, don't attempt any nics that haven't been
               configured by the user.  ie, don't try #3 above
    '''
    log.info('Attempting to bring up the network.')
    def doRaise(msg):
        raise Exception(msg)
    if failOnWarnings:
        logOrRaise = doRaise
    else:
        logOrRaise = log.warn

    # ensure we're only manipulating one COS nic
    disconnectDownloadNetwork()
    global _connectedVNic
    if _connectedVNic:
        log.info('Brought down the already-enabled Virtual NIC.')
        _connectedVNic = None

    argsForCosConnect = []
    allNicChoices = []

    downloadNicChoices = userchoices.getDownloadNic()
    if downloadNicChoices:
        log.info('The user chose specific network settings for downloading'
                 'remote media.  Those choices will be attempted first.')
        if not downloadNicChoices['device']:
            availableNIC = getPluggedInAvailableNIC(None)
            if availableNIC:
                downloadNicChoices.update(device=availableNIC)
                allNicChoices.append(downloadNicChoices)
            else:
                logOrRaise('Could not find a free Physical NIC to attach to'
                           ' download network specifications %s' 
                           % str(downloadNicChoices))
        else:
            allNicChoices.append(downloadNicChoices)

    cosNicChoices = userchoices.getCosNICs()
    if cosNicChoices:
        allNicChoices += cosNicChoices
    else:
        msg = 'No COS NICs have been added by the user.'
        logOrRaise(msg)

    for nicChoices in allNicChoices:
        log.debug('nicChoices %s' %str(nicChoices))
        log.debug('Setting vlan (%(vlanID)s), ipConf (%(bootProto)s, %(ip)s, %(netmask)s) '
                  'for NIC %(device)s' % nicChoices)
        assert nicChoices['device']
        nic = nicChoices['device'] # a reference to a PhysicalNicFacade
        vlanID = nicChoices['vlanID']
        if not vlanID:
            vlanID = None #make sure it's None, not just ''
        if nicChoices['bootProto'] == userchoices.NIC_BOOT_STATIC:
            if not nicChoices['ip'] or not nicChoices['netmask']:
                msg = ('COS NIC %s is not fully defined. Missing '
                       'IP address or netmask' % str(nic))
                logOrRaise(msg)
            ipConf = StaticIPConfig(nicChoices['ip'], nicChoices['netmask'])
        elif nicChoices['bootProto'] == userchoices.NIC_BOOT_DHCP:
            ipConf = DHCPIPConfig()
        else:
            msg = 'Unknown bootProto specified for %s' % nic
            logOrRaise(msg)
            ipConf = DHCPIPConfig()

        argsForCosConnect.append((nic, vlanID, ipConf))

    if not onlyConfiguredNics:
        # we've tried all the user-configured nics, now try the rest with DHCP
        configuredNics = [choices['device'] for choices in allNicChoices]
        unConfiguredNics =  set(getPhysicalNics()) - set(configuredNics)
        # sort these for repeatability's sake.
        unConfiguredNics = list(unConfiguredNics)
        unConfiguredNics.sort()
        for nic in unConfiguredNics:
            if not nic.isLinkUp:
                continue # it would be pointless to try unplugged NICs
            log.info('Setting unconfigured NIC %s to use DHCP' % nic)
            ipConf = DHCPIPConfig()
            argsForCosConnect.append((nic, None, ipConf))

    for nic, vlanID, ipConf in argsForCosConnect:
        try:
            log.info('Bringing up network interface for NIC %s. Using ipConf %s'
                     % (nic,ipConf))
            vnic = cosConnect(pNic=nic, vlanID=vlanID, ipConf=ipConf)
        except vmkctl.HostCtlException, ex:
            msg = 'vmkctl HostCtlException:'+ ex.GetMessage()
            logOrRaise(msg)
        else:
            log.info('COS has an enabled Virtual NIC %s.' % vnic)
            _connectedVNic = vnic
            break #we only need one to work