Class: Wright::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/wright/provider.rb,
lib/wright/provider/user.rb,
lib/wright/provider/file.rb,
lib/wright/provider/group.rb,
lib/wright/provider/symlink.rb,
lib/wright/provider/package.rb,
lib/wright/provider/directory.rb,
lib/wright/provider/package/yum.rb,
lib/wright/provider/package/apt.rb,
lib/wright/provider/user/gnu_passwd.rb,
lib/wright/provider/group/gnu_passwd.rb,
lib/wright/provider/package/homebrew.rb,
lib/wright/provider/user/darwin_directory_service.rb,
lib/wright/provider/group/darwin_directory_service.rb

Overview

Provider class.

Direct Known Subclasses

Directory, File, Group, Package, Symlink, User

Defined Under Namespace

Classes: Directory, File, Group, Package, Symlink, User

Constant Summary collapse

PROVIDER_DIR =

Wright standard provider directory

File.expand_path('provider', File.dirname(__FILE__))

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Provider

Initializes a Provider.

Parameters:

  • resource (Resource)

    the resource used by the provider


18
19
20
21
# File 'lib/wright/provider.rb', line 18

def initialize(resource)
  @resource = resource
  @updated = false
end

Instance Method Details

#envObject (private)


85
86
87
# File 'lib/wright/provider.rb', line 85

def env
  {}
end

#exec_or_fail(command, args, error_message) ⇒ String (private)

Runs a command or fails with an error message.

Parameters:

  • command (String)

    the command to run

  • args (Array<String>)

    the arguments that are passed to the command

  • error_message (String)

    the error message to display in case of an error

Returns:

  • (String)

    the stdout output of the command

Raises:

  • (RuntimeError)

    if the command did not exit successfully


76
77
78
79
80
81
82
83
# File 'lib/wright/provider.rb', line 76

def exec_or_fail(command, args, error_message)
  stdout, stderr, status = Open3.capture3(env, command, *args)
  return stdout if status.success?

  error = stderr.chomp
  error = stdout.chomp if error.empty?
  fail %(#{error_message}: "#{error}")
end

#unless_dry_run(message) ⇒ Object (private)

Logs an info message and runs a code block unless dry run mode is active.

Parameters:

  • message (String)

    the message that is passed to the logger


41
42
43
44
45
46
47
48
# File 'lib/wright/provider.rb', line 41

def unless_dry_run(message)
  if Wright.dry_run?
    Wright.log.info "(would) #{message}"
  else
    Wright.log.info message
    yield
  end
end

#unless_uptodate(action, message) ⇒ Object (private)

Checks if the provider is up-to-date, runs a code block and sets `@updated` to `true` if it is not.

Parameters:

  • action (String)

    the target action

  • message (String)

    a log message that is displayed if the provider is up-to-date


57
58
59
60
61
62
63
64
# File 'lib/wright/provider.rb', line 57

def unless_uptodate(action, message)
  if uptodate?(action)
    Wright.log.debug message
  else
    yield
    @updated = true
  end
end

#updated?Bool

Checks if the provider was updated since the last call to #updated?

Returns:

  • (Bool)

    true if the provider was updated and false otherwise


28
29
30
31
32
# File 'lib/wright/provider.rb', line 28

def updated?
  updated = @updated
  @updated = false
  updated
end