Class: Docker_Sync::SyncStrategy::Rsync

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/docker-sync/sync_strategy/rsync.rb

Instance Method Summary collapse

Constructor Details

#initialize(sync_name, options) ⇒ Rsync

Returns a new instance of Rsync.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 14

def initialize(sync_name, options)
  @sync_name = sync_name
  @options = options
  # if a custom image is set, apply it
  if @options.key?('image')
    @docker_image = @options['image']
  else
    @docker_image = 'eugenmayer/rsync'
  end

  begin
    Preconditions::rsync_available
  rescue Exception => e
    say_status 'error', "#{@sync_name} has been configured to sync with rsync, but no rsync binary available", :red
    say_status 'error', e.message, :red
    exit 1
  end
end

Instance Method Details

#cleanObject



153
154
155
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 153

def clean
  reset_container
end

#get_container_nameObject



75
76
77
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 75

def get_container_name
  return "#{@sync_name}"
end

#get_group_mappingObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 130

def get_group_mapping
  group_mapping = ''
  if @options.key?('sync_group')
    group_mapping = "-e GROUP=#{@options['sync_group']}"
    if @options.key?('sync_groupid')
      group_mapping = "#{group_mapping} -e GROUPID=#{@options['sync_groupid']}"
    end
  elsif @options.key?('sync_groupid')
    raise("#{get_container_name}: You have set a sync_groupid but no sync_group - you need to set both")
  end
  return group_mapping
end

#get_user_mappingObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 117

def get_user_mapping
  user_mapping = ''
  if @options.key?('sync_user')
    user_mapping = "-e OWNER=#{@options['sync_user']}"
    if @options.key?('sync_userid')
      user_mapping = "#{user_mapping} -e OWNERID=#{@options['sync_userid']}"
    end
  elsif @options.key?('sync_userid')
    raise("#{get_container_name}: You have set a sync_userid but no sync_user - you need to set both")
  end
  return user_mapping
end

#get_volume_nameObject



79
80
81
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 79

def get_volume_name
  return @sync_name
end

#reset_containerObject



147
148
149
150
151
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 147

def reset_container
  stop_container
  `docker rm #{get_container_name}`
  `docker volume rm #{get_volume_name}`
end

#runObject



33
34
35
36
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 33

def run
  start_container
  sync
end

#start_containerObject

starts a rsync docker container listening on the specific port this container exposes a named volume and is on one side used as the rsync-endpoint for the local rsync command, on the other side the volume is mounted into the app-container to share the code / content



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 86

def start_container
  say_status 'ok', 'Starting rsync', :white
  container_name = get_container_name
  volume_name = get_volume_name
  running = `docker ps --filter 'status=running' --filter 'name=#{container_name}' --format "{{.Names}}" | grep '^#{container_name}$'`
  if running == '' # container is yet not running
    say_status 'ok', "#{container_name} container not running", :white if @options['verbose']
    exists = `docker ps --filter "status=exited" --filter "name=#{container_name}" --format "{{.Names}}" | grep '^#{container_name}$'`
    if exists == '' # container has yet not been created
      say_status 'ok', "creating #{container_name} container", :white if @options['verbose']

      user_mapping = get_user_mapping
      group_mapping = get_group_mapping

      cmd = "docker run -p '#{@options['sync_host_port']}:873' -v #{volume_name}:#{@options['dest']} #{user_mapping} #{group_mapping} -e VOLUME=#{@options['dest']} -e TZ=${TZ-`readlink /etc/localtime | sed -e 's,/usr/share/zoneinfo/,,'`} --name #{container_name} -d #{@docker_image}"
    else # container already created, just start / reuse it
      say_status 'ok', "starting #{container_name} container", :white if @options['verbose']
      cmd = "docker start #{container_name}"
    end
    say_status 'command', cmd, :white if @options['verbose']
    `#{cmd}` || raise('Start failed')
  else
    say_status 'ok', "#{container_name} container still running", :blue if @options['verbose']
  end
  say_status 'ok', "#{container_name}: starting initial sync of #{@options['src']}", :white if @options['verbose']
  # this sleep is needed since the container could be not started
  sleep 1
  sync
  say_status 'success', 'Rsync server started', :green
end

#stopObject



157
158
159
160
161
162
163
164
165
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 157

def stop
  say_status 'ok', "Stopping sync container #{get_container_name}"
  begin
    stop_container
  rescue Exception => e
    say_status 'error', "Stopping failed of #{get_container_name}:", :red
    puts e.message
  end
end

#stop_containerObject



143
144
145
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 143

def stop_container
  `docker stop #{get_container_name}`
end

#syncObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 38

def sync
  args = sync_options
  cmd = 'rsync ' + args.join(' ')

  say_status 'command', cmd, :white if @options['verbose']

  out = `#{cmd}`
  if $?.exitstatus > 0
    say_status 'error', "Error starting sync, exit code #{$?.exitstatus}", :red
    say_status 'message', out
  else
    TerminalNotifier.notify(
      "Synced #{@options['src']}", :title => @sync_name
    ) if @options['notify_terminal']
    say_status 'ok', "Synced #{@options['src']}", :white
    if @options['verbose']
      say_status 'output', out
    end
  end
end

#sync_optionsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/docker-sync/sync_strategy/rsync.rb', line 59

def sync_options
  args = []
  unless @options['sync_excludes'].nil?
    args = @options['sync_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
  end
  args.push('-ap')
  args.push(@options['sync_args']) if @options.key?('sync_args')
  # we do not need to user usermap/groupmap since we started our container the way that it also maps user/group like we defined
  # in the config - see start_container
  #args.push("--usermap='*:#{@options['sync_user']}'") if @options.key?('sync_user')
  #args.push("--groupmap='*:#{@options['sync_group']}'") if @options.key?('sync_group')
  args.push("#{@options['src']}/") # add a trailing slash
  args.push("rsync://#{@options['sync_host_ip']}:#{@options['sync_host_port']}/volume")
  return args
end