Class: Docker_Sync::SyncStrategy::Unison_Onesided
- Inherits:
-
Object
- Object
- Docker_Sync::SyncStrategy::Unison_Onesided
- Includes:
- Thor::Shell
- Defined in:
- lib/docker-sync/sync_strategy/unison-onesided.rb
Constant Summary collapse
- UNISON_CONTAINER_PORT =
'5000'
Instance Method Summary collapse
- #clean ⇒ Object
- #get_container_name ⇒ Object
- #get_volume_name ⇒ Object
-
#initialize(sync_name, options) ⇒ Unison_Onesided
constructor
A new instance of Unison_Onesided.
- #reset_container ⇒ Object
- #run ⇒ Object
- #start_container ⇒ Object
- #stop ⇒ Object
- #stop_container ⇒ Object
- #sync ⇒ Object
- #sync_options ⇒ Object
Constructor Details
#initialize(sync_name, options) ⇒ Unison_Onesided
Returns a new instance of Unison_Onesided.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 15 def initialize(sync_name, ) @sync_name = sync_name @options = # if a custom image is set, apply it if @options.key?('image') @docker_image = @options['image'] else @docker_image = 'eugenmayer/unison:onesided' end begin Preconditions::unison_available rescue Exception => 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
122 123 124 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 122 def clean reset_container end |
#get_container_name ⇒ Object
104 105 106 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 104 def get_container_name return "#{@sync_name}" end |
#get_volume_name ⇒ Object
108 109 110 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 108 def get_volume_name return @sync_name end |
#reset_container ⇒ Object
116 117 118 119 120 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 116 def reset_container stop_container `docker rm #{get_container_name}` `docker volume rm #{get_volume_name}` end |
#run ⇒ Object
33 34 35 36 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 33 def run start_container sync end |
#start_container ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 77 def start_container say_status 'ok', 'Starting unison', :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 == '' 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'] cmd = "docker run -p '#{@options['sync_host_port']}:#{UNISON_CONTAINER_PORT}' -v #{volume_name}:#{@options['dest']} -e UNISON_DIR=#{@options['dest']} -e TZ=${TZ-`readlink /etc/localtime | sed -e 's,/usr/share/zoneinfo/,,'`} --name #{container_name} -d #{@docker_image}" else 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 end say_status 'ok', "starting initial sync of #{container_name}", :white if @options['verbose'] # this sleep is needed since the container could be not started sleep 1 sync say_status 'success', 'Unison server started', :green end |
#stop ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 126 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. end end |
#stop_container ⇒ Object
112 113 114 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 112 def stop_container `docker stop #{get_container_name}` end |
#sync ⇒ Object
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/unison-onesided.rb', line 38 def sync args = cmd = 'unison ' + args.join(' ') say_status 'command', cmd, :white if @options['verbose'] stdout, stderr, exit_status = Open3.capture3(cmd) if not exit_status.success? say_status 'error', "Error starting sync, exit code #{$?.exitstatus}", :red say_status 'message', stderr 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', stdout end end end |
#sync_options ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/docker-sync/sync_strategy/unison-onesided.rb', line 59 def args = [] unless @options['sync_excludes'].nil? args = @options['sync_excludes'].map { |pattern| "-ignore='Path #{pattern}'" } + args end args.push(@options['src']) args.push('-auto') args.push('-batch') args.push(@options['sync_args']) if @options.key?('sync_args') args.push("socket://#{@options['sync_host_ip']}:#{@options['sync_host_port']}/") args.push('-debug all') if @options['verbose'] if @options.key?('sync_user') || @options.key?('sync_group') || @options.key?('sync_groupid') || @options.key?('sync_userid') raise('Unison does not support sync_user, sync_group, sync_groupid or sync_userid - please use rsync if you need that') end return args end |