Class: Synapse::ConfigGenerator::FileOutput

Inherits:
BaseGenerator show all
Includes:
Logging
Defined in:
lib/synapse/config_generator/file_output.rb

Constant Summary collapse

NAME =
'file_output'.freeze

Instance Attribute Summary

Attributes inherited from BaseGenerator

#opts

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Methods inherited from BaseGenerator

#name, #normalize_watcher_provided_config

Constructor Details

#initialize(opts) ⇒ FileOutput

Returns a new instance of FileOutput.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/synapse/config_generator/file_output.rb', line 12

def initialize(opts)
  super(opts)

  unless opts.has_key?("output_directory")
    raise ArgumentError, "flat file generation requires an output_directory key"
  end

  begin
    FileUtils.mkdir_p(opts['output_directory'])
  rescue SystemCallError => err
    raise ArgumentError, "provided output directory #{opts['output_directory']} is not present or creatable"
  end

  # For tracking backends that we don't need to update
  @watcher_revisions = {}
end

Instance Method Details

#clean_old_watchers(current_watchers) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/synapse/config_generator/file_output.rb', line 67

def clean_old_watchers(current_watchers)
  # Cleanup old services that Synapse no longer manages
  FileUtils.cd(opts['output_directory']) do
    present_files = Dir.glob('*.json')
    managed_watchers = current_watchers.reject {|watcher| watcher.config_for_generator[name]['disabled']}
    managed_files = managed_watchers.collect {|watcher| "#{watcher.name}.json"}
    files_to_purge = present_files.select {|svc| not managed_files.include?(svc)}
    log.info "synapse: purging unknown service files #{files_to_purge}" if files_to_purge.length > 0
    FileUtils.rm(files_to_purge)
  end
end

#tick(watchers) ⇒ Object



29
30
# File 'lib/synapse/config_generator/file_output.rb', line 29

def tick(watchers)
end

#update_config(watchers) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/synapse/config_generator/file_output.rb', line 32

def update_config(watchers)
  watchers.each do |watcher|
    next if watcher.config_for_generator[name]['disabled']

    unless @watcher_revisions[watcher.name] == watcher.revision
      @watcher_revisions[watcher.name] = watcher.revision
      write_backends_to_file(watcher.name, watcher.backends)
    end
  end
  clean_old_watchers(watchers)
end

#write_backends_to_file(service_name, new_backends) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/synapse/config_generator/file_output.rb', line 44

def write_backends_to_file(service_name, new_backends)
  data_path = File.join(opts['output_directory'], "#{service_name}.json")
  begin
    old_backends = JSON.load(File.read(data_path))
  rescue Errno::ENOENT
    old_backends = nil
  end

  if old_backends == new_backends
    # Prevent modifying the file unless something has actually changed
    # This way clients can set watches on this file and update their
    # internal state only when the smartstack state has actually changed
    return false
  else
    # Atomically write new service configuration file
    temp_path = File.join(opts['output_directory'],
                          ".#{service_name}.json.tmp")
    File.open(temp_path, 'w', 0644) {|f| f.write(new_backends.to_json)}
    FileUtils.mv(temp_path, data_path)
    return true
  end
end