Skip to content

Instantly share code, notes, and snippets.

@maxlinc
Created June 28, 2013 14:38
Show Gist options
  • Save maxlinc/5885178 to your computer and use it in GitHub Desktop.
Save maxlinc/5885178 to your computer and use it in GitHub Desktop.
Kwalify (environment)
type: map
mapping:
redis_password:
type: str
required: yes
# Uncomment the next line to require redis passwords to be encrypted
# pattern: /ENC[.*]/
toggles:
type: map
required: yes
mapping:
active_groups:
desc: Toggles groups that should be active in the environment
type: seq
required: yes
sequence:
- type: str
overrides:
type: map
mapping:
=:
type: str
enum: [on, off]
admins:
type: seq
required: no
sequence:
- type: str
jobs_pool:
type: map
mapping:
=:
type: int
jobs_schedule:
type: map
mapping:
=:
type: map
mapping:
cron:
type: str
class:
type: str
required: yes
unique: yes
description:
type: str
required: yes
unique: yes
passenger_on:
type: bool
required: no
=:
type: str
require 'kwalify'
HIERA_DIR = 'hieradata'
namespace :hiera do
desc 'Validate YAML files'
task :validate do
puts 'Validating YAML'
raise_error = false
yamls = Dir["#{HIERA_DIR}/*.yaml"]
yamls.each do | yaml |
parser = parser_for yaml
begin
parser.parse_file(yaml)
rescue Kwalify::SyntaxError => e
raise_error = true
puts "YAML syntax error detected in #{yaml} on line #{e.linenum}"
end
errors = parser.errors()
if errors && !errors.empty?
raise_error = true
for e in errors
puts "#{yaml}:#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
end
end
end
fail if raise_error
puts 'All YAML is valid!'
end
class ToggleValidator < Kwalify::Validator
def initialize(schema = nil)
super(schema)
end
def validate_hook(value, rule, path, errors)
return unless rule.name == 'Toggles'
toggles = value.values.flatten.compact
unless toggles.uniq == toggles
non_dups = toggles.reject { |toggle| toggle if toggles.count(toggle) > 1 }
dups = (toggles - non_dups).uniq
msg = "Duplicate toggles were found: #{dups}"
errors << Kwalify::ValidationError.new(msg, path)
end
end
end
private
def parser_for(yaml)
case yaml
when /env_.*\.yaml/ then env_parser
when /toggles\.yaml/ then toggle_parser
else default_parser
end
end
def toggle_parser
@toggle_parser ||= begin
schema = Kwalify::Yaml.load_file('schemas/toggle_schema.yaml')
validator = ToggleValidator.new(schema)
Kwalify::Yaml::Parser.new(validator)
end
end
def env_parser
@env_parser ||= begin
schema = Kwalify::Yaml.load_file('schemas/env_schema.yaml')
validator = Kwalify::Validator.new(schema)
Kwalify::Yaml::Parser.new(validator)
end
end
def default_parser
@default_parser ||= Kwalify::Yaml::Parser.new
end
end
type: map
mapping:
"toggles":
type: map
mapping:
"available":
type: map
name: Toggles
mapping:
"accepted":
type: seq
sequence:
- type: str
"customer_qa":
type: seq
sequence:
- type: str
"tw_qa":
type: seq
sequence:
- type: str
"dev":
type: seq
sequence:
- type: str
"ready_for_acceptance":
type: seq
sequence:
- type: str
"overrides":
type: any
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment