Skip to content

Commit

Permalink
YaST Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Feb 12, 2020
1 parent be73eb9 commit b55cee6
Showing 1 changed file with 94 additions and 82 deletions.
176 changes: 94 additions & 82 deletions bin/yupdate
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ require "socket"
require "tmpdir"
require "uri"

require "yast"

module YUpdate
# version of the script
class Version
Expand All @@ -46,9 +48,17 @@ module YUpdate
Commands:
patch <github_repo> <branch>
patch <tarball_url>
patch <host[:port]>
patch <github_repo> <branch> Patch the installer with the sources from GitHub,
<github_repo> is a repository name (including the organization
or the user name, if missing "yast" is used by default),
<branch> is the Git branch to use
patch <tarball_url> Patch the installer with the sources provided by
a generic HTTP server
patch <host[:port]> Patch the installer with the sources provided by
a "rake server" task (for details see
https://github.com/yast/yast-rake/#server)
servers <host[:port]> List the rake servers running on the remote machine
Expand All @@ -72,88 +82,89 @@ module YUpdate
WARNING: This script is intended only for testing and development purposes,
using this tool makes the installation not supported!
See more details in [TODO README URL]
See more details in
https://github.com/yast/yast-installation/blob/master/doc/yupdate.md
HELP
end
end

# A Ruby Logger which logs in usual YaST y2log format,
# additionally it prints the messages also to STDOUT
class YastLogger < Logger
# redefine the format to the y2log format
def format_message(severity, timestamp, progname, msg)
puts msg
# remove the function name from the caller location
location = caller(3).first.gsub(/:in `.*'$/, "")
"#{timestamp.strftime(datetime_format)} #{severity} #{Socket.gethostname}(#{Process.pid}) #{progname} #{location} #{msg}\n"
end

def initialize(*args)
super
self.datetime_format = "%Y-%m-%d %H:%M:%S"
self.progname = "[#{File.basename($PROGRAM_NAME)}]"
self.level = ENV["Y2DEBUG"] == "1" ? Logger::DEBUG : Logger::INFO
end

private

# redefine severity formatting
def format_severity(severity)
"<#{severity}>"
end
end

# Provides the global shared Y2logger instance writing to /var/log/YaST2/y2log
# (or ~/.y2log if the file is not writable).
#
# It can be used for logging external Ruby scripts into y2log
class Y2Logger
include Singleton

Y2LOGFILE = "/var/log/YaST2/y2log".freeze
# # A Ruby Logger which logs in usual YaST y2log format,
# # additionally it prints the messages also to STDOUT
# class YastLogger < Logger
# # redefine the format to the y2log format
# def format_message(severity, timestamp, progname, msg)
# puts msg
# # remove the function name from the caller location
# location = caller(3).first.gsub(/:in `.*'$/, "")
# "#{timestamp.strftime(datetime_format)} #{severity} #{Socket.gethostname}(#{Process.pid}) #{progname} #{location} #{msg}\n"
# end

attr_accessor :log
# def initialize(*args)
# super
# self.datetime_format = "%Y-%m-%d %H:%M:%S"
# self.progname = "[#{File.basename($PROGRAM_NAME)}]"
# self.level = ENV["Y2DEBUG"] == "1" ? Logger::DEBUG : Logger::INFO
# end

def initialize
# Yast compatibility - log to home if not running as root
# (or if the file is not writable)
log_file = if File.exist?(Y2LOGFILE)
# is the file writable?
File.writable?(Y2LOGFILE) ? Y2LOGFILE : "#{ENV["HOME"]}/.y2log"
else
# is the parent directory writable?
File.writable?(File.dirname(Y2LOGFILE)) ? Y2LOGFILE : "#{ENV["HOME"]}/.y2log"
end
# private

# when creating the log file make sure it is readable only by the user
# (it might contain sensitive data like passwords, registration code, etc.)
File.write(log_file, "", perm: 0o600) unless File.exist?(log_file)
# # redefine severity formatting
# def format_severity(severity)
# "<#{severity}>"
# end
# end

# # Provides the global shared Y2logger instance writing to /var/log/YaST2/y2log
# # (or ~/.y2log if the file is not writable).
# #
# # It can be used for logging external Ruby scripts into y2log
# class Y2Logger
# include Singleton

# Y2LOGFILE = "/var/log/YaST2/y2log".freeze

# attr_accessor :log

# def initialize
# # Yast compatibility - log to home if not running as root
# # (or if the file is not writable)
# log_file = if File.exist?(Y2LOGFILE)
# # is the file writable?
# File.writable?(Y2LOGFILE) ? Y2LOGFILE : "#{ENV["HOME"]}/.y2log"
# else
# # is the parent directory writable?
# File.writable?(File.dirname(Y2LOGFILE)) ? Y2LOGFILE : "#{ENV["HOME"]}/.y2log"
# end

@log = YastLogger.new(log_file)
end
end
# # when creating the log file make sure it is readable only by the user
# # (it might contain sensitive data like passwords, registration code, etc.)
# File.write(log_file, "", perm: 0o600) unless File.exist?(log_file)

# This module provides access to Yast specific logging
#
# @example Use YastLogger in an easy way
# class Foo
# include YLogger
#
# def foo
# # this will be logged into y2log using the usual y2log format
# log.debug "debug"
# log.error "error"
# end
# @log = YastLogger.new(log_file)
# end
# end

# # This module provides access to Yast specific logging
# #
# # @example Use YastLogger in an easy way
# # class Foo
# # include Yast::Logger
# #
# # def foo
# # # this will be logged into y2log using the usual y2log format
# # log.debug "debug"
# # log.error "error"
# # end
# # end
# module YLogger
# def log
# Y2Logger.instance.log
# end
module YLogger
def log
Y2Logger.instance.log
end

def self.included(base)
base.extend self
end
end
# def self.included(base)
# base.extend self
# end
# end

# a simple /etc/install.inf parser/writer
class InstallInf
Expand Down Expand Up @@ -202,7 +213,7 @@ module YUpdate

# Class for managing the OverlayFS mounts
class OverlayFS
include YLogger
include Yast::Logger

OVERLAY_PREFIX = "/var/lib/YaST2/overlayfs".freeze

Expand All @@ -216,9 +227,10 @@ module YUpdate
attr_reader :dir

# manage the OverlayFS for this directory
def initialize(dir)
def initialize(directory)
# expand symlinks
@dir = File.realpath(dir)
@dir = File.realpath(directory)
raise "Path is not a directory: #{dir}" unless File.dir?(dir)
end

# create an OverlayFS overlay for this directory if it is not writable
Expand All @@ -240,7 +252,7 @@ module YUpdate
"upperdir=#{upperdir.shellescape},workdir=#{workdir.shellescape} #{dir.shellescape}")
end

# delete the OverlayFS for this directory, all changes will be lost
# delete the OverlayFS for this directory, all changes will be reverted back
def delete
log.debug "umount #{dir}"
system "umount #{dir.shellescape}"
Expand Down Expand Up @@ -323,7 +335,7 @@ module YUpdate

# a generic HTTP downloader
class Downloader
include YLogger
include Yast::Logger
attr_reader :url

def initialize(url)
Expand Down Expand Up @@ -441,7 +453,7 @@ module YUpdate

# install the YaST sources using the "rake install" call
class Installer
include YLogger
include Yast::Logger

# globs for ignored some files
SKIP_FILES = [
Expand Down Expand Up @@ -635,7 +647,7 @@ module YUpdate

# handle the "patch" command line option
class PatchCommand
include YLogger
include Yast::Logger

def initialize(argv)
@argv = argv
Expand Down Expand Up @@ -725,7 +737,7 @@ module YUpdate

# handle invalid command line options
class InvalidCommand
include YLogger
include Yast::Logger

def initialize(cmd)
@cmd = cmd
Expand Down

0 comments on commit b55cee6

Please sign in to comment.