Class: Docker_Sync::SyncStrategy::Unison
- Inherits:
-
Object
- Object
- Docker_Sync::SyncStrategy::Unison
- Includes:
- Execution, Thor::Shell
- Defined in:
- lib/docker-sync/sync_strategy/unison.rb
Constant Summary collapse
- UNISON_CONTAINER_PORT =
'5000'
Instance Method Summary collapse
- #clean ⇒ Object
- #get_container_name ⇒ Object
- #get_host_port(container_name, container_port) ⇒ Object
- #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
- #watch ⇒ Object
Methods included from Execution
Constructor Details
#initialize(sync_name, options) ⇒ Unison
Returns a new instance of Unison.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 19 def initialize(sync_name, ) = @sync_name = sync_name # if a custom image is set, apply it if .key?('image') @docker_image = ['image'] else @docker_image = 'eugenmayer/unison:unox' end begin Preconditions::unison_available Preconditions::unox_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
192 193 194 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 192 def clean reset_container end |
#get_container_name ⇒ Object
174 175 176 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 174 def get_container_name return "#{@sync_name}" end |
#get_host_port(container_name, container_port) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 162 def get_host_port(container_name, container_port) cmd = 'docker inspect --format=" {{ .NetworkSettings.Ports }} " ' + container_name + ' | /usr/bin/sed -E "s/.*map\[' + container_port + '[^ ]+ ([0-9]*)[^0-9].*/\1/"' say_status 'command', cmd, :white if ['verbose'] stdout, stderr, exit_status = Open3.capture3(cmd) if not 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",'') end |
#get_volume_name ⇒ Object
178 179 180 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 178 def get_volume_name return @sync_name end |
#increase_watcher_limit ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 44 def increase_watcher_limit current_max_files_per_proc = `sysctl kern.maxfilesperproc | awk '{print $2}'` if current_max_files_per_proc.to_f < ['max_inotify_watches'] cmd = 'sudo sysctl -w kern.maxfilesperproc=' + ['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 < ['max_inotify_watches'] cmd = 'sudo sysctl -w kern.maxfiles=' + ['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
186 187 188 189 190 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 186 def reset_container stop_container `docker rm #{get_container_name}` `docker volume rm #{get_volume_name}` end |
#run ⇒ Object
38 39 40 41 42 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 38 def run increase_watcher_limit if .key?('max_inotify_watches') start_container sync end |
#start_container ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 114 def start_container say_status 'ok', 'Starting unison', :white container_name = get_container_name volume_name = get_volume_name env = {} env['UNISON_EXCLUDES'] = ['sync_excludes'].map { |pattern| "-ignore='Path #{pattern}'" }.join(' ') if .key?('sync_excludes') env['UNISON_OWNER'] = ['sync_user'] if .key?('sync_user') env['MAX_INOTIFY_WATCHES'] = ['max_inotify_watches'] if .key?('max_inotify_watches') if ['sync_userid'] == 'from_host' env['UNISON_OWNER_UID'] = Process.uid else env['UNISON_OWNER_UID'] = ['sync_userid'] if .key?('sync_userid') end additional_docker_env = env.map{ |key,value| "-e #{key}=\"#{value}\"" }.join(' ') running = `docker ps --filter 'status=running' --filter 'name=#{container_name}' | sed -E -n 's/.*\\s(.*)$/\\1/p' | grep '^#{container_name}$'` if running == '' say_status 'ok', "#{container_name} container not running", :white if ['verbose'] exists = `docker ps --filter "status=exited" --filter "name=#{container_name}" | sed -E -n 's/.*\\s(.*)$/\\1/p' | grep '^#{container_name}$'` if exists == '' say_status 'ok', "creating #{container_name} container", :white if ['verbose'] run_privileged = '--privileged' if .key?('max_inotify_watches') #TODO: replace by the minimum capabilities required cmd = "docker run -p '127.0.0.1::#{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/,,'`} \ #{additional_docker_env} \ #{run_privileged} \ --name #{container_name} \ -d #{@docker_image}" else say_status 'ok', "starting #{container_name} container", :white if ['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 ['verbose'] `#{cmd}` || raise('Start failed') say_status 'ok', "starting initial sync of #{container_name}", :white if ['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 |
#stop ⇒ Object
196 197 198 199 200 201 202 203 204 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 196 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
182 183 184 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 182 def stop_container `docker stop #{get_container_name}` end |
#sync ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 74 def sync args = cmd = 'unison ' + args.join(' ') say_status 'command', cmd, :white if ['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 ['notify_terminal'] say_status 'ok', "Synced #{@options['src']}", :white if ['verbose'] say_status 'output', stdout end end end |
#sync_options ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 95 def args = [] unless ['sync_excludes'].nil? args = ['sync_excludes'].map { |pattern| "-ignore='Path #{pattern}'" } + args end args.push(['src']) args.push('-auto') args.push('-batch') args.push(['sync_args']) if .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 .key?('sync_group') || .key?('sync_groupid') raise('Unison does not support sync_user, sync_group, sync_groupid - please use rsync if you need that') end return args end |
#watch ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/docker-sync/sync_strategy/unison.rb', line 63 def watch args = args.push("-repeat watch") cmd = '' cmd = cmd + 'ulimit -n ' + ['max_inotify_watches'].to_s + ' && ' if .key?('max_inotify_watches') cmd = cmd + 'unison ' + args.join(' ') say_status 'command', cmd, :white if ['verbose'] forkexec(cmd, "Sync #{@sync_name}", :blue) end |