parameters: (banned_irqs)
docstring: Restart irqbalance if it's running and ban it from moving the IRQs from the
      given list.
docstring_summary: Restart irqbalance if it's running and ban it from moving the IRQs from the
      given list.
docstring_tokens: Restart irqbalance if it s running and ban it from moving the IRQs from the given list .
function: def restart_irqbalance(banned_irqs):
      """
      Restart irqbalance if it's running and ban it from moving the IRQs from the
      given list.
      """
      config_file = '/etc/default/irqbalance'
      options_key = 'OPTIONS'
      systemd = False
      banned_irqs_list = list(banned_irqs)
  
      # If there is nothing to ban - quit
      if not banned_irqs_list:
          return
  
      # return early if irqbalance is not running
      if not is_process_running('irqbalance'):
          perftune_print("irqbalance is not running")
          return
  
      # If this file exists - this a "new (systemd) style" irqbalance packaging.
      # This type of packaging uses IRQBALANCE_ARGS as an option key name, "old (init.d) style"
      # packaging uses an OPTION key.
      if os.path.exists('/lib/systemd/system/irqbalance.service'):
          options_key = 'IRQBALANCE_ARGS'
          systemd = True
  
      if not os.path.exists(config_file):
          if os.path.exists('/etc/sysconfig/irqbalance'):
              config_file = '/etc/sysconfig/irqbalance'
          elif os.path.exists('/etc/conf.d/irqbalance'):
              config_file = '/etc/conf.d/irqbalance'
              options_key = 'IRQBALANCE_OPTS'
              with open('/proc/1/comm', 'r') as comm:
                  systemd = 'systemd' in comm.read()
          else:
              perftune_print("Unknown system configuration - not restarting irqbalance!")
              perftune_print("You have to prevent it from moving IRQs {} manually!".format(banned_irqs_list))
              return
  
      orig_file = "{}.scylla.orig".format(config_file)
  
      # Save the original file
      if not dry_run_mode:
          if not os.path.exists(orig_file):
              print("Saving the original irqbalance configuration is in {}".format(orig_file))
              shutil.copyfile(config_file, orig_file)
          else:
              print("File {} already exists - not overwriting.".format(orig_file))
  
      # Read the config file lines
      cfile_lines = open(config_file, 'r').readlines()
  
      # Build the new config_file contents with the new options configuration
      perftune_print("Restarting irqbalance: going to ban the following IRQ numbers: {} ...".format(", ".join(banned_irqs_list)))
  
      # Search for the original options line
      opt_lines = list(filter(lambda line : re.search("^\s*{}".format(options_key), line), cfile_lines))
      if not opt_lines:
          new_options = "{}=\"".format(options_key)
      elif len(opt_lines) == 1:
          # cut the last "
          new_options = re.sub("\"\s*$", "", opt_lines[0].rstrip())
          opt_lines = opt_lines[0].strip()
      else:
          raise Exception("Invalid format in {}: more than one lines with {} key".format(config_file, options_key))
  
      for irq in banned_irqs_list:
          # prevent duplicate "ban" entries for the same IRQ
          patt_str = "\-\-banirq\={}\Z|\-\-banirq\={}\s".format(irq, irq)
          if not re.search(patt_str, new_options):
              new_options += " --banirq={}".format(irq)
  
      new_options += "\""
  
      if dry_run_mode:
          if opt_lines:
              print("sed -i 's/^{}/#{}/g' {}".format(options_key, options_key, config_file))
          print("echo {} | tee -a {}".format(new_options, config_file))
      else:
          with open(config_file, 'w') as cfile:
              for line in cfile_lines:
                  if not re.search("^\s*{}".format(options_key), line):
                      cfile.write(line)
  
              cfile.write(new_options + "\n")
  
      if systemd:
          perftune_print("Restarting irqbalance via systemctl...")
          run_one_command(['systemctl', 'try-restart', 'irqbalance'])
      else:
          perftune_print("Restarting irqbalance directly (init.d)...")
          run_one_command(['/etc/init.d/irqbalance', 'restart'])
