Class: Uberinstaller::PackageManager::Base

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/uberinstaller/package_managers/base.rb

Direct Known Subclasses

Apt, Dpkg, Git

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

configure_logger_for, #logger, logger_for

Constructor Details

#initializeBase

Create the package manager



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/uberinstaller/package_managers/base.rb', line 16

def initialize
  @commands = Hash.new
  @commands = {
    :add_repository => nil,
    :info => nil,
    :install => nil,
    :search => nil,
    :update => nil,
    :upgrade => nil
  }

  set_commands
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

All execution is handled dinamically via this function

If the method called is in the @commands Hash, the specified action is performed, otherwise a NoMethodError exception

Raises:

  • (NoMethodError)

    if the method specified is not available



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uberinstaller/package_managers/base.rb', line 64

def method_missing(m, *args, &block)
  if @commands.has_key? m
    debug m, args

    unless Config.dry_run
      Open3.popen3(make_command(m, args)) { |stdin, stdout, stderr, wait_thr|
        pid = wait_thr.pid # pid of the started process.
        logger.debug "Running pid: #{pid}"

        logger.debug stdout.readlines

        exit_status = wait_thr.value.to_i # Process::Status object returned.
        logger.debug "Exit status: #{exit_status}"
        logger.error 'Some error happended during execution:' unless exit_status == 0
        logger.error stderr.readlines unless exit_status == 0
      }
    end
  else
    raise NoMethodError
  end
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



13
14
15
# File 'lib/uberinstaller/package_managers/base.rb', line 13

def commands
  @commands
end

Instance Method Details

#debug(action, args = []) ⇒ Object

Print to log for debug purposes

Parameters:

  • action (String)

    the action that is being performed

  • args (Array) (defaults to: [])

    an array of arguments for the specified action



41
42
43
44
45
# File 'lib/uberinstaller/package_managers/base.rb', line 41

def debug(action, args = [])
  logger.debug "action : #{action}"
  logger.debug "args   : #{args.join(', ')}" unless args.empty?
  logger.debug "command: #{make_command(action, args)}"
end

#make_command(action, args = []) ⇒ Object

Creates a command putting together action and arguments

Parameters:

  • action (String)

    the action that is being performed

  • args (Array) (defaults to: [])

    an array of arguments for the specified action



51
52
53
54
55
56
# File 'lib/uberinstaller/package_managers/base.rb', line 51

def make_command(action, args = [])
  command = @commands[action.to_sym]
  command += " '" + args.join(' ') + "'" unless args.empty?

  command
end

#set_commandsObject

This method is a stub, here only for reference

In every subclass of PackageManager::Base this method must be redefined specifying the package manager specific command ( see Apt and Dpkg for example )



35
# File 'lib/uberinstaller/package_managers/base.rb', line 35

def set_commands; end