Class: SmartMachine::Grids::Terminal

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/grids/terminal.rb,
lib/smart_machine/grids/terminal/wetty.rb

Defined Under Namespace

Classes: Wetty

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:) ⇒ Terminal

Returns a new instance of Terminal.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/smart_machine/grids/terminal.rb', line 4

def initialize(name:)
  config = SmartMachine.config.grids.terminal.dig(name.to_sym)
  raise "terminal config for #{name} not found." unless config

  @image = "smartmachine/terminal:#{SmartMachine.version}"
  @host = config.dig(:host)
  @frontend = config.dig(:frontend)
  @packages = config.dig(:packages)
  @username = config.dig(:username)
  @password = config.dig(:password)

  @name = name.to_s
  @home_dir = File.expand_path('~')

  @wetty = Wetty.new(name: "#{@name}-wetty", host: @host, ssh_host: @name)
end

Instance Method Details

#downerObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/smart_machine/grids/terminal.rb', line 102

def downer
  # Stopping & Removing containers - in reverse order

  @wetty.downer

  print "-----> Stopping container #{@name} ... "
  if system("docker stop '#{@name}'", out: File::NULL)
    puts "done"
    print "-----> Removing container #{@name} ... "
    if system("docker rm '#{@name}'", out: File::NULL)
      puts "done"
    end
  end
end

#installerObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/smart_machine/grids/terminal.rb', line 21

def installer
  unless system("docker image inspect #{@image}", [:out, :err] => File::NULL)
    puts "-----> Creating image #{@image} ... "
    command = [
      "docker image build -t #{@image}",
      "--build-arg SMARTMACHINE_VERSION=#{SmartMachine.version}",
      "-f- #{SmartMachine.config.gem_dir}/lib/smart_machine/grids/terminal",
      "<<'EOF'\n#{dockerfile}EOF"
    ]
    if system(command.join(" "), out: File::NULL)
      puts "done"
    else
      raise "Error: Could not install image: #{@image}"
    end
  else
    raise "Error: Image already installed: #{@image}. Please uninstall using 'smartmachine grids terminal uninstall' and try installing again."
  end
end

#uninstallerObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smart_machine/grids/terminal.rb', line 40

def uninstaller
  unless system("docker inspect -f '{{.State.Running}}' '#{@name}'", [:out, :err] => File::NULL)
    if system("docker image inspect #{@image}", [:out, :err] => File::NULL)
      puts "-----> Removing image #{@image} ... "
      if system("docker image rm #{@image}", out: File::NULL)
        puts "done"
      end
    else
      raise "Error: Terminal already uninstalled. Please install using 'smartmachine grids terminal install' and try uninstalling again."
    end
  else
    raise "Error: Terminal is currently running. Please stop the terminal using 'smartmachine grids terminal down' and try uninstalling again."
  end
end

#uperObject



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/smart_machine/grids/terminal.rb', line 55

def uper
  if system("docker image inspect #{@image}", [:out, :err] => File::NULL)
    FileUtils.mkdir_p("#{@home_dir}/machine/grids/terminal/#{@name}/backups")

    # Creating & Starting containers
    print "-----> Creating container #{@name} ... "

    command = [
      "docker create",
      "--name='#{@name}'",
      "--env VIRTUAL_HOST=#{@host}",
      "--env VIRTUAL_PATH=#{@frontend}",
      "--env VIRTUAL_PORT=80",
      "--env LETSENCRYPT_HOST=#{@host}",
      "--env LETSENCRYPT_EMAIL=#{SmartMachine.config.sysadmin_email}",
      "--env LETSENCRYPT_TEST=false",
      "--env CONTAINER_NAME='#{@name}'",
      "--env PACKAGES='#{@packages.join(' ')}'",
      "--env USERNAME=#{@username}",
      "--env PASSWORD=#{@password}",
      "--publish='2223:2223'", # TODO: Remove this published port and move it behind the reverse proxy when ready.
      "--volume='#{@name}-home:/home'",
      "--volume='#{@home_dir}/smartmachine/grids/terminal/#{@name}/backups:/root/backups'", # TODO: Do not volumize backups folder by default. Give option in the config file to decide what volume should be exposed from host to terminal.
      "--volume='#{@home_dir}/smartmachine/apps/containers:/mnt/smartmachine/apps/containers'", # TODO: Do not volumize containers folder by default. Give option in the config file to decide what volume should be exposed from host to terminal.
      "--init",
      "--restart='always'",
      "--network='nginx-network'",
      "#{@image}"
    ]
    if system(command.compact.join(" "), out: File::NULL)
      puts "done"
      puts "-----> Starting container #{@name} ... "
      if system("docker start #{@name}", out: File::NULL)
        puts "done"

        @wetty.uper
      else
        raise "Error: Could not start container: #{@name}"
      end
    else
      raise "Error: Could not create container: #{@name}"
    end
  else
    raise "Error: Could not find image: #{@image}"
  end
end