service/src/service/environment.cr

77 lines
1.4 KiB
Crystal
Raw Normal View History

2019-06-09 16:59:05 +02:00
require "yaml"
class Environment
enum Type
Prefix
RootFileSystem
end
YAML.mapping({
name: String,
type: {
type: Type,
default: Type::Prefix
},
domain_name: {
type: String?,
key: "domain-name"
},
checks: {
# FIXME: Would probably need a more neutral namespace.
type: Array(ServiceDefinition::Checks),
default: Array(ServiceDefinition::Checks).new
}
})
def initialize()
@name = "root"
@type = Type::Prefix
@checks = Array(ServiceDefinition::Checks).new
# FIXME: Should this *really* be here?
@checks << ServiceDefinition::Checks.from_yaml <<-EOF
name: Creating data directory
directory: /srv/%{ENVIRONMENT}
command: mkdir -p /srv/%{ENVIRONMENT} && chmod a+rwt /srv/%{ENVIRONMENT}
EOF
end
class_getter root = Environment.new
class_getter all = [@@root] of Environment
def self.load(path)
Dir.each_child path do |child|
unless child.match /\.yaml$/
next
end
file_path = "#{path}/#{child}"
begin
environment = Environment.from_yaml File.read file_path
rescue e
STDERR << "error loading #{file_path}: " << e << "\n"
# FIXME: Print stacktrace? Debug mode?
next
end
@@all << environment
end
end
def self.get(name)
_def = @@all.find &.name.==(name)
if _def.nil?
raise Exception.new "Environment '#{name}' does not exist."
end
_def
end
def to_s
"#{name} (#{type.to_s.downcase})"
end
end