Class: SmartCloud::Apps::Rails

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_cloud/apps/rails.rb

Instance Method Summary collapse

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initializeRails

Returns a new instance of Rails.



5
6
# File 'lib/smart_cloud/apps/rails.rb', line 5

def initialize
end

Instance Method Details

#packObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/smart_cloud/apps/rails.rb', line 91

def pack
	set_logger_formatter_arrow

	if File.exist? "tmp/smartcloud/packed"
		begin
			pid = File.read('tmp/smartcloud/packed').to_i
			Process.kill('QUIT', pid)
		rescue Errno::ESRCH # No such process
		end
		exec "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

#start(appname, container_path, container_path_with_version) ⇒ Object



8
9
10
11
12
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
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
88
89
# File 'lib/smart_cloud/apps/rails.rb', line 8

def start(appname, container_path, container_path_with_version)
	return unless File.exist? "#{container_path_with_version}/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."

	# 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_with_version}/vendor/bundle")
	FileUtils.mkdir_p("#{container_path_with_version}/public/assets")
	FileUtils.mkdir_p("#{container_path_with_version}/public/packs")
	FileUtils.mkdir_p("#{container_path_with_version}/node_modules")
	FileUtils.mkdir_p("#{container_path_with_version}/storage")

	# Creating & Starting container
	container_id = `docker ps -a -q --filter='name=^#{appname}_1$' --filter='status=running'`.chomp
	new_container = container_id.empty? ? "#{appname}_1" : "#{appname}_2"
	old_container = container_id.empty? ? "#{appname}_2" : "#{appname}_1"

	SmartCloud::Apps::App.stop("#{new_container}")
	if system("docker create \
		--name='#{new_container}' \
		--env-file='#{container_path}/env' \
		--user `id -u`:`id -g` \
		--workdir /app \
		--expose='3000' \
		--volume='#{SmartCloud.config.user_home_path}/.smartcloud/config:#{SmartCloud.config.user_home_path}/.smartcloud/config' \
		--volume='#{container_path_with_version}:/app' \
		--volume='#{container_path}/app/vendor/bundle:/app/vendor/bundle' \
		--volume='#{container_path}/app/public/assets:/app/public/assets' \
		--volume='#{container_path}/app/public/packs:/app/public/packs' \
		--volume='#{container_path}/app/node_modules:/app/node_modules' \
		--volume='#{container_path}/app/storage:/app/storage' \
		--restart='always' \
		--init \
		--network='nginx-network' \
		smartcloud/buildpacks/rails", out: File::NULL)

		system("docker network connect elasticsearch-network #{new_container}")
		system("docker network connect minio-network #{new_container}")
		system("docker network connect mysql-network #{new_container}")

		if system("docker start --attach #{new_container}")
			logger.debug "Starting Web Server ..."
			if system("docker start #{new_container}", out: File::NULL)
				sleep 7
				logger.info "Web Server started successfully."
				SmartCloud::Apps::App.stop(old_container)
				SmartCloud::Apps::App.clean_up(container_path)
				logger.info "Launched Application ... Success."
				exit 10
			end
		else
			SmartCloud::Apps::App.stop("#{new_container}")
		end
	end

	logger.formatter = nil
end