Class: Execution

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, value) ⇒ Execution

Returns a new instance of Execution.



12
13
14
15
16
17
# File 'lib/execution.rb', line 12

def initialize title, value
  @commands   = []
  @title      = title
  @value      = value
  @always_run = false
end

Instance Attribute Details

#always_run(boolean) ⇒ Object

Returns the value of attribute always_run.



10
11
12
# File 'lib/execution.rb', line 10

def always_run
  @always_run
end

#userObject

Returns the value of attribute user.



10
11
12
# File 'lib/execution.rb', line 10

def user
  @user
end

Class Method Details

.block(title, value = nil, user = 'deploy', &block) ⇒ Object



40
41
42
43
44
# File 'lib/execution.rb', line 40

def self.block title, value = nil, user = 'deploy', &block
  @object         = new(title, value)
  @object.user    = user
  @object.execute &block
end

Instance Method Details



29
30
31
32
33
34
# File 'lib/execution.rb', line 29

def banner
  Output.jump
  Output.info @title, @value
  # Output.info 'Hash', serialize
  Output.ruler
end

#code_for(cmd) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/execution.rb', line 83

def code_for cmd
  if cmd[:path]
    "cd #{cmd[:path]} && sudo -u #{@user} #{cmd[:command]}"
  else
    "sudo -u #{@user} #{cmd[:command]}"
  end
end

#command(string, path = nil) ⇒ Object



95
96
97
# File 'lib/execution.rb', line 95

def command string, path = nil
  run string, path
end

#execute {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Execution)

    the object that the method was called on



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

def execute &block
  banner
  yield(self)
  run_commands
end

#ignoreObject



19
20
21
22
23
# File 'lib/execution.rb', line 19

def ignore
  Output.jump
  Output.error 'Ignoring execution', '..'
  Output.ruler
end

#run(string, path = nil) ⇒ Object



91
92
93
# File 'lib/execution.rb', line 91

def run string, path = nil
  @commands << { :command => string, :path => path }
end

#run_commandsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/execution.rb', line 52

def run_commands
  st = Status.get
  st.execution!.hashes = [] unless st.execution.respond_to?(:hashes)
  if st.execution.hashes.include?(serialize)
    Output.warn 'Ignoring', 'Already executed..'
  end
  if @always_run
    Output.warn 'Always run switch detected', 'Forcing execution!'
  end
  @commands.map { |cmd| code_for(cmd) }.each do |code|
    Output.command code
    unless @always_run
      next if st.execution.hashes.include? serialize
    end
    run_or_raise code
  end
  unless st.execution.hashes.include? serialize
    st.execution.hashes << serialize
  end
  nil
end

#run_or_raise(code) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/execution.rb', line 74

def run_or_raise code
  return if system code
  Output.jump
  Output.ruler :error
  Output.error 'EXECUTION FAILED', code
  Output.ruler :error
  raise 'Que pedo!'
end

#serializeObject



36
37
38
# File 'lib/execution.rb', line 36

def serialize
  MD5.hexdigest @title + (@commands * '')
end