Class: Kontena::Cli::Stacks::YAML::Validations::CustomValidators::HooksValidator

Inherits:
HashValidator::Validator::Base
  • Object
show all
Defined in:
lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb

Instance Method Summary collapse

Constructor Details

#initializeHooksValidator

Returns a new instance of HooksValidator.



3
4
5
# File 'lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb', line 3

def initialize
  super('stacks_valid_hooks')
end

Instance Method Details

#validate(key, value, validations, errors) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb', line 7

def validate(key, value, validations, errors)
  unless value.is_a?(Hash)
    errors[key] = "must be a mapping, not #{value.class}"
    return
  end

  value.keys.each do |hook|
    unless %w(pre_build pre_start post_start pre_stop).include?(hook)
      errors[key] = "invalid hook #{hook}"
    end
  end

  if value['pre_build']
    validate_pre_build_hooks(key, value['pre_build'], errors)
  end

  if value['pre_start']
    validate_pre_start_hooks(key, value['pre_start'], errors)
  end

  if value['post_start']
    validate_post_start_hooks(key, value['post_start'], errors)
  end

  if value['pre_stop']
    validate_pre_stop_hooks(key, value['pre_stop'], errors)
  end
end

#validate_post_start_hooks(key, post_start_hooks, errors) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb', line 68

def validate_post_start_hooks(key, post_start_hooks, errors)
  unless post_start_hooks.is_a?(Array)
    errors[key] = { 'post_start' => 'must be an array' }
    return
  end
  post_start_validation = {
    'name' => 'string',
    'instances' => (-> (value) { value.is_a?(Integer) || value == '*' }),
    'cmd' => 'string',
    'oneshot' => HashValidator.optional('boolean')
  }
  validator = HashValidator.validator_for(post_start_validation)
  post_start_hooks.each do |post_start|
    validator.validate('hooks.post_start', post_start, post_start_validation, errors)
  end
end

#validate_pre_build_hooks(key, pre_build_hooks, errors) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb', line 36

def validate_pre_build_hooks(key, pre_build_hooks, errors)
  unless pre_build_hooks.is_a?(Array)
    errors[key] = { 'pre_build' => "must be an array" }
    return
  end
  pre_build_validation = {
    'name' => 'string',
    'cmd' => 'string'
  }
  validator = HashValidator.validator_for(pre_build_validation)
  pre_build_hooks.each do |pre_build|
    validator.validate('hooks.pre_build', pre_build, pre_build_validation, errors)
  end
end

#validate_pre_start_hooks(key, pre_start_hooks, errors) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb', line 51

def validate_pre_start_hooks(key, pre_start_hooks, errors)
  unless pre_start_hooks.is_a?(Array)
    errors[key] = { 'pre_start' => 'must be an array' }
    return
  end
  pre_start_validation = {
    'name' => 'string',
    'instances' => (-> (value) { value.is_a?(Integer) || value == '*' }),
    'cmd' => 'string',
    'oneshot' => HashValidator.optional('boolean')
  }
  validator = HashValidator.validator_for(pre_start_validation)
  pre_start_hooks.each do |pre_start|
    validator.validate('hooks.pre_start', pre_start, pre_start_validation, errors)
  end
end

#validate_pre_stop_hooks(key, pre_stop_hooks, errors) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kontena/cli/stacks/yaml/custom_validators/hooks_validator.rb', line 85

def validate_pre_stop_hooks(key, pre_stop_hooks, errors)
  unless pre_stop_hooks.is_a?(Array)
    errors[key] = { 'pre_stop' => 'must be an array' }
    return
  end
  pre_stop_validation = {
    'name' => 'string',
    'instances' => (-> (value) { value.is_a?(Integer) || value == '*' }),
    'cmd' => 'string',
    'oneshot' => HashValidator.optional('boolean')
  }
  validator = HashValidator.validator_for(pre_stop_validation)
  pre_stop_hooks.each do |pre_stop|
    validator.validate('hooks.pre_stop', pre_stop, pre_stop_validation, errors)
  end
end