2
\$\begingroup\$

I have some scripts to automate software install, apply some configurations and so on after installing a new system.

I used to have them as bash scripts but it was generally a poor idea.

I have rewritten first segment into a ruby script. I am generally happy about it. But three global variables for loggers are awful and I have no good idea how to fix it. Other feedback is also welcomed.

Note that I dropped some package installs, to avoid scaring people away by 437 line script, this one has 162 lines. In case that full one is preferable - let me know, or copy it from [here][1].

require "open3"
require 'logger'

# https://ruby-doc.org/stdlib-2.6.5/libdoc/logger/rdoc/Logger.html
# how should I use it, without recreating and without passsing around?
$logger = nil
$error_logger = nil
$fatal_logger = nil

def initilize_loggers
  $logger = Logger.new('logfile_all.log')
  $logger.sev_threshold = Logger::DEBUG
  $error_logger = Logger.new('logfile_error.log')
  $error_logger.sev_threshold = Logger::ERROR
  $fatal_logger = Logger.new('logfile_fault.log')
  $fatal_logger.sev_threshold = Logger::FATAL
end

def close_loggers
  $logger.close
  $error_logger.close
  $fatal_logger.close
end

def main
  # from https://stackoverflow.com/q/52747306/4130619
  if Process.uid == 0
    # We're root!
  else
    puts("You must run this with root privileges via sudo")
    exit 1
  end
  initilize_loggers
  update_package_data
  install_packages
  standard_package_upgrade
  execute_command("sudo apt-get -qq autoremove -y")
  close_loggers
end

def log(severity, logged)
  $logger.log(severity, logged)
  $error_logger.log(severity, logged)
  $fatal_logger.log(severity, logged)
end

def log_command_result(command, stdout, stderr, status)
  if status.success?
    log(Logger::INFO, "command <#{command}> succeded with status <#{status}>, stderr <#{stderr}> stdout <#{stdout}>")
  else
    log(Logger::ERROR, "command <#{command}> failed with status <#{status}>, stderr <#{stderr}> stdout <#{stdout}>")
  end
end

def execute_external_command(command)
  log(Logger::DEBUG, "will be executing <#{command}>")
  stdout, stderr, status = Open3.capture3(command)
  log(Logger::DEBUG, "executed <#{command}>")
  log_command_result(command, stdout, stderr, status)
  return status.success?
end

def execute_command(command, retry_planned: false)
  puts command
  if execute_external_command(command)
    return true
  if retry_planned
    puts "   retry needed"
  else
    puts "   this command failed"
  end
  return false
end

def add_apt_repository(name)
  if execute_command("sudo add-apt-repository #{name} -y") == false
    log(Logger::FATAL, "adding ppa <#{name}> failed")
  end
  update_package_data()
end

def reboot_wifi
  # https://askubuntu.com/questions/597116/how-to-disable-wireless-from-command-line
  execute_command("nmcli radio wifi off")
  sleep 10
  execute_command("nmcli radio wifi on")
end

def install_package(package)
  command = "sudo apt-get -qq install #{package} -y"
  if execute_command(command, retry_planned: true)
    return true
  end
  log(Logger::WARN, "rebooting wifi and retrying, at least one of my computers has wifi problems solved by rebooting it")
  reboot_wifi()
  if execute_command(command)
    return true
  end
  log(Logger::FATAL, "after rebooting wifi <#{command}> still failed")
  return false
end

def uninstall_package(package)
  command = "sudo apt-get -qq remove #{package} -y"
  if execute_command(command)
    return true
  end
  log(Logger::FATAL, "uninstalling package <#{package}> failed")
  return false
end

def update_package_data
  if execute_command("sudo apt-get -qq update")
    return true
  end
  log(Logger::FATAL, "updating package database failed")
  return false
end

def standard_package_upgrade
  if execute_command("sudo apt-get -qq dist-upgrade -y")
    return true
  end
  log(Logger::FATAL, "upgrading packages failed")
  return false
end

def install_packages
  # launched by gs (conflicts with mispelled alias for git status)
  uninstall_package("ghostscript")

  # disable screen locking
  uninstall_package("light-locker")

  install_package("keepass2")

  # image viewer able to use trash
  uninstall_package("gpicview") # deletes without using trash
  # sudo apt-get -qq install gthumb -y
  install_package("eog")
  # install_package("geeqie") # animated gif support is missing, dead project

  # mail client
  install_package("claws-mail")

  # replaced by official PPA (https://www.clementine-player.org/downloads)
  # Clementine is dying - see
  # https://github.com/clementine-player/Clementine/graphs/contributors
  # ppa has no release for Ubuntu 18, EOL OS are listed
  # reported as https://github.com/clementine-player/Website/issues/9
  # find mp3 player with automatic scoring of songs based on what is played/skipped
  # see https://softwarerecs.stackexchange.com/q/70950/12551

  # The repository 'http://ppa.launchpad.net/me-davidsansome/clementine/ubuntu bionic Release' does not have a Release file.
  # issue: https://github.com/clementine-player/Clementine/issues/6051
  # add_apt_repository("ppa:me-davidsansome/clementine")
  install_package("clementine")

  add_apt_repository("ppa:maarten-baert/simplescreenrecorder")
  install_package("simplescreenrecorder")
end

main


[1]: https://gist.github.com/matkoniecz/4aeaeea001c154792e2ec3706ff9f8df
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.