smartcloud 0.0.151 → 0.0.156
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/smartcloud/grids/runner.rb +66 -63
- data/lib/smartcloud/logger.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0f4d3dda6b40f12732bfe026083343ca4b14e3df467ab9d50e5c73aff6069cc
|
4
|
+
data.tar.gz: c72c3cdbb604173423dae83b8b4338da8dd95523c1d3ef4e2f10a6da8fa48961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a8ab72089a33b697c7947fdbdb331293510a30dec7125a99d9126118bc245d10ab6a27d3ba2aea4d45f4ae1ca54a66672490fb525604631b13af101ec2ba996
|
7
|
+
data.tar.gz: 6166b6b3035bddef48c7611f27c6e8d0f5dd7e7065dfd639ea859bb00af919f149041ba4d03c596db86a0787794ac9a6ac56c27a4ed87a8db00d9f65d763494c
|
@@ -220,38 +220,25 @@ module Smartcloud
|
|
220
220
|
|
221
221
|
def self.prereceive_app(appname, username, oldrev, newrev, refname)
|
222
222
|
logger.formatter = proc do |severity, datetime, progname, message|
|
223
|
-
"\t\t\t#{severity}: #{message}\n"
|
223
|
+
"\t\t\t-----> #{severity}: #{message}\n"
|
224
224
|
end
|
225
|
+
logger.level = ::Logger::DEBUG
|
225
226
|
|
227
|
+
# Load vars and environment
|
226
228
|
container_path = "#{Smartcloud.config.user_home_path}/.smartcloud/grids/grid-runner/apps/containers/#{appname}"
|
229
|
+
return unless self.load_container_env_vars(container_path)
|
227
230
|
|
228
|
-
|
229
|
-
logger.info "
|
230
|
-
unless File.exist? "#{container_path}/env"
|
231
|
-
puts "Environment could not be loaded ... Failed."
|
232
|
-
exit 1
|
233
|
-
end
|
234
|
-
|
235
|
-
# Load ENV vars
|
236
|
-
File.open("#{container_path}/env").each_line do |line|
|
237
|
-
line.chomp!
|
238
|
-
next if line.empty? || line.start_with?('#')
|
239
|
-
key, value = line.split "="
|
240
|
-
ENV[key] = value
|
241
|
-
end
|
242
|
-
|
243
|
-
# Match Username
|
231
|
+
# Verify the user and ensure the user is correct and has access to this repository
|
232
|
+
logger.info "Verifying User ..."
|
244
233
|
unless ENV['USERNAME'] == username
|
245
|
-
|
246
|
-
|
234
|
+
logger.error "Unauthorized."
|
235
|
+
return
|
247
236
|
end
|
248
|
-
logger.formatter = nil
|
249
|
-
logger.info "done"
|
250
237
|
|
251
238
|
# Only run this script for the master branch. You can remove this
|
252
239
|
# if block if you wish to run it for others as well.
|
253
240
|
if refname == "refs/heads/master"
|
254
|
-
logger.info "
|
241
|
+
logger.info "Loading Application ..."
|
255
242
|
|
256
243
|
# Note: There should be no space between + and " in version.
|
257
244
|
# Note: date will be UTC date until timezone has been changed.
|
@@ -261,30 +248,15 @@ module Smartcloud
|
|
261
248
|
unless Dir.exist? container_path_with_version
|
262
249
|
FileUtils.mkdir_p(container_path_with_version)
|
263
250
|
if system("git archive #{newrev} | tar -x -C #{container_path_with_version}")
|
264
|
-
puts "done"
|
265
|
-
|
266
|
-
# Clean up very old versions
|
267
|
-
Dir.chdir("#{container_path}/releases") do
|
268
|
-
app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
|
269
|
-
destroy_count = app_versions.count - ENV['KEEP_RELEASES'].to_i
|
270
|
-
if destroy_count > 0
|
271
|
-
print "-----> Deleting older application releases ... "
|
272
|
-
destroy_count.times do
|
273
|
-
FileUtils.rm_r(File.join(Dir.pwd, app_versions.shift))
|
274
|
-
end
|
275
|
-
puts "done"
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
251
|
# Start App
|
280
252
|
self.start_app(appname)
|
281
253
|
else
|
282
|
-
|
283
|
-
|
254
|
+
logger.fatal "Could not extract new app version ... Failed."
|
255
|
+
return
|
284
256
|
end
|
285
257
|
else
|
286
|
-
|
287
|
-
|
258
|
+
logger.fatal "This version name already exists ... Failed."
|
259
|
+
return
|
288
260
|
end
|
289
261
|
end
|
290
262
|
|
@@ -296,45 +268,35 @@ module Smartcloud
|
|
296
268
|
container_path = "#{Smartcloud.config.user_home_path}/.smartcloud/grids/grid-runner/apps/containers/#{appname}"
|
297
269
|
|
298
270
|
Dir.chdir("#{container_path}/releases") do
|
271
|
+
# Getting App Version
|
299
272
|
if app_version == 0
|
300
273
|
app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
|
301
274
|
app_version = app_versions.last
|
302
275
|
end
|
303
|
-
|
304
276
|
container_path_with_version = "#{container_path}/releases/#{app_version}"
|
305
277
|
|
306
278
|
# Setup Buildpacker
|
307
279
|
buildpacker_path = "#{Smartcloud.config.root_path}/lib/smartcloud/grids/grid-runner/buildpacks/buildpacker.rb"
|
308
280
|
FileUtils.cp(buildpacker_path, container_path_with_version)
|
309
281
|
|
310
|
-
|
311
|
-
|
282
|
+
# Launching Application
|
283
|
+
logger.info "Launching Application ..."
|
312
284
|
if File.exist? "#{container_path_with_version}/bin/rails"
|
313
|
-
# Stopping & Removing container
|
314
|
-
self.stop_app(appname)
|
315
|
-
|
316
285
|
# Starting Rails App
|
317
286
|
self.start_app_rails(appname, container_path, container_path_with_version)
|
318
287
|
end
|
319
288
|
end
|
320
289
|
end
|
321
|
-
|
322
|
-
# These two lines are important to stop and remove container and then cancel pre-receive push if finally the app could not be started.
|
323
|
-
# If the app was finally started, then these two lines should never be executed.
|
324
|
-
self.stop_app(appname)
|
325
|
-
exit 1
|
326
290
|
end
|
327
291
|
|
328
292
|
def self.stop_app(appname)
|
329
293
|
if Smartcloud::Docker.running?
|
330
294
|
if system("docker inspect -f '{{.State.Running}}' #{appname}", [:out, :err] => File::NULL)
|
331
|
-
|
295
|
+
logger.debug "Stopping container #{appname} ..."
|
332
296
|
if system("docker stop '#{appname}'", out: File::NULL)
|
333
|
-
|
334
|
-
|
335
|
-
print "-----> Removing container #{appname} ... "
|
297
|
+
logger.debug "Removing container #{appname} ..."
|
336
298
|
if system("docker rm '#{appname}'", out: File::NULL)
|
337
|
-
|
299
|
+
logger.debug "Stopped & Removed #{appname} ..."
|
338
300
|
end
|
339
301
|
end
|
340
302
|
end
|
@@ -342,7 +304,11 @@ module Smartcloud
|
|
342
304
|
end
|
343
305
|
|
344
306
|
def self.start_app_rails(appname, container_path, container_path_with_version)
|
345
|
-
|
307
|
+
# Stopping & Removing not required app containers
|
308
|
+
# TODO: To be removed after dynamic container switching has been implemented as it should be done in the end at the time of cleanup after the new container is running.
|
309
|
+
self.stop_app(appname)
|
310
|
+
|
311
|
+
logger.info "Ruby on Rails application detected."
|
346
312
|
|
347
313
|
# Setup rails env
|
348
314
|
env_path = "#{container_path}/env"
|
@@ -353,7 +319,7 @@ module Smartcloud
|
|
353
319
|
system("grep -q '^RAILS_SERVE_STATIC_FILES=' #{env_path} || echo 'RAILS_SERVE_STATIC_FILES=enabled' >> #{env_path}")
|
354
320
|
system("grep -q '^LANG=' #{env_path} || echo 'LANG=en_US.UTF-8' >> #{env_path}")
|
355
321
|
system("grep -q '^RAILS_MASTER_KEY=' #{env_path} || echo 'RAILS_MASTER_KEY=yourmasterkey' >> #{env_path}")
|
356
|
-
logger.warn "
|
322
|
+
logger.warn "WARNING: Please set your RAILS_MASTER_KEY env var for this rails app." if system("grep -q '^RAILS_MASTER_KEY=yourmasterkey' #{env_path}")
|
357
323
|
|
358
324
|
# Setup gems folder. If this is not created then docker will create it while running the container,
|
359
325
|
# but the folder will have root user assigned instead of the current user.
|
@@ -361,11 +327,11 @@ module Smartcloud
|
|
361
327
|
|
362
328
|
# Setup Godfile
|
363
329
|
unless File.exist? "#{container_path_with_version}/Godfile"
|
364
|
-
|
365
|
-
page = <<~HEREDOC
|
330
|
+
logger.warn "WARNING: Godfile not detected. Adding a default Godfile. It is recommended to add your own Godfile."
|
331
|
+
page = <<~"HEREDOC"
|
366
332
|
God.watch do |w|
|
367
|
-
w.name =
|
368
|
-
w.start =
|
333
|
+
w.name = "web"
|
334
|
+
w.start = "bundle exec puma -C config/puma.rb"
|
369
335
|
w.behavior(:clean_pid_file)
|
370
336
|
w.keepalive
|
371
337
|
end
|
@@ -389,7 +355,44 @@ module Smartcloud
|
|
389
355
|
system("docker network connect mysql-network #{appname}")
|
390
356
|
|
391
357
|
system("docker start --attach #{appname}")
|
358
|
+
self.clean_up(container_path)
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
def self.clean_up(container_path)
|
363
|
+
logger.info "Cleaning up ..."
|
364
|
+
|
365
|
+
# Stopping & Removing not required app containers
|
366
|
+
appname = File.basename(container_path)
|
367
|
+
self.stop_app(appname)
|
368
|
+
|
369
|
+
# Clean up very old versions
|
370
|
+
Dir.chdir("#{container_path}/releases") do
|
371
|
+
app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
|
372
|
+
destroy_count = app_versions.count - ENV['KEEP_RELEASES'].to_i
|
373
|
+
if destroy_count > 0
|
374
|
+
logger.debug "Deleting older application releases ..."
|
375
|
+
destroy_count.times do
|
376
|
+
FileUtils.rm_r(File.join(Dir.pwd, app_versions.shift))
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
def self.load_container_env_vars(container_path)
|
383
|
+
unless File.exist? "#{container_path}/env"
|
384
|
+
logger.fatal "Environment could not be loaded ... Failed."
|
385
|
+
return false
|
392
386
|
end
|
387
|
+
|
388
|
+
File.open("#{container_path}/env").each_line do |line|
|
389
|
+
line.chomp!
|
390
|
+
next if line.empty? || line.start_with?('#')
|
391
|
+
key, value = line.split "="
|
392
|
+
ENV[key] = value
|
393
|
+
end
|
394
|
+
|
395
|
+
true
|
393
396
|
end
|
394
397
|
end
|
395
398
|
end
|
data/lib/smartcloud/logger.rb
CHANGED