Class: SmartMachine::Apps::Container

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/apps/container.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(name:, appname:, appversion:) ⇒ Container

Returns a new instance of Container.



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

def initialize(name:, appname:, appversion:)
  @name = name
  @appname = appname
  @appversion = appversion
  @appimage = "smartmachine/apps/#{@appname}:#{@appversion}"

  @home_dir = File.expand_path('~')
end

Instance Method Details

#commit_app_image!Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/smart_machine/apps/container.rb', line 98

def commit_app_image!
  if create!(using_command: "smartmachine buildpacker packer rails", using_buildpacker: true)
    logger.debug "-----> Starting attached container #{@name} ... "
    if system("docker start --attach #{@name}")
      logger.debug "-----> Committing container #{@name} to image... "
      if system("docker commit #{@name} #{@appimage}", out: File::NULL)
        stop!
        return true
      else
        stop!
        raise "Error: Could not commit #{@name} container to image."
      end
    else
      stop!
      raise "Error: Could not start attached #{@name} container"
    end
  else
    raise "Error: Could not create #{@name} container"
  end

  return false
end

#connect_to_network!(network_name:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/smart_machine/apps/container.rb', line 83

def connect_to_network!(network_name:)
  unless `docker network ls -q --filter name=^#{network_name}$`.chomp.empty?
    if system("docker network connect #{network_name} #{@name}", out: File::NULL)
      logger.debug "Connected to network #{network_name}."
      return true
    else
      raise "Error: Could not connect to #{network_name} network."
    end
  else
    raise "Error: The network with name #{network_name} was not found."
  end

  return false
end

#create!(using_command:, using_buildpacker: false) ⇒ Object



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

def create!(using_command:, using_buildpacker: false)
  unless using_buildpacker
    unless system("docker image inspect #{@appimage}", [:out, :err] => File::NULL)
      raise "Error: App Image with name #{@appimage} not found. Could not create #{@name} container."
    end
  end

  logger.debug "-----> Creating container #{@name} ... "
  command = [
    "docker create",
    "--name='#{@name}'",
    "--env-file='#{@home_dir}/machine/apps/containers/#{@appname}/env'",
    "--user `id -u`:`id -g`",
    "--workdir /app",
    "--expose='3000'",
    "--volume='#{@home_dir}/smartmachine/config/environment.rb:#{@home_dir}/machine/config/environment.rb'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/asdf:#{@home_dir}/.asdf'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/releases/#{@appversion}:/app'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/app/vendor/bundle:/app/vendor/bundle'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/app/public/assets:/app/public/assets'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/app/public/packs:/app/public/packs'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/app/node_modules:/app/node_modules'",
    "--volume='#{@home_dir}/smartmachine/apps/containers/#{@appname}/app/storage:/app/storage'",
    (using_buildpacker ? "--cpus=1 --memory=512m --memory-swap=1024m" : nil),
    "--restart='always'",
    "--init",
    # "--network='nginx-network'",
    (using_buildpacker ? "smartmachine/buildpackers/rails:#{SmartMachine.version}" : @appimage)
  ]
  command.push(using_command) if using_command.present?

  if system(command.compact.join(" "), out: File::NULL)
    logger.debug "done"
    return true
  else
    raise "Error: Could not create #{@name} container"
  end

  return false
end

#start!Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/smart_machine/apps/container.rb', line 54

def start!
  logger.debug "-----> Starting container #{@name} ... "
  if system("docker start #{@name}", out: File::NULL)
    logger.debug "done"
    return true
  else
    raise "Error: Could not start #{@name} container"
  end

  return false
end

#stop!Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smart_machine/apps/container.rb', line 66

def stop!
  container_id = `docker ps -a -q --filter='name=^#{@name}$'`.chomp
  unless container_id.empty?
    logger.debug "Stopping & Removing container #{@name} ..."
    if system("docker stop #{@name} && docker rm #{@name}", out: File::NULL)
      logger.debug "Stopped & Removed container #{@name} ..."
      return true
    else
      raise "Error: Container '#{@name}' could not be stopped and removed."
    end
  else
    raise "Error: Container '#{@name}' does not exist to stop."
  end

  return false
end