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.6.2'.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
86
87
88
89
90
|
# File 'lib/satorix.rb', line 86
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
|
99
100
101
|
# File 'lib/satorix.rb', line 99
def airbrake_configured?
[airbrake_project_id, airbrake_project_key].all? { |var| !var.empty? }
end
|
#airbrake_project_id ⇒ Object
104
105
106
|
# File 'lib/satorix.rb', line 104
def airbrake_project_id
ENV['SATORIX_CI_AIRBRAKE_PROJECT_ID'].to_s
end
|
#airbrake_project_key ⇒ Object
109
110
111
|
# File 'lib/satorix.rb', line 109
def airbrake_project_key
ENV['SATORIX_CI_AIRBRAKE_PROJECT_KEY'].to_s
end
|
#airbrake_start ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/satorix.rb', line 114
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
267
268
269
|
# File 'lib/satorix.rb', line 267
def app_dir
paths[:app_dir]
end
|
#available_jobs ⇒ Object
169
170
171
172
173
174
175
176
|
# File 'lib/satorix.rb', line 169
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
272
273
274
|
# File 'lib/satorix.rb', line 272
def bin_dir
File.join app_dir, 'bin'
end
|
#build_dir ⇒ Object
277
278
279
|
# File 'lib/satorix.rb', line 277
def build_dir
paths[:build_dir]
end
|
#ci_commit_ref_name ⇒ Object
189
190
191
|
# File 'lib/satorix.rb', line 189
def ci_commit_ref_name
ENV['CI_COMMIT_REF_NAME']
end
|
#ci_commit_sha ⇒ Object
194
195
196
|
# File 'lib/satorix.rb', line 194
def ci_commit_sha
ENV['CI_COMMIT_SHA']
end
|
#ci_job_name ⇒ Object
179
180
181
|
# File 'lib/satorix.rb', line 179
def ci_job_name
ENV['CI_JOB_NAME']
end
|
#ci_job_stage ⇒ Object
184
185
186
|
# File 'lib/satorix.rb', line 184
def ci_job_stage
ENV['CI_JOB_STAGE']
end
|
#current_branch ⇒ Object
199
200
201
|
# File 'lib/satorix.rb', line 199
def current_branch
@_current_branch ||= ci_commit_ref_name.to_s.upcase
end
|
#custom_loader_full_path ⇒ Object
134
135
136
|
# File 'lib/satorix.rb', line 134
def custom_loader_full_path
File.join build_dir, custom_loader_relative_path
end
|
#custom_loader_relative_path ⇒ Object
129
130
131
|
# File 'lib/satorix.rb', line 129
def custom_loader_relative_path
File.join 'satorix', 'custom.rb'
end
|
#django_app? ⇒ Boolean
327
328
329
330
331
332
|
# File 'lib/satorix.rb', line 327
def django_app?
File.exist?(File.join(build_dir, 'public', 'manage.py'))
end
|
#execute_as_user(user, &block) ⇒ Object
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
252
253
254
|
# File 'lib/satorix.rb', line 212
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/satorix.rb', line 57
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
204
205
206
207
208
|
# File 'lib/satorix.rb', line 204
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/satorix.rb', line 139
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
257
258
259
260
261
262
263
264
|
# File 'lib/satorix.rb', line 257
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
282
283
284
285
286
287
|
# File 'lib/satorix.rb', line 282
def prepare_app_environment
log_bench 'Preparing app environment...' do
add_user('satorix')
setup_directories
end
end
|
#project_name ⇒ Object
290
291
292
|
# File 'lib/satorix.rb', line 290
def project_name
ENV['CI_PROJECT_NAME']
end
|
#rails_app? ⇒ Boolean
317
318
319
320
321
322
323
324
|
# File 'lib/satorix.rb', line 317
def rails_app?
File.exist?(File.join(build_dir, 'config', 'application.rb'))
end
|
#ruby_gem? ⇒ Boolean
335
336
337
338
339
|
# File 'lib/satorix.rb', line 335
def ruby_gem?
Dir[File.join(build_dir, '*.gemspec')].any?
end
|
#setup_directories ⇒ Object
295
296
297
298
299
|
# File 'lib/satorix.rb', line 295
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
302
303
304
|
# File 'lib/satorix.rb', line 302
def skip_buildpack?
job_class.respond_to?(:skip_buildpack) && job_class.skip_buildpack
end
|
#user_exists?(username) ⇒ Boolean
93
94
95
96
|
# File 'lib/satorix.rb', line 93
def user_exists?(username)
Etc.getpwnam(username)
rescue
end
|
#yarn? ⇒ Boolean
307
308
309
|
# File 'lib/satorix.rb', line 307
def yarn?
File.exist? yarn_lock_file
end
|
#yarn_lock_file ⇒ Object
312
313
314
|
# File 'lib/satorix.rb', line 312
def yarn_lock_file
File.join(app_dir, 'yarn.lock')
end
|