Commit eac0b575 authored by Sebastian Stark's avatar Sebastian Stark

Merge remote-tracking branch 'jpiles/externalSchedules'

parents 1e3b7970 8092b735
......@@ -17,8 +17,9 @@ import (
)
const (
myName = "snaprd"
DATA_SUBDIR = ".data"
myName = "snaprd"
defaultSchedFileName = "/etc/" + myName + ".schedules"
DATA_SUBDIR = ".data"
)
type Opts []string
......@@ -47,6 +48,7 @@ type Config struct {
NoPurge bool
NoWait bool
NoLogDate bool
SchedFile string
}
// WriteCache writes the global configuration to disk as a json file.
......@@ -78,6 +80,10 @@ func (c *Config) ReadCache() error {
}
c.RsyncPath = t.RsyncPath
c.RsyncOpts = t.RsyncOpts
if t.SchedFile != "" {
c.SchedFile = t.SchedFile
schedules.AddFromFile(c.SchedFile)
}
c.Origin = t.Origin
if _, ok := schedules[t.Schedule]; ok == false {
log.Fatalln("no such schedule:", t.Schedule)
......@@ -96,6 +102,7 @@ func usage() {
Commands:
run Periodically create snapshots
list List snapshots
scheds List schedules
help Show usage instructions
Use <command> -h to show possible options for <command>.
Examples:
......@@ -142,7 +149,13 @@ func LoadConfig() *Config {
flags.BoolVar(&(config.NoLogDate),
"noLogDate", false,
"if set, does not print date and time in the log output. Useful if output is redirected to syslog")
flags.StringVar(&(config.SchedFile),
"schedFile", defaultSchedFileName,
"path to external schedules")
flags.Parse(os.Args[2:])
if config.SchedFile != "" {
schedules.AddFromFile(config.SchedFile)
}
if _, ok := schedules[config.Schedule]; ok == false {
log.Fatalln("no such schedule:", config.Schedule)
}
......@@ -173,7 +186,13 @@ func LoadConfig() *Config {
flags.StringVar(&(config.Schedule),
"schedule", "longterm",
"one of "+schedules.String())
flags.StringVar(&(config.SchedFile),
"schedFile", defaultSchedFileName,
"path to external schedules")
flags.Parse(os.Args[2:])
if config.SchedFile != "" {
schedules.AddFromFile(config.SchedFile)
}
err := config.ReadCache()
if err != nil {
log.Println("error reading cached settings (using defaults):", err)
......@@ -186,6 +205,18 @@ func LoadConfig() *Config {
usage()
os.Exit(0)
}
case "scheds":
{
flags := flag.NewFlagSet(subcmd, flag.ExitOnError)
flags.StringVar(&(config.SchedFile),
"schedFile", defaultSchedFileName,
"path to external schedules")
flags.Parse(os.Args[2:])
if config.SchedFile != "" {
schedules.AddFromFile(config.SchedFile)
}
return config
}
default:
{
log.Println("unknown subcommand:", subcmd)
......
......@@ -213,6 +213,8 @@ func main() {
case "list":
fmt.Printf("### Repository: %s, Origin: %s, Schedule: %s\n", config.repository, config.Origin, config.Schedule)
subcmdList()
case "scheds":
schedules.List()
}
os.Exit(0)
}
......@@ -5,6 +5,10 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
)
......@@ -41,6 +45,8 @@ func (il intervalList) goal(i int) int {
type scheduleList map[string]intervalList
type jsonInterval []map[string]time.Duration
func (schl *scheduleList) String() string {
a := []string{}
for sch := range *schl {
......@@ -58,3 +64,78 @@ var schedules = scheduleList{
"testing": {second * 5, second * 20, second * 140, second * 280, long},
"testing2": {second * 5, second * 20, second * 40, second * 80, long},
}
// Adds an external JSON file to the list of available scheds
func (schl scheduleList) AddFromFile(file string) {
// If we are using the default file name, and it doesn't exist, no problem, just return
if _, err := os.Stat(file); os.IsNotExist(err) && file == defaultSchedFileName {
return
}
schedFile, err := ioutil.ReadFile(file)
if err != nil {
fmt.Printf("Error opening schedule file: %v\n", err)
return
}
var readData map[string]jsonInterval
err = json.Unmarshal(schedFile, &readData)
if err != nil {
fmt.Printf("Error parsing data: %v\n", err)
return
}
for k, v := range readData {
schl[k] = v.IntervalList()
}
}
// Prints the stored schedules in the list
func (schl scheduleList) List() {
for name, sched := range schl {
fmt.Println(name, ": ", sched)
}
}
// Transform a JSON formatted intervalList like this:
// [
// { "day" : 1, "hour" : 12 },
// { "week" : 2 },
// { "month" : 1, "week" : 2}
// { "long" : 1}
// ]
// and it makes it equivalent to
// { 1*day + 12*hour, 2*week, 1*month + 2*week, long }
func (json jsonInterval) IntervalList() intervalList {
il := make(intervalList, len(json))
for i, interval := range json {
var duration time.Duration = 0
Loop:
for k, v := range interval {
switch k {
case "s", "second":
duration += v * second
case "m", "minute":
duration += v * minute
case "h", "hour":
duration += v * hour
case "d", "day":
duration += v * day
case "w", "week":
duration += v * week
case "M", "month":
duration += v * month
case "y", "year":
duration += v * year
case "l", "long":
duration = long
break Loop
}
}
il[i] = duration
}
return il
}
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