docker-sync 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f672a525e3ac21a5fe659eb7d0a15d8ba617e14
4
- data.tar.gz: cbacd747e7959c1319c1f897070bd1d6d3a7da18
3
+ metadata.gz: 8746cae5b189f20e47f395a21cf677b62746baca
4
+ data.tar.gz: 793703a123def635266d38e82e70e411bf1994d4
5
5
  SHA512:
6
- metadata.gz: c9ac6925db1d1e9da1088d6f85c82a0a47e4183933da35873df1aa02d7b93ed868976705cd496b9b98afaf41f10f551c1b1ca7f1baa95397b87e8cfe2aeb59cb
7
- data.tar.gz: 973e7208ff7e7f9d560ba96f3a93d40bd56f7a82f4f02207a865248cb6d9c11dbaf0785ef6ab677f2e9205198e1b96a61310ee5810a53d1730aa87b0e34e238c
6
+ metadata.gz: d030d530618e60eb2106c9552ad1a2a6d1a8b5f36de28bec3481b53d03f9b4a27f80968cb1129483a136f41a340f7ec832d140c26938457f79fcb12b57cc58c9
7
+ data.tar.gz: 16581581034acbadf1f20c11f953df4c62fe7b2ee5502e011ac44110ca50ca9750966eb62ea4429bb1831541a570c56570816d0e559b481112195fd9d23c46d6
data/Thorfile CHANGED
@@ -1,7 +1,7 @@
1
1
  #lib = File.expand_path('./lib', __dir__)
2
2
  $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
-
4
3
  thor = File.expand_path('../tasks', __FILE__)
4
+
5
5
  Dir.glob(File.join(thor, '/**/*.thor')).each { |taskfile|
6
6
  load taskfile
7
7
  }
data/lib/config.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'pp'
2
- require 'Pathname'
2
+ require 'pathname'
3
3
  # this has basically completely reused from Thor::runner.rb - thank you!
4
4
 
5
5
  module DockerSyncConfig
@@ -1,5 +1,7 @@
1
1
  require 'thor/shell'
2
+ # noinspection RubyResolve
2
3
  require 'docker_sync/sync_process'
4
+ # noinspection RubyResolve
3
5
  require 'execution'
4
6
  require 'yaml'
5
7
 
@@ -1,133 +1,91 @@
1
1
  require 'thor/shell'
2
- require 'execution'
3
- require 'shellwords'
2
+ # noinspection RubyResolve
3
+ require 'docker_sync/sync_strategy/rsync'
4
+ require 'docker_sync/sync_strategy/unison'
5
+ # noinspection RubyResolve
6
+ require 'docker_sync/watch_strategy/fswatch'
7
+
4
8
  module Docker_Sync
5
9
  class SyncProcess
6
10
  include Thor::Shell
7
- include Execution
8
11
  @options
9
12
  @sync_name
10
13
  @watch_thread
14
+ @sync_strategy
15
+ @watch_strategy
11
16
 
17
+ # noinspection RubyStringKeysInHashInspection
12
18
  def initialize(sync_name, options)
13
19
  defaults = {
14
- 'verbose' => false,
15
- 'sync_host_ip' => get_host_ip
20
+ 'verbose' => false,
21
+ 'sync_host_ip' => get_host_ip
16
22
  }
17
23
  @sync_name = sync_name
18
24
  @options = defaults.merge(options)
19
-
20
- end
21
-
22
- def get_host_ip
23
- return 'localhost'
25
+ @sync_strategy = nil
26
+ @watch_strategy = nil
27
+ set_sync_strategy
28
+ set_watch_strategy
24
29
  end
25
30
 
26
- def run
27
- start_container
28
- sync
29
- watch
30
- end
31
-
32
- def stop
33
- say_status 'ok', "Stopping sync container #{@sync_name}"
34
- begin
35
- `docker stop #{@sync_name}`
36
- rescue Exception => e
37
- say_status 'error', "Stopping failed of #{@sync_name}:", :red
38
- puts e.message
31
+ def set_sync_strategy
32
+ if @options.key?('sync_strategy')
33
+ case @options['sync_strategy']
34
+ when 'rsync'
35
+ @sync_strategy = Docker_Sync::SyncStrategy::Rsync.new(@sync_name, @options)
36
+ when 'unison'
37
+ @sync_strategy = Docker_Sync::SyncStrategy::Unison.new(@sync_name, @options)
38
+ else
39
+ @sync_strategy = Docker_Sync::SyncStrategy::Rsync.new(@sync_name, @options)
40
+ end
41
+ else
42
+ @sync_strategy = Docker_Sync::SyncStrategy::Rsync.new(@sync_name, @options)
39
43
  end
40
44
  end
41
45
 
