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

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



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

#airbrake_configured?Boolean

Returns:

  • (Boolean)


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_idObject



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

def airbrake_project_id
  ENV['SATORIX_CI_AIRBRAKE_PROJECT_ID'].to_s
end

#airbrake_project_keyObject



113
114
115
# File 'lib/satorix.rb', line 113

def airbrake_project_key
  ENV['SATORIX_CI_AIRBRAKE_PROJECT_KEY'].to_s
end

#airbrake_startObject



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

#app_dirObject



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

def app_dir
  paths[:app_dir]
end

#available_jobsObject



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_dirObject



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

def bin_dir
  File.join app_dir, 'bin'
end

#build_dirObject



281
282
283
# File 'lib/satorix.rb', line 281

def build_dir
  paths[:build_dir]
end

#ci_commit_ref_nameObject



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

def ci_commit_ref_name
  ENV['CI_COMMIT_REF_NAME']
end

#ci_commit_shaObject



198
199
200
# File 'lib/satorix.rb', line 198

def ci_commit_sha
  ENV['CI_COMMIT_SHA']
end

#ci_job_nameObject



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

def ci_job_name
  ENV['CI_JOB_NAME']
end

#ci_job_stageObject



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

def ci_job_stage
  ENV['CI_JOB_STAGE']
end

#current_branchObject



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_pathObject



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_pathObject



133
134
135
# File 'lib/satorix.rb', line 133

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

#default_jobsObject



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/satorix.rb', line 160

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)


331
332
333
334
335
336
# File 'lib/satorix.rb', line 331

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



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
    # 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



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



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_customObject



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
      # 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



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_environmentObject



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_nameObject



294
295
296
# File 'lib/satorix.rb', line 294

def project_name
  ENV['CI_PROJECT_NAME']
end

#rails_app?Boolean

Returns:

  • (Boolean)


321
322
323
324
325
326
327
328
# File 'lib/satorix.rb', line 321

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)


339
340
341
342
343
# File 'lib/satorix.rb', line 339

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



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

Returns:

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

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/satorix.rb', line 97

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

#yarn?Boolean

Returns:

  • (Boolean)


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

def yarn?
  File.exist? yarn_lock_file
end

#yarn_lock_fileObject



316
317
318
# File 'lib/satorix.rb', line 316

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