function_tokens: def restart_irqbalance ( banned_irqs ) : config_file = '/etc/default/irqbalance' options_key = 'OPTIONS' systemd = False banned_irqs_list = list ( banned_irqs ) # If there is nothing to ban - quit if not banned_irqs_list : return # return early if irqbalance is not running if not is_process_running ( 'irqbalance' ) : perftune_print ( "irqbalance is not running" ) return # If this file exists - this a "new (systemd) style" irqbalance packaging. # This type of packaging uses IRQBALANCE_ARGS as an option key name, "old (init.d) style" # packaging uses an OPTION key. if os . path . exists ( '/lib/systemd/system/irqbalance.service' ) : options_key = 'IRQBALANCE_ARGS' systemd = True if not os . path . exists ( config_file ) : if os . path . exists ( '/etc/sysconfig/irqbalance' ) : config_file = '/etc/sysconfig/irqbalance' elif os . path . exists ( '/etc/conf.d/irqbalance' ) : config_file = '/etc/conf.d/irqbalance' options_key = 'IRQBALANCE_OPTS' with open ( '/proc/1/comm' , 'r' ) as comm : systemd = 'systemd' in comm . read ( ) else : perftune_print ( "Unknown system configuration - not restarting irqbalance!" ) perftune_print ( "You have to prevent it from moving IRQs {} manually!" . format ( banned_irqs_list ) ) return orig_file = "{}.scylla.orig" . format ( config_file ) # Save the original file if not dry_run_mode : if not os . path . exists ( orig_file ) : print ( "Saving the original irqbalance configuration is in {}" . format ( orig_file ) ) shutil . copyfile ( config_file , orig_file ) else : print ( "File {} already exists - not overwriting." . format ( orig_file ) ) # Read the config file lines cfile_lines = open ( config_file , 'r' ) . readlines ( ) # Build the new config_file contents with the new options configuration perftune_print ( "Restarting irqbalance: going to ban the following IRQ numbers: {} ..." . format ( ", " . join ( banned_irqs_list ) ) ) # Search for the original options line opt_lines = list ( filter ( lambda line : re . search ( "^\s*{}" . format ( options_key ) , line ) , cfile_lines ) ) if not opt_lines : new_options = "{}=\"" . format ( options_key ) elif len ( opt_lines ) == 1 : # cut the last " new_options = re . sub ( "\"\s*$" , "" , opt_lines [ 0 ] . rstrip ( ) ) opt_lines = opt_lines [ 0 ] . strip ( ) else : raise Exception ( "Invalid format in {}: more than one lines with {} key" . format ( config_file , options_key ) ) for irq in banned_irqs_list : # prevent duplicate "ban" entries for the same IRQ patt_str = "\-\-banirq\={}\Z|\-\-banirq\={}\s" . format ( irq , irq ) if not re . search ( patt_str , new_options ) : new_options += " --banirq={}" . format ( irq ) new_options += "\"" if dry_run_mode : if opt_lines : print ( "sed -i 's/^{}/#{}/g' {}" . format ( options_key , options_key , config_file ) ) print ( "echo {} | tee -a {}" . format ( new_options , config_file ) ) else : with open ( config_file , 'w' ) as cfile : for line in cfile_lines : if not re . search ( "^\s*{}" . format ( options_key ) , line ) : cfile . write ( line ) cfile . write ( new_options + "\n" ) if systemd : perftune_print ( "Restarting irqbalance via systemctl..." ) run_one_command ( [ 'systemctl' , 'try-restart' , 'irqbalance' ] ) else : perftune_print ( "Restarting irqbalance directly (init.d)..." ) run_one_command ( [ '/etc/init.d/irqbalance' , 'restart' ] )