Commit a942a523 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Merge branch 'schemas' into dev

parents f4e6ac07 3b1680f8
......@@ -54,7 +54,7 @@ type (
// Services maps an (arbitrary) service identifier to service structs
// the service identifiers are referenced in GroupToServiceIDs and EntitlementToServiceIDs
Services map[ServiceID]Service
Services map[ServiceID]Service `json:"services"`
// GroupToServiceIDs determines which services are provided for users of the
// group
......@@ -69,25 +69,25 @@ type (
EntitlementToServiceIDs map[string][]ServiceID `json:"entitlement_to_service_ids"`
// FetchIntervalString gets parsed by time.ParseDuration
FetchIntervalString string `json:"fetch_interval"`
FetchIntervalString string `json:"fetch_interval,omitempty"`
// ReconnectTimeout gets parsed by time.ParseDuration
ReconnectTimeoutString string `json:"reconnect_timeout"`
ReconnectTimeoutString string `json:"reconnect_timeout,omitempty"`
// Debug flags control the log levels of the client.
Debug DebugConfig `json:"debug,omitempty"`
// After the duration of FetchInterval the client will fetch updates using the REST
// interface.
FetchInterval time.Duration
FetchInterval time.Duration `json:"-"`
// We reconnect to RabbitMQ after ReconnectTimeout.
ReconnectTimeout time.Duration
ReconnectTimeout time.Duration `json:"-"`
RabbitMQConfig RabbitMQConfig
RabbitMQConfig RabbitMQConfig `json:"-"`
// The Site is the name of the site of this client. It is fetched from the backend.
Site string
// Debug flags control the log levels of the client.
Debug DebugConfig `json:"debug"`
Site string `json:"-"`
}
// strippedConfig is sent to the backend on startup
......@@ -263,6 +263,9 @@ func (conf *Config) validateConfig() (err error) {
// ReadConfig reads a config file and validates it
func ReadConfig(configFile string) (conf *Config, err error) {
if configFile == "" {
return nil, fmt.Errorf("Please provide the --config flag. See --help")
}
log.Printf("[Conf] Reading config file %s", configFile)
configBytes, err := ioutil.ReadFile(configFile)
......
module git.scc.kit.edu/feudal/feudalClient
require (
git.scc.kit.edu/feudal/feudalScripts v1.1.0
git.scc.kit.edu/feudal/feudalScripts/v2 v2.0.0
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/alecthomas/jsonschema v0.0.0-20190122210438-a6952de1bbe6
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9
github.com/stretchr/testify v1.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.1.0
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
git.scc.kit.edu/feudal/feudalClient v0.1.0/go.mod h1:0h97fa5a+3UqI6rGnkcUhM4DyFvBt4zAXIja+EJbXls=
git.scc.kit.edu/feudal/feudalScripts v1.0.0/go.mod h1:5+SmQtrtjjLAI5rgZXVVRNvLfwlKw0uRDwWkFq8HUJo=
git.scc.kit.edu/feudal/feudalScripts v1.1.0 h1:/j1FUlvwT6gOyjIS7+NnpDEcRE/QZfxA0JX7TIfFNc8=
git.scc.kit.edu/feudal/feudalScripts v1.1.0/go.mod h1:IJmsus8xLMsKeb+n8Gqg2J4s6fRtiNwxXNyD+sC9OJU=
git.scc.kit.edu/feudal/feudalScripts/v2 v2.0.0 h1:3907HJ9XEZSuEZ0oAuijssG9SGFjAdmyDt8P7SlFFZY=
git.scc.kit.edu/feudal/feudalScripts/v2 v2.0.0/go.mod h1:EVXrOHOlnw3VZ02LZQDSN7bxldpJ7p31tfKZ5zqAEXA=
github.com/alecthomas/jsonschema v0.0.0-20190122210438-a6952de1bbe6 h1:xadBCbc8D9mmkaNfCsEBHbIoCjbayJXJNsY1JjPjNio=
github.com/alecthomas/jsonschema v0.0.0-20190122210438-a6952de1bbe6/go.mod h1:qpebaTNSsyUn5rPSJMsfqEtDw71TTggXM6stUDI16HA=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/streadway/amqp v0.0.0-20181107104731-27835f1a64e9/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9 h1:37QTz/gdHBLQcsmgMTnQDSWCtKzJ7YnfI2M2yTdr4BQ=
github.com/streadway/amqp v0.0.0-20181205114330-a314942b2fd9/go.mod h1:1WNBiOZtZQLpVAyu0iTduoJL9hEsMloAK5XWrtW0xdY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.1.0 h1:ngVtJC9TY/lg0AA/1k48FYhBrhRoFlEmWzsehpNAaZg=
github.com/xeipuuv/gojsonschema v1.1.0/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
......@@ -7,13 +7,13 @@ import (
"os"
"git.scc.kit.edu/feudal/feudalClient/config"
"git.scc.kit.edu/feudal/feudalClient/deployments"
deps "git.scc.kit.edu/feudal/feudalClient/deployments"
"git.scc.kit.edu/feudal/feudalClient/sink"
"git.scc.kit.edu/feudal/feudalClient/sink/script"
"git.scc.kit.edu/feudal/feudalClient/source"
"git.scc.kit.edu/feudal/feudalClient/source/amqp"
"git.scc.kit.edu/feudal/feudalClient/source/rest"
"gopkg.in/alecthomas/kingpin.v2"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
......@@ -23,12 +23,16 @@ var (
).Author(
"Lukas Burgey",
).Version(
"v2.0.1",
"v2.1.0",
)
cmdStart = app.Command("start", "Starts the client in its normal operation mode.").Default()
cmdDeregister = app.Command("deregister", "Before disabling the client: Use deregister to inform the backend that the client ceases operation.")
configFile = app.Flag("config", "Config file to file to use.").Short('c').Required().String()
cmdStart = app.Command("start", "Starts the client in its normal operation mode.").Default()
cmdDeregister = app.Command("deregister", "Before disabling the client: Use deregister to inform the backend that the client ceases operation.")
cmdSchema = app.Command("schema", "Can be used to generate a JSON schema (Draft 4, see json-schema.org) for the validation of script inputs, outputs and the config of this client.")
printSelector = cmdSchema.Arg("selector", "Select the schema to generate. Options: input, output, config").Required().Enum("input", "output", "config")
cmdValidate = app.Command("validate", "Validate a input/output/config json. The json needs to be passed via stdin.")
validateSelector = cmdValidate.Arg("selector", "Select the schema to validate against. Options: input, output, config").Required().Enum("input", "output", "config")
configFile = app.Flag("config", "Config file to use.").Short('c').String()
scriptDebugging = app.Flag("debug-scripts", "Display debugging info concerning executed scripts").Bool()
backendDebugging = app.Flag("debug-backend", "Display debugging info concerning the backend").Bool()
debugAll = app.Flag("debug", "Display all debugging info").Bool()
......@@ -106,17 +110,7 @@ func start(conf *config.Config) {
}
}
func main() {
var err error
// get arguments
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
// read the config file
conf, err := config.ReadConfig(*configFile)
if err != nil {
log.Fatalf("[Conf] %s", err)
}
func prepareDebugging(conf *config.Config) {
// Override the debug configuration with parameters
if *debugAll {
......@@ -142,6 +136,35 @@ func main() {
if conf.Debug.Sequential {
log.Printf("[Debug] Executing tasks sequentially")
}
}
func main() {
var err error
// get arguments
cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
// Execute commands that don't need the config
switch cmd {
case "schema":
printSchema(*printSelector)
return
case "validate":
result, err := validateJSON(*validateSelector, os.Stdin)
if err != nil {
log.Fatal(err)
}
printValidationResult(result)
return
}
// read the config file
conf, err := config.ReadConfig(*configFile)
if err != nil {
log.Fatalf("[Conf] %s", err)
}
prepareDebugging(conf)
switch cmd {
case "start":
......
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"git.scc.kit.edu/feudal/feudalClient/config"
scripts "git.scc.kit.edu/feudal/feudalScripts"
"github.com/alecthomas/jsonschema"
"github.com/xeipuuv/gojsonschema"
)
func generateSchema(selector string) (schemaBytes []byte) {
var schema *jsonschema.Schema
switch selector {
case "input":
schema = jsonschema.Reflect(scripts.Input{})
case "output":
schema = jsonschema.Reflect(scripts.Output{})
case "config":
schema = jsonschema.Reflect(config.Config{})
}
schemaBytes, err := json.Marshal(schema)
if err != nil {
panic(err)
}
return
}
func printSchema(schemaSelector string) {
fmt.Printf("%s", generateSchema(schemaSelector))
}
func validateJSON(schemaSelector string, reader io.Reader) (result *gojsonschema.Result, err error) {
// first find and prepare our schema
schemaLoader := gojsonschema.NewBytesLoader(generateSchema(schemaSelector))
// second prepare the json which we are validating
var doc []byte
doc, err = ioutil.ReadAll(reader)
if err != nil {
err = fmt.Errorf("Error reading json for validation: %s", err)
return
}
docLoader := gojsonschema.NewBytesLoader(doc)
// proceed with the validation and print errors if they occur
result, err = gojsonschema.Validate(schemaLoader, docLoader)
return
}
func printValidationResult(result *gojsonschema.Result) {
if result.Valid() {
log.Println("Validation successful")
return
}
log.Printf("Validation errors")
for _, err := range result.Errors() {
fmt.Println(err)
}
os.Exit(1)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment