Class: SmartMachine::Grids::Redis

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

Returns a new instance of Redis.



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

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

  @image = config.dig(:image)
  @port = config.dig(:port)
  @password = config.dig(:password)
  @appendonly = config.dig(:appendonly)
  @maxmemory = config.dig(:maxmemory)
  @maxmemory_policy = config.dig(:maxmemory_policy)
  if @image.start_with?("redislabs/redismod")
    @modules = config.dig(:modules)&.map { |module_name| "--loadmodule /usr/lib/redis/modules/#{module_name}.so" } || []
    @modules.push("Plugin /var/opt/redislabs/modules/rg/plugin/gears_python.so")
  else
    @modules = []
  end

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

Instance Method Details

#downerObject

Stopping & Removing containers - in reverse order



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/smart_machine/grids/redis.rb', line 63

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

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

#uperObject



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
# File 'lib/smart_machine/grids/redis.rb', line 25

def uper
  # Creating networks
  unless system("docker network inspect #{@name}-network", [:out, :err] => File::NULL)
    puts "-----> 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/redis/#{@name}/data")

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

  command = [
    "docker create",
    "--name='#{@name}'",
    "--user `id -u`:`id -g`",
    "--publish='#{@port}:#{@port}'",
    "--volume='#{@home_dir}/smartmachine/grids/redis/#{@name}/data:/data'",
    "--restart='always'",
    "--network='#{@name}-network'",
    "#{@image} --port #{@port} --requirepass #{@password} --appendonly #{@appendonly} --maxmemory #{@maxmemory} --maxmemory-policy #{@maxmemory_policy} #{@modules.join(' ')}".squish
  ]
  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