Module: Kontena::Cli::Stacks::YAML::Validations

Included in:
ValidatorV3
Defined in:
lib/kontena/cli/stacks/yaml/validations.rb

Defined Under Namespace

Modules: CustomValidators

Instance Method Summary collapse

Instance Method Details

#common_validationsObject



23
24
25
26
27
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 23

def common_validations
 {
    'image' => optional('string'), # it's optional because some base yml file might contain image option
    'extends' => optional('stacks_valid_extends'),
    'stateful' => optional('boolean'),
    'affinity' => optional('stacks_valid_affinities'),
    'cap_add' => optional('array'),
    'cap_drop' => optional('array'),
    'command' => optional('string'),
    'cpus' => optional('float'),
    'cpu_shares' => optional('integer'),
    'external_links' => optional('array'),
    'mem_limit' => optional('string'),
    'mem_swaplimit' => optional('string'),
    'shm_size' => optional('string'),
    'environment' => optional(-> (value) {
      if value.is_a?(Hash)
        value.all? do |k,v|
          k.kind_of?(String) && (
            v.kind_of?(String) ||
            v.kind_of?(Integer) ||
            v.kind_of?(TrueClass) ||
            v.kind_of?(FalseClass) ||
            v.nil?
          )
        end
      elsif value.is_a?(Array)
        value.all? { |v| v.kind_of?(String) && v =~ /\A[^=]+=/ }
      else
        false
      end
    }),
    'env_file' => optional(-> (value) { value.is_a?(String) || value.is_a?(Array) }),
    'instances' => optional('integer'),
    'links' => optional(-> (value) { value.is_a?(Array) || value.nil? }),
    'ports' => optional(-> (value) { value.is_a?(Array) && value.all? { |v| v.is_a?(String) && v.match(/\A(\d+\.\d+\.\d+\.\d+)?:?(\d+)\:(\d+)\/?(\w+)?\z/) } }),
    'pid' => optional('string'),
    'privileged' => optional('boolean'),
    'user' => optional('string'),
    'volumes' => optional('array'),
    'volumes_from' => optional('array'),
    'secrets' => optional('stacks_valid_secrets'),
    'certificates' => optional('stacks_valid_certificates'),
    'hooks' => optional('stacks_valid_hooks'),
    'only_if' => optional(-> (value) { value.is_a?(String) || value.is_a?(Hash) || value.is_a?(Array) }),
    'skip_if' => optional(-> (value) { value.is_a?(String) || value.is_a?(Hash) || value.is_a?(Array) }),
    'deploy' => optional({
      'strategy' => optional(%w(ha daemon random)),
      'wait_for_port' => optional('integer'),
      'min_health' => optional('float'),
      'interval' => optional(/^\d+(min|h|d|)$/)
    }),
    'health_check' => optional({
      'protocol' => /^(http|tcp)$/,
      'port' => 'integer',
      'uri' => optional(/\/[\S]*/),
      'timeout' => optional('integer'),
      'interval' => optional('integer'),
      'initial_delay' => optional('integer')
    }),
    'stop_signal' => optional('string'),
    'stop_grace_period' => optional(/(\d+(?:\.\d+)?)([hms])/),
    'read_only' => optional('boolean'),
    'entrypoint' => optional('string')
  }
end

#dependency_schemaObject



112
113
114
115
116
117
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 112

def dependency_schema
  {
    'stack' => optional('string'),
    'variables' => optional(-> (value) { value.is_a?(Hash) })
  }
end

#optional(type) ⇒ Object



90
91
92
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 90

def optional(type)
  HashValidator.optional(type)
end

#validate_dependencies(dependency_config) ⇒ Object



102
103
104
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 102

def validate_dependencies(dependency_config)
  HashValidator.validate(dependency_config, dependency_schema, true)
end

#validate_options(service_config) ⇒ Object



94
95
96
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 94

def validate_options(service_config)
  HashValidator.validate(service_config, @schema, true)
end

#validate_volume_options(volume_config) ⇒ Object



98
99
100
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 98

def validate_volume_options(volume_config)
  HashValidator.validate(volume_config, volume_schema, true)
end

#volume_schemaObject



106
107
108
109
110
# File 'lib/kontena/cli/stacks/yaml/validations.rb', line 106

def volume_schema
  {
    'external' => optional(-> (value) { value.is_a?(TrueClass) || (value.is_a?(Hash) && value['name'].is_a?(String)) })
  }
end