Module: Satorix
- Extended by:
- Satorix
- Includes:
- Shared::Console
- Included in:
- Satorix, CI::Deploy::Flynn
- Defined in:
- lib/satorix.rb,
lib/satorix/version.rb,
lib/satorix/shared/console.rb,
lib/satorix/CI/deploy/flynn.rb,
lib/satorix/CI/test/ruby/rspec.rb,
lib/satorix/CI/test/ruby/rubocop.rb,
lib/satorix/CI/deploy/flynn/scale.rb,
lib/satorix/CI/test/python/safety.rb,
lib/satorix/CI/test/ruby/brakeman.rb,
lib/satorix/CI/test/ruby/cucumber.rb,
lib/satorix/CI/deploy/flynn/routes.rb,
lib/satorix/CI/shared/yarn_manager.rb,
lib/satorix/CI/test/ruby/rails_test.rb,
lib/satorix/CI/test/shared/database.rb,
lib/satorix/CI/deploy/flynn/resources.rb,
lib/satorix/CI/shared/ruby/gem_manager.rb,
lib/satorix/CI/test/python/django_test.rb,
lib/satorix/CI/test/ruby/bundler_audit.rb,
lib/satorix/CI/shared/buildpack_manager.rb,
lib/satorix/CI/deploy/flynn/environment_variables.rb,
lib/satorix/CI/shared/buildpack_manager/buildpack.rb
Defined Under Namespace
Modules: CI, Shared
Constant Summary
collapse
- VERSION =
'1.5.4'.freeze
Instance Method Summary
collapse
#colorize, #colors, #humanize_time, #log, #log_bench, #log_command, #log_duration, #log_error, #log_error_and_abort, #log_header, #run_command, #source_env_from
Instance Method Details
#add_user(username) ⇒ Object
90
91
92
93
94
|
# File 'lib/satorix.rb', line 90
def add_user(username)
unless user_exists?(username)
run_command ['useradd', '--user-group', '--comment', "'#{ username } user'", '--shell', '/bin/bash', '--home', app_dir, username], quiet: true
end
end
|
103
104
105
|
# File 'lib/satorix.rb', line 103
def airbrake_configured?
[airbrake_project_id, airbrake_project_key].all? { |var| !var.empty? }
end
|
#airbrake_project_id ⇒ Object
108
109
110
|
# File 'lib/satorix.rb', line 108
def airbrake_project_id
ENV['SATORIX_CI_AIRBRAKE_PROJECT_ID'].to_s
end
|
#airbrake_project_key ⇒ Object
113
114
115
|
# File 'lib/satorix.rb', line 113
def airbrake_project_key
ENV['SATORIX_CI_AIRBRAKE_PROJECT_KEY'].to_s
end
|
#airbrake_start ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/satorix.rb', line 118
def airbrake_start
return unless airbrake_configured?
Airbrake.configure do |c|
c.project_id = airbrake_project_id
c.project_key = airbrake_project_key
end
at_exit do
Airbrake.notify_sync($ERROR_INFO) if $ERROR_INFO Airbrake.close end
end
|
#app_dir ⇒ Object
271
272
273
|
# File 'lib/satorix.rb', line 271
def app_dir
paths[:app_dir]
end
|
#available_jobs ⇒ Object
173
174
175
176
177
178
179
180
|
# File 'lib/satorix.rb', line 173
def available_jobs
@_available_jobs ||= load_custom.tap do |jobs|
default_jobs.each do |stage, job_definitions|
jobs[stage] ||= {}
job_definitions.each { |job, target| jobs[stage][job] = target }
end
end
end
|
#bin_dir ⇒ Object
276
277
278
|
# File 'lib/satorix.rb', line 276
def bin_dir
File.join app_dir, 'bin'
end
|
#build_dir ⇒ Object
281
282
283
|
# File 'lib/satorix.rb', line 281
def build_dir
paths[:build_dir]
end
|
#ci_commit_ref_name ⇒ Object
193
194
195
|
# File 'lib/satorix.rb', line 193
def ci_commit_ref_name
ENV['CI_COMMIT_REF_NAME']
end
|
#ci_commit_sha ⇒ Object
198
199
200
|
# File 'lib/satorix.rb', line 198
def ci_commit_sha
ENV['CI_COMMIT_SHA']
end
|
#ci_job_name ⇒ Object
183
184
185
|
# File 'lib/satorix.rb', line 183
def ci_job_name
ENV['CI_JOB_NAME']
end
|
#ci_job_stage ⇒ Object
188
189
190
|
# File 'lib/satorix.rb', line 188
def ci_job_stage
ENV['CI_JOB_STAGE']
end
|
#current_branch ⇒ Object
203
204
205
|
# File 'lib/satorix.rb', line 203
def current_branch
@_current_branch ||= ci_commit_ref_name.to_s.upcase
end
|
#custom_loader_full_path ⇒ Object
138
139
140
|
# File 'lib/satorix.rb', line 138
def custom_loader_full_path
File.join build_dir, custom_loader_relative_path
end
|
#custom_loader_relative_path ⇒ Object
133
134
135
|
# File 'lib/satorix.rb', line 133
def custom_loader_relative_path
File.join 'satorix', 'custom.rb'
end
|
#django_app? ⇒ Boolean
331
332
333
334
335
336
|
# File 'lib/satorix.rb', line 331
def django_app?
File.exist?(File.join(build_dir, 'public', 'manage.py'))
end
|
#execute_as_user(user, &block) ⇒ Object
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
252
253
254
255
256
257
258
|
# File 'lib/satorix.rb', line 216
def execute_as_user(user, &block)
u = (user.is_a? Integer) ? Etc.getpwuid(user) : Etc.getpwnam(user)
ENV['HOME'] = Satorix.app_dir
reader, writer = IO.pipe
Process.fork do
reader.close
Process.gid = Process.egid = u.gid
Process.uid = Process.euid = u.uid
result = block.call(user)
Marshal.dump(result, writer)
writer.close
Process.exit!(true)
end
writer.close
result = Marshal.load(reader)
reader.close
result
rescue EOFError
log_error 'This job has failed.'
abort
end
|
#go ⇒ Object
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
|
# File 'lib/satorix.rb', line 61
def go
airbrake_start
prepare_app_environment
begin
"Executing #{ ci_job_name } script for #{ ci_commit_ref_name }..."
execute_as_user 'satorix' do
Dir.chdir(build_dir)
unless skip_buildpack?
Satorix::CI::Shared::BuildpackManager.go
Dir.chdir(app_dir)
end
Satorix::CI::Shared::Ruby::GemManager.go if ruby_gem?
Satorix::CI::Shared::YarnManager.go if yarn?
job_class.go
end
"\nDone executing #{ ci_job_name } script for #{ ci_commit_ref_name }.\n"
rescue Exception => e
log 'If you feel this failure was in error, please check the issue tracker for more information.'
Airbrake.notify(e) if airbrake_configured?
raise e
end
end
|
#job_class ⇒ Object
208
209
210
211
212
|
# File 'lib/satorix.rb', line 208
def job_class
available_jobs[ci_job_stage.to_sym] && available_jobs[ci_job_stage.to_sym][ci_job_name.to_sym] || begin
log_error_and_abort "The #{ ci_job_name } job does not exist for the #{ ci_job_stage } stage!"
end
end
|
#load_custom ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/satorix.rb', line 143
def load_custom
log_bench "Looking for custom job definitions in #{ custom_loader_relative_path }....." do
if File.exist? custom_loader_full_path
log "Loading custom job definitions from #{ custom_loader_relative_path }."
require custom_loader_full_path
Satorix::Custom.available_jobs
else
log 'No custom jobs found.'
log "You can define custom jobs by adding a #{ custom_loader_relative_path } file to your app root."
log 'For more information, please refer to https://www.satorix.com/docs/articles/custom_satorix_jobs.'
{}
end
end
end
|
#paths ⇒ Object
261
262
263
264
265
266
267
268
|
# File 'lib/satorix.rb', line 261
def paths
ci_project_dir = ENV['CI_PROJECT_DIR'].to_s
{ buildpacks: '/tmp/buildpacks',
app_dir: '/tmp/app',
build_dir: ci_project_dir,
cache: File.join(ci_project_dir, 'tmp', 'satorix', 'cache'),
env: '/tmp/env' }
end
|
#prepare_app_environment ⇒ Object
286
287
288
289
290
291
|
# File 'lib/satorix.rb', line 286
def prepare_app_environment
log_bench 'Preparing app environment...' do
add_user('satorix')
setup_directories
end
end
|
#project_name ⇒ Object
294
295
296
|
# File 'lib/satorix.rb', line 294
def project_name
ENV['CI_PROJECT_NAME']
end
|
#rails_app? ⇒ Boolean
321
322
323
324
325
326
327
328
|
# File 'lib/satorix.rb', line 321
def rails_app?
File.exist?(File.join(build_dir, 'config', 'application.rb'))
end
|
#ruby_gem? ⇒ Boolean
339
340
341
342
343
|
# File 'lib/satorix.rb', line 339
def ruby_gem?
Dir[File.join(build_dir, '*.gemspec')].any?
end
|
#setup_directories ⇒ Object
299
300
301
302
303
|
# File 'lib/satorix.rb', line 299
def setup_directories
paths.values.each { |path| FileUtils.mkdir_p path }
paths.values.each { |path| FileUtils.chown_R 'satorix', 'satorix', path }
FileUtils.ln_s app_dir, '/app'
end
|
#skip_buildpack? ⇒ Boolean
306
307
308
|
# File 'lib/satorix.rb', line 306
def skip_buildpack?
job_class.respond_to?(:skip_buildpack) && job_class.skip_buildpack
end
|
#user_exists?(username) ⇒ Boolean
97
98
99
100
|
# File 'lib/satorix.rb', line 97
def user_exists?(username)
Etc.getpwnam(username)
rescue
end
|
#yarn? ⇒ Boolean
311
312
313
|
# File 'lib/satorix.rb', line 311
def yarn?
File.exist? yarn_lock_file
end
|
#yarn_lock_file ⇒ Object
316
317
318
|
# File 'lib/satorix.rb', line 316
def yarn_lock_file
File.join(app_dir, 'yarn.lock')
end
|