Class: Docker_Sync::SyncStrategy::Rsync
- Inherits:
-
Object
- Object
- Docker_Sync::SyncStrategy::Rsync
- Includes:
- Preconditions, Thor::Shell
- Defined in:
- lib/docker_sync/sync_strategy/rsync.rb
Instance Method Summary collapse
- #clean ⇒ Object
-
#initialize(sync_name, options) ⇒ Rsync
constructor
A new instance of Rsync.
- #reset_container ⇒ Object
- #run ⇒ Object
-
#start_container ⇒ Object
starts a rsync docker container listening on the specific port this container exposes a named volume and is on one side used as the rsync-endpoint for the local rsync command, on the other side the volume is mounted into the app-container to share the code / content.
- #stop ⇒ Object
- #stop_container ⇒ Object
- #sync ⇒ Object
- #sync_options ⇒ Object
Methods included from Preconditions
#check_all_preconditions, #docker_available, #docker_running, #docker_sync_available, #fswatch_available, #rsync_available, #unison_available
Constructor Details
#initialize(sync_name, options) ⇒ Rsync
Returns a new instance of Rsync.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 14 def initialize(sync_name, ) @sync_name = sync_name = begin rsync_available rescue Exception => e say_status 'error', "#{@sync_name} has been configured to sync with rsync, but no rsync binary available", :red say_status 'error', e., :red exit 1 end end |
Instance Method Details
#clean ⇒ Object
124 125 126 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 124 def clean reset_container end |
#reset_container ⇒ Object
118 119 120 121 122 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 118 def reset_container stop_container `docker rm #{@sync_name}` `docker volume rm #{@sync_name}` end |
#run ⇒ Object
27 28 29 30 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 27 def run start_container sync end |
#start_container ⇒ Object
starts a rsync docker container listening on the specific port this container exposes a named volume and is on one side used as the rsync-endpoint for the local rsync command, on the other side the volume is mounted into the app-container to share the code / content
68 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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 68 def start_container say_status 'ok', 'Starting rsync', :white running = `docker ps --filter 'status=running' --filter 'name=#{@sync_name}' | grep #{@sync_name}` if running == '' # container is yet not running say_status 'ok', "#{@sync_name} container not running", :white if ['verbose'] exists = `docker ps --filter "status=exited" --filter "name=#{@sync_name}" | grep #{@sync_name}` if exists == '' # container has yet not been created say_status 'ok', "creating #{@sync_name} container", :white if ['verbose'] user_mapping = '' if .key?('sync_user') user_mapping = "-e OWNER=#{@options['sync_user']}" if .key?('sync_userid') user_mapping = "#{user_mapping} -e OWNERID=#{@options['sync_userid']}" end elsif .key?('sync_userid') raise("#{@sync_name}: You have set a sync_userid but no sync_user - you need to set both") end group_mapping = '' if .key?('sync_group') group_mapping = "-e GROUP=#{@options['sync_group']}" if .key?('sync_groupid') group_mapping = "#{group_mapping} -e GROUPID=#{@options['sync_groupid']}" end elsif .key?('sync_groupid') raise("#{@sync_name}: You have set a sync_groupid but no sync_group - you need to set both") end cmd = "docker run -p '#{@options['sync_host_port']}:873' -v #{@sync_name}:#{@options['dest']} #{user_mapping} #{group_mapping} -e VOLUME=#{@options['dest']} --name #{@sync_name} -d eugenmayer/rsync" else # container already created, just start / reuse it say_status 'ok', "starting #{@sync_name} container", :white if ['verbose'] cmd = "docker start #{@sync_name}" end say_status 'command', cmd, :white if ['verbose'] `#{cmd}` || raise('Start failed') else say_status 'ok', "#{@sync_name} container still running", :blue if ['verbose'] end say_status 'ok', "starting initial #{@sync_name} of src", :white if ['verbose'] # this sleep is needed since the container could be not started sleep 1 sync say_status 'success', 'Rsync server started', :green end |
#stop ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 128 def stop say_status 'ok', "Stopping sync container #{@sync_name}" begin stop_container rescue Exception => e say_status 'error', "Stopping failed of #{@sync_name}:", :red puts e. end end |
#stop_container ⇒ Object
114 115 116 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 114 def stop_container `docker stop #{@sync_name}` end |
#sync ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 32 def sync args = cmd = 'rsync ' + args.join(' ') say_status 'command', cmd, :white if ['verbose'] out = `#{cmd}` if $?.exitstatus > 0 say_status 'error', "Error starting sync, exit code #{$?.exitstatus}", :red say_status 'message', out else say_status 'ok', "Synced #{@options['src']}", :white if ['verbose'] say_status 'output', out end end end |
#sync_options ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/docker_sync/sync_strategy/rsync.rb', line 50 def args = [] unless ['sync_excludes'].nil? args = ['sync_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args end args.push('-ap') args.push(['sync_args']) if .key?('sync_args') # we do not need to user usermap/groupmap since we started our container the way that it also maps user/group like we defined # in the config - see start_container #args.push("--usermap='*:#{@options['sync_user']}'") if @options.key?('sync_user') #args.push("--groupmap='*:#{@options['sync_group']}'") if @options.key?('sync_group') args.push("#{@options['src']}/") # add a trailing slash args.push("rsync://#{@options['sync_host_ip']}:#{@options['sync_host_port']}/volume") end |