Class: SmartMachine::Apps::App

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

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:, container_number: nil, username: nil) ⇒ App

Returns a new instance of App.



4
5
6
7
8
9
10
11
12
# File 'lib/smart_machine/apps/app.rb', line 4

def initialize(appname:, container_number: nil, username: nil)
  @home_dir = File.expand_path('~')
  @appname = appname
  @container_number = container_number
  @username = username

  @repository_path = "#{@home_dir}/machine/apps/repositories/#{@appname}.git"
  @container_path = "#{@home_dir}/machine/apps/containers/#{@appname}"
end

Instance Method Details

#cleanerObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/smart_machine/apps/app.rb', line 130

def cleaner
  env_vars = get_env_vars
  return unless env_vars

  logger.info "Cleaning up ..."

  # Clean up very old versions
  Dir.chdir("#{@container_path}/releases") do
    versions = Dir.glob('*').select { |f| File.directory? f }.sort
    destroy_count = versions.count - env_vars['KEEP_RELEASES'].to_i
    if destroy_count > 0
      logger.debug "Deleting older application releases ..."
      destroy_count.times do
        FileUtils.rm_r(File.join(Dir.pwd, versions.shift))
      end
    end
  end
end

#creater(appdomain:, prereceiver_name:) ⇒ Object



14
15
16
17
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
50
51
52
53
54
55
56
57
# File 'lib/smart_machine/apps/app.rb', line 14

def creater(appdomain:, prereceiver_name:)
  raise "Please provide appname and username" if @appname.empty? || @username.empty?
  prereceiver_name = prereceiver_name.to_s

  print "-----> Creating Application ... "

  # Checking if app with given name already exists
  if Dir.exist?(@repository_path)
    puts "failed. App with name '#{@appname}' already exists."
    exit
  end

  # Creating Directories
  FileUtils.mkdir_p(@repository_path)
  FileUtils.mkdir_p(@container_path)

  # Initializing bare repo and pre-receive
  Dir.chdir(@repository_path) do
    %x[git init --bare]
    %x[ln -s ../../../../grids/prereceiver/#{prereceiver_name}/pre-receive hooks/pre-receive]
    puts "done"
  end

  # Creating Environment File
  if File.exist?("#{@home_dir}/machine/config/environment.rb")
    require "#{@home_dir}/machine/config/environment"
  end
  unless File.exist? "#{@container_path}/env"
    print "-----> Creating App Environment ... "
    page = "            ## System\n            USERNAME=\#{@username}\n            KEEP_RELEASES=3\n\n            ## Docker\n            VIRTUAL_HOST=\#{@appname}.\#{appdomain}\n            VIRTUAL_PATH=/\n            LETSENCRYPT_HOST=\#{@appname}.\#{appdomain}\n            LETSENCRYPT_EMAIL=\#{@username}\n            LETSENCRYPT_TEST=false\n         HEREDOC\n    puts \"done\" if system(\"echo '\#{page}' > \#{@container_path}/env\")\n  end\nend\n"

#destroyerObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/smart_machine/apps/app.rb', line 59

def destroyer
  raise "Please provide appname" if @appname.empty?

  # Checking if app with given name exists
  unless Dir.exist?(@repository_path)
    raise "App with name '#{@appname}' does not exist. Please provide a valid appname."
  end

  container_id = `docker ps -a -q --filter='name=^#{@appname}$' --filter='status=running'`.chomp
  if container_id.empty?
    # Destroying Directories
    print "-----> Deleting App #{@appname} ... "
    FileUtils.rm_r(@repository_path)
    FileUtils.rm_r(@container_path)
    puts "done"
  end
end

#downerObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/smart_machine/apps/app.rb', line 108

def downer
  raise "Please provide appname" if @appname.empty?

  # Checking if app with given name exists
  unless Dir.exist?(@repository_path)
    raise "App with name '#{@appname}' does not exist. Please provide a valid appname."
  end

  container_name = @appname
  container_name += "_" + @container_number if @container_number

  container_id = `docker ps -a -q --filter='name=^#{container_name}$'`.chomp
  unless container_id.empty?
    logger.debug "Stopping & Removing container #{container_name} ..."
    if system("docker stop #{container_name} && docker rm #{container_name}", out: File::NULL)
      logger.debug "Stopped & Removed container #{container_name} ..."
    end
  else
    logger.debug "Container '#{container_name}' does not exist to stop."
  end
end

#get_env_varsObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/smart_machine/apps/app.rb', line 149

def get_env_vars
  unless File.exist? "#{@container_path}/env"
    logger.fatal "Environment could not be loaded ... Failed."
    return false
  end

  env_vars = {}
  File.open("#{@container_path}/env").each_line do |line|
    line.chomp!
    next if line.empty? || line.start_with?('#')
    key, value = line.split "="
    env_vars[key] = value
  end

  env_vars
end

#uper(version:) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/smart_machine/apps/app.rb', line 77

def uper(version:)
  raise "Please provide appname" if @appname.empty?

  # Checking if app with given name exists
  unless Dir.exist?(@repository_path)
    raise "App with name '#{@appname}' does not exist. Please provide a valid appname."
  end

  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
    # Getting App Version
    if version == 0
      versions = Dir.glob('*').select { |f| File.directory? f }.sort
      version = versions.last
    end

    logger.info "Launching Application ..."

    buildpacker_rails = SmartMachine::Buildpackers::Rails.new(appname: @appname, version: version)
    buildpacker_rails.uper
  end

  logger.formatter = nil
end