Class: Inprovise::ScriptRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/inprovise/script_runner.rb

Overview

Script runner for Inprovise

Author

Martin Corino

License

Distributes under the same license as Ruby

Defined Under Namespace

Classes: ValidationFailureError

Constant Summary collapse

COMMANDS =
{apply: %w{Applying to}, revert: %w(Reverting on), validate: %w(Validating on)}

Instance Method Summary collapse

Constructor Details

#initialize(node, script, skip_dependencies = false) ⇒ ScriptRunner

Returns a new instance of ScriptRunner.



9
10
11
12
13
14
15
16
# File 'lib/inprovise/script_runner.rb', line 9

def initialize(node, script, skip_dependencies=false)
  @node = node
  @script = script
  @index = Inprovise::ScriptIndex.default
  @perform = true
  @skip_dependencies = skip_dependencies
  @log = Inprovise::Logger.new(@node, script)
end

Instance Method Details

#demonstrate(command_name, config = nil) ⇒ Object



46
47
48
49
50
# File 'lib/inprovise/script_runner.rb', line 46

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

#exec(script, command_name, context) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/inprovise/script_runner.rb', line 91

def exec(script, command_name, context)
  cmds = script.command(command_name)
  context = context.for_user(script.user) if script.user
  context.log.set_task(script)
  context.log.command(command_name)
  context.script = script
  cmds.map {|cmd| context.exec(cmd) }
end

#execute(command_name, config = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/inprovise/script_runner.rb', line 33

def execute(command_name, config=nil)
  Inprovise.log.local("#{COMMANDS[command_name].first} #{script.name} #{COMMANDS[command_name].last} #{@node.to_s}")
  scrs = scripts
  scrs.reverse! if command_name.to_sym == :revert
  @log.say scrs.map(&:name).join(', ').yellow if Inprovise.verbosity > 0
  context = @perform ? Inprovise::ExecutionContext.new(@node, @log, @index, config) : Inprovise::MockExecutionContext.new(@node, @log, @index, config)
  context.config.command = command_name
  scrs.each { |script| script.merge_configuration(context.config) }
  scrs.each do |script|
    send(:"execute_#{command_name}", script, context)
  end
end

#execute_apply(script, context) ⇒ Object



52
53
54
55
56
# File 'lib/inprovise/script_runner.rb', line 52

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

#execute_revert(script, context) ⇒ Object



58
59
60
61
# File 'lib/inprovise/script_runner.rb', line 58

def execute_revert(script, context)
  return unless should_run?(script, :revert, context)
  exec(script, :revert, context)
end

#execute_validate(script, context) ⇒ Object



63
64
65
# File 'lib/inprovise/script_runner.rb', line 63

def execute_validate(script, context)
  validate!(script, context)
end

#is_valid?(script, context) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'lib/inprovise/script_runner.rb', line 84

def is_valid?(script, context)
  results = exec(script, :validate, context)
  rc = results.all?
  context.log.command("validate -> #{rc}") if Inprovise.verbosity > 0
  rc
end

#scriptObject



22
23
24
# File 'lib/inprovise/script_runner.rb', line 22

def script
  Inprovise::Script === @script ? @script : @index.get(@script)
end

#scriptsObject



26
27
28
29
30
31
# File 'lib/inprovise/script_runner.rb', line 26

def scripts
  return [script] if @skip_dependencies
  resolver = Inprovise::Resolver.new(script, @index)
  resolver.resolve
  resolver.scripts
end

#set_index(index) ⇒ Object



18
19
20
# File 'lib/inprovise/script_runner.rb', line 18

def set_index(index)
  @index = index
end

#should_run?(script, command_name, context) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/inprovise/script_runner.rb', line 67

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

#validate!(script, context) ⇒ Object



77
78
79
80
81
82
# File 'lib/inprovise/script_runner.rb', line 77

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