smartcloud 0.0.152 → 0.0.157
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 +74 -44
- 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: 032f1337c34584f6feeb39a11d56a506767512c20bce09d720e0eae56920acec
|
4
|
+
data.tar.gz: 88c4779b043ec39965c2b0ed5c9ca0cb03b9cf4a0e1dd36983e32e94066f17dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97ccd5c3e2625198d0b5afb756f9e1d61f923d8e99409621ac290ed732d0fc05c5134f70c712e7d37b511c328d970c63e0e78a5e079123741267948b626d1af4
|
7
|
+
data.tar.gz: e5424293c420c75ab9c00f9c1ec6b7eb94fe6b5086cec3be1cc5ad918e80b6d50bc31c0cc2d7c20659665aa69e791f66c6f7d676351d8435d8687bef74a140a9
|
@@ -222,25 +222,14 @@ module Smartcloud
|
|
222
222
|
logger.formatter = proc do |severity, datetime, progname, message|
|
223
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
|
-
|
231
|
+
# Verify the user and ensure the user is correct and has access to this repository
|
229
232
|
logger.info "Verifying User ..."
|
230
|
-
unless File.exist? "#{container_path}/env"
|
231
|
-
logger.fatal "Environment could not be loaded ... Failed."
|
232
|
-
return
|
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
|
244
233
|
unless ENV['USERNAME'] == username
|
245
234
|
logger.error "Unauthorized."
|
246
235
|
return
|
@@ -279,50 +268,33 @@ module Smartcloud
|
|
279
268
|
container_path = "#{Smartcloud.config.user_home_path}/.smartcloud/grids/grid-runner/apps/containers/#{appname}"
|
280
269
|
|
281
270
|
Dir.chdir("#{container_path}/releases") do
|
271
|
+
# Getting App Version
|
282
272
|
if app_version == 0
|
283
273
|
app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
|
284
274
|
app_version = app_versions.last
|
285
275
|
end
|
286
|
-
|
287
276
|
container_path_with_version = "#{container_path}/releases/#{app_version}"
|
288
277
|
|
289
278
|
# Setup Buildpacker
|
290
279
|
buildpacker_path = "#{Smartcloud.config.root_path}/lib/smartcloud/grids/grid-runner/buildpacks/buildpacker.rb"
|
291
280
|
FileUtils.cp(buildpacker_path, container_path_with_version)
|
292
281
|
|
282
|
+
# Launching Application
|
293
283
|
logger.info "Launching Application ..."
|
294
|
-
|
295
284
|
if File.exist? "#{container_path_with_version}/bin/rails"
|
296
|
-
# Stopping & Removing container
|
297
|
-
self.stop_app(appname)
|
298
|
-
|
299
285
|
# Starting Rails App
|
300
286
|
self.start_app_rails(appname, container_path, container_path_with_version)
|
301
287
|
end
|
302
288
|
end
|
303
289
|
end
|
304
|
-
|
305
|
-
# These two lines are important to stop and remove container and then cancel pre-receive push if finally the app could not be started.
|
306
|
-
# If the app was finally started, then these two lines should never be executed.
|
307
|
-
self.stop_app(appname)
|
308
290
|
end
|
309
291
|
|
310
292
|
def self.stop_app(appname)
|
311
|
-
|
312
|
-
if system("docker inspect -f '{{.State.Running}}' #{appname}", [:out, :err] => File::NULL)
|
313
|
-
logger.debug "Stopping container #{appname} ..."
|
314
|
-
if system("docker stop '#{appname}'", out: File::NULL)
|
315
|
-
logger.debug "Removing container #{appname} ..."
|
316
|
-
if system("docker rm '#{appname}'", out: File::NULL)
|
317
|
-
logger.info "Stopped & Removed #{appname} ..."
|
318
|
-
end
|
319
|
-
end
|
320
|
-
end
|
321
|
-
end
|
293
|
+
self.stop_container(appname)
|
322
294
|
end
|
323
295
|
|
324
296
|
def self.start_app_rails(appname, container_path, container_path_with_version)
|
325
|
-
logger.info "Ruby on Rails application detected"
|
297
|
+
logger.info "Ruby on Rails application detected."
|
326
298
|
|
327
299
|
# Setup rails env
|
328
300
|
env_path = "#{container_path}/env"
|
@@ -342,10 +314,10 @@ module Smartcloud
|
|
342
314
|
# Setup Godfile
|
343
315
|
unless File.exist? "#{container_path_with_version}/Godfile"
|
344
316
|
logger.warn "WARNING: Godfile not detected. Adding a default Godfile. It is recommended to add your own Godfile."
|
345
|
-
page = <<~HEREDOC
|
317
|
+
page = <<~"HEREDOC"
|
346
318
|
God.watch do |w|
|
347
|
-
w.name =
|
348
|
-
w.start =
|
319
|
+
w.name = "web"
|
320
|
+
w.start = "bundle exec puma -C config/puma.rb"
|
349
321
|
w.behavior(:clean_pid_file)
|
350
322
|
w.keepalive
|
351
323
|
end
|
@@ -354,8 +326,9 @@ module Smartcloud
|
|
354
326
|
end
|
355
327
|
|
356
328
|
# Creating & Starting container
|
329
|
+
self.stop_app("#{appname}_new")
|
357
330
|
if system("docker create \
|
358
|
-
--name='#{appname}' \
|
331
|
+
--name='#{appname}_new' \
|
359
332
|
--env-file='#{container_path}/env' \
|
360
333
|
--expose='5000' \
|
361
334
|
--volume='#{container_path_with_version}:/code' \
|
@@ -365,11 +338,17 @@ module Smartcloud
|
|
365
338
|
--network='nginx-network' \
|
366
339
|
smartcloud/buildpacks/rails", out: File::NULL)
|
367
340
|
|
368
|
-
system("docker network connect solr-network #{appname}")
|
369
|
-
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")
|
370
345
|
|
371
|
-
|
372
|
-
self.
|
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)
|
349
|
+
|
350
|
+
# Runs only if #{appname}_new starting has failed.
|
351
|
+
# self.stop_app("#{appname}_new")
|
373
352
|
end
|
374
353
|
end
|
375
354
|
|
@@ -381,12 +360,63 @@ module Smartcloud
|
|
381
360
|
app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
|
382
361
|
destroy_count = app_versions.count - ENV['KEEP_RELEASES'].to_i
|
383
362
|
if destroy_count > 0
|
363
|
+
logger.debug "Deleting older application releases ..."
|
384
364
|
destroy_count.times do
|
385
365
|
FileUtils.rm_r(File.join(Dir.pwd, app_versions.shift))
|
386
366
|
end
|
387
367
|
end
|
388
368
|
end
|
389
369
|
end
|
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
|
+
|
391
|
+
def self.load_container_env_vars(container_path)
|
392
|
+
unless File.exist? "#{container_path}/env"
|
393
|
+
logger.fatal "Environment could not be loaded ... Failed."
|
394
|
+
return false
|
395
|
+
end
|
396
|
+
|
397
|
+
File.open("#{container_path}/env").each_line do |line|
|
398
|
+
line.chomp!
|
399
|
+
next if line.empty? || line.start_with?('#')
|
400
|
+
key, value = line.split "="
|
401
|
+
ENV[key] = value
|
402
|
+
end
|
403
|
+
|
404
|
+
true
|
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
|
390
420
|
end
|
391
421
|
end
|
392
422
|
end
|
data/lib/smartcloud/logger.rb
CHANGED