Module: ModuleSync

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

Defined Under Namespace

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

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

.config_defaultsObject


13
14
15
16
17
18
19
20
# File 'lib/modulesync.rb', line 13

def self.config_defaults
  {
    :project_root         => 'modules/',
    :managed_modules_conf => 'managed_modules.yml',
    :configs              => '.',
    :tag_pattern          => '%s'
  }
end

.hook(options) ⇒ Object


58
59
60
61
62
63
64
65
66
67
# File 'lib/modulesync.rb', line 58

def self.hook(options)
  hook = Hook.new(HOOK_FILE, options)

  case options[:hook]
  when 'activate'
    hook.activate
  when 'deactivate'
    hook.deactivate
  end
end

.local_file(config_path, file) ⇒ Object


22
23
24
# File 'lib/modulesync.rb', line 22

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

.local_files(path) ⇒ Object


30
31
32
33
34
35
36
37
# File 'lib/modulesync.rb', line 30

def self.local_files(path)
  if File.exist?(path)
    local_files = Find.find(path).collect { |file| file unless 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


43
44
45
46
47
48
49
50
51
# File 'lib/modulesync.rb', line 43

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_configs(filename, global_defaults, defaults, module_defaults, module_configs) ⇒ Object


69
70
71
# File 'lib/modulesync.rb', line 69

def self.module_configs(filename, global_defaults, defaults, module_defaults, module_configs)
  global_defaults.merge(defaults[filename] || {}).merge(module_defaults).merge(module_configs[filename] || {})
end

.module_file(project_root, puppet_module, file) ⇒ Object


26
27
28
# File 'lib/modulesync.rb', line 26

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

.module_files(local_files, path) ⇒ Object


39
40
41
# File 'lib/modulesync.rb', line 39

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

.module_name(module_name, default_namespace) ⇒ Object


53
54
55
56
# File 'lib/modulesync.rb', line 53

def self.module_name(module_name, default_namespace)
  return [default_namespace, module_name] unless module_name.include?('/')
  ns, mod = module_name.split('/')
end

.unmanaged?(filename, global_defaults, defaults, module_defaults, module_configs) ⇒ Boolean

Returns:

  • (Boolean)

73
74
75
76
77
78
79
# File 'lib/modulesync.rb', line 73

def self.unmanaged?(filename, global_defaults, defaults, module_defaults, module_configs)
  Pathname.new(filename).ascend do |v|
    configs = module_configs(v.to_s, global_defaults, defaults, module_defaults, module_configs)
    return true if configs['unmanaged']
  end
  false
end

.update(options) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/modulesync.rb', line 81

def self.update(options)
  options = config_defaults.merge(options)
  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}"
    namespace, module_name = self.module_name(puppet_module, options[:namespace])
    unless options[:offline]
      git_base = options[:git_base]
      git_uri = "#{git_base}#{namespace}"
      Git.pull(git_uri, module_name, options[:branch], options[:project_root], opts || {})
    end
    module_configs = Util.parse_config("#{options[:project_root]}/#{module_name}/#{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]
    unmanaged_files = []
    files_to_manage.each do |filename|
      configs = module_configs(filename, global_defaults, defaults, module_defaults, module_configs)
      configs[:puppet_module] = module_name
      configs[:git_base] = git_base
      configs[:namespace] = namespace
      if unmanaged?(filename, global_defaults, defaults, module_defaults, module_configs)
        puts "Not managing #{filename} in #{module_name}"
        unmanaged_files << filename
      elsif configs['delete']
        Renderer.remove(module_file(options['project_root'], module_name, filename))
      else
        templatename = local_file(options[:configs], filename)
        begin
          erb = Renderer.build(templatename)
          template = Renderer.render(erb, configs)
          Renderer.sync(template, "#{options[:project_root]}/#{module_name}/#{filename}")
        rescue
          STDERR.puts "Error while rendering #{filename}"
          raise
        end
      end
    end
    files_to_manage -= unmanaged_files
    if options[:noop]
      Git.update_noop(module_name, options)
    elsif !options[:offline]
      Git.update(module_name, files_to_manage, options)
    end
  end
end