smartcloud 0.0.153 → 0.0.158

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c37e30863d1ea6b5a7a3c585cb4f41a6aadcf1f0dced48add9830a142213bc35
4
- data.tar.gz: 1280b3a725d4331feef89ed347b241da53dcb44b0a83e87d63a7130823638117
3
+ metadata.gz: fff4e804fc58c90059552cb40f468f7965a59e7a2846eabf717055da100ae6fb
4
+ data.tar.gz: c16f0c09b64dabbf9f9fd086b66eb029eec69c5cb4fb1640e0cbd6d0ccedb017
5
5
  SHA512:
6
- metadata.gz: 5135a9e4d789b0dcebf218ba6a24cf4344c766440a747695a16e82bbfa41dfafe5076c53b45a6dee4275ea2ebd50ac730e68e8025b20c020bce742320ca0cc98
7
- data.tar.gz: 6077c2671d93ae32d888474f9afd8e46d3ab9a13c9158cade314d49b4bcb9072faeafddaf54fd72bd22e966922ffd9937ead3f67ed88eeba637402d86f1b18a0
6
+ metadata.gz: 44f8f14e9cc581fb038c8d164cb07efdf0c893796df35f71133d62d54babde5023285fb66d143e45ccacd2864cbfe316f597570eefcd1a98405216701cf117d3
7
+ data.tar.gz: 8a491b59078f8bf1a273dbde6b54d012cb8f95b9fc303ce617696098909951fdfcec02e797fe78cca041c84cb7acb9bbd3891f8d690ee2fa0e9e8c35e0f6008e
@@ -1,30 +1,44 @@
1
+ require "logger"
2
+
1
3
  class Buildpacker
2
4
  def initialize
3
5
  end
4
6
 
5
7
  def pack
6
8
  pack_rails if File.exist? "bin/rails"
7
- puts "-----> Could not continue ... Launch Failed."
9
+ logger.info "Could not continue ... Launch Failed."
8
10
  end
9
11
 
10
12
  def pack_rails
11
13
  # Remove server.pid if it exists
12
14
  FileUtils.rm("tmp/pids/server.pid") if File.exist? "tmp/pids/server.pid"
13
15
 
14
- puts "-----> Performing bundle install ... "
16
+ logger.info "Performing bundle install ..."
15
17
  if system("bundle install --deployment --clean")
16
- puts "-----> Installing Javascript Dependencies & Pre-compiling Assets ... "
18
+ logger.info "Installing Javascript Dependencies & Pre-compiling Assets ..."
17
19
  if system("bundle exec rails assets:precompile", out: File::NULL)
18
- puts "done"
19
-
20
- puts "-----> Running Web Server ... "
20
+ logger.debug "Starting Server ..."
21
21
  if system("god -c Godfile -D")
22
- puts "-----> Launched Application ... Success."
22
+ logger.info "Launched Application ... Success."
23
23
  end
24
24
  # exit 0
25
25
  end
26
- end
26
+ end
27
27
  end
28
+
29
+ def self.logger
30
+ @logger ||= self.configure_logger_for(self.name)
31
+ end
32
+
33
+ def self.configure_logger_for(classname)
34
+ logger = ::Logger.new($stdout)
35
+ logger.level = ::Logger::DEBUG
36
+ logger.formatter = proc do |severity, datetime, progname, message|
37
+ "\t\t\t-----> #{severity}: #{message}\n"
38
+ end
39
+ logger.progname = classname
40
+ logger
41
+ end
28
42
  end
29
43
 
30
44
  buildpacker = Buildpacker.new
@@ -224,9 +224,8 @@ module Smartcloud
224
224
  end
225
225
  logger.level = ::Logger::DEBUG
226
226
 
227
+ # Load vars and environment
227
228
  container_path = "#{Smartcloud.config.user_home_path}/.smartcloud/grids/grid-runner/apps/containers/#{appname}"
228
-
229
- # Load ENV vars
230
229
  return unless self.load_container_env_vars(container_path)
231
230
 
232
231
  # Verify the user and ensure the user is correct and has access to this repository
@@ -291,23 +290,10 @@ module Smartcloud
291
290
  end
292
291
 
293
292
  def self.stop_app(appname)
294
- if Smartcloud::Docker.running?
295
- if system("docker inspect -f '{{.State.Running}}' #{appname}", [:out, :err] => File::NULL)
296
- logger.debug "Stopping container #{appname} ..."
297
- if system("docker stop '#{appname}'", out: File::NULL)
298
- logger.debug "Removing container #{appname} ..."
299
- if system("docker rm '#{appname}'", out: File::NULL)
300
- logger.debug "Stopped & Removed #{appname} ..."
301
- end
302
- end
303
- end
304
- end
293
+ self.stop_container(appname)
305
294
  end
306
295
 
307
296
  def self.start_app_rails(appname, container_path, container_path_with_version)
