mirror/merlin/common/sync.go

106 lines
2.4 KiB
Go

package common
import (
"fmt"
"os"
"time"
ini "gopkg.in/ini.v1"
)
// CSCSyncStandard performs a standard rsync job.
func (repo *Repo) CSCSyncStandard() {
startTime := time.Now().Unix()
status := FAILURE
defer func() {
repo.state.LastAttemptTime = startTime
repo.state.LastAttemptRunTime = time.Now().Unix() - startTime
repo.state.LastAttemptStatus = status
repo.state.isRunning = false
cfg := ini.Empty()
if err := ini.ReflectFrom(cfg, &repo.state); err != nil {
// log error
}
file, err := os.OpenFile(repo.cfg.StatePath+"/"+repo.Name, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
// log error
}
if _, err := cfg.WriteTo(file); err != nil {
// log error
}
repo.DoneChan <- repo.Name
}()
localDir := repo.cfg.DownloadDir + "/" + repo.LocalDir
err := os.MkdirAll(localDir, 0775)
if err != nil {
err = fmt.Errorf("Could not create directory %s: %w", localDir, err)
repo.Logger.Error(err)
return
}
rsyncHost := repo.RsyncHost
if repo.RsyncUser != "" {
rsyncHost = repo.RsyncUser + "@" + repo.RsyncHost
}
address := repo.cfg.IPv4Address
logFile := repo.LogFile
args := []string{
"nice", "rsync", "-aH", "--no-owner", "--no-group", "--delete-after",
"--delay-updates", "--safe-links", "--timeout=3600", "-4", "--address=" + address,
"--exclude", ".~tmp~/", "--quiet", "--stats", "--log-file=" + logFile,
}
if repo.PasswordFile != "" {
filename := repo.cfg.PasswordDir + "/" + repo.PasswordFile
args = append(args, "--password-file", filename)
}
args = append(args, "rsync://"+rsyncHost+"/"+repo.RsyncDir, localDir)
ch := SpawnProcess(repo, args)
if ch == nil {
return
}
cmd := <-ch
switch cmd.ProcessState.ExitCode() {
case 0:
status = SUCCESS
case -1:
status = TERMINATED
// default is already FAILURE
}
}
// StartSyncJob executes a particular sync job depending on repo.SyncType.
func (repo *Repo) StartSyncJob() {
switch repo.SyncType {
/*
# scripts used by merlin.py
csc-sync-debian
csc-sync-standard
csc-sync-ssh
csc-sync-apache
csc-sync-archlinux
csc-sync-debian-cd
csc-sync-gentoo
csc-sync-wget
csc-sync-s3
csc-sync-standard-ipv6
csc-sync-ceph
zfssync
report_mirror (what is this?)
# other things in bin/
csc-sync-archlinux-old
csc-sync-cdimage
csc-sync-chmod
csc-sync-badperms
make-torrents
ubuntu-releases-sync
*/
case "csc-sync-standard":
repo.CSCSyncStandard()
default:
repo.Logger.Error("Unrecognized sync type", "'"+repo.SyncType+"'")
}
}