capistrano-container-db 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/capistrano-container-db.gemspec +2 -2
- data/lib/capistrano/container/db.rb +3 -10
- data/lib/capistrano/tasks/db.rb +11 -11
- data/lib/db/dump_helper.rb +23 -16
- data/lib/db/helper.rb +55 -48
- data/lib/db/load_helper.rb +26 -19
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f9fafce47206c3a7e06f2738642dd8e4f1e474a
|
4
|
+
data.tar.gz: acbbaf8af6245ad654174f293d76df51a76c52ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b3c7918fc632dff312afb964d74aaa1a2057ec5a8591284e0c4fc86cb61edf33d9458a2b4826c29563ac5b52eaf89cd386d291180bd7eb91a41862ddb8a9af8
|
7
|
+
data.tar.gz: 3e831a15ffba3167f66759118441e5fcca4dc4d971f82ce3b28e1a38996d169acc732fd2bdc9fb646c1de8001bea5c62ded31c53fff4cd7f471f1b0f7741b481
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Helps managing databases on local and remote stages, also on remote [docker](https://www.docker.com/) container for Capistrano 3.x.
|
4
4
|
|
5
|
-
This project is in an early stage but helps me
|
5
|
+
This project is in an early stage but helps me a lot dealing with my container deployments and keeps my code clean. It is not only meant for mysql, but at the moment there is only support for mysql, feel free to contribute =)
|
6
6
|
|
7
7
|
This gem depends on [capistrano-container](https://github.com/creative-workflow/capistrano-container).
|
8
8
|
|
@@ -90,6 +90,10 @@ set :filter_on_import, lambda{ |sql_dump| return sql_dump } -> !not implemented
|
|
90
90
|
* add db:execute task
|
91
91
|
|
92
92
|
## Changes
|
93
|
+
### Version 0.0.6
|
94
|
+
* use clean module namespaces
|
95
|
+
* add `--lock-tables=false` as default dump arg
|
96
|
+
|
93
97
|
### Version 0.0.5
|
94
98
|
* add "CREATE DATABASE IF NOT EXISTS" to import statements
|
95
99
|
* provide additional mysql auth/dump/restore args
|
@@ -4,8 +4,8 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'capistrano-container-db'
|
7
|
-
spec.version = '0.0.
|
8
|
-
spec.date = '
|
7
|
+
spec.version = '0.0.6'
|
8
|
+
spec.date = '2018-02-03'
|
9
9
|
spec.summary = 'Helps managing databases on local and remote stages, also on remote docker container'
|
10
10
|
spec.description = spec.summary
|
11
11
|
spec.authors = ['Tom Hanoldt']
|
@@ -1,11 +1,4 @@
|
|
1
1
|
require_relative '../tasks/db.rb'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
module DB
|
6
|
-
require_relative '../../db/helper.rb'
|
7
|
-
require_relative '../../db/load_helper.rb'
|
8
|
-
require_relative '../../db/dump_helper.rb'
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
2
|
+
require_relative '../../db/helper.rb'
|
3
|
+
require_relative '../../db/load_helper.rb'
|
4
|
+
require_relative '../../db/dump_helper.rb'
|
data/lib/capistrano/tasks/db.rb
CHANGED
@@ -7,7 +7,7 @@ namespace :db do
|
|
7
7
|
task :execute do
|
8
8
|
on roles(:db, :container_host) do |host|
|
9
9
|
ask(:tmp_cmd, "mysql command")
|
10
|
-
Helper.execute_db_command_autodetect fetch(:tmp_cmd)
|
10
|
+
Capistrano::Container::DB::Helper.execute_db_command_autodetect fetch(:tmp_cmd)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -16,14 +16,14 @@ namespace :db do
|
|
16
16
|
task :export do
|
17
17
|
on roles(:db, :container_host) do |host|
|
18
18
|
if fetch(:db_is_container)
|
19
|
-
DumpHelper::dump_on_container_and_download container_by_name(fetch(:db_container_name))
|
20
|
-
elsif Helper::local_stage?
|
21
|
-
DumpHelper::dump_on_local
|
19
|
+
Capistrano::Container::DB::DumpHelper::dump_on_container_and_download container_by_name(fetch(:db_container_name))
|
20
|
+
elsif Capistrano::Container::DB::Helper::local_stage?
|
21
|
+
Capistrano::Container::DB::DumpHelper::dump_on_local
|
22
22
|
else
|
23
|
-
DumpHelper::dump_on_server_and_download
|
23
|
+
Capistrano::Container::DB::DumpHelper::dump_on_server_and_download
|
24
24
|
end
|
25
25
|
|
26
|
-
Helper::duplicate_local_dump_to_staged_dump
|
26
|
+
Capistrano::Container::DB::Helper::duplicate_local_dump_to_staged_dump
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -31,11 +31,11 @@ namespace :db do
|
|
31
31
|
task :import do
|
32
32
|
on roles(:db, :container_host) do
|
33
33
|
if fetch(:db_is_container)
|
34
|
-
LoadHelper::import_on_container container_by_name(fetch(:db_container_name))
|
35
|
-
elsif Helper::local_stage?
|
36
|
-
LoadHelper::import_on_local
|
34
|
+
Capistrano::Container::DB::LoadHelper::import_on_container container_by_name(fetch(:db_container_name))
|
35
|
+
elsif Capistrano::Container::DB::Helper::local_stage?
|
36
|
+
Capistrano::Container::DB::LoadHelper::import_on_local
|
37
37
|
else
|
38
|
-
LoadHelper::import_on_server
|
38
|
+
Capistrano::Container::DB::LoadHelper::import_on_server
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
@@ -50,7 +50,7 @@ namespace :load do
|
|
50
50
|
set :db_additional_restore_args, []
|
51
51
|
# dont use --database statement, so no use '...' will be generated and we
|
52
52
|
# can have different db names local and remote
|
53
|
-
set :db_additional_dump_args, ['--no-create-db']
|
53
|
+
set :db_additional_dump_args, ['--no-create-db --lock-tables=false']
|
54
54
|
set :db_remote_dump, '/tmp/dump.sql'
|
55
55
|
set :db_local_dump, 'config/db/dump.sql'
|
56
56
|
set :db_is_container, false
|
data/lib/db/dump_helper.rb
CHANGED
@@ -1,27 +1,34 @@
|
|
1
1
|
require_relative "helper.rb"
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
3
|
+
module Capistrano
|
4
|
+
module Container
|
5
|
+
module DB
|
6
|
+
module DumpHelper
|
7
|
+
def self.dump_on_local()
|
8
|
+
args = Helper::mysql_dump_args
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
run_locally do
|
11
|
+
execute "mysqldump #{args} > #{fetch(:db_local_dump)}"
|
12
|
+
end
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
def self.dump_on_server_and_download()
|
16
|
+
args = Helper::mysql_dump_args
|
14
17
|
|
15
|
-
|
18
|
+
execute "mysqldump #{args} > #{fetch(:db_remote_dump)}"
|
16
19
|
|
17
|
-
|
18
|
-
|
20
|
+
download!(fetch(:db_remote_dump), fetch(:db_local_dump))
|
21
|
+
end
|
19
22
|
|
20
|
-
|
21
|
-
|
23
|
+
def self.dump_on_container_and_download(container)
|
24
|
+
args = Helper::mysql_dump_args
|
22
25
|
|
23
|
-
|
26
|
+
container.execute("mysqldump #{args} > #{fetch(:db_remote_dump)}")
|
24
27
|
|
25
|
-
|
28
|
+
container.download!(fetch(:db_remote_dump), fetch(:db_local_dump))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
26
33
|
end
|
27
34
|
end
|
data/lib/db/helper.rb
CHANGED
@@ -1,52 +1,59 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
1
|
+
module Capistrano
|
2
|
+
module Container
|
3
|
+
module DB
|
4
|
+
module Helper
|
5
|
+
def self.mysql_dump_args
|
6
|
+
command = mysql_auth_args
|
7
|
+
command+= " #{fetch(:db_additional_dump_args).join(' ')} "
|
8
|
+
command+= " #{fetch(:db_name)}" unless fetch(:db_name).empty?
|
9
|
+
command
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.mysql_restore_args
|
13
|
+
command = mysql_auth_args
|
14
|
+
command+= " #{fetch(:db_additional_restore_args).join(' ')} "
|
15
|
+
command+= " #{fetch(:db_name)}" unless fetch(:db_name).empty?
|
16
|
+
command
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.mysql_auth_args
|
20
|
+
command = " -u #{fetch(:db_user)}"
|
21
|
+
command+= " -p#{fetch(:db_pass)}" unless fetch(:db_pass).empty?
|
22
|
+
command+= " #{fetch(:db_additional_auth_args).join(' ')} "
|
23
|
+
command
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.append_stage_to_filename(file_name, stage = 'local')
|
27
|
+
splitted = file_name.split('.')
|
28
|
+
extension = splitted.pop
|
29
|
+
splitted.push stage, extension
|
30
|
+
splitted.join('.')
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.duplicate_local_dump_to_staged_dump()
|
34
|
+
staged_file = append_stage_to_filename(fetch(:db_local_dump), fetch(:stage))
|
35
|
+
|
36
|
+
FileUtils.cp(fetch(:db_local_dump), staged_file)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.local_stage?
|
40
|
+
fetch(:local_stage_name).to_sym == fetch(:stage).to_sym
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.execute_db_command_autodetect(cmd)
|
44
|
+
cmd = "mysql #{Helper::mysql_auth_args} -e \"#{cmd}\""
|
45
|
+
|
46
|
+
if fetch(:db_is_container)
|
47
|
+
db_container = container_by_name fetch(:db_container_name)
|
48
|
+
on_container db_container do |container|
|
49
|
+
container.execute cmd
|
50
|
+
end
|
51
|
+
else
|
52
|
+
execute_local_or_remote cmd
|
53
|
+
end
|
54
|
+
end
|
47
55
|
end
|
48
|
-
|
49
|
-
execute_local_or_remote cmd
|
56
|
+
|
50
57
|
end
|
51
58
|
end
|
52
59
|
end
|
data/lib/db/load_helper.rb
CHANGED
@@ -1,32 +1,39 @@
|
|
1
1
|
require_relative "helper.rb"
|
2
2
|
require 'capistrano/container'
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
4
|
+
module Capistrano
|
5
|
+
module Container
|
6
|
+
module DB
|
7
|
+
module LoadHelper
|
8
|
+
def self.import_on_local()
|
9
|
+
LoadHelper.create_db_if_not_exists fetch(:db_name)
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
run_locally do
|
12
|
+
execute "mysql #{Helper::mysql_restore_args} < #{fetch(:db_local_dump)}"
|
13
|
+
end
|
14
|
+
end
|
12
15
|
|
13
|
-
|
14
|
-
|
16
|
+
def self.import_on_container(container)
|
17
|
+
container.upload!(fetch(:db_local_dump), fetch(:db_remote_dump))
|
15
18
|
|
16
|
-
|
19
|
+
LoadHelper.create_db_if_not_exists fetch(:db_name)
|
17
20
|
|
18
|
-
|
19
|
-
|
21
|
+
container.execute "mysql #{Helper::mysql_restore_args} < #{fetch(:db_remote_dump)}"
|
22
|
+
end
|
20
23
|
|
21
|
-
|
22
|
-
|
24
|
+
def self.import_on_server()
|
25
|
+
upload!(fetch(:db_local_dump), fetch(:db_remote_dump))
|
23
26
|
|
24
|
-
|
27
|
+
LoadHelper.create_db_if_not_exists fetch(:db_name)
|
25
28
|
|
26
|
-
|
27
|
-
|
29
|
+
execute("mysql #{Helper::mysql_restore_args} < #{fetch(:db_remote_dump)}")
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.create_db_if_not_exists(db)
|
33
|
+
Helper.execute_db_command_autodetect "CREATE DATABASE IF NOT EXISTS #{db};"
|
34
|
+
end
|
35
|
+
end
|
28
36
|
|
29
|
-
|
30
|
-
Helper.execute_db_command_autodetect "CREATE DATABASE IF NOT EXISTS #{db};"
|
37
|
+
end
|
31
38
|
end
|
32
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capistrano-container-db
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Hanoldt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: capistrano
|
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
106
106
|
version: '0'
|
107
107
|
requirements: []
|
108
108
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.6.14
|
110
110
|
signing_key:
|
111
111
|
specification_version: 4
|
112
112
|
summary: Helps managing databases on local and remote stages, also on remote docker
|