Class: Docksync::Rsync::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/docksync/rsync/sync.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sync

Returns a new instance of Sync.



4
5
6
7
# File 'lib/docksync/rsync/sync.rb', line 4

def initialize(options)
  @options = options
  @cid = @options[:cid]
end

Instance Method Details

#commandObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/docksync/rsync/sync.rb', line 46

def command
  # --numeric-ids               don't map uid/gid values by user/group name
  # --safe-links                ignore symlinks that point outside the tree
  # -a, --archive               recursion and preserve almost everything (-rlptgoD)
  # -x, --one-file-system       don't cross filesystem boundaries
  # -z, --compress              compress file data during the transfer
  # -S, --sparse                handle sparse files efficiently
  # -v, --verbose               verbose
  exclude = %w/.git tmp log/
  exclude += get_excludes('.gitignore')
  exclude += get_excludes('.dockerignore')
  exclude = exclude.uniq.map{|path| "--exclude='#{path}'"}.join(' ')
  options = "--delete --numeric-ids --safe-links -axzSv #{exclude}"
  src = get_src
  dest = "rsync://#{dockerhost}:#{dockerport}/volume/"

  "rsync #{options} #{src} #{dest}"
end

#docker_ps_for_portObject



16
17
18
# File 'lib/docksync/rsync/sync.rb', line 16

def docker_ps_for_port
  `docker ps | grep #{@cid}`
end

#dockerhostObject



9
10
11
12
13
14
# File 'lib/docksync/rsync/sync.rb', line 9

def dockerhost
  host = `boot2docker ip 2>&1 | grep address | awk '{print $NF}'`.strip # old version of boot2docker
  host = `boot2docker ip`.strip if host.empty?
  host = '192.168.59.103' if host.empty?
  host
end

#dockerportObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/docksync/rsync/sync.rb', line 20

def dockerport
  out = docker_ps_for_port
  md = out.match(/0\.0\.0\.0\:(\d+)->873/)
  if md
    md[1]
  else
    msg = "Could not find expose port 873.  Are you sure you exposed port 837 for #{@cid} container?".colorize(:red)
    if @options[:mute]
      return msg
    else
      puts msg
      leave
    end
  end
end

#get_excludes(file) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/docksync/rsync/sync.rb', line 65

def get_excludes(file)
  exclude = []
  path = "#{@options[:cwd]}/#{file}"
  if File.exist?(path)
    exclude = File.read(path).split("\n")
  end
  result = exclude.map {|i| i.strip}.reject {|i| i =~ /^#/ || i.empty?}
  result
end

#get_srcObject



75
76
77
78
# File 'lib/docksync/rsync/sync.rb', line 75

def get_src
  src = @options[:cwd] || '.'
  src[-1] == '/' ? src : "#{src}/"
end

#leaveObject



36
37
38
# File 'lib/docksync/rsync/sync.rb', line 36

def leave
  exit 0
end

#runObject



40
41
42
43
44
# File 'lib/docksync/rsync/sync.rb', line 40

def run
  puts "Executing: #{command}"
  system(command)
  raise "rsync execution returned failure" if ($?.exitstatus != 0)
end