42
- def start_container
43
- say_status 'ok', 'Starting rsync', :white
44
- running = `docker ps --filter 'status=running' --filter 'name=#{@sync_name}' | grep #{@sync_name}`
45
- if running == ''
46
- say_status 'ok', "#{@sync_name} container not running", :white
47
- exists = `docker ps --filter "status=exited" --filter "name=filesync_dw" | grep filesync_dw`
48
- if exists == ''
49
- say_status 'ok', "creating #{@sync_name} container", :white
50
- cmd = "docker run -p '#{@options['sync_host_port']}:873' -v #{@sync_name}:#{@options['dest']} -e VOLUME=#{@options['dest']} --name #{@sync_name} -d eugenmayer/rsync"
51
- else
52
- say_status 'success', "starting #{@sync_name} container", :green
53
- cmd = "docker start #{@sync_name}"
46
+ def set_watch_strategy
47
+ if @options.key?('watch_strategy')
48
+ case @options['watch_strategy']
49
+ when 'fswatch'
50
+ @watch_strategy = Docker_Sync::WatchStrategy::Fswatch.new(@sync_name, @options)
51
+ else
52
+ @watch_strategy = Docker_Sync::WatchStrategy::Fswatch.new(@sync_name, @options)
54
53
  end
55
- say_status 'command', cmd, :white
56
- `#{cmd}` || raise('Start failed')
57
54
  else
58
- say_status 'ok', "#{@sync_name} container still running", :blue
55
+ @watch_strategy = Docker_Sync::WatchStrategy::Fswatch.new(@sync_name, @options)
59
56
  end
60
- say_status 'success', "starting initial #{@sync_name} of src", :green
61
- # this sleep is needed since the container could be not started
62
- sleep 1
63
- sync
64
57
  end
65
58
 
66
- def stop_container
67
- `docker stop #{@sync_name}`
59
+ def get_host_ip
60
+ return 'localhost'
61
+ end
62
+
63
+ def run
64
+ @sync_strategy.run
65
+ @watch_strategy.run
68
66
  end
69
67
 
70
- def reset_container
71
- stop_container
72
- `docker rm #{@sync_name}`
73
- `docker volume rm #{@sync_name}`
68
+ def stop
69
+ @sync_strategy.stop
70
+ @watch_strategy.stop
74
71
  end
75
72
 
76
73
  def clean
77
- reset_container
74
+ @sync_strategy.clean
75
+ @watch_strategy.clean
78
76
  end
79
77
 
80
78
  def sync
