Class: DockerSync::SyncStrategy::Unison
- Inherits:
-
Object
- Object
- DockerSync::SyncStrategy::Unison
- Includes:
- Execution, Thor::Shell
- Defined in:
- lib/docker-sync/sync_strategy/unison.rb
Constant Summary collapse
- UNISON_CONTAINER_PORT =
'5000'.freeze
Instance Method Summary collapse
- #clean ⇒ Object
- #expand_ignore_strings ⇒ Object
- #get_container_name ⇒ Object
-
#get_host_port(container_name, _container_port) ⇒ Object
noinspection RubyUnusedLocalVariable.
- #get_volume_name ⇒ Object
- #increase_watcher_limit ⇒ Object
-
#initialize(sync_name, options) ⇒ Unison
constructor
A new instance of Unison.
- #reset_container ⇒ Object
- #run ⇒ Object
- #start_container ⇒ Object
- #stop ⇒ Object
- #stop_container ⇒ Object
- #sync ⇒ Object
- #sync_options ⇒ Object
-
#sync_prefer ⇒ Object
cares about conflict resolution.
- #watch ⇒ Object
Methods included from Execution
#fork_exec, #thread_exec, #with_time
Constructor Details
#initialize(sync_name, options) ⇒ Unison
Returns a new instance of Unison.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 18 def initialize(sync_name, ) @options = @sync_name = sync_name # if a custom image is set, apply it @docker_image = @options.key?('image') ? @options['image'] : 'ghcr.io/eugenmayer/unison:2.52.1-4.12.0' begin Dependencies::Unison.ensure! Dependencies::Unox.ensure! if Environment.mac? rescue StandardError => e say_status 'error', "#{@sync_name} has been configured to sync with unison, but no unison available", :red say_status 'error', e., :red exit 1 end end |
Instance Method Details
#clean ⇒ Object
273 274 275 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 273 def clean reset_container end |
#expand_ignore_strings ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 98 def = [] = [] exclude_type = 'Name' exclude_type = @options['sync_excludes_type'] unless @options['sync_excludes_type'].nil? # use the 'Name' exclude type for all default ignores # to prevent conflicts with the sync_excludes_type settings unless Environment.default_ignores.nil? = Environment.default_ignores.map do |pattern| "-ignore='Name #{pattern}'" end end unless @options['sync_excludes'].nil? = @options['sync_excludes'].map do |pattern| ignore_string = if exclude_type == 'none' # the ignore type like Name / Path are part of the pattern pattern.to_s else "#{exclude_type} #{pattern}" end "-ignore='#{ignore_string}'" end end .append().flatten! end |
#get_container_name ⇒ Object
255 256 257 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 255 def get_container_name @sync_name.to_s end |
#get_host_port(container_name, _container_port) ⇒ Object
noinspection RubyUnusedLocalVariable
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 243 def get_host_port(container_name, _container_port) cmd = 'docker inspect --format=\'{{(index (index .NetworkSettings.Ports "5000/tcp") 0).HostPort}}\' ' + container_name say_status 'command', cmd, :white if @options['verbose'] stdout, stderr, exit_status = Open3.capture3(cmd) unless exit_status.success? say_status 'command', cmd say_status 'error', "Error getting mapped port, exit code #{$?.exitstatus}", :red say_status 'message', stderr end stdout.gsub("\n", '') end |
#get_volume_name ⇒ Object
259 260 261 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 259 def get_volume_name @sync_name end |
#increase_watcher_limit ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 39 def increase_watcher_limit current_max_files_per_proc = `sysctl kern.maxfilesperproc | awk '{print $2}'` if current_max_files_per_proc.to_f < @options['max_inotify_watches'] cmd = 'sudo sysctl -w kern.maxfilesperproc=' + @options['max_inotify_watches'].to_s say_status 'command', cmd, :white `#{cmd}` || raise('Unable to increase maxfilesperproc') else say_status 'command', 'Current maxfilesperproc set to ' + current_max_files_per_proc.to_s, :white end current_max_files = `sysctl kern.maxfiles | awk '{print $2}'` if current_max_files.to_f < @options['max_inotify_watches'] cmd = 'sudo sysctl -w kern.maxfiles=' + @options['max_inotify_watches'].to_s say_status 'command', cmd, :white `#{cmd}` || raise('Unable to increase maxfiles') else say_status 'command', 'Current maxfiles set to ' + current_max_files.to_s, :white end end |
#reset_container ⇒ Object
267 268 269 270 271 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 267 def reset_container stop_container `docker ps -a | grep #{get_container_name} && docker rm #{get_container_name}` `docker volume ls -q | grep #{get_volume_name} && docker volume rm #{get_volume_name}` end |
#run ⇒ Object
33 34 35 36 37 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 33 def run increase_watcher_limit if @options.key?('max_inotify_watches') start_container sync end |
#start_container ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 157 def start_container say_status 'ok', "Starting unison for sync #{@sync_name}", :white container_name = get_container_name volume_name = get_volume_name env = {} if @options.key?('sync_user') raise 'sync_user is no longer supported, since it ise no needed, use sync_userid only please' end env['UNISON_SRC'] = '-socket 5000' env['UNISON_DEST'] = '/app_sync' env['MONIT_ENABLE'] = 'false' env['MONIT_INTERVAL'] = '' env['MONIT_HIGH_CPU_CYCLES'] = '' env['UNISON_ARGS'] = '' ignore_strings = env['UNISON_ARGS'] << ignore_strings.join(' ') env['UNISON_WATCH_ARGS'] = '' env['MAX_INOTIFY_WATCHES'] = @options['max_inotify_watches'] if @options.key?('max_inotify_watches') if @options['sync_userid'] == 'from_host' env['OWNER_UID'] = Process.uid else env['OWNER_UID'] = @options['sync_userid'] if @options.key?('sync_userid') end # start unison-image in unison socket mode mode env['HOSTSYNC_ENABLE'] = 0 env['UNISONSOCKET_ENABLE'] = 1 additional_docker_env = env.map { |key, value| "-e #{key}=\"#{value}\"" }.join(' ') running = `docker ps --filter 'status=running' --filter 'name=#{container_name}' --format "{{.Names}}" | grep '^#{container_name}$'` if 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 == '' run_privileged = '' if @options.key?('max_inotify_watches') run_privileged = '--privileged' end # TODO: replace by the minimum capabilities required tz_expression = '-e TZ=$(basename $(dirname `readlink /etc/localtime`))/$(basename `readlink /etc/localtime`)' say_status 'ok', 'Starting precopy', :white if @options['verbose'] # we just run the precopy script and remove the container cmd = "docker run --rm -v \"#{volume_name}:#{@options['dest']}\" -e APP_VOLUME=#{@options['dest']} #{tz_expression} #{additional_docker_env} #{run_privileged} --name #{container_name} #{@docker_image} /usr/local/bin/precopy_appsync" say_status 'precopy', cmd, :white if @options['verbose'] system(cmd) || raise('Precopy failed') say_status 'ok', "creating #{container_name} container", :white if @options['verbose'] cmd = "docker run -p '#{@options['sync_host_ip']}::#{UNISON_CONTAINER_PORT}' -v #{volume_name}:#{@options['dest']} -e APP_VOLUME=#{@options['dest']} #{tz_expression} #{additional_docker_env} #{run_privileged} --name #{container_name} -d #{@docker_image}" else say_status 'ok', "starting #{container_name} container", :white if @options['verbose'] cmd = "docker start #{container_name} && docker exec #{container_name} supervisorctl restart unison" end else say_status 'ok', "#{container_name} container still running, restarting unison in container", :blue cmd = "docker exec #{container_name} supervisorctl restart unison" end say_status 'command', cmd, :white if @options['verbose'] `#{cmd}` || raise('Start failed') say_status 'ok', "starting initial sync of #{container_name}", :white if @options['verbose'] # wait until container is started, then sync: sync_host_port = get_host_port(get_container_name, UNISON_CONTAINER_PORT) cmd = "unison -testserver \"#{@options['src']}\" \"socket://#{@options['sync_host_ip']}:#{sync_host_port}\"" say_status 'command', cmd, :white if @options['verbose'] attempt = 0 max_attempt = @options['max_attempt'] || 5 loop do # noinspection RubyUnusedLocalVariable stdout, stderr, exit_status = Open3.capture3(cmd) break if exit_status == 0 attempt += 1 if attempt > max_attempt raise "Failed to start unison container in time, try to increase max_attempt (currently #{max_attempt}) in your configuration. See https://github.com/EugenMayer/docker-sync/wiki/2.-Configuration for more informations" end sleep 1 end sync say_status 'success', 'Unison server started', :green end |
#stop ⇒ Object
277 278 279 280 281 282 283 284 285 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 277 def stop say_status 'ok', "Stopping sync container #{get_container_name}" begin stop_container rescue StandardError => e say_status 'error', "Stopping failed of #{get_container_name}:", :red puts e. end end |
#stop_container ⇒ Object
263 264 265 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 263 def stop_container `docker ps | grep #{get_container_name} && docker stop #{get_container_name} && docker wait #{get_container_name}` end |
#sync ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 69 def sync args = cmd = 'unison ' + args.join(' ') say_status 'command', cmd, :white if @options['verbose'] stdout, stderr, exit_status = Open3.capture3(cmd) if !exit_status.success? error_msg = "Error starting sync, exit code #{$?.exitstatus}" say_status 'error', error_msg, :red say_status 'message', stdout say_status 'message', stderr if @options['notify_terminal'] TerminalNotifier.notify( "#{error_msg}", :title => @sync_name, :subtitle => @options['src'], group: 'docker-sync' ) end else if @options['notify_terminal'] && @options['notify_terminal'] != 'errors_only' TerminalNotifier.notify( "Synced #{@options['src']}", title: @sync_name, group: 'docker-sync' ) end say_status 'ok', "Synced #{@options['src']}", :white say_status 'output', stdout if @options['verbose'] end end |
#sync_options ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 127 def args = [] args = + args args.push("'#{@options['src']}'") args.push('-auto') args.push('-batch') args.push(sync_prefer) args.push(@options['sync_args']) if @options.key?('sync_args') sync_host_port = get_host_port(get_container_name, UNISON_CONTAINER_PORT) args.push("socket://#{@options['sync_host_ip']}:#{sync_host_port}") if @options.key?('sync_group') || @options.key?('sync_groupid') raise('Unison does not support sync_group, sync_groupid - please use rsync if you need that') end args end |
#sync_prefer ⇒ Object
cares about conflict resolution
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 146 def sync_prefer sync_host_port = get_host_port(get_container_name, UNISON_CONTAINER_PORT) case @options.fetch('sync_prefer', 'default') when 'default' then "-prefer '#{@options['src']}' -copyonconflict" # thats our default, if nothing is set when 'src' then "-prefer '#{@options['src']}'" when 'dest' then "-prefer 'socket://#{@options['sync_host_ip']}:#{sync_host_port}'" else "-prefer '#{@options['sync_prefer']}'" end end |
#watch ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 58 def watch args = args.push('-repeat watch') cmd = '' cmd = cmd + 'ulimit -n ' + @options['max_inotify_watches'].to_s + ' && ' if @options.key?('max_inotify_watches') cmd = cmd + 'unison ' + args.join(' ') say_status 'command', cmd, :white if @options['verbose'] fork_exec(cmd, "Sync #{@sync_name}", :blue) end |