Class: Orca::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/orca/runner.rb

Defined Under Namespace

Classes: ValidationFailureError

Instance Method Summary collapse

Constructor Details

#initialize(node, package, skip_dependancies = false) ⇒ Runner

Returns a new instance of Runner.



2
3
4
5
6
7
8
# File 'lib/orca/runner.rb', line 2

def initialize(node, package, skip_dependancies=false)
  @node = node
  @package = package
  @perform = true
  @skip_dependancies = skip_dependancies
  @log = Orca::Logger.new(@node, package)
end

Instance Method Details

#demonstrate(command_name) ⇒ Object



63
64
65
66
67
# File 'lib/orca/runner.rb', line 63

def demonstrate(command_name)
  @perform = false
  execute(command_name)
  @perform = true
end

#exec(pkg, command_name) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/orca/runner.rb', line 69

def exec(pkg, command_name)
  context = @perform ? Orca::ExecutionContext.new(@node, @log) : Orca::MockExecutionContext.new(@node, @log)
  cmds = pkg.command(command_name)
  context = context.for_user(pkg.user) if pkg.user
  context.log.set_package(pkg)
  context.log.command(command_name)
  cmds.map {|cmd| context.apply(cmd) }
end

#execute(command_name) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/orca/runner.rb', line 17

def execute(command_name)
  pkgs = packages
  pkgs.reverse! if command_name.to_sym == :remove
  @log.say pkgs.map(&:name).join(', ').yellow
  pkgs.each do |pkg|
    send(:"execute_#{command_name}", pkg)
  end
end

#execute_apply(pkg) ⇒ Object



26
27
28
29
30
# File 'lib/orca/runner.rb', line 26

def execute_apply(pkg)
  return unless should_run?(pkg, :apply)
  exec(pkg, :apply)
  validate!(pkg)
end

#execute_remove(pkg) ⇒ Object



32
33
34
35
# File 'lib/orca/runner.rb', line 32

def execute_remove(pkg)
  return unless should_run?(pkg, :remove)
  exec(pkg, :remove)
end

#execute_validate(pkg) ⇒ Object



37
38
39
# File 'lib/orca/runner.rb', line 37

def execute_validate(pkg)
  validate!(pkg)
end

#is_valid?(pkg) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/orca/runner.rb', line 58

def is_valid?(pkg)
  results = exec(pkg, :validate)
  results.all?
end

#packagesObject



10
11
12
13
14
15
# File 'lib/orca/runner.rb', line 10

def packages
  return [@package] if @skip_dependancies
  resolver = Orca::Resolver.new(@package)
  resolver.resolve
  resolver.packages
end

#should_run?(pkg, command_name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/orca/runner.rb', line 41

def should_run?(pkg, command_name)
  return false unless pkg.provides_command?(command_name)
  return true unless @perform
  return true unless command_name == :apply || command_name == :remove
  return true unless pkg.provides_command?(:validate)
  is_present = is_valid?(pkg)
  return !is_present if command_name == :apply
  return is_present
end

#validate!(pkg) ⇒ Object



51
52
53
54
55
56
# File 'lib/orca/runner.rb', line 51

def validate!(pkg)
  return true unless @perform
  return unless pkg.provides_command?(:validate)
  return if is_valid?(pkg)
  raise ValidationFailureError.new(@node, pkg)
end