Class: Consist::Step

Inherits:
Object
  • Object
show all
Includes:
SSHKit::DSL
Defined in:
lib/consist/step.rb

Instance Method Summary collapse

Constructor Details

#initialize(id:, &definition) ⇒ Step

Returns a new instance of Step.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/consist/step.rb', line 9

def initialize(id:, &definition)
  @commands = []
  @id = id
  @required_user = :root

  if definition
    instance_eval(&definition)
  else
    contents = Consist::Resolver.new(pwd: Dir.pwd).resolve_artifact(type: :step, id:)
    instance_eval(contents)
  end
end

Instance Method Details

#check(status:, path: nil, file: nil, message: "", &block) ⇒ Object



54
55
56
# File 'lib/consist/step.rb', line 54

def check(status:, path: nil, file: nil, message: "", &block)
  @commands << {type: :check, message:, status:, file:, path:, block: -> { instance_eval(&block) }}
end

#id(id = nil) ⇒ Object



22
23
24
25
# File 'lib/consist/step.rb', line 22

def id(id = nil)
  @id = id if id
  @id
end

#mutate_file(mode:, target_file:, match:, target_string:, delim: "/", message: "") ⇒ Object



46
47
48
# File 'lib/consist/step.rb', line 46

def mutate_file(mode:, target_file:, match:, target_string:, delim: "/", message: "")
  @commands << {type: :mutate, mode:, message:, match:, target_file:, delim:, target_string:}
end

#name(name = nil) ⇒ Object



27
28
29
30
# File 'lib/consist/step.rb', line 27

def name(name = nil)
  @name = name if name
  @name
end

#perform(executor) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/consist/step.rb', line 58

def perform(executor)
  @commands.each do |command|
    banner(command[:message]) unless command[:message].empty?

    execable = Object.const_get("Consist::Commands::#{command[:type].capitalize}").new(command)
    executor.as @required_user do
      execable.perform!(executor)
    end
  end
end

#required_user(user = nil) ⇒ Object



32
33
34
35
# File 'lib/consist/step.rb', line 32

def required_user(user = nil)
  @required_user = user if user
  @required_user
end

#shell(message = "", params: {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/consist/step.rb', line 37

def shell(message = "", params: {})
  return unless block_given?

  command = yield
  commands = command.split(/(?<!\\)\n/).select { !_1.start_with?("#") }.compact

  @commands << {message:, type: :exec, commands:, params:}
end

#upload_file(local_file:, remote_path:, message: "") ⇒ Object



50
51
52
# File 'lib/consist/step.rb', line 50

def upload_file(local_file:, remote_path:, message: "")
  @commands << {message:, type: :upload, local_file:, remote_path:}
end