308
- # Stopping & Removing not required app containers
309
- self.stop_app(appname)
310
-
311
297
  logger.info "Ruby on Rails application detected."
312
298
 
313
299
  # Setup rails env
@@ -328,10 +314,10 @@ module Smartcloud
328
314
  # Setup Godfile
329
315
  unless File.exist? "#{container_path_with_version}/Godfile"
330
316
  logger.warn "WARNING: Godfile not detected. Adding a default Godfile. It is recommended to add your own Godfile."
331
- page = <<~HEREDOC
317
+ page = <<~"HEREDOC"
332
318
  God.watch do |w|
333
- w.name = 'web'
334
- w.start = 'bundle exec puma -C config/puma.rb'
319
+ w.name = "web"
320
+ w.start = "bundle exec puma -C config/puma.rb"
335
321
  w.behavior(:clean_pid_file)
336
322
  w.keepalive
337
323
  end
@@ -340,8 +326,9 @@ module Smartcloud
340
326
  end
341
327
 
342
328
  # Creating & Starting container
329
+ self.stop_app("#{appname}_new")
343
330
  if system("docker create \
344
- --name='#{appname}' \
331
+ --name='#{appname}_new' \
345
332
  --env-file='#{container_path}/env' \
346
333
  --expose='5000' \
347
334
  --volume='#{container_path_with_version}:/code' \
@@ -351,20 +338,23 @@ module Smartcloud
351
338
  --network='nginx-network' \
352
339
  smartcloud/buildpacks/rails", out: File::NULL)
353
340
 
354
- system("docker network connect solr-network #{appname}")
355
- system("docker network connect mysql-network #{appname}")
341
+ system("docker network connect solr-network #{appname}_new")
342
+ system("docker network connect mysql-network #{appname}_new")
343
+
344
+ system("docker start --attach #{appname}_new")
345
+
346
+ # Runs only if #{appname}_new has been started successfully and is reachable.
347
+ # self.hot_reloading_app(appname)
348
+ # self.clean_up(container_path)
356
349
 
357
- system("docker start --attach #{appname}")
358
- self.clean_up(container_path)
350
+ # Runs only if #{appname}_new starting has failed.
351
+ # self.stop_app("#{appname}_new")
359
352
  end
360
353
  end
361
354
 
362
355
  def self.clean_up(container_path)
363
356
  logger.info "Cleaning up ..."
364
357
 
365
- # Stopping & Removing not required app containers
366
- self.stop_app(appname)
367
-
368
358
  # Clean up very old versions
369
359
  Dir.chdir("#{container_path}/releases") do
370
360
  app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
@@ -378,10 +368,30 @@ module Smartcloud
378
368
  end
379
369
  end
380
370
 
371
+ # Hot reloading app containers and removing old app container
372
+ def self.hot_reloading_app(appname)
373
+ logger.debug "Hot reloading '#{appname}' containers ..."
374
+
375
+ container_id = `docker ps -a -q --filter='name=^#{appname}$'`.chomp
376
+ unless container_id.empty?
377
+ if system("docker container rename #{appname} #{appname}_old", [:out, :err] => File::NULL)
378
+ logger.debug "Container renamed from #{appname} #{appname}_old"
379
+ if system("docker container rename #{appname}_new #{appname}", [:out, :err] => File::NULL)
380
+ logger.debug "Container renamed from #{appname}_new #{appname}"
381
+ self.stop_app("#{appname}_old")
382
+ end
383
+ end
384
+ else
385
+ if system("docker container rename #{appname}_new #{appname}", [:out, :err] => File::NULL)
386
+ logger.debug "Container renamed from #{appname}_new #{appname}"
387
+ end
388
+ end
389
+ end
390
+
381
391
  def self.load_container_env_vars(container_path)
382
392
  unless File.exist? "#{container_path}/env"
383
393
  logger.fatal "Environment could not be loaded ... Failed."
384
- false
394
+ return false
385
395
  end
386
396
 
387
397
  File.open("#{container_path}/env").each_line do |line|
@@ -393,6 +403,20 @@ module Smartcloud
393
403
 
394
404
  true
395
405
  end
406
+
407
+ def self.stop_container(container_name)
408
+ if Smartcloud::Docker.running?
409
+ container_id = `docker ps -a -q --filter='name=^#{container_name}$'`.chomp
410
+ unless container_id.empty?
411
+ logger.debug "Stopping & Removing container #{container_name} ..."
412
+ if system("docker stop #{container_name} && docker rm #{container_name}", out: File::NULL)
413
+ logger.debug "Stopped & Removed container #{container_name} ..."
414
+ end
415
+ else
416
+ logger.debug "Container '#{container_name}' does not exist to stop."
417
+ end
418
+ end
419
+ end
396
420
  end
397
421
  end
398
422
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartcloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.153
4
+ version: 0.0.158
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timeboard