summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2017-07-10 19:33:56 +0100
committerKatolaZ <katolaz@freaknet.org>2017-07-10 19:33:56 +0100
commit3b752dc02e8ac2048c3dc6efa37145c679502c84 (patch)
tree492425e6ce04a525147c0e0131a8e06a4f15e202
parentd0afee5defa7c342a97e4f444f715eb70f0779a3 (diff)
added global configuration system (config.go)
-rw-r--r--config.go112
-rw-r--r--scorsh.cfg53
-rw-r--r--scorsh.go41
3 files changed, 168 insertions, 38 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..6d8786f
--- /dev/null
+++ b/config.go
@@ -0,0 +1,112 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "github.com/go-yaml/yaml"
+ "io"
+ "io/ioutil"
+ "log"
+ "os"
+)
+
+type SCORSHWorker_cfg struct {
+ Name string `yaml:"w_name"`
+ Repos []string `yaml:"w_repos"`
+ Folder string `yaml:"w_folder"`
+ Logfile string `yaml:"w_logfile"`
+ Tagfile string `yaml:"w_tagfile"`
+ Keyrings []string `yaml:"w_keyrings"`
+}
+
+type SCORSHcfg struct {
+ Spooldir string `yaml:"s_spooldir"`
+ Logfile string `yaml:"s_logfile"`
+ LogPrefix string `yaml:"s_logprefix"`
+ Workers []SCORSHWorker_cfg `yaml:"s_workers"`
+}
+
+// Read a configuration from fname or die
+
+func ReadConfig(fname string) *SCORSHcfg {
+
+ data, err := ioutil.ReadFile(fname)
+ if err != nil {
+ log.Fatal("Error while reading file: ", err)
+ }
+
+ var cfg *SCORSHcfg
+ cfg = new(SCORSHcfg)
+
+ // Unmarshal the YAML configuration file into a SCORSHcfg structure
+ err = yaml.Unmarshal(data, cfg)
+ if err != nil {
+ log.Fatal("Error while reading configuration: ", err)
+ }
+
+ fmt.Printf("%s", cfg)
+
+ // If the user has not set a spooldir, crash loudly
+ if cfg.Spooldir == "" {
+ log.Fatal("No spooldir defined in ", fname, ". Exiting\n")
+ }
+
+ // Check if the user has set a custom logprefix
+ if cfg.LogPrefix != "" {
+ log.SetPrefix(cfg.LogPrefix)
+ }
+
+ // Check if the user wants to redirect the logs to a file
+ if cfg.Logfile != "" {
+ f, err := os.Open(cfg.Logfile)
+ if err != nil {
+ log.SetOutput(io.Writer(f))
+ } else {
+ log.Printf("Error opening logfile: \n", err)
+ }
+ }
+
+ // If we got so far, then there is some sort of config in cfg
+ log.Printf("Successfully read config from %s\n", fname)
+
+ return cfg
+
+}
+
+func (cfg *SCORSHcfg) String() string {
+
+ var buff bytes.Buffer
+
+ buff.WriteString("spooldir: ")
+ buff.WriteString(cfg.Spooldir)
+ buff.WriteString("\nlogfile: ")
+ buff.WriteString(cfg.Logfile)
+ buff.WriteString("\nlogprefix: ")
+ buff.WriteString(cfg.LogPrefix)
+ buff.WriteString("\nWorkers: \n")
+
+ for _, w := range cfg.Workers {
+ buff.WriteString("---\n name: ")
+ buff.WriteString(w.Name)
+ buff.WriteString("\n repos: ")
+ for _, r := range w.Repos {
+ buff.WriteString("\n ")
+ buff.WriteString(r)
+ }
+ buff.WriteString("\n folder: ")
+ buff.WriteString(w.Folder)
+ buff.WriteString("\n logfile: ")
+ buff.WriteString(w.Logfile)
+ buff.WriteString("\n tagfile: ")
+ buff.WriteString(w.Tagfile)
+ buff.WriteString("\n keyrings: ")
+ for _, k := range w.Keyrings {
+ buff.WriteString("\n ")
+ buff.WriteString(k)
+ }
+ buff.WriteString("\n...\n")
+
+ }
+
+ return buff.String()
+}
diff --git a/scorsh.cfg b/scorsh.cfg
new file mode 100644
index 0000000..a1320a3
--- /dev/null
+++ b/scorsh.cfg
@@ -0,0 +1,53 @@
+#
+# This is a typical scorsh configuration. We declare here the list of
+# workers, with the corresponding repo/branches regular expressions
+# and the associated folder
+#
+
+---
+s_spooldir: "/var/spool/scorsh"
+s_logfile: "/var/log/scorsh/scorsh.log"
+s_logprefix: "[scorsh]"
+
+s_workers:
+ [
+ {
+ w_name: catchall,
+ w_repos: ["*:*"], # All branches in all repos
+ w_folder: ./catchall,
+ w_logfile: ./catchall/catchall.log,
+ w_tagfile: "./catchall/tags.cfg",
+ w_keyrings: [
+ "./catchall/catchall_keyring.asc"
+ ]
+ },
+ {
+ w_name: ascii,
+ w_repos: ["*:suites/ascii", # branch "suites/ascii" in all the repos
+ "*:suites/ascii-updates",
+ "*:suites/ascii-security"
+ ],
+ w_folder: ./ascii,
+ w_logfile: ./worker_ascii.log,
+ w_tagfile: "./ascii/tags.cfg",
+ w_keyrings: [
+ "./${w_folder}/ascii_keyring.asc"
+ ]
+ },
+ {
+ w_name: ascii-side-branches ,
+ w_repos: [
+ "*:suites/ascii-proposed",
+ "*:suites/ascii-proposed-updates"
+ ],
+ w_folder: ./ascii-side-branches,
+ w_logfile: ./worker_ascii_side_branches.log,
+ w_tagfile: "./ascii-side-branches/tags.cfg",
+ w_keyrings: [
+ "./${w_folder}/ascii_keyring.asc",
+ "./${w_folder}/ascii_proposed_keyring.asc",
+ "./${w_folder}/ascii_proposed_updates_keyring.asc"
+ ],
+ }
+ ]
+... \ No newline at end of file
diff --git a/scorsh.go b/scorsh.go
index a5adbb8..08fc962 100644
--- a/scorsh.go
+++ b/scorsh.go
@@ -2,9 +2,7 @@ package main
import (
"errors"
- "golang.org/x/crypto/openpgp"
"log"
- "os"
"flag"
)
@@ -57,46 +55,13 @@ func SCORSHErr(err int) error {
}
-func SCORSHWorker(keyring string, c_msg chan SCORSHmsg, c_status chan int) {
-
- // read the worker configuration file
-
- // Open the keyring file
- f, err := os.Open(keyring)
- defer f.Close()
-
- if err != nil {
- log.Printf("[worker] cannot open file %s\n", keyring)
- c_status <- SCORSH_ERR_NO_FILE
- return
- }
-
- // load the keyring
- kr, err := openpgp.ReadArmoredKeyRing(f)
-
- if err != nil {
- log.Printf("[worker] cannot open keyring %s\n", keyring)
- log.Printf("%s\n", err)
- c_status <- SCORSH_ERR_KEYRING
- return
- }
-
- // wait for messages from the c_msg channel
-
- msg := <-c_msg
-
- // process message
- ret := walk_commits(msg, kr)
-
- c_status <- ret
-
-}
func main() {
flag.Parse()
-
-
+ cfg := ReadConfig(*conf_file)
+
+ log.Printf("%s\n", cfg)
}