Class: SmartMachine::Apps::Manager

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/apps/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#machine_has_engine_installed?, #platform_on_machine?, #user_bash

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initialize(appname:, appversion: nil) ⇒ Manager

Returns a new instance of Manager.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/smart_machine/apps/manager.rb', line 6

def initialize(appname:, appversion: nil)
  @appname = appname
  @appversion = appversion

  @home_dir = File.expand_path('~')
  @container_path = "#{@home_dir}/machine/apps/containers/#{@appname}"

  # Checking if given appname exists
  repository_path = "#{@home_dir}/machine/apps/repositories/#{@appname}.git"
  raise "App with name '#{@appname}' does not exist. Please provide a valid appname." unless Dir.exist?(repository_path)
end

Instance Attribute Details

#appnameObject

Returns the value of attribute appname.



4
5
6
# File 'lib/smart_machine/apps/manager.rb', line 4

def appname
  @appname
end

#appversionObject

Returns the value of attribute appversion.



4
5
6
# File 'lib/smart_machine/apps/manager.rb', line 4

def appversion
  @appversion
end

Instance Method Details

#deployObject



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
# File 'lib/smart_machine/apps/manager.rb', line 18

def deploy
  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

  Dir.chdir("#{@container_path}/releases") do
    # Setting app version to latest if not set.
    unless @appversion
      versions = Dir.glob('*').select { |f| File.directory? f }.sort
      @appversion = versions.last
    end

    buildpacker = SmartMachine::Buildpackers::Rails.new(appname: @appname, appversion: @appversion)
    if buildpacker.package
      if release
        sleep 7
        clean
        logger.info "Launched Application ... Success."
        exit 10
      else
        raise "Error: Could not release the application."
      end
    else
      raise "Error: Could not package the application."
    end
  end

  logger.formatter = nil
end

#env_varsObject



84
85
86
# File 'lib/smart_machine/apps/manager.rb', line 84

def env_vars
  @env_vars ||= load_env_vars
end

#ps_scaler(formation:) ⇒ Object



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
# File 'lib/smart_machine/apps/manager.rb', line 51

def ps_scaler(formation:)
  Dir.chdir("#{@container_path}/releases") do
    # Setting app version to latest if not set.
    unless @appversion
      versions = Dir.glob('*').select { |f| File.directory? f }.sort
      @appversion = versions.last
    end

    formation.each do |proc_name, final_count|
      config = processes.dig(proc_name.to_sym)
      raise "no config found for the #{proc_name} process." unless config.present?

      current_count = `docker ps -aq --filter name="#{@appname}-#{@appversion}-#{proc_name}-" | wc -l`

      final_count = final_count.to_i
      current_count = current_count.to_i

      if final_count > current_count
        ((current_count + 1)..final_count).each do |index|
          containerize_process!(name: "#{@appname}-#{@appversion}-#{proc_name}-#{index}", config: config)
        end
      elsif final_count < current_count
        ((final_count + 1)..current_count).each do |index|
          container = SmartMachine::Apps::Container.new(name: "#{@appname}-#{@appversion}-#{proc_name}-#{index}", appname: @appname, appversion: @appversion)
          container.stop!
        end
      else
        # No Operation. Don't do anything.
      end
    end
  end
end