114 lines
2.5 KiB
Crystal
114 lines
2.5 KiB
Crystal
require "yaml"
|
|
require "spec"
|
|
|
|
class ServiceDefinition
|
|
struct Consumes
|
|
def initialize(@token : String)
|
|
@optional = false
|
|
|
|
if @token.match /\?$/
|
|
@token = @token.gsub /\?$/, ""
|
|
@optional = true
|
|
end
|
|
end
|
|
YAML.mapping({
|
|
token: String,
|
|
optional: {
|
|
type: Bool,
|
|
default: false
|
|
}
|
|
})
|
|
end
|
|
struct Provides
|
|
def initialize(@token : String)
|
|
end
|
|
YAML.mapping({
|
|
token: String
|
|
})
|
|
end
|
|
struct Checks
|
|
def initialize(section : Specs::Section)
|
|
@name = section.content["name"].as_s
|
|
@file = section.content["file"]?.try &.as_s
|
|
@directory = section.content["directory"]?.try &.as_s
|
|
@command = section.content["command"].as_s
|
|
end
|
|
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.
|
|
user: String?, # User that should run the service.
|
|
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
|
|
}
|
|
})
|
|
|
|
class_getter all = [] of ServiceDefinition
|
|
|
|
def initialize(specs : Specs)
|
|
sections = specs.sections
|
|
pp! specs
|
|
specs = specs.assignments
|
|
@name = specs["name"].as_s
|
|
@command = specs["command"].as_s
|
|
@stop_command = specs["stop-command"]?.try &.as_s
|
|
@directory = specs["directory"]?.try &.as_s
|
|
@provides = specs["provides"]?.try &.as_a_or_s.map { |x| Provides.new x } || Array(Provides).new
|
|
@consumes = specs["consumes"]?.try &.as_a_or_s.map { |x| Consumes.new x } || Array(Consumes).new
|
|
@checks = sections.select(&.name.== "check").map { |x| Checks.new x } || Array(Checks).new
|
|
@environment_variables = specs["environment-variables"]?.try &.as_a_or_s || Array(String).new
|
|
end
|
|
|
|
def self.load(path)
|
|
Dir.each_child path do |child|
|
|
if child.match /\.yaml$/
|
|
@@all << ServiceDefinition.from_yaml File.read "#{path}/#{child}"
|
|
elsif child.match /\.spec$/
|
|
@@all << ServiceDefinition.new Specs.parse("#{path}/#{child}").not_nil!
|
|
else
|
|
next
|
|
end
|
|
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
|
|
|