Something went wrong. Try again.
osu!lazer maps/scores/settings bidirectional diff/export/sync tool
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167module Osync.Ssh
open System.Diagnosticsopen System.IO
/// Shell-escape a string for use inside an SSH command string./// Only use this for arguments passed to runRemote or writeRemoteFile,/// NOT for scp/rsync paths (those use ArgumentList, which bypasses the shell).let shellEscape (s: string) : string = "'" + s.Replace("'", "'\\''") + "'"
let private startProcess (psi: ProcessStartInfo) : Result<Process, string> = match Process.Start(psi) with | null -> Error $"Failed to start {psi.FileName}. Is it installed and in PATH?" | proc -> Ok proc
let private waitWithTimeout (proc: Process) (timeoutMs: int) (name: string) : Result<unit, string> = if proc.WaitForExit(timeoutMs) then Ok() else proc.Kill() Error $"{name} timed out after {timeoutMs / 1000}s"
let runRemote (host: string) (command: string) : Result<string, string> = let psi = ProcessStartInfo("ssh", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true)
psi.ArgumentList.Add(host) psi.ArgumentList.Add(command)
match startProcess psi with | Error e -> Error e | Ok proc -> use proc = proc let stderrTask = proc.StandardError.ReadToEndAsync() let stdout = proc.StandardOutput.ReadToEnd() let stderr = stderrTask.Result
match waitWithTimeout proc 300_000 "ssh" with | Error e -> Error e | Ok() -> match proc.ExitCode with | 0 -> Ok(stdout) | _ -> Error(stderr)
let writeRemoteFile (host: string) (remotePath: string) (content: string) : Result<unit, string> = let psi = ProcessStartInfo("ssh", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardError = true)
psi.ArgumentList.Add(host) psi.ArgumentList.Add($"cat > {shellEscape remotePath}")
match startProcess psi with | Error e -> Error e | Ok proc -> use proc = proc proc.StandardInput.Write(content) proc.StandardInput.Close()
let stderr = proc.StandardError.ReadToEnd()
match waitWithTimeout proc 60_000 "ssh write" with | Error e -> Error e | Ok() -> match proc.ExitCode with | 0 -> Ok() | _ -> Error(stderr)
let hashToPath (hash: string) : string = Path.Combine(hash.Substring(0, 1), hash.Substring(0, 2), hash)
let private scpFile (src: string) (dest: string) : Result<unit, string> = let psi = ProcessStartInfo("scp", UseShellExecute = false, RedirectStandardError = true)
psi.ArgumentList.Add(src) psi.ArgumentList.Add(dest)
match startProcess psi with | Error e -> Error e | Ok proc -> use proc = proc let stderr = proc.StandardError.ReadToEnd()
match waitWithTimeout proc 300_000 "scp" with | Error e -> Error e | Ok() -> match proc.ExitCode with | 0 -> Ok() | _ -> Error $"scp failed: {stderr}"
let copyRealmToTemp (host: string option) (realmPath: string) : Result<string, string> = let tempPath = Path.Combine(Path.GetTempPath(), $"osync-realm-{Path.GetRandomFileName()}")
match host with | None -> try File.Copy(realmPath, tempPath) Ok tempPath with ex -> Error $"Failed to copy realm: {ex.Message}" | Some hostname -> match scpFile $"{hostname}:{realmPath}" tempPath with | Ok() -> Ok tempPath | Error e -> Error e
let scpToRemote (localPath: string) (hostname: string) (remotePath: string) : Result<string, string> = match scpFile localPath $"{hostname}:{remotePath}" with | Ok() -> Ok remotePath | Error e -> Error e
let rsyncFiles (srcHost: string option) (destHost: string option) (srcDataPath: string) (destDataPath: string) (hashes: string seq) : Result<unit, string> = if Seq.isEmpty hashes then Ok() else if srcHost.IsSome && destHost.IsSome then Error "rsync does not support both source and destination being remote. Run osync on one of the machines instead." else let listFile = Path.Combine(Path.GetTempPath(), $"osync-rsync-{Path.GetRandomFileName()}")
try File.WriteAllLines(listFile, hashes |> Seq.map hashToPath)
let srcFilesDir = Path.Combine(srcDataPath, "files") let destFilesDir = Path.Combine(destDataPath, "files")
// Use --rsync-path to cd on the remote side so the remote path is // just ".". This avoids spaces in remote paths (e.g. macOS // "Application Support") which rsync cannot handle portably. let (srcFiles, destFiles, extraArgs) = match srcHost, destHost with | Some hostname, None -> let rsyncPath = $"cd {shellEscape srcFilesDir} && rsync" ($"{hostname}:.", destFilesDir + "/", [ "--rsync-path"; rsyncPath ]) | None, Some hostname -> let rsyncPath = $"cd {shellEscape destFilesDir} && rsync" (srcFilesDir + "/", $"{hostname}:.", [ "--rsync-path"; rsyncPath ]) | _ -> (srcFilesDir + "/", destFilesDir + "/", [])
let psi = ProcessStartInfo("rsync", UseShellExecute = false)
for arg in [ "-a"; "--info=progress2" ] @ extraArgs @ [ "--files-from"; listFile; srcFiles; destFiles ] do psi.ArgumentList.Add(arg)
match startProcess psi with | Error e -> Error e | Ok proc -> use proc = proc proc.WaitForExit()
match proc.ExitCode with | 0 -> Ok() | _ -> Error "rsync failed (see output above)" finally if File.Exists(listFile) then File.Delete(listFile)