lib/docker-sync/sync_strategy/unison.rb in docker-sync-0.4.6 vs lib/docker-sync/sync_strategy/unison.rb in docker-sync-0.5.0.pre.beta1
- old
+ new
@@ -1,13 +1,12 @@
require 'thor/shell'
-require 'docker-sync/preconditions/strategy'
require 'docker-sync/execution'
require 'open3'
require 'socket'
require 'terminal-notifier'
-module Docker_Sync
+module DockerSync
module SyncStrategy
class Unison
include Thor::Shell
include Execution
@@ -24,12 +23,12 @@
@docker_image = @options['image']
else
@docker_image = 'eugenmayer/unison'
end
begin
- DockerSync::Preconditions::Strategy.instance.unison_available
- rescue Exception => e
+ Dependencies::Unison.ensure!
+ rescue StandardError => e
say_status 'error', "#{@sync_name} has been configured to sync with unison, but no unison available", :red
say_status 'error', e.message, :red
exit 1
end
end
@@ -65,11 +64,11 @@
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']
- forkexec(cmd, "Sync #{@sync_name}", :blue)
+ fork_exec(cmd, "Sync #{@sync_name}", :blue)
end
def sync
args = sync_options
cmd = 'unison ' + args.join(' ')
@@ -133,13 +132,13 @@
# cares about conflict resolution
def sync_prefer
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 #{@options['dest']}"
- else "-prefer #{@options['sync_prefer']}"
+ when 'src' then "-prefer '#{@options['src']}'"
+ when 'dest' then "-prefer '#{@options['dest']}'"
+ else "-prefer '#{@options['sync_prefer']}'"
end
end
def start_container
say_status 'ok', "Starting unison for sync #{@sync_name}", :white
@@ -163,10 +162,11 @@
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 == ''
say_status 'ok', "creating #{container_name} container", :white if @options['verbose']
+ run_privileged = ''
run_privileged = '--privileged' if @options.key?('max_inotify_watches') #TODO: replace by the minimum capabilities required
cmd = "docker run -p '#{@options['sync_host_ip']}::#{UNISON_CONTAINER_PORT}' -v #{volume_name}:#{@options['dest']} -e VOLUME=#{@options['dest']} -e TZ=${TZ-`readlink /etc/localtime | sed -e 's,/usr/share/zoneinfo/,,'`} #{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"
@@ -183,26 +183,27 @@
cmd = "unison -testserver #{@options['dest']} \"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
- raise 'Failed to start unison container in time, try to increase max_attempt in your configuration. See https://github.com/EugenMayer/docker-sync/wiki/2.-Configuration for more informations' 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" if attempt > max_attempt
sleep 1
end
sync
say_status 'success', 'Unison server started', :green
end
+ # noinspection RubyUnusedLocalVariable
def get_host_port(container_name, container_port)
- File.exist?('/usr/bin/sed') ? sed = '/usr/bin/sed' : sed = `which sed`.chomp # use macOS native sed in /usr/bin/sed first, fallback to sed in $PATH if it's not there
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)
- if not exit_status.success?
+ unless exit_status.success?
say_status 'command', cmd
say_status 'error', "Error getting mapped port, exit code #{$?.exitstatus}", :red
say_status 'message', stderr
end
return stdout.gsub("\n",'')
@@ -232,10 +233,10 @@
def stop
say_status 'ok', "Stopping sync container #{get_container_name}"
begin
stop_container
- rescue Exception => e
+ rescue StandardError => e
say_status 'error', "Stopping failed of #{get_container_name}:", :red
puts e.message
end
end
end