Module: ModuleSync
- Includes:
- Constants
- Defined in:
- lib/modulesync.rb,
lib/modulesync/cli.rb,
lib/modulesync/hook.rb,
lib/modulesync/util.rb,
lib/modulesync/version.rb,
lib/modulesync/cli/thor.rb,
lib/modulesync/renderer.rb,
lib/modulesync/settings.rb,
lib/modulesync/constants.rb,
lib/modulesync/repository.rb,
lib/modulesync/git_service.rb,
lib/modulesync/source_code.rb,
lib/modulesync/puppet_module.rb,
lib/modulesync/git_service/base.rb,
lib/modulesync/git_service/github.rb,
lib/modulesync/git_service/gitlab.rb,
lib/modulesync/git_service/factory.rb
Defined Under Namespace
Modules: CLI, Constants, GitService, Renderer, Util
Classes: Error, Hook, PuppetModule, Repository, Settings, SourceCode
Constant Summary
collapse
- VERSION =
'4.4.0'
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
.clone(cli_options) ⇒ Object
201
202
203
204
205
206
207
|
# File 'lib/modulesync.rb', line 201
def self.clone(cli_options)
@options = config_defaults.merge(cli_options)
managed_modules.each do |puppet_module|
puppet_module.repository.clone unless puppet_module.repository.cloned?
end
end
|
.config_defaults ⇒ Object
23
24
25
26
27
28
29
30
|
# File 'lib/modulesync.rb', line 23
def self.config_defaults
{
project_root: 'modules/',
managed_modules_conf: 'managed_modules.yml',
configs: '.',
tag_pattern: '%s',
}
end
|
.config_path(file, options) ⇒ Object
160
161
162
163
164
|
# File 'lib/modulesync.rb', line 160
def self.config_path(file, options)
return file if Pathname.new(file).absolute?
File.join(options[:configs], file)
end
|
.execute(cli_options) ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/modulesync.rb', line 209
def self.execute(cli_options)
@options = config_defaults.merge(cli_options)
errors = {}
managed_modules.each do |puppet_module|
$stdout.puts "#{puppet_module.given_name}:"
puppet_module.repository.clone unless puppet_module.repository.cloned?
if @options[:default_branch]
puppet_module.repository.switch branch: false
else
puppet_module.repository.switch branch: @options[:branch]
end
command_args = cli_options[:command_args]
env_whitelist = (@options[:env] || '').split(',')
local_script = File.expand_path command_args[0]
command_args[0] = local_script if File.exist?(local_script)
command_env = ENV.reject { |k, _v| k.match?(/(^BUNDLE|^SOURCE_DATE_EPOCH$|^GEM_|RUBY)/) && !env_whitelist.include?(k) }
result = system command_env, *command_args, unsetenv_others: true, chdir: puppet_module.working_directory
unless result
message = "Command execution failed ('#{@options[:command_args].join ' '}': #{$CHILD_STATUS})"
raise Thor::Error, message if @options[:fail_fast]
errors[puppet_module.given_name] = message
warn message
end
$stdout.puts ''
end
unless errors.empty?
raise Thor::Error, <<~MSG
Error(s) during `execute` command:
#{errors.map { |name, message| " * #{name}: #{message}" }.join "\n"}
MSG
end
exit 1 unless errors.empty?
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
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/modulesync.rb', line 50
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
warn "#{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
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/modulesync.rb', line 83
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
36
37
38
39
40
41
42
43
44
|
# File 'lib/modulesync.rb', line 36
def self.local_file(config_path, file)
path = File.join(config_path, MODULE_FILES_DIR, file)
if !File.exist?("#{path}.erb") && File.exist?(path)
warn "Warning: using '#{path}' as template without '.erb' suffix"
path
else
"#{path}.erb"
end
end
|
.manage_file(puppet_module, filename, settings, options) ⇒ Object
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
|
# File 'lib/modulesync.rb', line 94
def self.manage_file(puppet_module, filename, settings, options)
configs = settings.build_file_configs(filename)
target_file = puppet_module.path(filename)
if configs['delete']
Renderer.remove(target_file)
else
template_file = local_file(options[:configs], filename)
begin
erb = Renderer.build(template_file)
metadata = {
module_name: settings.additional_settings[:puppet_module],
namespace: settings.additional_settings[:namespace],
workdir: puppet_module.working_directory,
target_file: target_file,
}
template = Renderer.render(erb, configs, metadata)
mode = File.stat(template_file).mode
Renderer.sync(template, target_file, mode)
rescue StandardError
warn "#{puppet_module.given_name}: Error while rendering file: '#{filename}'"
raise
end
end
end
|
.manage_module(puppet_module, module_files, defaults) ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/modulesync.rb', line 120
def self.manage_module(puppet_module, module_files, defaults)
puts "Syncing '#{puppet_module.given_name}'"
unless options[:offline]
puppet_module.repository.prepare_workspace(branch: options[:branch],
operate_offline: false)
end
module_configs = Util.parse_config puppet_module.path(MODULE_CONF_FILE)
settings = Settings.new(defaults[GLOBAL_DEFAULTS_KEY] || {},
defaults,
module_configs[GLOBAL_DEFAULTS_KEY] || {},
module_configs,
puppet_module: puppet_module.repository_name,
git_base: options[:git_base],
namespace: puppet_module.repository_namespace)
settings.unmanaged_files(module_files).each do |filename|
$stdout.puts "Not managing '#{filename}' in '#{puppet_module.given_name}'"
end
files_to_manage = settings.managed_files(module_files)
files_to_manage.each { |filename| manage_file(puppet_module, filename, settings, options) }
if options[:noop]
puts "Using no-op. Files in '#{puppet_module.given_name}' may be changed but will not be committed."
changed = puppet_module.repository.show_changes(options)
changed && options[:pr] && puppet_module.open_pull_request
elsif !options[:offline]
pushed = puppet_module.repository.submit_changes(files_to_manage, options)
if pushed && options[:bump]
new = puppet_module.bump(options[:message], options[:changelog])
puppet_module.repository.tag(new, options[:tag_pattern]) if options[:tag]
end
options[:pr] && (pushed || puppet_module.pull_request_branch_ready?) && puppet_module.open_pull_request
end
end
|
.managed_modules ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/modulesync.rb', line 67
def self.managed_modules
config_file = config_path(options[:managed_modules_conf], options)
filter = options[:filter]
negative_filter = options[:negative_filter]
managed_modules = Util.parse_config(config_file)
if managed_modules.empty?
warn "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.map { |given_name, options| PuppetModule.new(given_name, options) }
end
|
.options ⇒ Object
32
33
34
|
# File 'lib/modulesync.rb', line 32
def self.options
@options
end
|
.push(cli_options) ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
# File 'lib/modulesync.rb', line 269
def self.push(cli_options)
@options = config_defaults.merge(cli_options)
if @options[:branch].nil?
raise Thor::Error,
"Error: 'branch' option is missing, please set it in configuration or in command line."
end
managed_modules.each do |puppet_module|
puppet_module.repository.push branch: @options[:branch], remote_branch: @options[:remote_branch]
rescue ModuleSync::Error => e
raise Thor::Error, "#{puppet_module.given_name}: #{e.message}"
end
end
|
.relative_names(file_list, path) ⇒ Object
63
64
65
|
# File 'lib/modulesync.rb', line 63
def self.relative_names(file_list, path)
file_list.map { |file| file.sub(/#{path}/, '') }
end
|
.reset(cli_options) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/modulesync.rb', line 253
def self.reset(cli_options)
@options = config_defaults.merge(cli_options)
if @options[:branch].nil?
raise Thor::Error,
"Error: 'branch' option is missing, please set it in configuration or in command line."
end
managed_modules.each do |puppet_module|
puppet_module.repository.reset_workspace(
branch: @options[:branch],
source_branch: @options[:source_branch],
operate_offline: @options[:offline],
)
end
end
|
.update(cli_options) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/modulesync.rb', line 170
def self.update(cli_options)
@options = config_defaults.merge(cli_options)
defaults = Util.parse_config(config_path(CONF_FILE, options))
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)
initialized_modules = managed_modules
initialized_modules.each(&:git_service) if options[:pr]
errors = false
initialized_modules.each do |puppet_module|
manage_module(puppet_module, module_files, defaults)
rescue ModuleSync::Error, Git::Error => e
message = e.message || 'Error during `update`'
warn "#{puppet_module.given_name}: #{message}"
exit 1 unless options[:skip_broken]
errors = true
$stdout.puts "Skipping '#{puppet_module.given_name}' as update process failed"
rescue StandardError
raise unless options[:skip_broken]
errors = true
$stdout.puts "Skipping '#{puppet_module.given_name}' as update process failed"
end
exit 1 if errors && options[:fail_on_warnings]
end
|
Instance Method Details
#config_path(file, options) ⇒ Object
166
167
168
|
# File 'lib/modulesync.rb', line 166
def config_path(file, options)
self.class.config_path(file, options)
end
|