Class: Nvoi::External::Database::Mysql
- Inherits:
-
Provider
- Object
- Provider
- Nvoi::External::Database::Mysql
show all
- Defined in:
- lib/nvoi/external/database/mysql.rb
Overview
MySQL provider using mysqldump/mysql via kubectl exec
Instance Method Summary
collapse
Methods inherited from Provider
#extension, #needs_container?
Instance Method Details
#app_env(creds, host:) ⇒ Object
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/nvoi/external/database/mysql.rb', line 30
def app_env(creds, host:)
{
"DATABASE_URL" => build_url(creds, host:),
"MYSQL_HOST" => host,
"MYSQL_PORT" => creds.port,
"MYSQL_USER" => creds.user,
"MYSQL_PASSWORD" => creds.password,
"MYSQL_DATABASE" => creds.database
}
end
|
#build_url(creds, host: nil) ⇒ Object
16
17
18
19
|
# File 'lib/nvoi/external/database/mysql.rb', line 16
def build_url(creds, host: nil)
h = host || creds.host
"mysql://#{creds.user}:#{creds.password}@#{h}:#{creds.port}/#{creds.database}"
end
|
#container_env(creds) ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/nvoi/external/database/mysql.rb', line 21
def container_env(creds)
{
"MYSQL_USER" => creds.user,
"MYSQL_PASSWORD" => creds.password,
"MYSQL_DATABASE" => creds.database,
"MYSQL_ROOT_PASSWORD" => creds.password
}
end
|
#create_database(ssh, opts) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/nvoi/external/database/mysql.rb', line 74
def create_database(ssh, opts)
cmd = "kubectl exec -n default #{opts.pod_name} -- " \
"mysql -u #{opts.user} -p#{opts.password} -e \"CREATE DATABASE #{opts.database}\""
ssh.execute(cmd)
rescue Errors::SshCommandError => e
raise Errors::DatabaseError.new("create_database", "failed to create database: #{e.message}")
end
|
#default_port ⇒ Object
8
9
10
|
# File 'lib/nvoi/external/database/mysql.rb', line 8
def default_port
"3306"
end
|
#dump(ssh, opts) ⇒ Object
41
42
43
44
45
46
47
48
|
# File 'lib/nvoi/external/database/mysql.rb', line 41
def dump(ssh, opts)
cmd = "kubectl exec -n default #{opts.pod_name} -- " \
"mysqldump -u #{opts.user} -p#{opts.password} #{opts.database} " \
"--single-transaction --routines --triggers"
ssh.execute(cmd)
rescue Errors::SshCommandError => e
raise Errors::DatabaseError.new("dump", "mysqldump failed: #{e.message}")
end
|
#parse_url(url) ⇒ Object
12
13
14
|
# File 'lib/nvoi/external/database/mysql.rb', line 12
def parse_url(url)
parse_standard_url(url, default_port)
end
|
#restore(ssh, data, opts) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/nvoi/external/database/mysql.rb', line 50
def restore(ssh, data, opts)
create_database(ssh, Types::CreateOptions.new(
pod_name: opts.pod_name,
database: opts.database,
user: opts.user,
password: opts.password
))
temp_file = "/tmp/restore_#{opts.database}.sql"
write_cmd = "cat > #{temp_file} << 'SQLDUMP'\n#{data}\nSQLDUMP"
ssh.execute(write_cmd)
ssh.execute("kubectl cp #{temp_file} default/#{opts.pod_name}:#{temp_file}")
restore_cmd = "kubectl exec -n default #{opts.pod_name} -- " \
"sh -c 'mysql -u #{opts.user} -p#{opts.password} #{opts.database} < #{temp_file}'"
ssh.execute(restore_cmd)
ssh.execute_ignore_errors("rm -f #{temp_file}")
ssh.execute_ignore_errors("kubectl exec -n default #{opts.pod_name} -- rm -f #{temp_file}")
rescue Errors::SshCommandError => e
raise Errors::DatabaseError.new("restore", "mysql restore failed: #{e.message}")
end
|