Class: CapistranoMulticonfigParallel::JobCommand
- Inherits:
-
Object
- Object
- CapistranoMulticonfigParallel::JobCommand
show all
- Includes:
- ApplicationHelper, FileUtils
- Defined in:
- lib/capistrano_multiconfig_parallel/classes/job_command.rb
Overview
class used to find application dependencies
Instance Attribute Summary collapse
Instance Method Summary
collapse
action_confirmed?, fetch_parsed_string, fetch_remaining_arguments, find_remaining_args, get_question_details, message_from_bundler?, message_is_about_a_task?, message_is_for_stdout?, msg_for_stdin?, parse_json, parse_task_string, percent_of, regex_last_match, setup_command_line_standard, truncate, wrap_string
env_key_format, env_prefix, filtered_env_keys_format, setup_flags_for_job, trace_flag
Methods included from GemHelper
fetch_gem_version, find_loaded_gem, find_loaded_gem_property, format_gem_version, get_parsed_version, verify_gem_version
app_names_from_stages, check_stage_path, checks_paths, fetch_stages_app, fetch_stages_from_file, fetch_stages_paths, independent_deploy?, multi_apps?, sorted_paths, stages, stages_paths, stages_root
check_hash_set, check_numeric, strip_characters_from_string, value_is_array?, verify_array_of_strings, verify_empty_options, warn_array_without_strings
Methods included from CoreHelper
app_debug_enabled?, ask_confirm, ask_stdout_confirmation, check_terminal_tty, debug_websocket?, development_debug?, error_filtered?, execute_with_rescue, find_worker_log, force_confirmation, format_error, log_error, log_output_error, log_to_file, multi_fetch_argv, print_to_log_file, rescue_error, rescue_interrupt, setup_filename_logger, setup_logger_formatter, show_warning, terminal_actor, terminal_errors?, websocket_config, websocket_server_config
arg_is_in_default_config?, check_file, create_log_file, custom_commands, default_config_keys, default_internal_config, default_internal_configuration_params, detect_root, enable_main_log_file, fail_capfile_not_found, fetch_default_internal_config, find_config_type, find_env_multi_cap_root, find_file_by_names, find_file_in_directory, get_current_gem_name, internal_config_directory, internal_config_file, log_directory, main_log_file, multi_level_prop, pathname_is_root?, pwd_directory, pwd_parent_dir, root, setup_default_configuration_types, sliced_default_config, try_detect_file
Constructor Details
Returns a new instance of JobCommand.
12
13
14
15
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 12
def initialize(job)
@job = job
@job_final_gemfile = job_gemfile_multi
end
|
Instance Attribute Details
#job ⇒ Object
Returns the value of attribute job.
9
10
11
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 9
def job
@job
end
|
#job_capistrano_version ⇒ Object
Returns the value of attribute job_capistrano_version.
9
10
11
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 9
def job_capistrano_version
@job_capistrano_version
end
|
#job_final_capfile ⇒ Object
Returns the value of attribute job_final_capfile.
9
10
11
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 9
def job_final_capfile
@job_final_capfile
end
|
#job_final_gemfile ⇒ Object
Returns the value of attribute job_final_gemfile.
9
10
11
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 9
def job_final_gemfile
@job_final_gemfile
end
|
#legacy_capistrano ⇒ Object
Returns the value of attribute legacy_capistrano.
9
10
11
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 9
def legacy_capistrano
@legacy_capistrano
end
|
#tempfile ⇒ Object
Returns the value of attribute tempfile.
9
10
11
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 9
def tempfile
@tempfile
end
|
Instance Method Details
#bash_bin_path ⇒ Object
164
165
166
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 164
def bash_bin_path
@bash_bin_path ||= `which bash`
end
|
#bundle_gemfile_env(gemfile = job_gemfile) ⇒ Object
63
64
65
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 63
def bundle_gemfile_env(gemfile = job_gemfile)
"BUNDLE_GEMFILE=#{gemfile}"
end
|
#capfile_name ⇒ Object
38
39
40
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 38
def capfile_name
find_capfile.present? ? find_capfile.basename : nil
end
|
#capistrano_action ⇒ Object
98
99
100
101
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 98
def capistrano_action
argv = task_arguments.present? ? "[#{task_arguments}]" : ''
"#{action}#{argv}"
end
|
#capistrano_sentinel_available? ⇒ Boolean
77
78
79
80
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 77
def capistrano_sentinel_available?
gitflow_version = job_gem_version(capistrano_sentinel_name)
gitflow_version.present? ? true : false
end
|
#capistrano_sentinel_name ⇒ Object
73
74
75
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 73
def capistrano_sentinel_name
"capistrano_sentinel"
end
|
#capistrano_sentinel_needs_updating? ⇒ Boolean
138
139
140
141
142
143
144
145
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 138
def capistrano_sentinel_needs_updating?
if capistrano_sentinel_available?
loaded_capistrano_sentinel_version == job_capistrano_sentinel_version
else
true
end
end
|
#check_capistrano_sentinel_availability ⇒ Object
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 275
def check_capistrano_sentinel_availability
if capistrano_sentinel_available?
@job_final_gemfile = job_gemfile
else
FileUtils.rm_rf(job_gemfile_multi) if File.exists?(job_gemfile_multi)
FileUtils.touch(job_gemfile_multi)
File.open(job_gemfile_multi, 'w') do |f|
cmd=" source \"https://rubygems.org\" do\n gem \"\#{capistrano_sentinel_name}\", '\#{find_loaded_gem_property(capistrano_sentinel_name)}'\n end\n instance_eval(File.read(File.dirname(__FILE__) + \"/Gemfile\"))\n CMD\n f.write(cmd)\n end\n FileUtils.copy(File.join(job_path, 'Gemfile.lock'), \"\#{job_gemfile_multi}.lock\")\n end\nend\n"
|
#check_rvm_loaded ⇒ Object
195
196
197
198
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 195
def check_rvm_loaded
return "cd #{job_path}" unless rvm_enabled_for_job?
"source #{rvm_scripts_path} && rvm rvmrc trust #{job_path} && cd #{job_path} && source #{job_rvmrc_file}"
end
|
#create_job_tempfile_command(output) ⇒ Object
172
173
174
175
176
177
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 172
def create_job_tempfile_command(output)
@tempfile = Tempfile.new(["multi_cap_#{job.id}_command_", ".rb"], encoding: 'utf-8')
@tempfile.write(output)
ObjectSpace.undefine_finalizer(@tempfile)
@tempfile.close
end
|
#env_option_filtered?(key, filtered_keys_array = []) ⇒ Boolean
103
104
105
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 103
def env_option_filtered?(key, filtered_keys_array = [])
filtered_env_keys.include?(env_key_format(key)) || filtered_keys_array.include?(key.to_s)
end
|
#execute_standard_deploy(action = nil) ⇒ Object
328
329
330
331
332
333
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 328
def execute_standard_deploy(action = nil)
run_shell_command(fetch_deploy_command)
rescue => ex
rescue_error(ex, 'stderr')
execute_standard_deploy('deploy:rollback') if action.blank? && @name == 'deploy'
end
|
#fetch_bundler_check_command(gemfile = job_gemfile) ⇒ Object
25
26
27
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 25
def fetch_bundler_check_command(gemfile = job_gemfile)
"#{check_rvm_loaded} && if [ `which bundler |wc -l` = 0 ]; then gem install bundler;fi && (#{bundle_gemfile_env(gemfile)} bundle check || #{bundle_gemfile_env(gemfile)} bundle install )"
end
|
#fetch_bundler_worker_command ⇒ Object
29
30
31
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 29
def fetch_bundler_worker_command
get_command_script(fetch_bundler_check_command, "bundler")
end
|
#fetch_deploy_command ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 237
def fetch_deploy_command
prepare_application_for_deployment
environment_options = setup_command_line.join(' ')
command = "#{fetch_bundler_check_command(@job_final_gemfile)} && WEBSOCKET_LOGGING=#{debug_websocket?} LOG_FILE=#{websocket_config.fetch('log_file_path', nil)} #{bundle_gemfile_env(@job_final_gemfile)} bundle exec cap #{job_stage} #{capistrano_action} #{environment_options}"
if @job_final_capfile != job_capfile
command += " -f #{@job_final_capfile}"
end
get_command_script(command)
end
|
#filtered_env_keys ⇒ Object
59
60
61
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 59
def filtered_env_keys
%w(STAGES ACTION)
end
|
#find_capfile(custom_path = job_path) ⇒ Object
34
35
36
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 34
def find_capfile(custom_path = job_path)
@capfile_path ||= find_file_by_names(custom_path, 'capfile').first
end
|
#gem_specs ⇒ Object
42
43
44
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 42
def gem_specs
@specs = lockfile_parser.specs
end
|
#get_command_script(command, action = nil) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 212
def get_command_script(command, action = nil)
command = rvm_bash_prefix(command)
command = command.inspect
command_text =" require 'rubygems'\n require 'bundler'\n Bundler.with_clean_env {\n Kernel.exec(\#{command})\n }\n CMD\n\n if rvm_enabled_for_job?\n create_job_tempfile_command(command_text)\n log_to_worker(\"JOB \#{@job_id} created Tempfile \#{@tempfile.path} with contents \#{File.read(@tempfile.path)}\", action)\n \"ruby \#{@tempfile.path}\"\n else\n final_command=<<-CMD\n cd \#{job_path} && bundle exec ruby -e \"\#{command_text}\"\n CMD\n log_to_worker(\"JOB \#{@job_id} prepared command \#{final_command}\", action)\n final_command\n end\nend\n"
|
#gitflow_enabled? ⇒ Boolean
68
69
70
71
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 68
def gitflow_enabled?
gitflow_version = job_gem_version("capistrano-gitflow")
gitflow_version.present? ? true : false
end
|
#job_capfile ⇒ Object
251
252
253
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 251
def job_capfile
File.join(job_path, capfile_name.to_s)
end
|
#job_capfile_multi ⇒ Object
255
256
257
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 255
def job_capfile_multi
"#{job_capfile}.multi_cap"
end
|
#job_capistrano_sentinel_version ⇒ Object
86
87
88
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 86
def job_capistrano_sentinel_version
job_gem_version(capistrano_sentinel_name)
end
|
#job_gem_version(gem_name) ⇒ Object
54
55
56
57
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 54
def job_gem_version(gem_name)
gem_spec = gem_specs.find {|spec| spec.name == gem_name}
gem_spec.present? ? gem_spec.version.to_s : nil
end
|
#job_gemfile ⇒ Object
46
47
48
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 46
def job_gemfile
File.join(job_path, 'Gemfile')
end
|
#job_gemfile_lock ⇒ Object
50
51
52
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 50
def job_gemfile_lock
File.join(job_path, 'Gemfile.lock')
end
|
#job_gemfile_multi ⇒ Object
259
260
261
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 259
def job_gemfile_multi
File.join(job_path, "Gemfile.multi_cap")
end
|
#job_path ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 147
def job_path
if path.present? && File.directory?(path) && find_capfile(path).present?
path
else
detect_root
end
end
|
#job_rvmrc_enabled? ⇒ Boolean
187
188
189
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 187
def job_rvmrc_enabled?
File.exists?(job_rvmrc_file)
end
|
#job_rvmrc_file ⇒ Object
183
184
185
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 183
def job_rvmrc_file
File.join(job_path, '.rvmrc')
end
|
#job_stage ⇒ Object
94
95
96
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 94
def job_stage
multi_apps?(job_path) && app.present? ? "#{app}:#{stage}" : "#{stage}"
end
|
#job_stage_for_terminal ⇒ Object
90
91
92
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 90
def job_stage_for_terminal
app.present? ? "#{app}:#{stage}" : "#{stage}"
end
|
#legacy_capistrano_version? ⇒ Boolean
130
131
132
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 130
def legacy_capistrano_version?
verify_gem_version(job_capistrano_version, '3.0', operator: '<')
end
|
#loaded_capistrano_sentinel_version ⇒ Object
82
83
84
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 82
def loaded_capistrano_sentinel_version
find_loaded_gem_property(capistrano_sentinel_name)
end
|
#lockfile_parser ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 17
def lockfile_parser
if File.exists?(job_gemfile) && File.exists?(job_gemfile_lock)
@lockfile_parser ||= Bundler::LockfileParser.new(Bundler.read_file("#{job_gemfile_lock}"))
else
raise "please install the gems separately for this application #{job_path} and re-try again!"
end
end
|
#log_to_worker(message, action = nil) ⇒ Object
204
205
206
207
208
209
210
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 204
def log_to_worker(message, action = nil)
if action.present? || action.to_s == id.to_s
log_to_file(message, job_id: id, prefix: action)
else
log_to_file(message)
end
end
|
#prepare_application_for_deployment ⇒ Object
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 263
def prepare_application_for_deployment
config = @job.application.patched_job_paths.find{|hash| hash[:path] == job_path}
if config.present?
@job_final_gemfile = config[:gemfile]
@job_final_capfile = config[:capfile]
else
check_capistrano_sentinel_availability
prepare_capfile
@job.application.patched_job_paths << {path: job_path, gemfile: @job_final_gemfile, capfile: @job_final_capfile}
end
end
|
#prepare_capfile ⇒ Object
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 296
def prepare_capfile
capfile_valid = File.foreach(job_capfile).grep(/#{capistrano_sentinel_name}/).any?
if capistrano_sentinel_available? && capfile_valid.present?
@job_final_capfile = job_capfile
elsif capfile_valid.blank? && capistrano_sentinel_available?
@job_final_capfile = job_capfile
File.open(job_capfile, 'a+') do |f|
cmd=" require \"\#{capistrano_sentinel_name}\"\n CMD\n f.write(cmd)\n end\n elsif capistrano_sentinel_available? == false\n @job_final_capfile = job_capfile_multi\n FileUtils.copy(job_capfile, job_capfile_multi)\n File.open(job_capfile_multi, 'a+') do |f|\n cmd=<<-CMD\n require \"\#{capistrano_sentinel_name}\"\n CMD\n f.write(cmd)\n end\n end\nend\n"
|
#rollback_changes_to_application ⇒ Object
321
322
323
324
325
326
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 321
def rollback_changes_to_application
FileUtils.rm_rf(job_gemfile_multi) if File.exists?(job_gemfile_multi)
FileUtils.rm_rf("#{job_gemfile_multi}.lock") if File.exists?("#{job_gemfile_multi}.lock")
FileUtils.rm_rf(job_capfile_multi) if File.exists?(job_capfile_multi)
FileUtils.rm_rf(@tempfile.path) if defined?(@tempfile) && @tempfile
end
|
#rvm_bash_prefix(command) ⇒ Object
200
201
202
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 200
def rvm_bash_prefix(command)
rvm_enabled_for_job? ? "bash --login -c '#{command}'" : command
end
|
#rvm_bin_path ⇒ Object
160
161
162
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 160
def rvm_bin_path
@rvm_path ||= `which rvm`
end
|
#rvm_enabled_for_job? ⇒ Boolean
191
192
193
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 191
def rvm_enabled_for_job?
job_rvmrc_enabled? && rvm_installed? && bash_bin_path.present?
end
|
#rvm_installed? ⇒ Boolean
168
169
170
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 168
def rvm_installed?
rvm_bin_path.present?
end
|
#rvm_scripts_path ⇒ Object
179
180
181
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 179
def rvm_scripts_path
File.join(File.dirname(File.dirname(rvm_bin_path)), 'scripts', 'rvm')
end
|
#setup_command_line(*args) ⇒ Object
121
122
123
124
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 121
def setup_command_line(*args)
new_arguments, options = setup_command_line_standard(*args)
setup_env_options(options).concat(new_arguments)
end
|
#setup_env_options(options = {}) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 107
def setup_env_options(options = {})
array_options = []
filtered_keys = options.delete(:filtered_keys) || []
env_options.each do |key, value|
array_options << "#{env_prefix(key, legacy_capistrano)} #{env_key_format(key, legacy_capistrano)}=#{value}" if value.present? && !env_option_filtered?(key, filtered_keys)
end
setup_remaining_flags(array_options, options)
end
|
#setup_remaining_flags(array_options, options) ⇒ Object
116
117
118
119
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 116
def setup_remaining_flags(array_options, options)
array_options << trace_flag(legacy_capistrano) if app_debug_enabled?
array_options.concat(setup_flags_for_job(options))
end
|
#user_home_directory ⇒ Object
155
156
157
158
|
# File 'lib/capistrano_multiconfig_parallel/classes/job_command.rb', line 155
def user_home_directory
user = Etc.getlogin
Dir.home(user)
end
|