def _ensureIfDown(ifname, ifconfig_output, logger): """ Check if an interface is down (useful before ifup...) If it is not down, perform an ifdown on it """ if "%s " % ifname not in ifconfig_output: logger.debug("Ok, %s is down as expected" % ifname) return logger.debug( "Found interface %s to be up. " "Setting it down prior to ifup it by safety" % ifname ) command = '/sbin/ifdown %s' % ifname process = createProcess(logger, command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env={}) return_code, stdout, stderr = communicateProcess(logger, process, TIMEOUT) if return_code != 0: logger.error("Error while running '%s'.\n%s" % (command, '\n'.join(stdout))) else: logger.debug("...ok")
def _setethernetspeed(self, ethernet): if ethernet.eth_auto: cmd = "/usr/sbin/ethtool -s %s autoneg on" % ethernet.system_name self.responsible.feedback( tr("Setting up speed for interface %(DESCRIPTION)s: auto"), DESCRIPTION=ethernet.fullName() ) else: args = { 'name': ethernet.system_name, 'duplex': "full" if ethernet.eth_duplex == Ethernet.FULL else "half", 'speed': ethernet.eth_speed } cmd = "/usr/sbin/ethtool -s %(name)s autoneg off speed "\ "%(speed)s duplex %(duplex)s" % args self.responsible.feedback( tr( "Setting up speed for interface %(DESCRIPTION)s: " "speed: %(SPEED)s, duplex: %(DUPLEX)s."), DESCRIPTION=ethernet.fullName(), SPEED=args['speed'], DUPLEX=args['duplex'] ) process = createProcess(self, cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env={}) retcode, stdout, stderr = communicateProcess(self, process, 30) if retcode == 0: return else: self.responsible.feedback("Could not set speed.") #Explicitely catched raise EthernetSpeedError("Error while running [%s]." % cmd)
def killdhclients(self): for name in ('dhclient', 'dhclient3'): command = 'killall %s' % name process = createProcess(self, command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env={}) return_code, stdout, stderr = communicateProcess(self, process, 20) if stdout: self.debug("stdout: %s" % '\n'.join(stdout))
def _start_or_stopNetworkDefer(self, log_prefix, command): process = createProcess(self, command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env={}) retcode, stdout, stderr = communicateProcess(self, process, TIMEOUT) if not check_and_correct_lo(self): self.critical("Networking configuration problem. :-(") if retcode == 0: return for line in stdout: self.info("%s: %s" % (log_prefix, line)) line_lower = line.lower() if (u'failed' in line_lower) or (u'error' in line_lower): raise ConfigError(line)
def _ensureIfsDown(interfaces_list, logger): command = "/sbin/ifconfig" process = createProcess(logger, command.split(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env={}) return_code, stdout, stderr = communicateProcess(logger, process, TIMEOUT) stdout, stderr = __stdoutAndStderr(process) if return_code != 0: output = '\n'.join(stdout) logger.critical("Error while running '%s'.\n%s" % (command, output)) raise NetCfgError("Could not run ifconfig properly ?!\n%s" % output) ifconfig_output = stdout for ifname in interfaces_list.split(): _ensureIfDown(ifname, ifconfig_output, logger)
def runCommandAndCheck(logger, command, timeout=DEFAULT_TIMEOUT, **popen_args): # Return (process, stdout) where stdout is a list of unicode strings popen_args.update({'stdout': PIPE, 'stderr': STDOUT}) cmdstr = formatCommand(command) process = createProcess(logger, command, **popen_args) status, stdout, stderr = communicateProcess(logger, process, timeout) if status: output = u'\n'.join(stdout) raise RunCommandError(cmdstr, unicode(status), output) else: command_str = popen_args.get('cmdstr', command) logger.debug("Success : '%s'" % unicode(command_str)) return process, stdout
def _parseGetent(self): groups = set() process = createProcess(self, ['/usr/bin/getent', 'group'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) # read output with a timeout of 5 minutes status, stdout, stderr = communicateProcess(self, process, 60.0*5) if status: logs = stdout[20:] + stderr[20:] raise ComponentError(tr("getent command failed:\n%s"), logs) for line in stdout: parts = line.split(u':') name = parts[0] gid = int(parts[2]) groups.add((name, gid)) return groups
def service_getDiagnosticFile(self, context): """ Return a diagnostic file, containing the result of various command """ process = createProcess(self, '/usr/share/ufwi_rpcd/scripts/diagnostic', stdout=subprocess.PIPE, stderr=subprocess.PIPE, locale=False) return_code, out, err = communicateProcess(self, process, DEFAULT_TIMEOUT) if return_code != 0: raise CreateDiagFailed(err) tmp_dir = out[0].strip() with open(tmp_dir + "/diagnostic.tar.gz") as fd: result = encodeFileContent(fd.read()) try: rmtree(tmp_dir) except Exception, err: self.error( 'Could not delete temporary diagnostic directory (%s).', err)