swarm_cluster_cli_ope 0.3.2 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +27 -4
- data/lib/swarm_cluster_cli_ope/cli.rb +40 -11
- data/lib/swarm_cluster_cli_ope/configuration.rb +9 -0
- data/lib/swarm_cluster_cli_ope/sync_configs/base_database.rb +49 -0
- data/lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb +4 -3
- data/lib/swarm_cluster_cli_ope/sync_configs/mysql.rb +8 -44
- data/lib/swarm_cluster_cli_ope/sync_configs/post_gres.rb +115 -0
- data/lib/swarm_cluster_cli_ope/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcf7eb87d7665ee32ead2dff9a05a3a7abb09587c6ffb0dcea8fa8c1dee8564b
|
4
|
+
data.tar.gz: f6f4716e71b2505d0b0e1928679ba92bc572c4382bb165f4ae3a77de5c01abcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbe56a69b398f32dfd2ba9ef1328fa3c10f6182f689b38089d8aa3a25e67130ee246459cf84e1b06e688172843b4b64c5aeaaec08fa9856fffd3ab5921625d59
|
7
|
+
data.tar.gz: bd0b42b2043c0462914cfe240c59c931554b675001781642674bc82425c4c5df10aeb782acdcdaac4b1727e3e948bda9f93646455e5eaeb80ad91263239ee71f
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -130,13 +130,36 @@ Possibili CFGS per tipologia:
|
|
130
130
|
- local: -> hash di configurazioni per il DB locale
|
131
131
|
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
132
132
|
- mysql_password_env: "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
133
|
+
- mysql_password: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
133
134
|
- mysql_user_env: "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
135
|
+
- mysql_user: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
136
|
+
- database_name_env: "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
137
|
+
- database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
|
138
|
+
- remote: -> hash di configurazioni per il DB remoto
|
139
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
140
|
+
- mysql_password_env: "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
141
|
+
- mysql_password: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
142
|
+
- mysql_user_env: "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
143
|
+
- mysql_user: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
134
144
|
- database_name_env: "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
145
|
+
- database_name: "MYSQL_DATABASE" -> valore in chiaro, in sostituzione della variabile ambiente
|
146
|
+
- pg:
|
147
|
+
- local: -> hash di configurazioni per il DB locale
|
148
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
149
|
+
- pg_password_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: POSTGRES_PASSWORD
|
150
|
+
- pg_password: "" -> valore in chiaro, in sostituzione della variabile ambiente
|
151
|
+
- pg_user_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: POSTGRES_USER
|
152
|
+
- pg_user: "postgres" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: postgres
|
153
|
+
- database_name_env: "POSTGRES_DB" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: POSTGRES_DB
|
154
|
+
- database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
|
135
155
|
- remote: -> hash di configurazioni per il DB remoto
|
136
156
|
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
137
|
-
-
|
138
|
-
-
|
139
|
-
-
|
157
|
+
- pg_password_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: POSTGRES_PASSWORD
|
158
|
+
- pg_password: "" -> valore in chiaro, in sostituzione della variabile ambiente
|
159
|
+
- pg_user_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: POSTGRES_USER
|
160
|
+
- pg_user: "postgres" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: postgres
|
161
|
+
- database_name_env: "POSTGRES_DB" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: POSTGRES_DB
|
162
|
+
- database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
|
140
163
|
|
141
164
|
#### EXAMPLE:
|
142
165
|
Esempio di sincronizzazione di un file sqlite3 e una cartella
|
@@ -180,7 +203,7 @@ docker stack deploy -c test_folder/test_2/docker_compose.yml test2
|
|
180
203
|
Per simulare una sincronizzazione fra locale e remoto di un mysql, lanciamo lo stesso stack anche come compose, in modo
|
181
204
|
da trovarci sulla stessa macchina con tutte e due le situazioni
|
182
205
|
```shell script
|
183
|
-
docker-compose
|
206
|
+
docker-compose -f test_folder/test_1/docker-compose-local.yml up -d
|
184
207
|
```
|
185
208
|
|
186
209
|
|
@@ -243,6 +243,11 @@ module SwarmClusterCliOpe
|
|
243
243
|
end
|
244
244
|
end
|
245
245
|
|
246
|
+
desc "version", "versione della cli"
|
247
|
+
def version
|
248
|
+
say VERSION
|
249
|
+
end
|
250
|
+
|
246
251
|
desc "stacksync [DIRECTION:pull|push]", "Si occupa di scaricare|caricare,utilizzando le configurazioni presenti, i dati dallo stack remoto"
|
247
252
|
long_desc <<-LONGDESC.gsub("\n", "\x5")
|
248
253
|
le configurazioni sono contenute nell'array: sync_configs.
|
@@ -270,17 +275,40 @@ module SwarmClusterCliOpe
|
|
270
275
|
--remote: -> path al file remoto (contesto del container)
|
271
276
|
|
272
277
|
mysql:
|
273
|
-
--
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
278
|
+
--local: -> hash di configurazioni per il DB locale
|
279
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
280
|
+
- mysql_password_env: "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
281
|
+
- mysql_password: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
282
|
+
- mysql_user_env: "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
283
|
+
- mysql_user: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
284
|
+
- database_name_env: "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
285
|
+
- database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
|
286
|
+
--remote: -> hash di configurazioni per il DB remoto
|
287
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
288
|
+
- mysql_password_env: "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
289
|
+
- mysql_password: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
290
|
+
- mysql_user_env: "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
291
|
+
- mysql_user: "root" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: root
|
292
|
+
- database_name_env: "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
293
|
+
- database_name: "MYSQL_DATABASE" -> valore in chiaro, in sostituzione della variabile ambiente
|
294
|
+
pg:
|
295
|
+
--local: -> hash di configurazioni per il DB locale
|
296
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
297
|
+
- pg_password_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: POSTGRES_PASSWORD
|
298
|
+
- pg_password: "" -> valore in chiaro, in sostituzione della variabile ambiente
|
299
|
+
- pg_user_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: POSTGRES_USER
|
300
|
+
- pg_user: "postgres" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: postgres
|
301
|
+
- database_name_env: "POSTGRES_DB" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: POSTGRES_DB
|
302
|
+
- database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
|
303
|
+
--remote: -> hash di configurazioni per il DB remoto
|
304
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
305
|
+
- pg_password_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: POSTGRES_PASSWORD
|
306
|
+
- pg_password: "" -> valore in chiaro, in sostituzione della variabile ambiente
|
307
|
+
- pg_user_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: POSTGRES_USER
|
308
|
+
- pg_user: "postgres" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: postgres
|
309
|
+
- database_name_env: "POSTGRES_DB" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: POSTGRES_DB
|
310
|
+
- database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
|
311
|
+
|
284
312
|
|
285
313
|
EXAMPLE:
|
286
314
|
Esempio di sincronizzazione di un file sqlite3 e una cartella
|
@@ -325,6 +353,7 @@ module SwarmClusterCliOpe
|
|
325
353
|
say "----------->>>>>>"
|
326
354
|
say "[ #{sync.class.name} ]"
|
327
355
|
sync.send(direction)
|
356
|
+
say "COMPLETE"
|
328
357
|
say "<<<<<<-----------"
|
329
358
|
end
|
330
359
|
end
|
@@ -188,6 +188,8 @@ module SwarmClusterCliOpe
|
|
188
188
|
SyncConfigs::Rsync.new(self, c)
|
189
189
|
when 'mysql'
|
190
190
|
SyncConfigs::Mysql.new(self, c)
|
191
|
+
when 'pg'
|
192
|
+
SyncConfigs::PostGres.new(self, c)
|
191
193
|
else
|
192
194
|
logger.error { "CONFIGURAIONE NON PREVISTA: #{c[:how]}" }
|
193
195
|
nil
|
@@ -249,6 +251,13 @@ module SwarmClusterCliOpe
|
|
249
251
|
hash[key] = self.class.read_base.merge(project_cfgs)
|
250
252
|
end
|
251
253
|
|
254
|
+
configuration_version = @_merged_configurations[@environment][:version]
|
255
|
+
if Gem::Version.new(configuration_version) > Gem::Version.new(VERSION)
|
256
|
+
puts "WARNING: Versione del file di configurazione [#{configuration_version}] più aggiornata della gemma [#{VERSION}], eseguire upgrade
|
257
|
+
gem update swarm_cluster_cli_ope"
|
258
|
+
exit
|
259
|
+
end
|
260
|
+
|
252
261
|
@_merged_configurations[@environment]
|
253
262
|
|
254
263
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module SwarmClusterCliOpe
|
2
|
+
module SyncConfigs
|
3
|
+
class BaseDatabase < Base
|
4
|
+
|
5
|
+
# @return [SwarmClusterCliOpe::SyncConfigs::EnvConfigs]
|
6
|
+
def remote
|
7
|
+
self.class::EnvConfigs.new(self, @configs[:configs][:remote] || {}, -> { container })
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [SwarmClusterCliOpe::SyncConfigs::EnvConfigs]
|
11
|
+
def local
|
12
|
+
self.class::EnvConfigs.new(self, @configs[:configs][:local] || {}, -> { local_container })
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# Classe interna che rappresenta le configurazioni del DB
|
17
|
+
class EnvConfigs < SwarmClusterCliOpe::SyncConfigs::EnvConfigs
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Funzione che ricapitola le informazioni utilizzate per eseguire l'operazione
|
23
|
+
def resume(direction)
|
24
|
+
puts "RESUME - #{direction}
|
25
|
+
service: #{service}
|
26
|
+
local:
|
27
|
+
service_name: #{local.service_name}
|
28
|
+
database_name: #{local.database_name}
|
29
|
+
username: #{local.username}
|
30
|
+
password: #{local.password}
|
31
|
+
remote:
|
32
|
+
service_name: #{remote.service_name}
|
33
|
+
database_name: #{remote.database_name}
|
34
|
+
username: #{remote.username}
|
35
|
+
password: #{remote.password}"
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def local_container
|
42
|
+
# il nome dello stack del compose usiamo come standard il nome della cartella, come lo fà già composer di default
|
43
|
+
Models::ComposeContainer.find_by_service_name(local.service_name, stack_name: @stack_cfgs.local_compose_project_name)
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -35,7 +35,8 @@ module SwarmClusterCliOpe
|
|
35
35
|
# @param [String,Symbol] configuration_name -> nome della configurazione da utilizzare per estrapolare la configurazione
|
36
36
|
# in automatico viene tenuto conto se cercare per la versione
|
37
37
|
# con _env o senza....precedenza SENZA
|
38
|
-
|
38
|
+
# @param [nil,String] default_value se non è estrapolato nessun valore, viene utilizzato il valore di default
|
39
|
+
def self.define_cfgs(name, default_env:, configuration_name:,default_value:nil)
|
39
40
|
configuration_name ||= name
|
40
41
|
|
41
42
|
define_method(name) do
|
@@ -43,10 +44,10 @@ module SwarmClusterCliOpe
|
|
43
44
|
|
44
45
|
#valore restituito direttamente dalla configurazione
|
45
46
|
if @configs.key?(configuration_name)
|
46
|
-
value = @configs["#{configuration_name}".to_sym]
|
47
|
+
value = @configs["#{configuration_name}".to_sym] || default_value
|
47
48
|
else
|
48
49
|
env_var = @configs["#{configuration_name}_env".to_sym] || default_env
|
49
|
-
value = find_env_file_variable(env_var)
|
50
|
+
value = find_env_file_variable(env_var) || default_value
|
50
51
|
end
|
51
52
|
self.instance_variable_set("@#{name}", value)
|
52
53
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
module SwarmClusterCliOpe
|
2
2
|
module SyncConfigs
|
3
|
-
class Mysql <
|
3
|
+
class Mysql < BaseDatabase
|
4
4
|
|
5
5
|
# @return [TrueClass, FalseClass]
|
6
6
|
def pull
|
7
7
|
resume('pull')
|
8
8
|
if yes?("Confermare il comando?[y,yes]")
|
9
9
|
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
10
|
-
container.exec("bash -c 'mysqldump -u #{remote.
|
10
|
+
container.exec("bash -c 'mysqldump -u #{remote.username} --password=#{remote.password} #{remote.database_name} | gzip -c -f' > #{tmp_file}")
|
11
11
|
local_container.copy_in(tmp_file, tmp_file)
|
12
|
-
local_container.exec("bash -c 'zcat #{tmp_file} | mysql -u #{local.
|
12
|
+
local_container.exec("bash -c 'zcat #{tmp_file} | mysql -u #{local.username} --password=#{local.password} #{local.database_name}'")
|
13
13
|
end
|
14
14
|
true
|
15
15
|
end
|
@@ -19,59 +19,23 @@ module SwarmClusterCliOpe
|
|
19
19
|
resume('PUSH')
|
20
20
|
if yes?("ATTENZIONE !!!!!!PUSH!!!!! - Confermare il comando?[y,yes]")
|
21
21
|
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
22
|
-
local_container.exec("bash -c 'mysqldump -u #{local.
|
22
|
+
local_container.exec("bash -c 'mysqldump -u #{local.username} --password=#{local.password} #{local.database_name} | gzip -c -f' > #{tmp_file}")
|
23
23
|
container.copy_in(tmp_file, tmp_file)
|
24
|
-
container.exec("bash -c 'zcat #{tmp_file} | mysql -u #{remote.
|
24
|
+
container.exec("bash -c 'zcat #{tmp_file} | mysql -u #{remote.username} --password=#{remote.password} #{remote.database_name}'")
|
25
25
|
end
|
26
26
|
true
|
27
27
|
end
|
28
28
|
|
29
|
-
# @return [SwarmClusterCliOpe::SyncConfigs::Mysql::EnvConfigs]
|
30
|
-
def remote
|
31
|
-
EnvConfigs.new(self, @configs[:configs][:remote] || {}, -> { container })
|
32
|
-
end
|
33
|
-
|
34
|
-
# @return [SwarmClusterCliOpe::SyncConfigs::Mysql::EnvConfigs]
|
35
|
-
def local
|
36
|
-
EnvConfigs.new(self, @configs[:configs][:local] || {}, -> { local_container })
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
29
|
##
|
41
30
|
# Classe interna che rappresenta le configurazioni del DB
|
42
|
-
class EnvConfigs <
|
31
|
+
class EnvConfigs < BaseDatabase::EnvConfigs
|
43
32
|
|
44
33
|
define_cfgs :database_name, default_env: "MYSQL_DATABASE", configuration_name: :database_name
|
45
|
-
define_cfgs :
|
46
|
-
define_cfgs :
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
##
|
52
|
-
# Funzione che ricapitola le informazioni utilizzate per eseguire l'operazione
|
53
|
-
def resume(direction)
|
54
|
-
puts "RESUME - #{direction}
|
55
|
-
service: #{service}
|
56
|
-
local:
|
57
|
-
service_name: #{local.service_name}
|
58
|
-
database_name: #{local.database_name}
|
59
|
-
mysql_user: #{local.mysql_user}
|
60
|
-
mysql_password: #{local.mysql_password}
|
61
|
-
remote:
|
62
|
-
service_name: #{remote.service_name}
|
63
|
-
database_name: #{remote.database_name}
|
64
|
-
mysql_user: #{remote.mysql_user}
|
65
|
-
mysql_password: #{remote.mysql_password}"
|
34
|
+
define_cfgs :username, default_env: "MYSQL_USER", configuration_name: :mysql_user, default_value: 'root'
|
35
|
+
define_cfgs :password, default_env: "MYSQL_PASSWORD", configuration_name: :mysql_password, default_value: 'root'
|
66
36
|
|
67
37
|
end
|
68
38
|
|
69
|
-
private
|
70
|
-
|
71
|
-
def local_container
|
72
|
-
# il nome dello stack del compose usiamo come standard il nome della cartella, come lo fà già composer di default
|
73
|
-
Models::ComposeContainer.find_by_service_name(local.service_name, stack_name: @stack_cfgs.local_compose_project_name)
|
74
|
-
end
|
75
39
|
|
76
40
|
end
|
77
41
|
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module SwarmClusterCliOpe
|
2
|
+
module SyncConfigs
|
3
|
+
class PostGres < BaseDatabase
|
4
|
+
|
5
|
+
# @return [TrueClass, FalseClass]
|
6
|
+
def pull
|
7
|
+
resume('pull')
|
8
|
+
|
9
|
+
dump_cmd = dump_cmd(remote.username, remote.password, remote.database_name)
|
10
|
+
logger.info{ "DUMP COMMAND: #{dump_cmd.join(' ')}"}
|
11
|
+
if yes?("Confermare il comando?[y,yes]")
|
12
|
+
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
13
|
+
container.exec("bash -c '#{dump_cmd.join(' ')}' > #{tmp_file}")
|
14
|
+
local_container.copy_in(tmp_file, tmp_file)
|
15
|
+
|
16
|
+
# drop old db and recreate
|
17
|
+
drop_cmd = drop_cmd(local.username, local.password, local.database_name)
|
18
|
+
logger.info{ "DROP COMMAND: #{drop_cmd.join(' ')}"}
|
19
|
+
local_container.exec("bash -c '#{drop_cmd.join(' ')}'")
|
20
|
+
create_cmd = create_cmd(local.username, local.password, local.database_name)
|
21
|
+
logger.info{ "CREATE COMMAND: #{create_cmd.join(' ')}"}
|
22
|
+
local_container.exec("bash -c '#{create_cmd.join(' ')}'")
|
23
|
+
|
24
|
+
restore_cmd = restore_cmd(local.username, local.password, local.database_name, tmp_file)
|
25
|
+
logger.info{ "RESTORE COMMAND: #{restore_cmd.join(' ')}"}
|
26
|
+
local_container.exec("bash -c '#{restore_cmd.join(' ')}'")
|
27
|
+
end
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [TrueClass, FalseClass]
|
32
|
+
def push
|
33
|
+
resume('PUSH')
|
34
|
+
|
35
|
+
dump_cmd = dump_cmd(local.username, local.password, local.database_name)
|
36
|
+
say "DUMP COMMAND: #{dump_cmd.join(' ')}"
|
37
|
+
if yes?("ATTENZIONE !!!!!!PUSH!!!!! - Confermare il comando?[y,yes]")
|
38
|
+
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
39
|
+
local_container.exec("bash -c '#{dump_cmd.join(' ')}' > #{tmp_file}")
|
40
|
+
container.copy_in(tmp_file, tmp_file)
|
41
|
+
|
42
|
+
# drop old db and recreate
|
43
|
+
drop_cmd = drop_cmd(remote.username, remote.password, remote.database_name)
|
44
|
+
logger.info{ "DROP COMMAND: #{drop_cmd.join(' ')}"}
|
45
|
+
container.exec("bash -c '#{drop_cmd.join(' ')}'")
|
46
|
+
|
47
|
+
create_cmd = create_cmd(remote.username, remote.password, remote.database_name)
|
48
|
+
logger.info{ "CREATE COMMAND: #{create_cmd.join(' ')}"}
|
49
|
+
container.exec("bash -c '#{create_cmd.join(' ')}'")
|
50
|
+
|
51
|
+
restore_cmd = restore_cmd(remote.username, remote.password, remote.database_name, tmp_file)
|
52
|
+
say "RESTORE COMMAND: #{restore_cmd.join(' ')}"
|
53
|
+
container.exec("bash -c '#{restore_cmd.join(' ')}'")
|
54
|
+
end
|
55
|
+
true
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Classe interna che rappresenta le configurazioni del DB
|
60
|
+
class EnvConfigs < BaseDatabase::EnvConfigs
|
61
|
+
|
62
|
+
define_cfgs :database_name, default_env: "POSTGRES_DB", configuration_name: :database_name
|
63
|
+
define_cfgs :username, default_env: "POSTGRES_USER", configuration_name: :pg_user, default_value: 'postgres'
|
64
|
+
define_cfgs :password, default_env: "POSTGRES_PASSWORD", configuration_name: :pg_password
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def create_cmd(username, password, database_name)
|
71
|
+
create_cmd = []
|
72
|
+
create_cmd << "PGPASSWORD=\"#{password}\""
|
73
|
+
create_cmd << 'createdb'
|
74
|
+
create_cmd << "--username=#{username}"
|
75
|
+
create_cmd << database_name
|
76
|
+
end
|
77
|
+
|
78
|
+
def drop_cmd(username, password, database_name)
|
79
|
+
drop_cmd = []
|
80
|
+
drop_cmd << "PGPASSWORD=\"#{password}\""
|
81
|
+
drop_cmd << 'dropdb'
|
82
|
+
drop_cmd << "--username=#{username}"
|
83
|
+
drop_cmd << database_name
|
84
|
+
drop_cmd
|
85
|
+
end
|
86
|
+
|
87
|
+
def restore_cmd(username, password, database_name, tmp_file)
|
88
|
+
restore_cmd = []
|
89
|
+
restore_cmd << "PGPASSWORD=\"#{password}\""
|
90
|
+
restore_cmd << 'pg_restore'
|
91
|
+
restore_cmd << '--no-acl'
|
92
|
+
restore_cmd << '--no-owner'
|
93
|
+
restore_cmd << "--username=#{username}"
|
94
|
+
restore_cmd << "--dbname=#{database_name}"
|
95
|
+
restore_cmd << tmp_file
|
96
|
+
restore_cmd
|
97
|
+
end
|
98
|
+
|
99
|
+
def dump_cmd(username, password, database_name)
|
100
|
+
dump_cmd = []
|
101
|
+
dump_cmd << "PGPASSWORD=\"#{password}\""
|
102
|
+
dump_cmd << 'pg_dump'
|
103
|
+
dump_cmd << '--no-acl'
|
104
|
+
dump_cmd << '--no-owner'
|
105
|
+
dump_cmd << "--username=#{username}"
|
106
|
+
dump_cmd << '--format=custom'
|
107
|
+
dump_cmd << '--compress=9'
|
108
|
+
dump_cmd << database_name
|
109
|
+
dump_cmd
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swarm_cluster_cli_ope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marino Bonetti
|
@@ -110,9 +110,11 @@ files:
|
|
110
110
|
- lib/swarm_cluster_cli_ope/shell_command_execution.rb
|
111
111
|
- lib/swarm_cluster_cli_ope/shell_command_response.rb
|
112
112
|
- lib/swarm_cluster_cli_ope/sync_configs/base.rb
|
113
|
+
- lib/swarm_cluster_cli_ope/sync_configs/base_database.rb
|
113
114
|
- lib/swarm_cluster_cli_ope/sync_configs/copy.rb
|
114
115
|
- lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb
|
115
116
|
- lib/swarm_cluster_cli_ope/sync_configs/mysql.rb
|
117
|
+
- lib/swarm_cluster_cli_ope/sync_configs/post_gres.rb
|
116
118
|
- lib/swarm_cluster_cli_ope/sync_configs/rsync.rb
|
117
119
|
- lib/swarm_cluster_cli_ope/sync_configs/sqlite3.rb
|
118
120
|
- lib/swarm_cluster_cli_ope/version.rb
|