Class: Mcrain::Mysql

Inherits:
Base
  • Object
show all
Defined in:
lib/mcrain/mysql.rb

Constant Summary collapse

DB_DIR_ON_CONTAINER =
'/var/lib/mysql'.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#skip_reset_after_teardown

Instance Method Summary collapse

Methods inherited from Base

#initialize, #logger, #reset, #setup, #start, #start_callback, #stop_or_kill_and_remove, #teardown, #wait, #wait_port, work_dir

Methods included from ClientProvider

#build_client, #client, #client_instantiation_script, #client_script

Methods included from ContainerController

#add_volume_options, #container, #container_image, #find_portno, #host, included, #info, #ip, #name, #port, #ssh_uri, #url

Constructor Details

This class inherits a constructor from Mcrain::Base

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



36
37
38
# File 'lib/mcrain/mysql.rb', line 36

def database
  @database
end

#db_dirObject

Returns the value of attribute db_dir.



35
36
37
# File 'lib/mcrain/mysql.rb', line 35

def db_dir
  @db_dir
end

#passwordObject

Returns the value of attribute password.



37
38
39
# File 'lib/mcrain/mysql.rb', line 37

def password
  @password
end

#usernameObject

Returns the value of attribute username.



37
38
39
# File 'lib/mcrain/mysql.rb', line 37

def username
  @username
end

Instance Method Details

#build_docker_optionsObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mcrain/mysql.rb', line 41

def build_docker_options
  r = super

  username = self.username || "root" # overwrite locally
  key_user = (username == "root") ? nil                   : "MYSQL_USER"
  key_pw   = (username == "root") ? "MYSQL_ROOT_PASSWORD" : "MYSQL_PASSWORD"
  envs = []
  envs << (password.blank? ? "MYSQL_ALLOW_EMPTY_PASSWORD=yes" : "#{key_pw}=#{password}")
  envs << "#{key_user}=#{username}"  if key_user
  envs << "MYSQL_DATABASE=#{database}" if database
  add_volume_options(r, DB_DIR_ON_CONTAINER, File.expand_path(db_dir)) if db_dir && !db_dir.empty?
  r['Env'] = envs unless envs.empty?
  return r
end

#client_classObject



16
17
18
# File 'lib/mcrain/mysql.rb', line 16

def client_class
  ::Mysql2::Client
end

#client_init_argsObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/mcrain/mysql.rb', line 20

def client_init_args
  options = {
    host: host,
    port: port,
    username: username || "root"
  }
  options[:password] = password if password.present?
  options[:database] = database if database.present?
  return [options]
end

#client_requireObject



12
13
14
# File 'lib/mcrain/mysql.rb', line 12

def client_require
  'mysql2'
end

#wait_for_readyObject



31
32
33
# File 'lib/mcrain/mysql.rb', line 31

def wait_for_ready
  client.query("show databases").to_a
end