service/src/service/environment.cr

71 lines
1.3 KiB
Crystal
Raw Permalink Normal View History

require "specparser"
2019-06-09 16:59:05 +02: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
getter files = Array(ServiceDefinition::FileDefinition).new
2019-06-09 16:59:05 +02:00
2019-10-26 14:00:11 +02:00
# The place well put services data and configuration.
@root : String?
@context : Service::Context
def initialize(context)
initialize context, "root"
end
def initialize(@context, @name, type = "prefix")
@type = Type.parse type
@files = Array(ServiceDefinition::FileDefinition).new
2019-08-10 17:16:24 +02:00
end
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|
@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
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
def to_spec
[
"type: #{@type.to_s}"
].join("\n") + "\n"
end
def get_provider(token)
@context.services.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