Class: SmartMachine::Grids::Elasticsearch

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_machine/grids/elasticsearch.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:) ⇒ Elasticsearch

Returns a new instance of Elasticsearch.



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

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

  @port = config.dig(:port)

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

Instance Method Details

#downerObject



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

def downer
  # Stopping & Removing containers - in reverse order
  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

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

#uperObject



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/grids/elasticsearch.rb', line 14

def uper
  # Creating networks
  unless system("docker network inspect #{@name}-network", [:out, :err] => File::NULL)
    print "-----> Creating network #{@name}-network ... "
    if system("docker network create #{@name}-network", out: File::NULL)
      puts "done"
    end
  end

  FileUtils.mkdir_p("#{@home_dir}/machine/grids/elasticsearch/#{@name}/data")
  FileUtils.mkdir_p("#{@home_dir}/machine/grids/elasticsearch/#{@name}/logs")

  # Creating & Starting containers
  print "-----> Creating container #{@name} ... "
  command = [
    "docker create",
    "--name='#{@name}'",
    "--label='smartmachine.elasticsearch.name=#{@name}'",
    "--env discovery.type=single-node",
    "--env cluster.name=#{@name}-cluster",
    "--env 'ES_JAVA_OPTS=-Xms512m -Xmx512m -Des.enforce.bootstrap.checks=true'",
    "--env bootstrap.memory_lock=true",
    "--ulimit memlock=-1:-1",
    "--ulimit nofile=65535:65535",
    "--user `id -u`:`id -g`",
    "--publish='#{@port}:#{@port}'",
    "--volume='#{@home_dir}/smartmachine/grids/elasticsearch/#{@name}/data:/usr/share/elasticsearch/data'",
    "--volume='#{@home_dir}/smartmachine/grids/elasticsearch/#{@name}/logs:/usr/share/elasticsearch/logs'",
    "--restart='always'",
    "--network='#{@name}-network'",
    "elasticsearch:7.4.1"
  ]
  if system(command.compact.join(" "), out: File::NULL)
    puts "done"
    puts "-----> Starting container #{@name} ... "
    if system("docker start #{@name}", out: File::NULL)
      puts "done"
    else
      raise "Error: Could not start the created #{@name} container"
    end
  else
    raise "Error: Could not create #{@name} container"
  end
end