Module: ModuleSync

Includes:
Constants
Defined in:
lib/modulesync.rb,
lib/modulesync/cli.rb,
lib/modulesync/git.rb,
lib/modulesync/hook.rb,
lib/modulesync/util.rb,
lib/modulesync/renderer.rb,
lib/modulesync/constants.rb

Defined Under Namespace

Modules: Constants, Git, Hook, Renderer, Util Classes: CLI

Constant Summary

Constants included from Constants

Constants::CONF_FILE, Constants::GLOBAL_DEFAULTS_KEY, Constants::HOOK_FILE, Constants::MODULESYNC_CONF_FILE, Constants::MODULE_CONF_FILE, Constants::MODULE_FILES_DIR

Class Method Summary collapse

Class Method Details

.local_file(config_path, file) ⇒ Object



12
13
14
# File 'lib/modulesync.rb', line 12

def self.local_file(config_path, file)
  "#{config_path}/#{MODULE_FILES_DIR}/#{file}"
end

.local_files(path) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/modulesync.rb', line 20

def self.local_files(path)
  if File.exists?(path)
    local_files = Find.find(path).collect { |file| file if !File.directory?(file) }.compact
  else
    puts "#{path} does not exist. Check that you are working in your module configs directory or that you have passed in the correct directory with -c."
    exit
  end
end

.managed_modules(path, filter) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/modulesync.rb', line 33

def self.managed_modules(path, filter)
  managed_modules = Util.parse_config(path)
  if managed_modules.empty?
    puts "No modules found at #{path}. Check that you specified the right configs directory containing managed_modules.yml."
    exit
  end
  managed_modules.select! { |m| m =~ Regexp.new(filter) } unless filter.nil?
  managed_modules
end

.module_file(project_root, puppet_module, file) ⇒ Object



16
17
18
# File 'lib/modulesync.rb', line 16

def self.module_file(project_root, puppet_module, file)
  "#{project_root}/#{puppet_module}/#{file}"
end

.module_files(local_files, path) ⇒ Object



29
30
31
# File 'lib/modulesync.rb', line 29

def self.module_files(local_files, path)
  local_files.map { |file| file.sub(/#{path}/, '') }
end

.run(args) ⇒ Object



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
# File 'lib/modulesync.rb', line 43

def self.run(args)
  cli = CLI.new
  cli.parse_opts(args)
  options  = cli.options
  if options[:command] == 'update'
    defaults = Util.parse_config("#{options[:configs]}/#{CONF_FILE}")

    path = "#{options[:configs]}/#{MODULE_FILES_DIR}"
    local_files = self.local_files(path)
    module_files = self.module_files(local_files, path)

    managed_modules = self.managed_modules("#{options[:configs]}/managed_modules.yml", options[:filter])

    # managed_modules is either an array or a hash
    managed_modules.each do |puppet_module, opts|
      puts "Syncing #{puppet_module}"
      unless options[:offline]
        git_base = "#{options[:git_base]}#{options[:namespace]}"
        Git.pull(git_base, puppet_module, options[:branch], options[:project_root], opts || {})
      end
      module_configs = Util.parse_config("#{options[:project_root]}/#{puppet_module}/#{MODULE_CONF_FILE}")
      global_defaults = defaults[GLOBAL_DEFAULTS_KEY] || {}
      module_defaults = module_configs[GLOBAL_DEFAULTS_KEY] || {}
      files_to_manage = (module_files | defaults.keys | module_configs.keys) - [GLOBAL_DEFAULTS_KEY]
      files_to_delete = []
      files_to_manage.each do |file|
        file_configs = global_defaults.merge(defaults[file] || {}).merge(module_defaults).merge(module_configs[file] || {})
        file_configs[:puppet_module] = puppet_module
        if file_configs['unmanaged']
          puts "Not managing #{file} in #{puppet_module}"
          files_to_delete << file
        elsif file_configs['delete']
          Renderer.remove(module_file(options['project_root'], puppet_module, file))
        else
          erb = Renderer.build(local_file(options[:configs], file))
          template = Renderer.render(erb, file_configs)
          Renderer.sync(template, "#{options[:project_root]}/#{puppet_module}/#{file}")
        end
      end
      files_to_manage -= files_to_delete
      if options[:noop]
        Git.update_noop(puppet_module, options)
      elsif not options[:offline]
        Git.update(puppet_module, files_to_manage, options)
      end
    end
  elsif options[:command] == 'hook'
    Hook.hook(args[1], options)
  end
end