mirror/merlin/sync/command.go

247 lines
6.7 KiB
Go
Raw Normal View History

2021-12-11 18:28:09 -05:00
package sync
2021-10-23 23:14:41 -04:00
import (
"fmt"
2021-11-14 17:22:32 -05:00
"math/rand"
2021-11-11 22:27:48 -05:00
"strconv"
2021-12-11 18:28:09 -05:00
"git.csclub.uwaterloo.ca/public/merlin/config"
2021-10-23 23:14:41 -04:00
)
2021-12-11 18:28:09 -05:00
func buildRsyncHost(repo *config.Repo) string {
2021-10-29 00:41:01 -04:00
if repo.RsyncUser != "" {
repo.RsyncHost = repo.RsyncUser + "@" + repo.RsyncHost
}
return "rsync://" + repo.RsyncHost + "/" + repo.RsyncDir
2021-10-29 00:41:01 -04:00
}
2021-12-11 18:28:09 -05:00
func buildRsyncDaemonHost(repo *config.Repo) string {
2021-11-06 23:07:59 -04:00
if repo.RsyncUser != "" {
repo.RsyncHost = repo.RsyncUser + "@" + repo.RsyncHost
2021-10-23 23:14:41 -04:00
}
2021-11-06 23:07:59 -04:00
return repo.RsyncHost + "::" + repo.RsyncDir
}
2021-10-23 23:14:41 -04:00
2021-12-11 18:28:09 -05:00
func buildRsyncSSHHost(repo *config.Repo) string {
2021-11-11 22:27:48 -05:00
if repo.RsyncUser != "" {
repo.RsyncHost = repo.RsyncUser + "@" + repo.RsyncHost
}
return repo.RsyncHost + ":" + repo.RsyncDir
}
2021-12-11 18:28:09 -05:00
func buildDownloadDir(repo *config.Repo) string {
return config.Conf.DownloadDir + "/" + repo.LocalDir
2021-11-06 23:07:59 -04:00
}
2021-12-11 18:28:09 -05:00
func cscSyncApache(repo *config.Repo) []string {
2021-10-23 23:14:41 -04:00
args := []string{
2021-11-06 23:07:59 -04:00
"nice", "rsync", "-az", "--no-owner", "--no-group", "--delete", "--safe-links",
2021-12-11 18:28:09 -05:00
"--timeout=3600", "-4", "--address=" + config.Conf.IPv4Address,
2021-11-06 23:07:59 -04:00
"--exclude", ".~tmp~/",
2021-11-20 01:04:45 -05:00
"--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
2021-10-23 23:14:41 -04:00
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncDaemonHost(repo), buildDownloadDir(repo))
2021-10-23 23:14:41 -04:00
2021-11-06 23:07:59 -04:00
return args
2021-10-23 23:14:41 -04:00
}
2021-12-11 18:28:09 -05:00
func cscSyncArchLinux(repo *config.Repo) []string {
2021-10-29 00:41:01 -04:00
tempDir := "" // is this option even needed?
// add option for verbose flag?
args := []string{
"rsync", "-rtlH", "--safe-links", "--delete-after", "--timeout=600",
"--contimeout=60", "-p", "--delay-updates", "--no-motd",
2021-12-11 18:28:09 -05:00
"--temp-dir=" + tempDir, "--log-file=" + repo.RsyncLogFile, "--address=" + config.Conf.IPv4Address,
2021-10-29 00:41:01 -04:00
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncHost(repo), buildDownloadDir(repo))
2021-10-29 00:41:01 -04:00
2021-11-06 23:07:59 -04:00
return args
}
2021-12-11 18:28:09 -05:00
func cscSyncBadPerms(repo *config.Repo) []string {
2021-11-06 23:07:59 -04:00
args := []string{
"nice", "rsync", "-aH", "--no-owner", "--no-group", "--chmod=o=rX", "--delete",
2021-12-11 18:28:09 -05:00
"--timeout=3600", "-4", "--address=" + config.Conf.IPv4Address,
2021-11-06 23:07:59 -04:00
"--exclude", ".~tmp~/",
2021-11-20 01:04:45 -05:00
"--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncDaemonHost(repo), buildDownloadDir(repo))
2021-10-29 00:41:01 -04:00
2021-11-06 23:07:59 -04:00
return args
2021-11-07 02:15:11 -05:00
}
2021-11-06 23:07:59 -04:00
2021-11-11 22:27:48 -05:00
// TODO ceph
2021-12-11 18:28:09 -05:00
func cscSyncCDImage(repo *config.Repo) []string {
2021-11-06 23:07:59 -04:00
args := []string{
"nice", "rsync", "-aH", "--no-owner", "--no-group", "--delete",
2021-12-11 18:28:09 -05:00
"--timeout=3600", "-4", "--address=" + config.Conf.IPv4Address,
2021-11-20 01:04:45 -05:00
"--exclude", ".*/", "--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
2021-10-29 00:41:01 -04:00
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncDaemonHost(repo), buildDownloadDir(repo))
2021-10-29 00:41:01 -04:00
2021-11-06 23:07:59 -04:00
return args
2021-10-29 00:41:01 -04:00
}
2021-12-11 18:28:09 -05:00
func cscSyncChmod(repo *config.Repo) []string {
2021-11-06 23:07:59 -04:00
args := []string{
"nice", "rsync", "-aH", "--no-owner", "--no-group", "--delete-after", "--delay-updates", "--safe-links",
2021-12-11 18:28:09 -05:00
"--timeout=3600", "-4", "--address=" + config.Conf.IPv4Address,
2021-11-20 01:04:45 -05:00
"--exclude", ".~tmp~/", "--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
2021-11-06 23:07:59 -04:00
"--chmod=u=rwX,go=rX",
2021-10-23 23:14:41 -04:00
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncHost(repo), buildDownloadDir(repo))
2021-11-06 23:07:59 -04:00
return args
2021-11-07 02:15:11 -05:00
}
2021-11-06 22:05:10 -04:00
2021-12-11 18:28:09 -05:00
func cscSyncDebian(repo *config.Repo) []string {
2021-11-06 22:05:10 -04:00
// sync /pool
args := []string{"nice", "rsync", "-rlHtvp",
"--exclude", ".~tmp~/", "--timeout=3600", "-4",
2021-12-11 18:28:09 -05:00
"--address=" + config.Conf.IPv4Address,
}
2021-11-07 02:15:11 -05:00
// $RSYNC_HOST::$RSYNC_DIR/pool/ $TO/pool/ >> $LOGFILE 2>&1
2021-11-06 23:07:59 -04:00
return args
2021-11-06 22:05:10 -04:00
}
2021-12-11 18:28:09 -05:00
func cscSyncDebianCD(repo *config.Repo) []string {
2021-11-06 22:05:10 -04:00
// this is basically the same as CSCSyncDebian, except it has an extra --exclude
args := []string{"nice", "rsync", "-rlHtvp", "--delete",
"--exclude", ".~tmp~/", "--timeout=3600", "-4",
2021-12-11 18:28:09 -05:00
"--address=" + config.Conf.IPv4Address,
2021-11-06 22:05:10 -04:00
// "--exclude", "Archive-Update-in-Progress-${HOSTNAME}"
}
2021-11-07 02:15:11 -05:00
// $RSYNC_HOST::$RSYNC_DIR $TO >> $LOGFILE 2>&1
2021-11-06 23:07:59 -04:00
return args
2021-11-06 22:05:10 -04:00
}
2021-12-11 18:28:09 -05:00
func cscSyncGentoo(repo *config.Repo) []string {
2021-11-06 23:07:59 -04:00
repo.RsyncUser = "gentoo"
repo.PasswordFile = "gentoo-distfiles"
2021-12-11 18:28:09 -05:00
return cscSyncStandard(repo)
2021-11-06 23:07:59 -04:00
}
2021-10-29 00:41:01 -04:00
2021-11-11 22:27:48 -05:00
// TODO s3
2021-12-11 18:28:09 -05:00
func cscSyncSSH(repo *config.Repo) []string {
2021-11-11 22:27:48 -05:00
args := []string{
"rsync", "-aH", "--no-owner", "--no-group", "--delete",
"--timeout=3600", "-4",
"--exclude", ".~tmp~/",
2021-11-20 01:04:45 -05:00
"--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
2021-11-11 22:27:48 -05:00
}
// not sure if we should be assuming ssh identity file is the password file
args = append(args, "-e", fmt.Sprintf("'ssh -i %s'", repo.PasswordFile))
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncSSHHost(repo), buildDownloadDir(repo))
2021-11-11 22:27:48 -05:00
return args
}
2021-12-11 18:28:09 -05:00
func cscSyncStandard(repo *config.Repo) []string {
2021-10-29 00:41:01 -04:00
args := []string{
2021-11-06 23:07:59 -04:00
"nice", "rsync", "-aH", "--no-owner", "--no-group", "--delete-after",
2021-12-11 18:28:09 -05:00
"--delay-updates", "--safe-links", "--timeout=3600", "-4", "--address=" + config.Conf.IPv4Address,
2021-11-20 01:04:45 -05:00
"--exclude", ".~tmp~/", "--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
}
2021-11-06 23:07:59 -04:00
if repo.PasswordFile != "" {
2021-12-11 18:28:09 -05:00
filename := config.Conf.PasswordDir + "/" + repo.PasswordFile
2021-11-06 23:07:59 -04:00
args = append(args, "--password-file", filename)
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncHost(repo), buildDownloadDir(repo))
2021-10-29 00:41:01 -04:00
2021-11-06 23:07:59 -04:00
return args
}
2021-12-11 18:28:09 -05:00
func cscSyncStandardIPV6(repo *config.Repo) []string {
2021-11-06 23:07:59 -04:00
args := []string{
"nice", "rsync", "-aH", "--no-owner", "--no-group", "--delete-after",
2021-12-11 18:28:09 -05:00
"--delay-updates", "--safe-links", "--timeout=3600", "-6", "--address=" + config.Conf.IPv4Address,
2021-11-20 01:04:45 -05:00
"--exclude", ".~tmp~/", "--quiet", "--stats", "--log-file=" + repo.RsyncLogFile,
2021-10-29 00:41:01 -04:00
}
2021-12-11 18:28:09 -05:00
args = append(args, buildRsyncDaemonHost(repo), buildDownloadDir(repo))
2021-10-29 00:41:01 -04:00
2021-11-06 23:07:59 -04:00
return args
2021-10-29 00:41:01 -04:00
}
2021-11-14 17:22:32 -05:00
// for testing, to be removed later
2021-12-11 18:28:09 -05:00
func cscSyncDummy(repo *config.Repo) []string {
2021-11-14 17:22:32 -05:00
sleepDur := strconv.FormatInt(rand.Int63n(10)+5, 10)
args := []string{"sleep", sleepDur}
return args
}
2021-11-11 22:27:48 -05:00
2021-11-06 23:07:59 -04:00
// executes a particular sync job depending on repo.SyncType.
2021-12-11 18:28:09 -05:00
func getSyncCommand(repo *config.Repo) []string {
2021-10-23 23:14:41 -04:00
/*
# 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
*/
2021-11-11 22:27:48 -05:00
switch repo.SyncType {
2021-11-14 17:22:32 -05:00
case "csc-sync-apache":
2021-12-11 18:28:09 -05:00
return cscSyncApache(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-archlinux":
2021-12-11 18:28:09 -05:00
return cscSyncArchLinux(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-badperms":
2021-12-11 18:28:09 -05:00
return cscSyncBadPerms(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-cdimage":
2021-12-11 18:28:09 -05:00
return cscSyncCDImage(repo)
2021-11-14 17:22:32 -05:00
// case "csc-sync-ceph":
2021-12-11 18:28:09 -05:00
// return cscSyncCeph(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-chmod":
2021-12-11 18:28:09 -05:00
return cscSyncChmod(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-debian":
2021-12-11 18:28:09 -05:00
return cscSyncDebian(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-debian-cd":
2021-12-11 18:28:09 -05:00
return cscSyncDebianCD(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-gentoo":
2021-12-11 18:28:09 -05:00
return cscSyncGentoo(repo)
2021-11-14 17:22:32 -05:00
// case "csc-sync-s3":
2021-12-11 18:28:09 -05:00
// return cscSyncS3(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-ssh":
2021-12-11 18:28:09 -05:00
return cscSyncSSH(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-standard":
2021-12-11 18:28:09 -05:00
return cscSyncStandard(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-standard-ipv6":
2021-12-11 18:28:09 -05:00
return cscSyncStandardIPV6(repo)
2021-11-11 22:27:48 -05:00
// case "csc-sync-wget":
2021-12-11 18:28:09 -05:00
// return cscSyncWget(repo)
2021-11-14 17:22:32 -05:00
case "csc-sync-dummy":
2021-12-11 18:28:09 -05:00
return cscSyncDummy(repo)
2021-10-23 23:14:41 -04:00
default:
repo.Logger.Error("Unrecognized sync type", "'"+repo.SyncType+"'")
2021-11-06 23:07:59 -04:00
}
return []string{}
}