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

Methods included from Shared::Console

#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

#airbrake_configured?Boolean

Returns:

  • (Boolean)


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_idObject



104
105
106
# File 'lib/satorix.rb', line 104

def airbrake_project_id
  ENV['SATORIX_CI_AIRBRAKE_PROJECT_ID'].to_s
end

#airbrake_project_keyObject



109
110
111
# File 'lib/satorix.rb', line 109

def airbrake_project_key
  ENV['SATORIX_CI_AIRBRAKE_PROJECT_KEY'].to_s
end

#airbrake_startObject



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 # https://github.com/airbrake/airbrake-ruby#reporting-critical-exceptions
    Airbrake.close # https://github.com/airbrake/airbrake-ruby#airbrakeclose
  end
end

#app_dirObject



267
268
269
# File 'lib/satorix.rb', line 267

def app_dir
  paths[:app_dir]
end

#available_jobsObject



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_dirObject



272
273
274
# File 'lib/satorix.rb', line 272

def bin_dir
  File.join app_dir, 'bin'
end

#build_dirObject



277
278
279
# File 'lib/satorix.rb', line 277

def build_dir
  paths[:build_dir]
end

#ci_commit_ref_nameObject



189
190
191
# File 'lib/satorix.rb', line 189

def ci_commit_ref_name
  ENV['CI_COMMIT_REF_NAME']
end

#ci_commit_shaObject



194
195
196
# File 'lib/satorix.rb', line 194

def ci_commit_sha
  ENV['CI_COMMIT_SHA']
end

#ci_job_nameObject



179
180
181
# File 'lib/satorix.rb', line 179

def ci_job_name
  ENV['CI_JOB_NAME']
end

#ci_job_stageObject



184
185
186
# File 'lib/satorix.rb', line 184

def ci_job_stage
  ENV['CI_JOB_STAGE']
end

#current_branchObject



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_pathObject



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_pathObject



129
130
131
# File 'lib/satorix.rb', line 129

def custom_loader_relative_path
  File.join 'satorix', 'custom.rb'
end

#default_jobsObject



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/satorix.rb', line 156

def default_jobs
  { deploy: { deploy_with_flynn: Satorix::CI::Deploy::Flynn },
    test: { brakeman: Satorix::CI::Test::Ruby::Brakeman,
            bundler_audit: Satorix::CI::Test::Ruby::BundlerAudit,
            cucumber: Satorix::CI::Test::Ruby::Cucumber,
            django_test: Satorix::CI::Test::Python::DjangoTest,
            rails_test: Satorix::CI::Test::Ruby::RailsTest,
            rspec: Satorix::CI::Test::Ruby::Rspec,
            rubocop: Satorix::CI::Test::Ruby::Rubocop,
            safety: Satorix::CI::Test::Python::Safety } }
end

#django_app?Boolean

Returns:

  • (Boolean)


327
328
329
330
331
332
# File 'lib/satorix.rb', line 327

def django_app?
  # Satorix requires django apps be in a public directory.
  # The manage.py file seems to be present in all django applications.
  # It may be useful to add additional checks, for increased precision, in the future.
  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
    # the child process won't need to read from the pipe
    reader.close

    # use primary group ID of target user
    # This needs to be done first as we won't have
    # permission to change our group after changing EUID
    Process.gid = Process.egid = u.gid

    # set real and effective UIDs to target user
    Process.uid = Process.euid = u.uid

    # get the result and write it to the IPC pipe
    result = block.call(user)
    Marshal.dump(result, writer)
    writer.close

    # prevent shutdown hooks from running
    Process.exit!(true)
  end

  # back to reality... we won't be writing anything
  writer.close

  # block until there's data to read
  result = Marshal.load(reader)

  # done with that!
  reader.close

  # return block result
  result
rescue EOFError
  log_error 'This job has failed.'
  abort
end

#goObject



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
    log_header "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

    log_header "\nDone executing #{ ci_job_name } script for #{ ci_commit_ref_name }.\n"
  rescue Exception => e
    # TODO: add link to issue tracker
    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_classObject



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_customObject



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
      # TODO : create this documentation and link to it in the message below
      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

#pathsObject



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_environmentObject



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_nameObject



290
291
292
# File 'lib/satorix.rb', line 290

def project_name
  ENV['CI_PROJECT_NAME']
end

#rails_app?Boolean

Returns:

  • (Boolean)


317
318
319
320
321
322
323
324
# File 'lib/satorix.rb', line 317

def rails_app?
  # This used to follow the Heroku buildpack, with:
  # File.exist?(File.join(build_dir, 'Gemfile'))
  # It has been adjusted to accommodate non-rails apps that use ruby gems.
  # The config/application.rb file seems to be present in all rails applications.
  # It may be useful to add additional checks, for increased precision, in the future.
  File.exist?(File.join(build_dir, 'config', 'application.rb'))
end

#ruby_gem?Boolean

Returns:

  • (Boolean)


335
336
337
338
339
# File 'lib/satorix.rb', line 335

def ruby_gem?
  # Checks for the presence of a *.gemspec file in the project root.
  # It may be useful to add additional checks, for increased precision, in the future.
  Dir[File.join(build_dir, '*.gemspec')].any?
end

#setup_directoriesObject



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

Returns:

  • (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

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/satorix.rb', line 93

def user_exists?(username)
  Etc.getpwnam(username)
rescue
end

#yarn?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/satorix.rb', line 307

def yarn?
  File.exist? yarn_lock_file
end

#yarn_lock_fileObject



312
313
314
# File 'lib/satorix.rb', line 312

def yarn_lock_file
  File.join(app_dir, 'yarn.lock')
end