Class: Pups::Config

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Config

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
# File 'lib/pups/config.rb', line 13

def initialize(config)
  @config = config
  validate!(@config)
  @params = @config["params"]
  @params ||= {}
  ENV.each do |k,v|
    @params["$ENV_#{k}"] = v
  end
  inject_hooks
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/pups/config.rb', line 3

def config
  @config
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/pups/config.rb', line 3

def params
  @params
end

Class Method Details

.interpolate_params(cmd, params) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/pups/config.rb', line 106

def self.interpolate_params(cmd, params)
  return unless cmd
  processed = cmd.dup
  params.each do |k,v|
    processed.gsub!("$#{k}", v.to_s)
  end
  processed
end

.load_config(config) ⇒ Object



9
10
11
# File 'lib/pups/config.rb', line 9

def self.load_config(config)
  new YAML.load(config)
end

.load_file(config_file) ⇒ Object



5
6
7
# File 'lib/pups/config.rb', line 5

def self.load_file(config_file)
  new YAML.load_file(config_file)
end

Instance Method Details

#inject_hooksObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pups/config.rb', line 28

def inject_hooks
  return unless hooks = @config["hooks"]

  run = @config["run"]

  positions = {}
  run.each do |row|
    if Hash === row
      command = row.first
      if Hash === command[1]
        hook = command[1]["hook"]
        positions[hook] = row if hook
      end
    end
  end

  hooks.each do |full, list|

    offset = nil
    name = nil

    if full =~ /^after_/
      name = full[6..-1]
      offset = 1
    end

    if full =~ /^before_/
      name = full[7..-1]
      offset = 0
    end

    index = run.index(positions[name])

    if index && index >= 0
      run.insert(index + offset, *list)
    else
      Pups.log.info "Skipped missing #{full} hook"
    end

  end
end

#interpolate_params(cmd) ⇒ Object



102
103
104
# File 'lib/pups/config.rb', line 102

def interpolate_params(cmd)
  self.class.interpolate_params(cmd,@params)
end

#runObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pups/config.rb', line 70

def run
  run_commands
rescue => e
  puts
  puts
  puts "FAILED"
  puts "-" * 20
  puts "#{e.class}: #{e}"
  puts "Location of failure: #{e.backtrace[0]}"
  if @last_command
    puts "#{@last_command[:command]} failed with the params #{@last_command[:params].inspect}"
  end
  exit 1
end

#run_commandsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pups/config.rb', line 85

def run_commands
  @config["run"].each do |item|
    item.each do |k,v|
      type = case k
                when "exec" then Pups::ExecCommand
                when "merge" then Pups::MergeCommand
                when "replace" then Pups::ReplaceCommand
                when "file" then Pups::FileCommand
                else raise SyntaxError.new("Invalid run command #{k}")
            end

      @last_command = { command: k, params: v }
      type.run(v, @params)
    end
  end
end

#validate!(conf) ⇒ Object



24
25
26
# File 'lib/pups/config.rb', line 24

def validate!(conf)
  # raise proper errors if nodes are missing etc
end