Class: AwsRsync::Sync

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sync

Returns a new instance of Sync.



3
4
5
# File 'lib/aws_rsync/sync.rb', line 3

def initialize(options)
  @options = options
end

Instance Method Details

#build_exclude_optionsObject

The rsync exclude options are automatically calculated and added with some sane defaults. You can override the options with the environment variable AWS_RSYNC_EXCLUDE. To have no exclude options at all use a blank string: AWS_RSYNC_EXCLUDE=”



49
50
51
52
53
54
55
56
57
# File 'lib/aws_rsync/sync.rb', line 49

def build_exclude_options
  return ENV['AWS_RSYNC_EXCLUDE'] if ENV['AWS_RSYNC_EXCLUDE']

  exclude = %w/.git tmp log/ # default values
  exclude += get_excludes('.gitignore')
  exclude += get_excludes('.dockerignore')
  exclude = exclude.uniq.map{|path| "--exclude='#{path}'"}.join(' ')
  exclude
end

#build_main_optionsObject

A default set of options is provided. You can override this with the environment variable AWS_RSYNC_OPTIONS.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/aws_rsync/sync.rb', line 30

def build_main_options
  return ENV['AWS_RSYNC_OPTIONS'] if ENV['AWS_RSYNC_OPTIONS']

  # --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
  #     --delete                delete extraneous files from destination dirs
  #
  "--delete --numeric-ids --safe-links -axzSv"
end

#commandObject



18
19
20
21
22
23
24
25
# File 'lib/aws_rsync/sync.rb', line 18

def command
  main_options = build_main_options
  exclude = build_exclude_options
  options = "#{main_options} #{exclude}"
  dest = "#{user}@#{host.ip}:#{folder}"

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

#folderObject

destination folder



68
69
70
71
# File 'lib/aws_rsync/sync.rb', line 68

def folder
  src = src == '.' ? Dir.pwd : src
  @options[:folder] || File.basename(src)
end

#get_excludes(file) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/aws_rsync/sync.rb', line 78

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

#hostObject



63
64
65
# File 'lib/aws_rsync/sync.rb', line 63

def host
  @host ||= Host.new(@options)
end

#note_timeObject



88
89
90
# File 'lib/aws_rsync/sync.rb', line 88

def note_time
  puts "Last synced at: #{Time.now}".colorize(:green)
end

#runObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/aws_rsync/sync.rb', line 7

def run
  puts "=> #{command}".colorize(:green)
  return if @options[:noop]
  success = system(command)
  unless success
    puts "ERROR: rsync command failed".colorize(:red)
    exit 1
  end
  note_time
end

#srcObject



73
74
75
76
# File 'lib/aws_rsync/sync.rb', line 73

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

#userObject



59
60
61
# File 'lib/aws_rsync/sync.rb', line 59

def user
  @options[:user] || "ec2-user"
end