Top Level Namespace

Defined Under Namespace

Modules: Deplomat

Instance Method Summary collapse

Instance Method Details

#add_task(*tasks) ⇒ Object Also known as: add_tasks

This wrapper allows us to insert external before/after tasks into every method executed by deplomat. Very useful for the one-time-tasks implementation.



37
38
39
# File 'lib/deplomat/directives.rb', line 37

def add_task(*tasks)
  $deplomat_tasks += tasks
end

#after_task(task_name, requisite_number, &block) ⇒ Object



49
50
51
52
53
54
# File 'lib/deplomat/directives.rb', line 49

def after_task(task_name, requisite_number, &block)
  $deplomat_tasks_callbacks[task_name] ||= {}
  $deplomat_tasks_callbacks[task_name][:after] ||= []
  ($deplomat_tasks_callbacks[task_name][:after] << lambda { print_task_status(task_name, "finished") }) if $log_tasks_status
  $deplomat_tasks_callbacks[task_name][:after]  << lambda { block.call; update_requisite_number!(requisite_number) }
end

#before_task(task_name, requisite_number, &block) ⇒ Object



42
43
44
45
46
47
# File 'lib/deplomat/directives.rb', line 42

def before_task(task_name, requisite_number, &block)
  $deplomat_tasks_callbacks[task_name] ||= {}
  $deplomat_tasks_callbacks[task_name][:before] ||= []
  ($deplomat_tasks_callbacks[task_name][:before] << lambda { print_task_status(task_name, "started") }) if $log_tasks_status
  $deplomat_tasks_callbacks[task_name][:before]  << lambda { block.call; update_requisite_number!(requisite_number) }
end

#execute_in(env:) ⇒ Object



6
7
8
# File 'lib/deplomat/directives.rb', line 6

def execute_in(env:)
  yield if $env == env
end

#execute_partial(name, args = {}) ⇒ Object



10
11
12
13
14
# File 'lib/deplomat/directives.rb', line 10

def execute_partial(name, args={})
  print_to_terminal $partials[name][:before_message]
  $partials[name][:block].call(args)
  print_to_terminal $partials[name][:after_message]
end

#execute_tasks!Object



56
57
58
59
60
61
62
# File 'lib/deplomat/directives.rb', line 56

def execute_tasks!
  $deplomat_tasks.each do |t|
    $deplomat_tasks_callbacks[t][:before].each { |block| block.call } if $deplomat_tasks_callbacks[t] && $deplomat_tasks_callbacks[t][:before]
    self.send(t)
    $deplomat_tasks_callbacks[t][:after].each { |block| block.call } if $deplomat_tasks_callbacks[t] && $deplomat_tasks_callbacks[t][:after]
  end
end

#load_requisites!(counter, requisites_path: "#{Dir.pwd}/deployment_requisites") ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/deplomat/directives.rb', line 70

def load_requisites!(counter, requisites_path: "#{Dir.pwd}/deployment_requisites")

  log = []
  log_requisites_count = "Checking #{requisites_path} dir for requisites..."
  files = Dir["#{requisites_path}/*.rb"].map do |fn|
    number = fn.split("/").last.match(/\A\d+_/).to_a.first&.chomp("_")
    number ? [number.to_i, fn] : nil
  end

  if files
    files = files.compact               # ignore files that don't start with a number
    .sort { |x,y| x.first <=> y.first } # Sort files according to the number in front of their names
    .map { |fn| fn.last }               # lose the folded Array we used to store the consecutive requisite number
    .slice(counter..-1)               # lose files whose consecutive requisite number is below or equals the counter
  end

  if files && files.size > 0
    log_requisites_count += "found #{files.size} new requisites."
    files.each do |fn|
      log << "Loading requisite: #{fn.split("/").last }"
      require fn
    end
  else
    log_requisites_count += "no files found"
  end
  log.unshift log_requisites_count

  return { counter: (files ? (counter + files.size) : 0), log: log }

end

#partial(name, message: [], &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/deplomat/directives.rb', line 16

def partial(name, message: [], &block)
  message         = [message] if message.kind_of?(String)
  before_message  = message[0]
  after_message   = message[1]
  $partials[name] = { block: block, before_message: before_message, after_message: after_message }
end


30
31
32
# File 'lib/deplomat/directives.rb', line 30

def print_task_status(name, status)
  print_to_terminal("\n--- #{name} task #{status.upcase}", color: :light_blue, newline: true)
end


23
24
25
26
27
28
# File 'lib/deplomat/directives.rb', line 23

def print_to_terminal(message, color: nil, newline: true)
  return unless message
  message += "\n" if newline
  message = message.send(color) if color
  $stdout.print message
end

#update_requisite_number!(n) ⇒ Object

This should be redefined in the deployment script.



65
66
67
68
# File 'lib/deplomat/directives.rb', line 65

def update_requisite_number!(n)
  # call Node#update_requisite_number! here
  # or do something else, more complicated.
end