Class: SmartMachine::Engine

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/engine.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

#initializeEngine

Returns a new instance of Engine.



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

def initialize
  @scp = SmartMachine::SCP.new
  @grids_nginx = SmartMachine::Grids::Nginx.new
  @syncer = SmartMachine::Syncer.new
  @machine = SmartMachine::Machine.new

  @gem_cache_dir = Gem::Specification.find_by_name("smartmachine").cache_dir

  if platform_on_machine?(os: "linux", distro_name: "debian")
    @docker_gid = "getent group docker | cut -d: -f3"
    @docker_gname = "docker"
    @docker_socket_path = "/var/run/docker.sock"
    @remote_smartmachine_dir = "/home/`whoami`/smartmachine"
  else
    raise("OS not supported to set docker_gid, docker_gname and docker_socket_path")
  end
end

Instance Method Details

#installObject



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
58
59
60
61
# File 'lib/smart_machine/engine.rb', line 22

def install
  puts "-----> Installing SmartMachine Engine"

  if @machine.run_on_machine commands: "mkdir -p #{@remote_smartmachine_dir}/tmp/engine"
    @scp.upload!(local_path: "#{@gem_cache_dir}/smartmachine-#{SmartMachine.version}.gem", remote_path: "~/smartmachine/tmp/engine")
  end

  puts "-----> Creating image for Engine ... "
  command = [
    "docker image build --quiet --tag #{engine_image_name_with_version}",
    "--build-arg TZDATA_TIMEZONE='#{SmartMachine.config.engine.dig(:engineone).dig(:timezone)}'",
    "--build-arg SMARTMACHINE_MASTER_KEY=#{SmartMachine::Credentials.new.read_key}",
    "--build-arg USER_NAME=`id -un`",
    "--build-arg USER_UID=`id -u`",
    "--build-arg DOCKER_GID=`#{@docker_gid}`",
    "--build-arg DOCKER_GNAME=#{@docker_gname}",
    "-f- #{@remote_smartmachine_dir}/tmp/engine",
    "<<'EOF'\n#{dockerfile}EOF"
  ]
  @machine.run_on_machine commands: command.join(" ")
  puts "done"

  puts "-----> Adding Engine to PATH ... "
  commands = [
    "mkdir -p #{@remote_smartmachine_dir}/bin && touch #{@remote_smartmachine_dir}/bin/smartengine",
    "echo '#{smartengine_binary_template}' > #{@remote_smartmachine_dir}/bin/smartengine",
    "chmod +x #{@remote_smartmachine_dir}/bin/smartengine",
    "sudo ln -sf #{@remote_smartmachine_dir}/bin/smartengine /usr/local/bin/smartengine",
    "rm -r #{@remote_smartmachine_dir}/tmp/engine",
    "smartengine --version"
  ]
  @machine.run_on_machine(commands: commands)
  puts "done"

  @grids_nginx.create_htpasswd_files

  @syncer.sync(initial: true)

  puts "-----> SmartMachine Engine Installation Complete"
end

#uninstallObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/smart_machine/engine.rb', line 63

def uninstall
  puts "-----> Uninstalling SmartMachine Engine"

  commands = [
    "sudo rm /usr/local/bin/smartengine",
    "sudo rm #{@remote_smartmachine_dir}/bin/smartengine",
    "docker rmi $(docker images -q #{engine_image_name})"
  ]
  @machine.run_on_machine(commands: commands)

  puts "-----> SmartMachine Engine Uninstallation Complete"
end