Class: Conjure::Service::Database::Mysql

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure/service/database/mysql.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Mysql

Returns a new instance of Mysql.



5
6
7
8
9
# File 'lib/conjure/service/database/mysql.rb', line 5

def initialize(options)
  @target = options[:target]
  @db_name = options[:database_name]
  @adapter_name = options[:adapter_name]
end

Instance Method Details

#adapter_nameObject



63
64
65
# File 'lib/conjure/service/database/mysql.rb', line 63

def adapter_name
  @adapter_name || "mysql2"
end

#base_imageObject



11
12
13
14
15
16
17
18
# File 'lib/conjure/service/database/mysql.rb', line 11

def base_image
  @base_image ||= @target.shell.prepare(
    label: "mysql",
    setup_commands: [
      "apt-get install -y mysql-server mysql-client"
    ],
  )
end

#client_optionsObject



59
60
61
# File 'lib/conjure/service/database/mysql.rb', line 59

def client_options
  "-u root -h #{ip_address} #{@db_name}"
end

#containerObject



35
36
37
# File 'lib/conjure/service/database/mysql.rb', line 35

def container
  @container ||= server_image.run
end

#export(file) ⇒ Object



47
48
49
50
51
52
# File 'lib/conjure/service/database/mysql.rb', line 47

def export(file)
  File.open file, "w" do |f|
    f.write base_image.command("/usr/bin/mysqldump #{client_options}")
  end
  Log.info "[export] #{File.size file} bytes exported to #{file}"
end

#import(file) ⇒ Object



54
55
56
57
# File 'lib/conjure/service/database/mysql.rb', line 54

def import(file)
  base_image.command "echo 'source /files/#{File.basename file}' | /usr/bin/mysql #{client_options}", files: [file]
  Log.info "[import] #{File.size file} bytes imported from #{file}"
end

#ip_addressObject



43
44
45
# File 'lib/conjure/service/database/mysql.rb', line 43

def ip_address
  container.ip_address
end

#nameObject



39
40
41
# File 'lib/conjure/service/database/mysql.rb', line 39

def name
  @db_name
end

#runObject



31
32
33
# File 'lib/conjure/service/database/mysql.rb', line 31

def run
  container
end

#server_imageObject



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

def server_image
  @server_image ||= base_image.prepare(
    label: "mysqlserver",
    setup_commands: [
      "/usr/sbin/mysqld & sleep 5; echo \"GRANT ALL ON *.* TO root@'%' IDENTIFIED BY '' WITH GRANT OPTION\" | /usr/bin/mysql",
    ],
    daemon_command: "/usr/sbin/mysqld --bind-address=0.0.0.0",
    volumes: ["/var/lib/mysql"],
  )
end