service/src/service_definition.cr

88 lines
1.4 KiB
Crystal

require "yaml"
class ServiceDefinition
struct Consumes
YAML.mapping({
token: String,
optional: {
type: Bool,
default: false
}
})
end
struct Provides
YAML.mapping({
token: String
})
end
struct Checks
YAML.mapping({
name: String,
file: String?,
directory: String?,
command: String
})
end
YAML.mapping({
name: String,
command: String,
stop_command: {
type: String?,
key: "stop-command"
},
directory: String?, # Place to chdir to before running @command.
environment: {
type: Environment,
default: Environment.root
},
provides: {
type: Array(Provides),
default: [] of Provides
},
consumes: {
type: Array(Consumes),
default: [] of Consumes
},
checks: {
type: Array(Checks),
default: [] of Checks
},
environment_variables: {
type: Array(String),
key: "environment-variables",
default: [] of String
}
})
def self.new(name)
Service.from_yaml File.read "#{name}.yaml"
end
class_getter all = [] of ServiceDefinition
def self.load(path)
Dir.each_child path do |child|
unless child.match /\.yaml$/
next
end
@@all << ServiceDefinition.from_yaml File.read "#{path}/#{child}"
end
end
def self.get(name) : ServiceDefinition
_def = @@all.find &.name.==(name)
if _def.nil?
raise Exception.new "Service '#{name}' does not exist."
end
_def
end
def to_s
name
end
end