Class: Prepper::Package

Inherits:
Object
  • Object
show all
Includes:
Tools::Apt, Tools::File, Tools::Rbenv, Tools::Text, Tools::Users, SSHKit::DSL
Defined in:
lib/prepper/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools::Rbenv

#install_rbenv, #install_ruby

Methods included from Tools::Text

#append_text, #has_text?

Methods included from Tools::File

#chown, #directory, #file, #has_directory?, #has_file?, #has_symlink?, #matches_content?, #render_template, #symlink

Methods included from Tools::Users

#add_user, #has_user?

Methods included from Tools::Apt

#apt_install, #apt_update, #has_apt_package?

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Package

Returns a new instance of Package.



12
13
14
15
16
17
18
19
# File 'lib/prepper/package.rb', line 12

def initialize(name, opts = {}, &block)
  @name = name
  @opts = opts
  @runner = opts[:runner]
  @verifications = []
  @commands = []
  instance_eval &block if block_given?
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#runnerObject

Returns the value of attribute runner.



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

def runner
  @runner
end

#verificationsObject

Returns the value of attribute verifications.



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

def verifications
  @verifications
end

Instance Method Details

#add_command(command, opts = {}) ⇒ Object



50
51
52
53
54
# File 'lib/prepper/package.rb', line 50

def add_command(command, opts = {})
  opts[:user] ||= "root"
  opts[:within] ||= "/"
  @commands << Command.new(command, opts)
end

#execute_command(command) ⇒ Object



56
57
58
# File 'lib/prepper/package.rb', line 56

def execute_command(command)
  run_command(:execute, command)
end

#processObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/prepper/package.rb', line 32

def process
  unless should_run?
    SSHKit.config.output.write(SSHKit::LogMessage.new(1, "Skipping package #{name}"))
    return
  end
  @commands.each do |command|
    if command.verifier
      if !test_command(command.verifier)
        execute_command(command)
      else
        SSHKit.config.output.write(SSHKit::LogMessage.new(1, "Skipping command #{command.to_s}"))
      end
    else
      execute_command(command)
    end
  end
end

#run_command(method, command) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/prepper/package.rb', line 64

def run_command(method, command)
  on [runner.server_hash], in: :sequence do |host|
    within command.within do
      as command.user  do
        with command.env do
          if respond_to? command.to_s.to_sym
            send command.to_s.to_sym, *command.opts[:params]
          else
            if method == :execute
              execute command.to_s
            else

            send(method, command.to_s)
            end
          end
        end
      end
    end
  end
end

#should_run?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/prepper/package.rb', line 21

def should_run?
  return true if @verifications.empty?
  return @verifications.all? do |verification|
    !test_command(verification.call)
  end
end

#test_command(command) ⇒ Object



60
61
62
# File 'lib/prepper/package.rb', line 60

def test_command(command)
  run_command(:test, command)
end

#verify(&block) ⇒ Object



28
29
30
# File 'lib/prepper/package.rb', line 28

def verify(&block)
  @verifications << block
end