Class: Docker_Sync::SyncStrategy::NativeOsx

Inherits:
Object
  • Object
show all
Includes:
Execution, Thor::Shell
Defined in:
lib/docker-sync/sync_strategy/native_osx.rb

Instance Method Summary collapse

Methods included from Execution

#forkexec, #threadexec

Constructor Details

#initialize(sync_name, options) ⇒ NativeOsx

Returns a new instance of NativeOsx.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 20

def initialize(sync_name, options)
  @options = options
  @sync_name = sync_name
  # if a custom image is set, apply it
  if @options.key?('image')
    @docker_image = @options['image']
  else
    @docker_image = 'eugenmayer/unison:hostsync_0.2'
  end

  # TODO: remove this when we have a more stable image, but for now, we need this
  uc = UpdateChecker.new
  uc.check_unison_hostsync_image(true)

  begin
    DockerSync::Preconditions::Strategy.instance.docker_available
  rescue Exception => e
    say_status 'error', "#{@sync_name} has been configured to sync with native docker volume, but docker is not found", :red
    say_status 'error', e.message, :red
    exit 1
  end
end

Instance Method Details

#cleanObject



116
117
118
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 116

def clean
  reset_container
end

#get_container_nameObject



130
131
132
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 130

def get_container_name
  @sync_name.to_s
end

#runObject



102
103
104
105
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 102

def run
  start_container
  sync
end

#start_containerObject

Raises:

  • ('sync_user is no longer supported, since it ise no needed, use sync_userid only please')


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 43

def start_container
  say_status 'ok', "Starting native_osx for sync #{@sync_name}", :white
  container_name = get_container_name
  host_sync_src = @options['src']
  volume_app_sync_name = @sync_name
  env = {}
  raise 'sync_user is no longer supported, since it ise no needed, use sync_userid only please', :yellow if @options.key?('sync_user')

  ignore_strings = expand_ignore_strings
  env['UNISON_EXCLUDES'] = ignore_strings.join(' ')

  if @options.key?('sync_args')
    sync_args = @options['sync_args']
    sync_args = @options['sync_args'].join(' ') if @options['sync_args'].kind_of?(Array)
    env['UNISON_ARGS'] = sync_args
  end

  env['UNISON_SYNC_PREFER'] = sync_prefer
  env['MAX_INOTIFY_WATCHES'] = @options['max_inotify_watches'] if @options.key?('max_inotify_watches')
  if @options['sync_userid'] == 'from_host'
    env['OWNER_UID'] = Process.uid
  else
    env['OWNER_UID'] = @options['sync_userid'] if @options.key?('sync_userid')
  end

  host_disk_mount_mode = '' # see https://github.com/moby/moby/pull/31047
  host_disk_mount_mode = ":#{@options['host_disk_mount_mode']}" if @options.key?('host_disk_mount_mode')

  additional_docker_env = env.map{ |key,value| "-e #{key}=\"#{value}\"" }.join(' ')
  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']
      run_privileged = ''
      run_privileged = '--privileged' if @options.key?('max_inotify_watches') #TODO: replace by the minimum capabilities required
      say_status 'ok', 'Starting precopy', :white if @options['verbose']
      # we just run the precopy script and remove the container
      cmd = "docker run --rm -v #{volume_app_sync_name}:/app_sync -v #{host_sync_src}:/host_sync#{host_disk_mount_mode} -e HOST_VOLUME=/host_sync -e APP_VOLUME=/app_sync -e TZ=${TZ-`readlink /etc/localtime | sed -e 's,/usr/share/zoneinfo/,,'`} #{additional_docker_env} #{run_privileged} --name #{container_name} #{@docker_image} /usr/local/bin/precopy_appsync"
      `#{cmd}` || raise('Precopy failed')
      say_status 'ok', 'Starting container', :white if @options['verbose']
      # this will be run below and start unison, since we did not manipulate CMD
      cmd = "docker run -d -v #{volume_app_sync_name}:/app_sync -v #{host_sync_src}:/host_sync -e HOST_VOLUME=/host_sync -e APP_VOLUME=/app_sync -e TZ=${TZ-`readlink /etc/localtime | sed -e 's,/usr/share/zoneinfo/,,'`} #{additional_docker_env} #{run_privileged} --name #{container_name} #{@docker_image}"
    else
      say_status 'ok', "starting #{container_name} container", :white if @options['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 @options['verbose']
  `#{cmd}` || raise('Start failed')
  say_status 'ok', "starting initial sync of #{container_name}", :white if @options['verbose']
  # wait until container is started, then sync:
  say_status 'success', 'Sync container started', :green
end

#stopObject



120
121
122
123
124
125
126
127
128
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 120

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.message
  end
end

#syncObject



111
112
113
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 111

def sync
  # nop
end

#watchObject



107
108
109
# File 'lib/docker-sync/sync_strategy/native_osx.rb', line 107

def watch
  # nop
end