81
- args = sync_options
82
- cmd = 'rsync ' + args.join(' ')
83
-
84
- say_status 'command', cmd, :white if @options['verbose']
85
-
86
- out = `#{cmd}`
87
- if $?.exitstatus > 0
88
- say_status 'error', "Error starting sync, exit code #{$?.exitstatus}", :red
89
- say_status 'message', out
90
- else
91
- say_status 'success', "Synced #{@options['src']}", :green
92
- if @options['verbose']
93
- say_status 'output', out
94
- end
95
- end
96
- end
97
-
98
- def sync_options
99
- args = []
100
- unless @options['sync_excludes'].nil?
101
- args = @options['sync_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
102
- end
103
- args.push('-ap')
104
- args.push(@options['sync_args']) if @options.key?('sync_args')
105
- args.push("#{@options['src']}/") # add a trailing slash
106
- args.push("rsync://#{@options['sync_host_ip']}:#{@options['sync_host_port']}/volume")
79
+ # TODO: probably use run here
80
+ @sync_strategy.sync
107
81
  end
108
82
 
109
83
  def watch
110
- args = watch_options
111
- say_status 'ok', "Starting to watch #{@options['src']} - Press CTRL-C to stop", :green
112
- cmd = 'fswatch ' + args.join(' ')
113
- say_status 'command', cmd, :white if @options['verbose']
114
-
115
- @watch_thread = threadexec(cmd, "Sync #{@sync_name}", :blue)
116
- end
117
-
118
- def watch_options
119
- args = []
120
- unless @options['watch_excludes'].nil?
121
- args = @options['watch_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
122
- end
123
- args.push('-orIE')
124
- args.push(@options['watch_args']) if @options.key?('watch_args')
125
- args.push(@options['src'])
126
- args.push(" | xargs -I -n1 thor sync:sync -n #{@sync_name} --config='#{@options['config_path']}'")
84
+ @watch_strategy.run
127
85
  end
128
86
 
129
87
  def watch_thread
130
- return @watch_thread
88
+ return @watch_strategy.watch_thread
131
89
  end
132
90
  end
133
91
  end
@@ -0,0 +1,100 @@
1
+ require 'thor/shell'
2
+
3
+ module Docker_Sync
4
+ module SyncStrategy
5
+ class Rsync
6
+ include Thor::Shell
7
+ @options
8
+ @sync_name
9
+ @watch_thread
10
+
11
+ def initialize(sync_name, options)
12
+ @sync_name = sync_name
13
+ @options = options
14
+ end
15
+
16
+ def run
17
+ start_container
18
+ sync
19
+ end
20
+
21
+ def sync
22
+ args = sync_options
23
+ cmd = 'rsync ' + args.join(' ')
24
+
25
+ say_status 'command', cmd, :white if @options['verbose']
26
+
27
+ out = `#{cmd}`
28
+ if $?.exitstatus > 0
29
+ say_status 'error', "Error starting sync, exit code #{$?.exitstatus}", :red
30
+ say_status 'message', out
31
+ else
32
+ say_status 'success', "Synced #{@options['src']}", :green
33
+ if @options['verbose']
34
+ say_status 'output', out
35
+ end
36
+ end
37
+ end
38
+
39
+ def sync_options
40
+ args = []
41
+ unless @options['sync_excludes'].nil?
42
+ args = @options['sync_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
43
+ end
44
+ args.push('-ap')
45
+ args.push(@options['sync_args']) if @options.key?('sync_args')
46
+ args.push("#{@options['src']}/") # add a trailing slash
47
+ args.push("rsync://#{@options['sync_host_ip']}:#{@options['sync_host_port']}/volume")
48
+ end
49
+
50
+
51
+ def start_container
52
+ say_status 'ok', 'Starting rsync', :white
53
+ running = `docker ps --filter 'status=running' --filter 'name=#{@sync_name}' | grep #{@sync_name}`
54
+ if running == ''
55
+ say_status 'ok', "#{@sync_name} container not running", :white
56
+ exists = `docker ps --filter "status=exited" --filter "name=filesync_dw" | grep filesync_dw`
57
+ if exists == ''
58
+ say_status 'ok', "creating #{@sync_name} container", :white
59
+ cmd = "docker run -p '#{@options['sync_host_port']}:873' -v #{@sync_name}:#{@options['dest']} -e VOLUME=#{@options['dest']} --name #{@sync_name} -d eugenmayer/rsync"
60
+ else
61
+ say_status 'success', "starting #{@sync_name} container", :green
62
+ cmd = "docker start #{@sync_name}"
63
+ end
64
+ say_status 'command', cmd, :white
65
+ `#{cmd}` || raise('Start failed')
66
+ else
67
+ say_status 'ok', "#{@sync_name} container still running", :blue
68
+ end
69
+ say_status 'success', "starting initial #{@sync_name} of src", :green
70
+ # this sleep is needed since the container could be not started
71
+ sleep 1
72
+ sync
73
+ end
74
+
75
+ def stop_container
76
+ `docker stop #{@sync_name}`
77
+ end
78
+
79
+ def reset_container
80
+ stop_container
81
+ `docker rm #{@sync_name}`
82
+ `docker volume rm #{@sync_name}`
83
+ end
84
+
85
+ def clean
86
+ reset_container
87
+ end
88
+
89
+ def stop
90
+ say_status 'ok', "Stopping sync container #{@sync_name}"
91
+ begin
92
+ stop_container
93
+ rescue Exception => e
94
+ say_status 'error', "Stopping failed of #{@sync_name}:", :red
95
+ puts e.message
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,105 @@
1
+ require 'thor/shell'
2
+
3
+ module Docker_Sync
4
+ module SyncStrategy
5
+ class Unison
6
+ include Thor::Shell
7
+ @options
8
+ @sync_name
9
+ @watch_thread
10
+ UNISON_IMAGE = 'leighmcculloch/unison'
11
+ UNISON_VERSION = '2.48.3'
12
+ UNISON_CONTAINER_PORT = '5000'
13
+ def initialize(sync_name, options)
14
+ @sync_name = sync_name
15
+ @options = options
16
+ end
17
+
18
+ def run
19
+ start_container
20
+ sync
21
+ end
22
+
23
+ def sync
24
+ args = sync_options
25
+ cmd = 'unison ' + args.join(' ')
26
+
27
+ say_status 'command', cmd, :white if @options['verbose']
28
+
29
+ out = `#{cmd}`
30
+ if $?.exitstatus > 0
31
+ say_status 'error', "Error starting sync, exit code #{$?.exitstatus}", :red
32
+ say_status 'message', out
33
+ else
34
+ say_status 'success', "Synced #{@options['src']}", :green
35
+ if @options['verbose']
36
+ say_status 'output', out
37
+ end
38
+ end
39
+ end
40
+
41
+ def sync_options
42
+ args = []
43
+
44
+ unless @options['sync_excludes'].nil?
45
+ # TODO: does unison support excludes as a command paramter? seems to be a config-value only
46
+ say_status 'warning','Excludes are yet not implemented for unison!', :orange
47
+ # args = @options['sync_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
48
+ end
49
+ args.push(@options['src'])
50
+ args.push('-auto')
51
+ args.push('-batch')
52
+ args.push(@options['sync_args']) if @options.key?('sync_args')
53
+ args.push("socket://#{@options['sync_host_ip']}:#{@options['sync_host_port']}/")
54
+ end
55
+
56
+ def start_container
57
+ say_status 'ok', 'Starting rsync', :white
58
+ running = `docker ps --filter 'status=running' --filter 'name=#{@sync_name}' | grep #{@sync_name}`
59
+ if running == ''
60
+ say_status 'ok', "#{@sync_name} container not running", :white
61
+ exists = `docker ps --filter "status=exited" --filter "name=filesync_dw" | grep filesync_dw`
62
+ if exists == ''
63
+ say_status 'ok', "creating #{@sync_name} container", :white
64
+ cmd = "docker run -p '#{@options['sync_host_port']}:#{UNISON_CONTAINER_PORT}' -v #{@sync_name}:#{@options['dest']} -e UNISON_VERSION=#{UNISON_VERSION} -e UNISON_WORKING_DIR=#{@options['dest']} --name #{@sync_name} -d #{UNISON_IMAGE}"
65
+ else
66
+ say_status 'success', "starting #{@sync_name} container", :green
67
+ cmd = "docker start #{@sync_name}"
68
+ end
69
+ say_status 'command', cmd, :white
70
+ `#{cmd}` || raise('Start failed')
71
+ else
72
+ say_status 'ok', "#{@sync_name} container still running", :blue
73
+ end
74
+ say_status 'success', "starting initial #{@sync_name} of src", :green
75
+ # this sleep is needed since the container could be not started
76
+ sleep 1
77
+ sync
78
+ end
79
+
80
+ def stop_container
81
+ `docker stop #{@sync_name}`
82
+ end
83
+
84
+ def reset_container
85
+ stop_container
86
+ `docker rm #{@sync_name}`
87
+ `docker volume rm #{@sync_name}`
88
+ end
89
+
90
+ def clean
91
+ reset_container
92
+ end
93
+
94
+ def stop
95
+ say_status 'ok', "Stopping sync container #{@sync_name}"
96
+ begin
97
+ stop_container
98
+ rescue Exception => e
99
+ say_status 'error', "Stopping failed of #{@sync_name}:", :red
100
+ puts e.message
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,52 @@
1
+ require 'thor/shell'
2
+ require 'execution'
3
+ module Docker_Sync
4
+ module WatchStrategy
5
+ class Fswatch
6
+ include Thor::Shell
7
+ include Execution
8
+ @options
9
+ @sync_name
10
+ @watch_thread
11
+
12
+ def initialize(sync_name, options)
13
+ @sync_name = sync_name
14
+ @options = options
15
+ end
16
+
17
+ def run
18
+ watch
19
+ end
20
+
21
+ def stop
22
+ end
23
+
24
+ def clean
25
+ end
26
+
27
+ def watch
28
+ args = watch_options
29
+ say_status 'ok', "Starting to watch #{@options['src']} - Press CTRL-C to stop", :green
30
+ cmd = 'fswatch ' + args.join(' ')
31
+ say_status 'command', cmd, :white if @options['verbose']
32
+
33
+ @watch_thread = threadexec(cmd, "Sync #{@sync_name}", :blue)
34
+ end
35
+
36
+ def watch_options
37
+ args = []
38
+ unless @options['watch_excludes'].nil?
39
+ args = @options['watch_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
40
+ end
41
+ args.push('-orIE')
42
+ args.push(@options['watch_args']) if @options.key?('watch_args')
43
+ args.push(@options['src'])
44
+ args.push(" | xargs -I -n1 thor sync:sync -n #{@sync_name} --config='#{@options['config_path']}'")
45
+ end
46
+
47
+ def watch_thread
48
+ return @watch_thread
49
+ end
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker-sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugen Mayer
@@ -51,6 +51,9 @@ files:
51
51
  - lib/config.rb
52
52
  - lib/docker_sync/sync_manager.rb
53
53
  - lib/docker_sync/sync_process.rb
54
+ - lib/docker_sync/sync_strategy/rsync.rb
55
+ - lib/docker_sync/sync_strategy/unison.rb
56
+ - lib/docker_sync/watch_strategy/fswatch.rb
54
57
  - lib/execution.rb
55
58
  - tasks/sync.thor
56
59
  homepage: https://github.com/EugenMayer/docker_sync