class ConfigCurator::ConfigFile

A config file is a file that should be copied.

Attributes

fmode[RW]
group[RW]
owner[RW]

Public Instance Methods

destination() click to toggle source

(see Unit#destination) @note Use {Unit#source} by default.

Calls superclass method
# File lib/config_curator/units/config_file.rb, line 21
def destination
  super
  @destination ||= @source
end
install() click to toggle source

(see Unit#install)

Calls superclass method ConfigCurator::Unit#install
# File lib/config_curator/units/config_file.rb, line 35
def install
  s = super
  return s unless s
  install_file
  set_mode
  set_owner
  true
end
install?() click to toggle source

(see Unit#install?)

Calls superclass method ConfigCurator::Unit#install?
# File lib/config_curator/units/config_file.rb, line 45
def install?
  s = super
  return s unless s
  fail InstallFailed, 'No file source path specified.' if source_path.nil?
  fail InstallFailed, "Source path does not exist: #{source}" unless File.exist? source_path
  true
end
source() click to toggle source

Will use files of the form `filename.hostname.ext` if found. (see Unit#source)

Calls superclass method
# File lib/config_curator/units/config_file.rb, line 8
def source
  path = super
  host_specific_file = search_for_host_specific_file path if path

  if host_specific_file
    return host_specific_file
  else
    return path
  end
end
uninstall(*args) click to toggle source

(see Unit#uninstall)

Calls superclass method ConfigCurator::Unit#uninstall
# File lib/config_curator/units/config_file.rb, line 27
def uninstall(*args)
  s = super(*args)
  return s unless s
  uninstall_file
  true
end

Private Instance Methods

install_file() click to toggle source

Recursively creates the necessary directories and install the file.

# File lib/config_curator/units/config_file.rb, line 61
def install_file
  FileUtils.mkdir_p File.dirname(destination_path)
  FileUtils.copy source_path, destination_path, preserve: true
end
search_for_host_specific_file(path) click to toggle source

Will look for files with the naming pattern `filename.hostname.ext`. @param path [String] path to the non-host-specific file rubocop:disable Metrics/MethodLength

# File lib/config_curator/units/config_file.rb, line 81
def search_for_host_specific_file(path)
  directory = File.dirname path
  filename = File.basename path
  extension = File.extname path
  basename = filename.chomp(extension)
  if Dir.exist? directory
    files = Dir.entries(directory)

    file = files.grep(/^#{filename}\.#{hostname.downcase}$/).first
    return File.join directory, file unless file.nil?

    extension.gsub!(/^\./, '\.')
    regex = /^#{basename}\.#{hostname.downcase}#{extension}$/
    file = files.grep(regex).first
    return File.join directory, file unless file.nil?
  end
  nil
end
set_mode() click to toggle source

Sets file mode.

# File lib/config_curator/units/config_file.rb, line 67
def set_mode
  FileUtils.chmod fmode, destination_path unless fmode.nil?
end
set_owner() click to toggle source

Sets file owner and group.

# File lib/config_curator/units/config_file.rb, line 72
def set_owner
  FileUtils.chown owner, group, destination_path
end
uninstall_file() click to toggle source

Uninstalls the file by removing it.

# File lib/config_curator/units/config_file.rb, line 56
def uninstall_file
  FileUtils.remove_entry_secure destination_path if File.exist? destination_path
end