Class: ConfigCurator::Unit

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/config_curator/unit.rb

Overview

A unit is the base class for a type of configuration that should be installed. All units must specify a #source and a #destination.

Direct Known Subclasses

Component, ConfigFile, Symlink

Defined Under Namespace

Classes: InstallFailed

Constant Summary collapse

DEFAULT_OPTIONS =

Default #options.

{
  # Unit installed relative to this path.
  root: Dir.home,

  # Automatically uninstall units that would not be installed.
  uninstall: false,

  # Package tool to use. See #package_lookup.
  package_tool: nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}, logger: nil) ⇒ Unit

Returns a new instance of Unit.



28
29
30
31
# File 'lib/config_curator/unit.rb', line 28

def initialize(options: {}, logger: nil)
  self.options options
  self.logger = logger unless logger.nil?
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



14
15
16
# File 'lib/config_curator/unit.rb', line 14

def destination
  @destination
end

#hostsArray

Unit will be installed on these hosts. If empty, installed on any host.

Returns:

  • (Array)

    list of hostnames



64
65
66
# File 'lib/config_curator/unit.rb', line 64

def hosts
  @hosts
end

#loggerLogger

Logger instance to use.

Returns:

  • (Logger)

    logger instance



43
44
45
# File 'lib/config_curator/unit.rb', line 43

def logger
  @logger
end

#packagesArray

Unit installed only if listed packages are installed.

Returns:

  • (Array)

    list of package names



70
71
72
# File 'lib/config_curator/unit.rb', line 70

def packages
  @packages
end

#sourceObject

Returns the value of attribute source.



14
15
16
# File 'lib/config_curator/unit.rb', line 14

def source
  @source
end

Instance Method Details

#allowed_host?Boolean

Checks if the unit should be installed on this host.

Returns:

  • (Boolean)

    if the hostname is in #hosts



110
111
112
113
# File 'lib/config_curator/unit.rb', line 110

def allowed_host?
  return true if hosts.empty?
  hosts.include? hostname
end

#destination_pathString

Full path to destination.

Returns:

  • (String)

    expanded path to destination



57
58
59
# File 'lib/config_curator/unit.rb', line 57

def destination_path
  File.expand_path File.join(options[:root], destination) unless destination.nil?
end

#installBoolean

Installs the unit.

Returns:

  • (Boolean)

    if the unit was installed



95
96
97
98
# File 'lib/config_curator/unit.rb', line 95

def install
  return false unless install?
  true
end

#install?Boolean

Checks if the unit should be installed.

Returns:

  • (Boolean)

    if the unit should be installed



102
103
104
105
106
# File 'lib/config_curator/unit.rb', line 102

def install?
  return false unless allowed_host?
  return false unless packages_installed?
  true
end

#options(options = {}) ⇒ Hash

Uses DEFAULT_OPTIONS as initial value.

Parameters:

  • options (Hash) (defaults to: {})

    merged with current options

Returns:

  • (Hash)

    current options



36
37
38
39
# File 'lib/config_curator/unit.rb', line 36

def options(options = {})
  @options ||= DEFAULT_OPTIONS
  @options = @options.merge options
end

#package_lookupObject

A PackageLookup object for this unit.



75
76
77
# File 'lib/config_curator/unit.rb', line 75

def package_lookup
  @package_lookup ||= PackageLookup.new tool: options[:package_tool]
end

#packages_installed?Boolean

Checks if the packages required for this unit are installed.

Returns:

  • (Boolean)

    if the packages in #packages are installed



117
118
119
# File 'lib/config_curator/unit.rb', line 117

def packages_installed?
  packages.map(&method(:pkg_exists?)).delete_if { |e| e }.empty?
end

#source_pathString

Full path to source.

Returns:

  • (String)

    expanded path to source



51
52
53
# File 'lib/config_curator/unit.rb', line 51

def source_path
  File.expand_path source unless source.nil?
end

#uninstall(force: false) ⇒ Boolean

Uninstalls the unit.

Returns:

  • (Boolean)

    if the unit was uninstalled



81
82
83
84
# File 'lib/config_curator/unit.rb', line 81

def uninstall(force: false)
  return true if uninstall? || force
  false
end

#uninstall?Boolean

Checks if the unit should be uninstalled.

Returns:

  • (Boolean)

    if the unit should be uninstalled



88
89
90
91
# File 'lib/config_curator/unit.rb', line 88

def uninstall?
  return true if !install? && options[:uninstall]
  false
end