2019-10-19 18:08:34 +02:00
|
|
|
|
require "specparser"
|
2019-06-09 16:59:05 +02:00
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
require "./context.cr"
|
|
|
|
|
|
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
|
2019-11-08 14:36:59 +01:00
|
|
|
|
getter files = Array(ServiceDefinition::FileDefinition).new
|
2019-06-09 16:59:05 +02:00
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
# The place we’ll put services’ data and configuration.
|
|
|
|
|
@root : String?
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context : Service::Context
|
|
|
|
|
|
|
|
|
|
def initialize(context)
|
|
|
|
|
initialize context, "root"
|
2019-10-24 18:29:22 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def initialize(@context, @name, type = "prefix")
|
2019-10-24 18:29:22 +02:00
|
|
|
|
@type = Type.parse type
|
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
@files = Array(ServiceDefinition::FileDefinition).new
|
2019-08-10 17:16:24 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def initialize(@context, @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|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
@files << ServiceDefinition::FileDefinition.new check
|
2019-08-10 17:16:24 +02:00
|
|
|
|
end
|
2019-06-09 16:59:05 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
def root
|
|
|
|
|
@root || "#{SERVED_DATA_DIRECTORY}/#{@name}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-24 18:29:22 +02:00
|
|
|
|
def write(dir : String)
|
|
|
|
|
File.write "#{dir}/#{@name}.spec", to_spec
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-25 17:59:53 +02:00
|
|
|
|
def remove(dir : String)
|
|
|
|
|
File.delete "#{dir}/#{@name}.spec"
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-24 18:29:22 +02:00
|
|
|
|
def to_spec
|
|
|
|
|
[
|
|
|
|
|
"type: #{@type.to_s}"
|
|
|
|
|
].join("\n") + "\n"
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-10 22:29:11 +02:00
|
|
|
|
def get_provider(token)
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.services.find do |service|
|
2019-08-10 22:29:11 +02:00
|
|
|
|
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
|
|
|
|
|
|