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,

  # 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


25
26
27
28
# File 'lib/config_curator/unit.rb', line 25

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.


61
62
63
# File 'lib/config_curator/unit.rb', line 61

def hosts
  @hosts
end

#loggerLogger

Logger instance to use.


40
41
42
# File 'lib/config_curator/unit.rb', line 40

def logger
  @logger
end

#packagesArray

Unit installed only if listed packages are installed.


67
68
69
# File 'lib/config_curator/unit.rb', line 67

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.


92
93
94
95
# File 'lib/config_curator/unit.rb', line 92

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

#destination_pathString

Full path to destination.


54
55
56
# File 'lib/config_curator/unit.rb', line 54

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

#installObject

Installs the unit.


77
78
79
80
# File 'lib/config_curator/unit.rb', line 77

def install
  return false unless install?
  true
end

#install?Boolean

Checks if the unit should be installed.


84
85
86
87
88
# File 'lib/config_curator/unit.rb', line 84

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

#options(options = {}) ⇒ Hash

Uses DEFAULT_OPTIONS as initial value.


33
34
35
36
# File 'lib/config_curator/unit.rb', line 33

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

#package_lookupObject

A PackageLookup object for this unit.


72
73
74
# File 'lib/config_curator/unit.rb', line 72

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.


99
100
101
# File 'lib/config_curator/unit.rb', line 99

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

#source_pathString

Full path to source.


48
49
50
# File 'lib/config_curator/unit.rb', line 48

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