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/cli/thor.rb,
lib/modulesync/renderer.rb,
lib/modulesync/settings.rb,
lib/modulesync/constants.rb,
lib/modulesync/pr/github.rb,
lib/modulesync/pr/gitlab.rb

Overview

rubocop:disable Metrics/ModuleLength

Defined Under Namespace

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

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

Instance Method Summary collapse

Class Method Details

.config_defaultsObject



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

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

.config_path(file, options) ⇒ Object



142
143
144
145
# File 'lib/modulesync.rb', line 142

def self.config_path(file, options)
  return file if Pathname.new(file).absolute?
  File.join(options[:configs], file)
end

.create_pr_managerObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/modulesync.rb', line 208

def self.create_pr_manager
  github_token = ENV.fetch('GITHUB_TOKEN', '')
  gitlab_token = ENV.fetch('GITLAB_TOKEN', '')

  if !github_token.empty?
    require 'modulesync/pr/github'
    ModuleSync::PR::GitHub.new(github_token, ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com'))
  elsif !gitlab_token.empty?
    require 'modulesync/pr/gitlab'
    ModuleSync::PR::GitLab.new(gitlab_token, ENV.fetch('GITLAB_BASE_URL', 'https://gitlab.com/api/v4'))
  else
    warn '--pr specified without environment variables GITHUB_TOKEN or GITLAB_TOKEN'
  end
end

.find_template_files(local_template_dir) ⇒ Object

List all template files.

Only select *.erb files, and strip the extension. This way all the code will only have to handle bare paths, except when reading the actual ERB text



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/modulesync.rb', line 36

def self.find_template_files(local_template_dir)
  if File.exist?(local_template_dir)
    Find.find(local_template_dir).find_all { |p| p =~ /.erb$/ && !File.directory?(p) }
        .collect { |p| p.chomp('.erb') }
        .to_a
  else
    $stderr.puts "#{local_template_dir} 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 1
  end
end

.hook(options) ⇒ Object



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

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



24
25
26
# File 'lib/modulesync.rb', line 24

def self.local_file(config_path, file)
  File.join(config_path, MODULE_FILES_DIR, file)
end

.manage_file(filename, settings, 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
# File 'lib/modulesync.rb', line 81

def self.manage_file(filename, settings, options)
  namespace = settings.additional_settings[:namespace]
  module_name = settings.additional_settings[:puppet_module]
  configs = settings.build_file_configs(filename)
  target_file = module_file(options[:project_root], namespace, module_name, filename)
  if configs['delete']
    Renderer.remove(target_file)
  else
    templatename = local_file(options[:configs], filename)
    begin
      erb = Renderer.build(templatename)
      # Meta data passed to the template as @metadata[:name]
       = {
        :module_name => module_name,
        :workdir     => module_file(options[:project_root], namespace, module_name),
        :target_file => target_file,
      }
      template = Renderer.render(erb, configs, )
      Renderer.sync(template, target_file)
    rescue # rubocop:disable Lint/RescueWithoutErrorClass
      $stderr.puts "Error while rendering #{filename}"
      raise
    end
  end
end

.manage_module(puppet_module, module_files, module_options, defaults, options) ⇒ Object



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
135
136
137
138
139
140
# File 'lib/modulesync.rb', line 107

def self.manage_module(puppet_module, module_files, module_options, defaults, options)
  default_namespace = options[:namespace]
  if module_options.is_a?(Hash) && module_options.key?(:namespace)
    default_namespace = module_options[:namespace]
  end
  namespace, module_name = module_name(puppet_module, default_namespace)
  git_repo = File.join(namespace, module_name)
  unless options[:offline]
    Git.pull(options[:git_base], git_repo, options[:branch], options[:project_root], module_options || {})
  end

  module_configs = Util.parse_config(module_file(options[:project_root], namespace, module_name, MODULE_CONF_FILE))
  settings = Settings.new(defaults[GLOBAL_DEFAULTS_KEY] || {},
                          defaults,
                          module_configs[GLOBAL_DEFAULTS_KEY] || {},
                          module_configs,
                          :puppet_module => module_name,
                          :git_base => options[:git_base],
                          :namespace => namespace)
  settings.unmanaged_files(module_files).each do |filename|
    $stdout.puts "Not managing #{filename} in #{module_name}"
  end

  files_to_manage = settings.managed_files(module_files)
  files_to_manage.each { |filename| manage_file(filename, settings, options) }

  if options[:noop]
    Git.update_noop(git_repo, options)
    options[:pr] && pr(module_options).manage(namespace, module_name, options)
  elsif !options[:offline]
    pushed = Git.update(git_repo, files_to_manage, options)
    pushed && options[:pr] && pr(module_options).manage(namespace, module_name, options)
  end
end

.managed_modules(config_file, filter, negative_filter) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/modulesync.rb', line 53

def self.managed_modules(config_file, filter, negative_filter)
  managed_modules = Util.parse_config(config_file)
  if managed_modules.empty?
    $stderr.puts "No modules found in #{config_file}." \
      ' Check that you specified the right :configs directory and :managed_modules_conf file.'
    exit 1
  end
  managed_modules.select! { |m| m =~ Regexp.new(filter) } unless filter.nil?
  managed_modules.reject! { |m| m =~ Regexp.new(negative_filter) } unless negative_filter.nil?
  managed_modules
end

.module_file(project_root, namespace, puppet_module, *parts) ⇒ Object



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

def self.module_file(project_root, namespace, puppet_module, *parts)
  File.join(project_root, namespace, puppet_module, *parts)
end

.module_name(module_name, default_namespace) ⇒ Object



65
66
67
68
# File 'lib/modulesync.rb', line 65

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

.pr(module_options) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/modulesync.rb', line 187

def self.pr(module_options)
  module_options ||= {}
  github_conf = module_options[:github]
  gitlab_conf = module_options[:gitlab]

  if !github_conf.nil?
    base_url = github_conf[:base_url] || ENV.fetch('GITHUB_BASE_URL', 'https://api.github.com')
    require 'modulesync/pr/github'
    ModuleSync::PR::GitHub.new(github_conf[:token], base_url)
  elsif !gitlab_conf.nil?
    base_url = gitlab_conf[:base_url] || ENV.fetch('GITLAB_BASE_URL', 'https://gitlab.com/api/v4')
    require 'modulesync/pr/gitlab'
    ModuleSync::PR::GitLab.new(gitlab_conf[:token], base_url)
  elsif @pr.nil?
    $stderr.puts 'No GitHub or GitLab token specified for --pr!'
    raise
  else
    @pr
  end
end

.relative_names(file_list, path) ⇒ Object



49
50
51
# File 'lib/modulesync.rb', line 49

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

.update(options) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/modulesync.rb', line 151

def self.update(options)
  options = config_defaults.merge(options)
  defaults = Util.parse_config(config_path(CONF_FILE, options))
  if options[:pr]
    unless options[:branch]
      $stderr.puts 'A branch must be specified with --branch to use --pr!'
      raise
    end

    @pr = create_pr_manager if options[:pr]
  end

  local_template_dir = config_path(MODULE_FILES_DIR, options)
  local_files = find_template_files(local_template_dir)
  module_files = relative_names(local_files, local_template_dir)

  managed_modules = self.managed_modules(config_path(options[:managed_modules_conf], options),
                                         options[:filter],
                                         options[:negative_filter])

  errors = false
  # managed_modules is either an array or a hash
  managed_modules.each do |puppet_module, module_options|
    begin
      mod_options = module_options.nil? ? nil : Util.symbolize_keys(module_options)
      manage_module(puppet_module, module_files, mod_options, defaults, options)
    rescue # rubocop:disable Lint/RescueWithoutErrorClass
      $stderr.puts "Error while updating #{puppet_module}"
      raise unless options[:skip_broken]
      errors = true
      $stdout.puts "Skipping #{puppet_module} as update process failed"
    end
  end
  exit 1 if errors && options[:fail_on_warnings]
end

Instance Method Details

#config_path(file, options) ⇒ Object



147
148
149
# File 'lib/modulesync.rb', line 147

def config_path(file, options)
  self.class.config_path(file, options)
end