Class: SmartMachine::Buildpackers::Rails

Inherits:
SmartMachine::Base show all
Defined in:
lib/smart_machine/buildpackers/rails.rb

Instance Method Summary collapse

Methods inherited from SmartMachine::Base

#machine_has_engine_installed?, #platform_on_machine?, #user_bash

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initialize(appname:, appversion:) ⇒ Rails

Returns a new instance of Rails.



6
7
8
9
10
11
# File 'lib/smart_machine/buildpackers/rails.rb', line 6

def initialize(appname:, appversion:)
  @home_dir = File.expand_path('~')
  @appname = appname
  @appversion = appversion
  @container_path = "#{@home_dir}/machine/apps/containers/#{@appname}"
end

Instance Method Details

#packageObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smart_machine/buildpackers/rails.rb', line 13

def package
  return unless File.exist? "#{@container_path}/releases/#{@appversion}/bin/rails"

  logger.formatter = proc do |severity, datetime, progname, message|
    severity_text = { "DEBUG" => "\u{1f527} #{severity}:", "INFO" => " \u{276f}", "WARN" => "\u{2757} #{severity}:",
                     "ERROR" => "\u{274c} #{severity}:", "FATAL" => "\u{2b55} #{severity}:", "UNKNOWN" => "\u{2753} #{severity}:"
                    }
    "\t\t\t\t#{severity_text[severity]} #{message}\n"
  end

  logger.info "Ruby on Rails application detected."
  logger.info "Packaging Application ..."

  # Setup rails env
  env_path = "#{@container_path}/env"
  system("grep -q '^## Rails' #{env_path} || echo '## Rails' >> #{env_path}")
  system("grep -q '^MALLOC_ARENA_MAX=' #{env_path} || echo '# MALLOC_ARENA_MAX=2' >> #{env_path}")
  system("grep -q '^RAILS_ENV=' #{env_path} || echo 'RAILS_ENV=production' >> #{env_path}")
  system("grep -q '^RACK_ENV=' #{env_path} || echo 'RACK_ENV=production' >> #{env_path}")
  system("grep -q '^RAILS_LOG_TO_STDOUT=' #{env_path} || echo 'RAILS_LOG_TO_STDOUT=enabled' >> #{env_path}")
  system("grep -q '^RAILS_SERVE_STATIC_FILES=' #{env_path} || echo 'RAILS_SERVE_STATIC_FILES=enabled' >> #{env_path}")
  system("grep -q '^LANG=' #{env_path} || echo 'LANG=en_US.UTF-8' >> #{env_path}")
  system("grep -q '^RAILS_MASTER_KEY=' #{env_path} || echo 'RAILS_MASTER_KEY=yourmasterkey' >> #{env_path}")
  logger.warn "Please set your RAILS_MASTER_KEY env var for this rails app." if system("grep -q '^RAILS_MASTER_KEY=yourmasterkey' #{env_path}")

  # Setup app folders needed for volumes. If this is not created then docker will create it while running the container,
  # but the folder will have root user assigned instead of the current user.
  FileUtils.mkdir_p("#{@container_path}/app/vendor/bundle")
  FileUtils.mkdir_p("#{@container_path}/app/public/assets")
  FileUtils.mkdir_p("#{@container_path}/app/public/packs")
  FileUtils.mkdir_p("#{@container_path}/app/node_modules")
  FileUtils.mkdir_p("#{@container_path}/app/storage")
  FileUtils.mkdir_p("#{@container_path}/asdf")
  FileUtils.mkdir_p("#{@container_path}/releases/#{@appversion}/vendor/bundle")
  FileUtils.mkdir_p("#{@container_path}/releases/#{@appversion}/public/assets")
  FileUtils.mkdir_p("#{@container_path}/releases/#{@appversion}/public/packs")
  FileUtils.mkdir_p("#{@container_path}/releases/#{@appversion}/node_modules")
  FileUtils.mkdir_p("#{@container_path}/releases/#{@appversion}/storage")

  # Creating a valid docker app image.
  container = SmartMachine::Apps::Container.new(name: "#{@appname}-#{@appversion}-packed", appname: @appname, appversion: @appversion)
  if container.commit_app_image!
    logger.formatter = nil
    return true
  end

  logger.formatter = nil
  return false
end

#packerObject



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/smart_machine/buildpackers/rails.rb', line 63

def packer
  set_logger_formatter_arrow

  # TODO: The exec of final process should be done only in the Manager#containerize_process! and should be removed from here.
  # This method should only pack a fully functioning container and do nothing else.
  if File.exist? "tmp/smartmachine/packed"
    begin
      pid = File.read('tmp/smartmachine/packed').to_i
      Process.kill('QUIT', pid)
    rescue Errno::ESRCH # No such process
    end
    exec(user_bash("bundle exec puma --config config/puma.rb"))
  else
    if initial_setup? && bundle_install? && precompile_assets? && db_migrate? && test_web_server?
      logger.formatter = nil

      exit 0
    else
      logger.error "Could not continue ... Launch Failed."
      logger.formatter = nil

      exit 1
    end
  end
end