Module: Twin::Sync

Defined in:
lib/twin/sync.rb

Class Method Summary collapse

Class Method Details

.mounted?(path) ⇒ Boolean

True if the path lives on a mounted volume other than the root filesystem. Walks up parents until it finds a mount point (different device than parent) or hits “/” (path is on the root volume, not externally mounted).

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/twin/sync.rb', line 10

def mounted?(path)
  return false unless File.exist?(path)
  current = File.expand_path(path)
  until current == "/"
    parent = File.expand_path("..", current)
    return true if File.stat(current).dev != File.stat(parent).dev
    current = parent
  end
  false
rescue Errno::ENOENT
  false
end

.run(args) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/twin/sync.rb', line 59

def run(args)
  require "open3"
  stdout, stderr, status = Open3.capture3(*args)
  [stdout + stderr, status]
rescue Errno::ENOENT
  raise "command not found: #{args.first}"
end

.run_job(cfg, job, dry_run: false) ⇒ Object

Sync one Job. Returns [success, combined_output].



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/twin/sync.rb', line 24

def run_job(cfg, job, dry_run: false)
  src = job.source_path
  tgt = job.target_path

  return [false, "source not found: #{src}"] unless File.exist?(src)

  FileUtils.mkdir_p(File.dirname(tgt))

  args = ["rsync", "-av", "--update"]
  args << "--dry-run" if dry_run
  cfg.global_excludes.each { |ex| args << "--exclude=#{ex}" }
  job.excludes.each       { |ex| args << "--exclude=#{ex}" }

  if File.directory?(src)
    args << "#{src}/" << "#{tgt}/"
  else
    args << src << tgt
  end

  output, status = run(args)
  return [false, output] unless status.success?

  if !job.cmd.empty? && !dry_run
    cmd_out, _ = run(["sh", "-c", job.cmd])
    output += "\ncmd: #{job.cmd}\n#{cmd_out}"
  end

  [true, output]
end

.run_program(cfg, program, dry_run: false) ⇒ Object

Sync all jobs in a Program. Returns array of [job, success, output].



55
56
57
# File 'lib/twin/sync.rb', line 55

def run_program(cfg, program, dry_run: false)
  program.active_jobs.map { |job| [job, *run_job(cfg, job, dry_run: dry_run)] }
end