From 3769303ad5086627d2b755306a387202c744aed3 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Thu, 8 Aug 2019 17:12:23 +0200 Subject: [PATCH] WIP support for .spec service files. --- shard.yml | 4 +++ src/service/service_definition.cr | 47 +++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/shard.yml b/shard.yml index 1746b59..ac47286 100644 --- a/shard.yml +++ b/shard.yml @@ -13,4 +13,8 @@ targets: status: main: src/status.cr +dependencies: + spec: + git: https://git.karchnu.fr/JunkOS/recipes-parser + license: MIT diff --git a/src/service/service_definition.cr b/src/service/service_definition.cr index 6be2c2e..afa76ae 100644 --- a/src/service/service_definition.cr +++ b/src/service/service_definition.cr @@ -1,7 +1,16 @@ 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: { @@ -11,11 +20,19 @@ class ServiceDefinition }) 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?, @@ -32,10 +49,6 @@ class ServiceDefinition }, directory: String?, # Place to chdir to before running @command. user: String?, # User that should run the service. - environment: { - type: Environment, - default: Environment.root - }, provides: { type: Array(Provides), default: [] of Provides @@ -55,19 +68,31 @@ class ServiceDefinition } }) - def self.new(name) - Service.from_yaml File.read "#{name}.yaml" - end - 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| - unless child.match /\.yaml$/ + 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 - - @@all << ServiceDefinition.from_yaml File.read "#{path}/#{child}" end end