2019-10-19 18:08:34 +02:00
|
|
|
require "specparser"
|
2019-06-09 16:59:05 +02:00
|
|
|
|
|
|
|
class Environment
|
|
|
|
enum Type
|
|
|
|
Prefix
|
|
|
|
RootFileSystem
|
|
|
|
end
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
getter name : String
|
|
|
|
getter type : Type = Type::Prefix
|
|
|
|
getter domain_name : String?
|
|
|
|
getter checks = Array(ServiceDefinition::Checks).new
|
2019-06-09 16:59:05 +02:00
|
|
|
|
|
|
|
def initialize()
|
2019-10-24 18:29:22 +02:00
|
|
|
initialize "root"
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(@name, type = "prefix")
|
|
|
|
@type = Type.parse type
|
|
|
|
|
2019-06-09 16:59:05 +02:00
|
|
|
@checks = Array(ServiceDefinition::Checks).new
|
|
|
|
|
|
|
|
# FIXME: Should this *really* be here?
|
2019-08-10 17:16:24 +02:00
|
|
|
@checks << ServiceDefinition::Checks.new "Creating data directory",
|
|
|
|
"mkdir -p /srv/${ENVIRONMENT} && chmod a+rwt /srv/${ENVIRONMENT}",
|
|
|
|
directory: "/srv/${ENVIRONMENT}"
|
|
|
|
end
|
|
|
|
|
2019-10-25 12:34:21 +02:00
|
|
|
def initialize(@name, specs : SpecParser)
|
2019-08-10 17:16:24 +02:00
|
|
|
assignments = specs.assignments
|
|
|
|
|
|
|
|
assignments["type"].try &.as_s.tap do |type|
|
|
|
|
@type = Type.parse type
|
|
|
|
end
|
|
|
|
|
|
|
|
specs.sections.select(&.name.==("check")).each do |check|
|
|
|
|
@checks << ServiceDefinition::Checks.new check
|
|
|
|
end
|
2019-06-09 16:59:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
class_getter root = Environment.new
|
|
|
|
class_getter all = [@@root] of Environment
|
|
|
|
|
|
|
|
def self.load(path)
|
2019-10-19 18:40:47 +02:00
|
|
|
return unless Dir.exists? path
|
|
|
|
|
2019-06-09 16:59:05 +02:00
|
|
|
Dir.each_child path do |child|
|
2019-08-10 17:16:24 +02:00
|
|
|
unless child.match /\.spec$/
|
2019-06-09 16:59:05 +02:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
file_path = "#{path}/#{child}"
|
|
|
|
|
|
|
|
begin
|
2019-10-25 12:34:21 +02:00
|
|
|
name = File.basename(child, ".spec")
|
|
|
|
specs = SpecParser.parse File.read(file_path)
|
|
|
|
|
|
|
|
environment = Environment.new name, specs
|
2019-06-09 16:59:05 +02:00
|
|
|
rescue e
|
|
|
|
STDERR << "error loading #{file_path}: " << e << "\n"
|
|
|
|
# FIXME: Print stacktrace? Debug mode?
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
@@all << environment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-24 18:29:22 +02:00
|
|
|
def write(dir : String)
|
|
|
|
File.write "#{dir}/#{@name}.spec", to_spec
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_spec
|
|
|
|
[
|
|
|
|
"type: #{@type.to_s}"
|
|
|
|
].join("\n") + "\n"
|
|
|
|
end
|
|
|
|
|
2019-06-09 16:59:05 +02:00
|
|
|
def self.get(name)
|
|
|
|
_def = @@all.find &.name.==(name)
|
|
|
|
|
|
|
|
if _def.nil?
|
2019-10-24 12:59:19 +02:00
|
|
|
raise ::Service::Exception.new "Environment '#{name}' does not exist."
|
2019-06-09 16:59:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
_def
|
|
|
|
end
|
|
|
|
|
2019-08-10 22:29:11 +02:00
|
|
|
def get_provider(token)
|
|
|
|
Service.all.find do |service|
|
|
|
|
service.environment == self && service.provides? token
|
|
|
|
end.try &.id
|
|
|
|
end
|
|
|
|
|
2019-06-09 16:59:05 +02:00
|
|
|
def to_s
|
|
|
|
"#{name} (#{type.to_s.downcase})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|