Class: Docker_sync::SyncManager

Inherits:
Object
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/docker-sync/sync_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SyncManager

Returns a new instance of SyncManager.



19
20
21
22
23
24
25
26
27
28
# File 'lib/docker-sync/sync_manager.rb', line 19

def initialize(options)
  DockerSyncConfig.load_dotenv

  @sync_processes = []
  @config_syncs = []
  @config_global = []
  @config_string = options[:config_string]
  @config_path = options[:config_path]
  load_configuration
end

Instance Method Details

#clean(sync_name = nil) ⇒ Object



125
126
127
128
129
130
# File 'lib/docker-sync/sync_manager.rb', line 125

def clean(sync_name = nil)
  init_sync_processes(sync_name)
  @sync_processes.each { |sync_process|
    sync_process.clean
  }
end

#create_sync(sync_name, sync_configuration) ⇒ Object



177
178
179
180
# File 'lib/docker-sync/sync_manager.rb', line 177

def create_sync(sync_name, sync_configuration)
  sync_process = Docker_Sync::SyncProcess.new(sync_name, sync_configuration)
  return sync_process
end

#get_sync_pointsObject



51
52
53
# File 'lib/docker-sync/sync_manager.rb', line 51

def get_sync_points
  return @config_syncs
end

#global_optionsObject



47
48
49
# File 'lib/docker-sync/sync_manager.rb', line 47

def global_options
  return @config_global
end

#init_sync_processes(sync_name = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/docker-sync/sync_manager.rb', line 110

def init_sync_processes(sync_name = nil)
  return if @sync_processes.size != 0
  if sync_name.nil?
    @config_syncs.each { |name, sync_configuration|
      @sync_processes.push(create_sync(name, sync_configuration))
    }
  else
    unless @config_syncs.key?(sync_name)
      raise("Could not find sync configuration with name #{sync_name}")
    end
    @sync_processes.push(create_sync(sync_name, @config_syncs[sync_name]))
  end
end

#join_threadsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/docker-sync/sync_manager.rb', line 154

def join_threads
  begin
    @sync_processes.each do |sync_process|
      if sync_process.watch_thread
        sync_process.watch_thread.join
      end
      if sync_process.watch_fork
        Process.wait(sync_process.watch_fork)
      end
    end

  rescue SystemExit, Interrupt
    say_status 'shutdown', 'Shutting down...', :blue
    @sync_processes.each do |sync_process|
      sync_process.stop
    end

  rescue Exception => e
    puts "EXCEPTION: #{e.inspect}"
    puts "MESSAGE: #{e.message}"
  end
end

#load_configurationObject



37
38
39
40
41
42
43
44
45
# File 'lib/docker-sync/sync_manager.rb', line 37

def load_configuration
  # try to interpolate supplied inline config string, alternatively load the configuration file
  config = ConfigTemplate::interpolate_config_string(@config_string || load_configuration_file())

  validate_config(config)
  @config_global = config['options'] || {}
  @config_syncs = config['syncs']
  upgrade_syncs_config
end

#load_configuration_fileObject



30
31
32
33
34
35
# File 'lib/docker-sync/sync_manager.rb', line 30

def load_configuration_file
  unless File.exist?(@config_path)
    raise "Config could not be loaded from #{@config_path} - it does not exist"
  end
  return File.read(@config_path)
end

#run(sync_name = nil) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/docker-sync/sync_manager.rb', line 146

def run(sync_name = nil)
  init_sync_processes(sync_name)

  @sync_processes.each { |sync_process|
    sync_process.run
  }
end

#start_container(sync_name = nil) ⇒ Object



139
140
141
142
143
144
# File 'lib/docker-sync/sync_manager.rb', line 139

def start_container(sync_name = nil)
  init_sync_processes(sync_name)
  @sync_processes.each { |sync_process|
    sync_process.start_container
  }
end

#stopObject



182
183
184
185
186
187
188
189
# File 'lib/docker-sync/sync_manager.rb', line 182

def stop
  @sync_processes.each { |sync_process|
    sync_process.stop
    unless sync_process.watch_thread.nil?
      sync_process.watch_thread.kill unless sync_process.watch_thread.nil?
    end
  }
end

#sync(sync_name = nil) ⇒ Object



132
133
134
135
136
137
# File 'lib/docker-sync/sync_manager.rb', line 132

def sync(sync_name = nil)
  init_sync_processes(sync_name)
  @sync_processes.each { |sync_process|
    sync_process.sync
  }
end

#upgrade_syncs_configObject



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
# File 'lib/docker-sync/sync_manager.rb', line 55

def upgrade_syncs_config
  @config_syncs.each do |name, config|
    @config_syncs[name]['config_path'] = @config_path

    # [nbr] convert the sync src from relative to absolute path
    #	preserve '/' as it may be significant to the sync cmd
          absolute_path = File.expand_path(@config_syncs[name]['src'])
    absolute_path << "/" if @config_syncs[name]['src'].end_with?("/")
    @config_syncs[name]['src'] = absolute_path

    @config_syncs[name]['cli_mode'] = @config_global['cli_mode'] || 'auto'

    # set the global verbose setting, if the sync-endpoint does not define a own one
    unless config.key?('verbose')
      @config_syncs[name]['verbose'] = false
      if @config_global.key?('verbose')
        @config_syncs[name]['verbose'] = @config_global['verbose']
      end
    end

    # set default value for 'dest'
    if !@config_syncs[name].key?('dest')
      @config_syncs[name]['dest'] = '/sync'
    end

    # for each strategy check if a custom image has been defined and inject that into the sync-endpoints
    # which do fit for this strategy
    %w(rsync unison).each do |strategy|
      if config.key?("#{strategy}_image") && @config_syncs[name]['sync_strategy'] == strategy
        @config_syncs[name]['image'] = config["#{strategy}_image"]
      end
    end
  end
end

#validate_config(config) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/docker-sync/sync_manager.rb', line 90

def validate_config(config)
  unless config.key?('syncs')
    raise ('no syncs defined')
  end

  config['syncs'].each do |name, sync_config|
    validate_sync_config(name, sync_config)
  end

  return true
end

#validate_sync_config(name, sync_config) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/docker-sync/sync_manager.rb', line 102

def validate_sync_config(name, sync_config)
  config_mandatory = %w[src]
  config_mandatory.push('sync_host_port') if sync_config['sync_strategy'] == 'rsync' #TODO: Implement autodisovery for other strategies
  config_mandatory.each do |key|
    raise ("#{name} does not have #{key} configuration value set - this is mandatory") unless sync_config.key?(key)
  end
end

#watch_startObject



197
198
199
200
201
# File 'lib/docker-sync/sync_manager.rb', line 197

def watch_start
  @sync_processes.each { |sync_process|
    sync_process.watch
  }
end

#watch_stopObject



191
192
193
194
195
# File 'lib/docker-sync/sync_manager.rb', line 191

def watch_stop
  @sync_processes.each { |sync_process|
    sync_process.watch_thread.kill unless sync_process.watch_thread.nil?
  }
end