Class: SmartMachine::Grids::Mysql

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

Returns a new instance of Mysql.



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

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

  @port = config.dig(:port)
  @networks = Array(config.dig(:networks))
  @root_password = config.dig(:root_password)
  @username = config.dig(:username)
  @password = config.dig(:password)
  @database_name = config.dig(:database_name)

  @name = name.to_s
  @home_dir = File.expand_path('~')
  @backups_path = "#{@home_dir}/smartmachine/grids/mysql/#{@name}/backups"
end

Instance Method Details

#backup(*args) ⇒ Object

Create backup using the grids backup command



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

def backup(*args)
  args.flatten!
  type = args.empty? ? '--snapshot' : args.shift

  if type == "--daily"
    run_backup(type: "daily")
  elsif type == "--promote-to-weekly"
    run_backup(type: "weekly")
  elsif type == "--snapshot"
    run_backup(type: "snapshot")
  elsif type == "--transfer"
    transfer_backups_to_external_storage
  end
end

#downerObject

Stopping & Removing containers - in reverse order



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smart_machine/grids/mysql.rb', line 65

def downer
  @networks.each do |network|
    raise "Error: Could not disconnect container: #{network} - #{@name}" unless system("docker network disconnect #{network} #{@name}", out: File::NULL)
  end

  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

#flushlogs(*args) ⇒ Object

Flushing logs



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/smart_machine/grids/mysql.rb', line 87

def flushlogs(*args)
  puts "-----> Flushing logs for #{@name} ... "
  if system("docker exec #{@name} sh -c \
              'exec mysqladmin \
              --user=root \
              --password=#{@root_password} \
              flush-logs'")

    puts "done"
  else
    puts "error"
  end
end

#uperObject



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
58
59
60
61
62
# File 'lib/smart_machine/grids/mysql.rb', line 20

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/mysql/#{@name}/data")
  FileUtils.mkdir_p("#{@home_dir}/machine/grids/mysql/#{@name}/backups")

  # Creating & Starting containers
  puts "-----> Creating container #{@name} ... "
  command = [
    "docker create",
    "--name='#{@name}'",
    "--env MYSQL_ROOT_PASSWORD=#{@root_password}",
    "--env MYSQL_USER=#{@username}",
    "--env MYSQL_PASSWORD=#{@password}",
    "--env MYSQL_DATABASE=#{@database_name}",
    "--user `id -u`:`id -g`",
    "--publish='#{@port}:#{@port}'",
    "--volume='#{@home_dir}/smartmachine/grids/mysql/#{@name}/data:/var/lib/mysql'",
    "--restart='always'",
    "--network='#{@name}-network'",
    "mysql:8.0.18"
  ]
  if system(command.compact.join(" "), out: File::NULL)
    puts "done"
    puts "-----> Starting container #{@name} ... "
    if system("docker start #{@name}", out: File::NULL)
      @networks.each do |network|
        raise "Error: Could not connect container: #{network} - #{@name}" unless system("docker network connect #{network} #{@name}", out: File::NULL)
      end
      puts "done"
    else
      raise "Error: Could not start the created #{@name} container"
    end
  else
    raise "Error: Could not create #{@name} container"
  end
end