Class: Docker_Sync::SyncStrategy::Unison_DualSide
- Inherits:
-
Object
- Object
- Docker_Sync::SyncStrategy::Unison_DualSide
- Includes:
- Thor::Shell
- Defined in:
- lib/docker-sync/sync_strategy/unison-dualside.rb
Constant Summary collapse
- UNISON_CONTAINER_PORT =
'5000'
Instance Method Summary collapse
- #clean ⇒ Object
- #get_container_name ⇒ Object
-
#get_host_ip ⇒ Object
Try to guess the IP of the host to call the unison server from the container Or use the value specified in the config option.
- #get_volume_name ⇒ Object
-
#initialize(sync_name, options) ⇒ Unison_DualSide
constructor
A new instance of Unison_DualSide.
- #reset_container ⇒ Object
- #run ⇒ Object
- #start_container ⇒ Object
-
#start_local_server ⇒ Object
Start an unison server on the host.
- #stop ⇒ Object
- #stop_container ⇒ Object
-
#stop_local_server ⇒ Object
Kill the local unison server.
- #sync ⇒ Object
- #sync_options ⇒ Object
Constructor Details
#initialize(sync_name, options) ⇒ Unison_DualSide
Returns a new instance of Unison_DualSide.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 16 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:dualside' 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
183 184 185 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 183 def clean reset_container end |
#get_container_name ⇒ Object
165 166 167 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 165 def get_container_name return "#{@sync_name}" end |
#get_host_ip ⇒ Object
Try to guess the IP of the host to call the unison server from the container Or use the value specified in the config option
150 151 152 153 154 155 156 157 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 150 def get_host_ip if @options['unison-dualside_host_ip'] != 'auto' && @options.key?('unison-dualside_host_ip') host_ip = @options['host_ip'] elsif @options['unison-dualside_host_ip'] == 'auto' || (not @options.key?('unison-dualside_host_ip')) host_ip = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address end return host_ip end |
#get_volume_name ⇒ Object
169 170 171 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 169 def get_volume_name return @sync_name end |
#reset_container ⇒ Object
177 178 179 180 181 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 177 def reset_container stop_container `docker rm #{get_container_name}` `docker volume rm #{get_volume_name}` end |
#run ⇒ Object
34 35 36 37 38 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 34 def run start_container start_local_server sync end |
#start_container ⇒ Object
81 82 83 84 85 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 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 81 def start_container say_status 'ok', 'Starting unison', :white container_name = get_container_name volume_name = get_volume_name env = {} env['ENABLE_WATCH'] = 'true' # Excludes for fswatch and unison uses the same value both in the host and in the container env['FSWATCH_EXCLUDES'] = @options['watch_excludes'].map { |pattern| "--exclude='#{pattern}'" }.join(' ') if @options.key?('watch_excludes') env['UNISON_EXCLUDES'] = @options['sync_excludes'].map { |pattern| "-ignore='Path #{pattern}'" }.join(' ') if @options.key?('sync_excludes') # Specify the IP of the host to call the host unison server from the container env['HOST_IP'] = get_host_ip env['UNISON_HOST_PORT'] = @options['unison-dualside_host_server_port'] if @options['sync_userid'] == 'from_host' env['UNISON_DIR_OWNER'] = Process.uid end additional_docker_env = env.map{ |key,value| "-e #{key}=\"#{value}\"" }.join(' ') running = `docker ps --filter 'status=running' --filter 'name=#{container_name}' | 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}" | 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']} \ #{additional_docker_env} \ --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 #{container_name} of src", :white if @options['verbose'] # this sleep is needed since the container could be not started sleep 5 # TODO: replace with unison -testserver sync say_status 'success', 'Unison server started', :green end |
#start_local_server ⇒ Object
Start an unison server on the host
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 129 def start_local_server say_status 'ok', 'Starting local unison server', :white if not @options.key?('unison-dualside_host_server_port') raise('Missing unison-dualside_host_server_port option. In order to get files synced back automatically from the container, you must specify the port of the local unison server.') end cmd = "unison -socket #{@options['unison-dualside_host_server_port']}" say_status 'command', cmd, :white if @options['verbose'] # Store the pid of the local unison server, to kill it when docker-sync stops @local_server_pid = Process.fork do # Run the local unison server in the source folder Dir.chdir(@options['src']){ `#{cmd}` || raise('Start of local unison server failed') } end end |
#stop ⇒ Object
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 187 def stop say_status 'ok', "Stopping sync container #{get_container_name}" begin stop_container stop_local_server rescue Exception => e say_status 'error', "Stopping failed of #{get_container_name}:", :red puts e. end end |
#stop_container ⇒ Object
173 174 175 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 173 def stop_container `docker stop #{get_container_name}` end |
#stop_local_server ⇒ Object
Kill the local unison server
160 161 162 163 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 160 def stop_local_server Process.kill "TERM", @local_server_pid Process.wait @local_server_pid end |
#sync ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 40 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 say_status 'ok', "Synced #{@options['src']}", :white if @options['verbose'] say_status 'output', stdout end end end |
#sync_options ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/docker-sync/sync_strategy/unison-dualside.rb', line 58 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('-owner') if @options['sync_userid'] == 'from_host' args.push('-numericids') if @options['sync_userid'] == 'from_host' 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') raise('Unison does not support sync_user, sync_group, sync_groupid - please use rsync if you need that') end if @options.key?('sync_userid') && @options['sync_userid'] != 'from_host' raise('Unison does not support sync_userid with a parameter different than \'from_host\'') end return args end |