213 lines
6.1 KiB
Go
213 lines
6.1 KiB
Go
package common
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
ini "gopkg.in/ini.v1"
|
|
)
|
|
|
|
const (
|
|
DAILY = 86400
|
|
TWICE_DAILY = DAILY / 2
|
|
HOURLY = 3600
|
|
BI_HOURLY = HOURLY * 2
|
|
TRI_HOURLY = HOURLY * 3
|
|
TWICE_HOURLY = HOURLY / 2
|
|
TEN_MINUTELY = 600
|
|
FIVE_MINUTELY = 300
|
|
|
|
CONFIG_PATH = "merlin-config.ini"
|
|
|
|
DEFAULT_MAX_JOBS = 6
|
|
DEFAULT_MAX_TIME = DAILY / 4
|
|
DEFAULT_SOCK_PATH = "/run/merlin/merlin.sock"
|
|
DEFAULT_STATE_PATH = "/home/mirror/merlin/states"
|
|
DEFAULT_LOG_FILE = "/home/mirror/merlin/logs/transfer.log"
|
|
DEFAULT_PASSWORD_DIR = "/home/mirror/passwords"
|
|
DEFAULT_DOWNLOAD_DIR = "/mirror/root"
|
|
)
|
|
|
|
var frequencies = map[string]int{
|
|
"daily": DAILY,
|
|
"twice-daily": TWICE_DAILY,
|
|
"hourly": HOURLY,
|
|
"bi-hourly": BI_HOURLY,
|
|
"tri-hourly": TRI_HOURLY,
|
|
"twice-hourly": TWICE_HOURLY,
|
|
"ten-minutely": TEN_MINUTELY,
|
|
"five-minutely": FIVE_MINUTELY,
|
|
}
|
|
|
|
// Last job attempt statuses
|
|
const (
|
|
NOT_RUN_YET = iota
|
|
SUCCESS
|
|
FAILURE // change to TIMEOUT?
|
|
TERMINATED // was killed by a signal
|
|
)
|
|
|
|
type Repo struct {
|
|
// this should be one of "csc-sync-standard", etc.
|
|
SyncType string `ini:"sync_type"`
|
|
// a human-readable frequency, e.g. "bi-hourly"
|
|
FrequencyStr string `ini:"frequency"`
|
|
// the maximum time (in seconds) that each child process of this repo
|
|
// can for before being killed
|
|
MaxTime int `ini:"max_time"`
|
|
// where to download the files for this repo (relative to the download
|
|
// dir in the config)
|
|
LocalDir string `ini:"local_dir"`
|
|
// the remote host to rsync from
|
|
RsyncHost string `ini:"rsync_host"`
|
|
// the remote directory on the rsync host
|
|
RsyncDir string `ini:"rsync_dir"`
|
|
// the rsync user (optional)
|
|
RsyncUser string `ini:"rsync_user"`
|
|
// the file storing the password for rsync (optional)
|
|
PasswordFile string `ini:"password_file"`
|
|
// the log file for rsync (optional, defaults to Config.LogFile)
|
|
LogFile string `ini:"log_file"`
|
|
// the desired interval (in seconds) between successive runs
|
|
Frequency int `ini:"-"`
|
|
// the name of this repo
|
|
Name string `ini:"-"`
|
|
Logger *Logger `ini:"-"`
|
|
// the repo should stop syncing if StopChan is closed
|
|
StopChan <-chan bool `ini:"-"`
|
|
// the repo will write its name to DoneChan when it has finished a job
|
|
// (successfully or unsuccessfully)
|
|
DoneChan chan<- string `ini:"-"`
|
|
// a reference to the global config
|
|
cfg *Config `ini:"-"`
|
|
// a struct that stores the repo's status
|
|
state State `ini:"-"`
|
|
}
|
|
|
|
type Config struct {
|
|
// the maximum number of jobs allowed to execute concurrently
|
|
MaxJobs int `ini:"max_jobs"`
|
|
// the default MaxTime for each repo
|
|
MaxTime int `ini:"max_time"`
|
|
// the Unix socket path which arthur will use to communicate with us
|
|
SockPath string `ini:"sock_path"`
|
|
// the path to where the state of each repo's sync is saved
|
|
StatePath string `ini:"states_path"`
|
|
// the default LogFile for each repo
|
|
LogFile string `ini:"log_file"`
|
|
// the IP addresses to use for rsync
|
|
IPv4Address string `ini:"ipv4_address"`
|
|
IPv6Address string `ini:"ipv6_address"`
|
|
// the directory where rsync passwords are stored
|
|
PasswordDir string `ini:"password_dir"`
|
|
// the directory where rsync should download files
|
|
DownloadDir string `ini:"download_dir"`
|
|
// the DoneChan for each repo (shared instance)
|
|
DoneChan <-chan string `ini:"-"`
|
|
// a list of all of the repos
|
|
Repos []*Repo `ini:"-"`
|
|
}
|
|
|
|
type State struct {
|
|
// these are stored in the states folder
|
|
// the Unix epoch timestamp at which this repo last attempted a job
|
|
LastAttemptTime int64 `ini:"last_attempt_time"`
|
|
// the number of seconds this repo ran for during its last attempted job
|
|
LastAttemptRunTime int64 `ini:"last_attempt_runtime"`
|
|
// whether the last attempt was successful or not
|
|
LastAttemptStatus int `ini:"last_attempt_status"`
|
|
// whether this repo is running a job or not
|
|
isRunning bool `ini:"is_running"`
|
|
}
|
|
|
|
// IsRunning returns true if the repo is currently running a sync job.
|
|
func (repo *Repo) IsRunning() bool {
|
|
return repo.state.isRunning
|
|
}
|
|
|
|
// RunIfScheduled starts a sync job for this repo if more than repo.Frequency
|
|
// seconds have elapsed since its last job.
|
|
// It returns true iff a job is started.
|
|
func (repo *Repo) RunIfScheduled() bool {
|
|
// sanity check; don't run if a job is already running
|
|
if repo.state.isRunning {
|
|
return false
|
|
}
|
|
if time.Now().Unix()-repo.state.LastAttemptTime > int64(repo.Frequency) {
|
|
// this should be set in the caller's thread so that the check
|
|
// above will always work
|
|
repo.state.isRunning = true
|
|
go repo.StartSyncJob()
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// GetConfig reads the config from a JSON file, initializes default values,
|
|
// and initializes the non-configurable fields of each repo.
|
|
// It returns a Config.
|
|
func GetConfig() Config {
|
|
data, err := ini.Load(CONFIG_PATH)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
cfg := Config{
|
|
MaxJobs: DEFAULT_MAX_JOBS,
|
|
MaxTime: DEFAULT_MAX_TIME,
|
|
SockPath: DEFAULT_SOCK_PATH,
|
|
StatePath: DEFAULT_STATE_PATH,
|
|
LogFile: DEFAULT_LOG_FILE,
|
|
PasswordDir: DEFAULT_PASSWORD_DIR,
|
|
DownloadDir: DEFAULT_DOWNLOAD_DIR,
|
|
Repos: make([]*Repo, 0),
|
|
}
|
|
if err := data.MapTo(&cfg); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := os.MkdirAll(cfg.StatePath, 0755); err != nil {
|
|
panic("Could not create states path at " + cfg.StatePath)
|
|
} else if cfg.IPv4Address == "" {
|
|
panic("Missing IPv4 address from config")
|
|
} else if cfg.IPv6Address == "" {
|
|
panic("Missing IPv6 address from config")
|
|
}
|
|
|
|
doneChan := make(chan string)
|
|
cfg.DoneChan = doneChan
|
|
for _, section := range data.Sections() {
|
|
if section.Name() == "DEFAULT" {
|
|
continue
|
|
}
|
|
repo := Repo{Name: section.Name()}
|
|
if err := section.MapTo(&repo); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
repo.Frequency = frequencies[repo.FrequencyStr]
|
|
if repo.MaxTime == 0 {
|
|
repo.MaxTime = cfg.MaxTime
|
|
}
|
|
repo.Logger = NewLogger(repo.Name)
|
|
repo.DoneChan = doneChan
|
|
repo.StopChan = make(chan bool, 1)
|
|
repo.cfg = &cfg
|
|
|
|
repo.state = State{
|
|
LastAttemptTime: 0,
|
|
LastAttemptRunTime: 0,
|
|
LastAttemptStatus: NOT_RUN_YET,
|
|
isRunning: false,
|
|
}
|
|
if err := ini.MapTo(&repo.state, cfg.StatePath+"/"+repo.Name); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
cfg.Repos = append(cfg.Repos, &repo)
|
|
}
|
|
if len(cfg.Repos) == 0 {
|
|
panic("No repos found in config")
|
|
}
|
|
return cfg
|
|
}
|