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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2a18735b026181f5e90dad4741681d35470e1f26ebd8c0547c8edde06293b077
4
- data.tar.gz: 7330097d043ba14c784b1e0fad727e01d33ed6ac0e0334e608034d3487515bc6
3
+ metadata.gz: d0f4d3dda6b40f12732bfe026083343ca4b14e3df467ab9d50e5c73aff6069cc
4
+ data.tar.gz: c72c3cdbb604173423dae83b8b4338da8dd95523c1d3ef4e2f10a6da8fa48961
5
5
  SHA512:
6
- metadata.gz: 458a67c95aab46fcff93735b5910755b2604b900ff0d25803f1d73e0a8a89c9d314260bde8bbdaae83c84abf1c51c3f858ce8d98917570353bae45e485347224
7
- data.tar.gz: 41a670101a7f60efbc2577d7daac16d8a91c685045cbba1206e337d0d9b1b18aca5e202b856cc45c084fba1e34fb239ff76f2fdfd71b0c25ec5b18c3162bf3c1
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
- ## Verify the user and ensure the user is correct and has access to this repository
229
- logger.info "-----> Verifying User ... "
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
- puts "Unauthorized."
246
- exit 1
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 "-----> Initializing Application ... "
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
- puts "Could not extract new app version ... Failed."
283
- exit 1
254
+ logger.fatal "Could not extract new app version ... Failed."
255
+ return
284
256
  end
285
257
  else
286
- puts "This version name already exists ... Failed."
287
- exit 1
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
- puts "-----> Launching Application ... "
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
- print "-----> Stopping container #{appname} ... "
295
+ logger.debug "Stopping container #{appname} ..."
332
296
  if system("docker stop '#{appname}'", out: File::NULL)
333
- puts "done"
334
-
335
- print "-----> Removing container #{appname} ... "
297
+ logger.debug "Removing container #{appname} ..."
336
298
  if system("docker rm '#{appname}'", out: File::NULL)
337
- puts "done"
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
- puts "-----> Ruby on Rails application detected."
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 "-----> WARNING: Please set your RAILS_MASTER_KEY env var for this rails app." if system("grep -q '^RAILS_MASTER_KEY=yourmasterkey' #{env_path}")
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
- puts "-----> WARNING: Godfile not detected. Adding a default Godfile. It is recommended to add your own Godfile."
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 = 'web'
368
- w.start = 'bundle exec puma -C config/puma.rb'
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
@@ -24,6 +24,7 @@ module Smartcloud
24
24
 
25
25
  def configure_logger_for(classname)
26
26
  logger = ::Logger.new($stdout)
27
+ logger.level = ::Logger::INFO
27
28
  logger.progname = classname
28
29
  logger
29
30
  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.151
4
+ version: 0.0.156
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timeboard