Commit 3c78804f authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Merge branch 'config' into dev

Closes #6
parents 49ed9f4e da6f005b
Pipeline #57032 failed with stages
......@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
......@@ -265,26 +266,21 @@ 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)
func ReadConfig(r io.Reader, fileName string) (conf *Config, err error) {
configBytes, err := ioutil.ReadAll(r)
if err != nil {
return
}
conf = new(Config)
if strings.HasSuffix(configFile, ".yaml") {
if strings.HasSuffix(fileName, ".yaml") {
err = yaml.Unmarshal(configBytes, conf)
if err != nil {
err = fmt.Errorf("Error decoding yaml: %s", err)
return
}
} else if strings.HasSuffix(configFile, ".json") {
} else if strings.HasSuffix(fileName, ".json") {
err = json.Unmarshal(configBytes, conf)
if err != nil {
err = fmt.Errorf("Error decoding json: %s", err)
......
......@@ -2,6 +2,11 @@
"feudal_backend_host": "<foo>.kit.edu",
"username": "<username registerred at the portal>",
"password": "<password registerred at the portal>",
"debug": {
"backend": true,
"scripts": false,
"sequential_execution": false
},
"services": {
"<local identifier>": {
"name": "<service name>",
......
# the host
feudal_backend_host: "<hostname>"
debug:
# debug output from feudalScripts (aka feudalAdapters)
scripts: false
# debug communication with the backend
backend: true
# execute deployments in sequence and not parallel (-> nicer log output)
sequential_execution: false
username: '<username registerred at the portal>'
password: '<password registerred at the portal>'
services:
'<local identifier>':
name: <service name>
description: This is the foo service
command: ./fooService
name: '<service name>'
description: 'This is the foo service'
command: './fooService'
group_to_service_ids:
'<group name>':
......
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"git.scc.kit.edu/feudal/feudalClient/config"
deps "git.scc.kit.edu/feudal/feudalClient/deployments"
......@@ -114,8 +117,8 @@ func prepareDebugging(conf *config.Config) {
// Override the debug configuration with parameters
if *debugAll {
*scriptDebugging = true
*backendDebugging = true
conf.Debug.Backend = true
conf.Debug.Scripts = true
}
if *scriptDebugging {
conf.Debug.Scripts = true
......@@ -138,6 +141,82 @@ func prepareDebugging(conf *config.Config) {
}
}
// search for a config file
// dir pattern: $HOME/.config -> /etc/feudal -> $CWD
// file pattern: *.yaml -> *.json
func findConfigFile(arg string) (handle *os.File, err error) {
globString := func(gs string) string {
var files []string
files, err = filepath.Glob(gs)
if err == nil && len(files) > 0 {
log.Printf("Globbed: %v", files)
_, err = os.Stat(files[0])
if err == nil {
return files[0]
} else {
log.Printf("Error opening: %s", err)
}
}
return ""
}
globDir := func(ds string) (name string) {
name = globString(path.Join(ds, "*.yaml"))
if name != "" {
return
}
name = globString(path.Join(ds, "*.json"))
if name != "" {
return
}
return
}
findName := func() string {
name := ""
if *configFile != "" {
// check if file exists
_, err = os.Stat(*configFile)
if err == nil {
return *configFile
} else {
log.Printf("Error opening: %s", err)
}
}
var home, cwd string
// search $HOME/.config
if home, err = os.UserHomeDir(); err == nil {
name = globDir(path.Join(home, ".config/feudal"))
if name != "" {
return name
}
}
name = globDir("/etc/feudal")
if name != "" {
return name
}
cwd, err = os.Getwd()
if err == nil {
name = globDir(cwd)
if name != "" {
return name
}
}
return ""
}
firstName := findName()
if firstName == "" {
err = fmt.Errorf(`No config file!
Provide -c/--config or place a config json/yaml in $HOME/.config/feudal or /etc/feudal or the current working directory`)
return
}
return os.Open(firstName)
}
func main() {
var err error
......@@ -158,8 +237,15 @@ func main() {
return
}
// search appropriate config
handle, err := findConfigFile(*configFile)
if err != nil {
log.Fatalf("[Conf] Error opening config file: %s", err)
}
log.Printf("[Conf] Opened config file %s", handle.Name())
// read the config file
conf, err := config.ReadConfig(*configFile)
conf, err := config.ReadConfig(handle, handle.Name())
if err != nil {
log.Fatalf("[Conf] %s", err)
}
......
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