swarm_cluster_cli_ope 0.3 → 0.4.2
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 +6 -6
- data/README.md +28 -5
- data/lib/swarm_cluster_cli_ope/cli.rb +50 -16
- data/lib/swarm_cluster_cli_ope/configuration.rb +16 -3
- data/lib/swarm_cluster_cli_ope/sync_configs/base_database.rb +51 -0
- data/lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb +19 -3
- data/lib/swarm_cluster_cli_ope/sync_configs/mysql.rb +10 -44
- data/lib/swarm_cluster_cli_ope/sync_configs/post_gres.rb +159 -0
- data/lib/swarm_cluster_cli_ope/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aba2cb5ef2e4841d95dedbfcd147a2b48048ba625c6b85f5559c1fd6d59bf530
|
4
|
+
data.tar.gz: edcdb54c2423b0aef73bba9d098d0738eb89d907aae65203b8ec473e2a40464e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71eb5c600a1615c5c292f439ca8afb6ca6ff43ef93fdf6d1b249617028351c9569a1c7c1cc7b56b75eb57fa6008a4b7682ed0b6d850e42929ce3c41678dba968
|
7
|
+
data.tar.gz: 23d553a4b9c64b0a39b5efd7358751bed3ff7944884b767efce7419ccdc23b1423cf369111795d501ee76fa1b6cbd2940963186753e2405b9d14c1b7367d4de0
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
swarm_cluster_cli_ope (0.
|
4
|
+
swarm_cluster_cli_ope (0.4.2)
|
5
5
|
activesupport
|
6
6
|
open4
|
7
7
|
thor (~> 1.0)
|
@@ -10,23 +10,23 @@ PATH
|
|
10
10
|
GEM
|
11
11
|
remote: https://rubygems.org/
|
12
12
|
specs:
|
13
|
-
activesupport (6.0.3)
|
13
|
+
activesupport (6.0.3.3)
|
14
14
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
15
|
i18n (>= 0.7, < 2)
|
16
16
|
minitest (~> 5.1)
|
17
17
|
tzinfo (~> 1.1)
|
18
18
|
zeitwerk (~> 2.2, >= 2.2.2)
|
19
|
-
concurrent-ruby (1.1.
|
20
|
-
i18n (1.8.
|
19
|
+
concurrent-ruby (1.1.7)
|
20
|
+
i18n (1.8.5)
|
21
21
|
concurrent-ruby (~> 1.0)
|
22
|
-
minitest (5.14.
|
22
|
+
minitest (5.14.2)
|
23
23
|
open4 (1.3.4)
|
24
24
|
rake (12.3.3)
|
25
25
|
thor (1.0.1)
|
26
26
|
thread_safe (0.3.6)
|
27
27
|
tzinfo (1.2.7)
|
28
28
|
thread_safe (~> 0.1)
|
29
|
-
zeitwerk (2.
|
29
|
+
zeitwerk (2.4.0)
|
30
30
|
|
31
31
|
PLATFORMS
|
32
32
|
ruby
|
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
|
134
|
-
-
|
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
|
135
138
|
- remote: -> hash di configurazioni per il DB remoto
|
136
139
|
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
137
|
-
- mysql_password_env: "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
138
|
-
-
|
139
|
-
-
|
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
|
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
|
155
|
+
- remote: -> hash di configurazioni per il DB remoto
|
156
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
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
|
@@ -317,11 +345,17 @@ module SwarmClusterCliOpe
|
|
317
345
|
raise "ONLY [push|pull] action accepted"
|
318
346
|
end
|
319
347
|
cfgs.env(options[:environment]) do |cfgs|
|
320
|
-
cfgs.sync_configurations
|
321
|
-
|
322
|
-
say "
|
323
|
-
|
324
|
-
|
348
|
+
sync_cfgs = cfgs.sync_configurations
|
349
|
+
if sync_cfgs.empty?
|
350
|
+
say "Attenzione, configurazioni di sincronizzazione vuoto. Leggere la documentazione"
|
351
|
+
else
|
352
|
+
sync_cfgs.each do |sync|
|
353
|
+
say "----------->>>>>>"
|
354
|
+
say "[ #{sync.class.name} ]"
|
355
|
+
sync.send(direction)
|
356
|
+
say "COMPLETE"
|
357
|
+
say "<<<<<<-----------"
|
358
|
+
end
|
325
359
|
end
|
326
360
|
end
|
327
361
|
end
|
@@ -140,7 +140,8 @@ module SwarmClusterCliOpe
|
|
140
140
|
if stack_name
|
141
141
|
File.open(File.join(FileUtils.pwd, self.class.cfgs_project_file_name(with_env: @environment)), "wb") do |f|
|
142
142
|
f.write({
|
143
|
-
stack_name: stack_name
|
143
|
+
stack_name: stack_name,
|
144
|
+
version: VERSION
|
144
145
|
}.to_json)
|
145
146
|
end
|
146
147
|
end
|
@@ -169,13 +170,16 @@ module SwarmClusterCliOpe
|
|
169
170
|
# il docker-compose.yml file
|
170
171
|
# @return [String]
|
171
172
|
def local_compose_project_name
|
172
|
-
File.basename(FileUtils.pwd)
|
173
|
+
File.basename(FileUtils.pwd).downcase
|
173
174
|
end
|
174
175
|
|
175
176
|
##
|
176
177
|
# Elenco di tutte le configurazioni di sincronizzazione
|
178
|
+
# @return [Array]
|
177
179
|
def sync_configurations
|
178
|
-
merged_configurations[:sync_configs]
|
180
|
+
cfgs = merged_configurations[:sync_configs]
|
181
|
+
return [] if cfgs.nil? or !cfgs.is_a?(Array)
|
182
|
+
cfgs.collect do |c|
|
179
183
|
|
180
184
|
case c[:how]
|
181
185
|
when 'sqlite3'
|
@@ -184,6 +188,8 @@ module SwarmClusterCliOpe
|
|
184
188
|
SyncConfigs::Rsync.new(self, c)
|
185
189
|
when 'mysql'
|
186
190
|
SyncConfigs::Mysql.new(self, c)
|
191
|
+
when 'pg'
|
192
|
+
SyncConfigs::PostGres.new(self, c)
|
187
193
|
else
|
188
194
|
logger.error { "CONFIGURAIONE NON PREVISTA: #{c[:how]}" }
|
189
195
|
nil
|
@@ -245,6 +251,13 @@ module SwarmClusterCliOpe
|
|
245
251
|
hash[key] = self.class.read_base.merge(project_cfgs)
|
246
252
|
end
|
247
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
|
+
|
248
261
|
@_merged_configurations[@environment]
|
249
262
|
|
250
263
|
end
|
@@ -0,0 +1,51 @@
|
|
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.dig(:configs, :remote) || {}, -> { container })
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [SwarmClusterCliOpe::SyncConfigs::EnvConfigs]
|
11
|
+
def local
|
12
|
+
self.class::EnvConfigs.new(self, @configs.dig(: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
|
+
version: #{local.database_version}
|
32
|
+
remote:
|
33
|
+
service_name: #{remote.service_name}
|
34
|
+
database_name: #{remote.database_name}
|
35
|
+
username: #{remote.username}
|
36
|
+
password: #{remote.password}
|
37
|
+
version: #{remote.database_version}"
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def local_container
|
44
|
+
# il nome dello stack del compose usiamo come standard il nome della cartella, come lo fà già composer di default
|
45
|
+
Models::ComposeContainer.find_by_service_name(local.service_name, stack_name: @stack_cfgs.local_compose_project_name)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -27,13 +27,29 @@ module SwarmClusterCliOpe
|
|
27
27
|
end
|
28
28
|
|
29
29
|
|
30
|
-
|
30
|
+
##
|
31
|
+
# Costruisce i metodi che restituiscono i valori delle configurazioni
|
32
|
+
#
|
33
|
+
# @param [String,Symbol] name -> nome della stringa con cui viene generato il metodo
|
34
|
+
# @param [String,Symbol] default_env -> nome env default nel caso non sia passato
|
35
|
+
# @param [String,Symbol] configuration_name -> nome della configurazione da utilizzare per estrapolare la configurazione
|
36
|
+
# in automatico viene tenuto conto se cercare per la versione
|
37
|
+
# con _env o senza....precedenza SENZA
|
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)
|
31
40
|
configuration_name ||= name
|
32
41
|
|
33
42
|
define_method(name) do
|
34
43
|
return self.instance_variable_get("@#{name}") if self.instance_variable_defined?("@#{name}")
|
35
|
-
|
36
|
-
|
44
|
+
|
45
|
+
#valore restituito direttamente dalla configurazione
|
46
|
+
if @configs.key?(configuration_name)
|
47
|
+
value = @configs["#{configuration_name}".to_sym] || default_value
|
48
|
+
else
|
49
|
+
env_var = @configs["#{configuration_name}_env".to_sym] || default_env
|
50
|
+
value = find_env_file_variable(env_var) || default_value
|
51
|
+
end
|
52
|
+
self.instance_variable_set("@#{name}", value)
|
37
53
|
end
|
38
54
|
|
39
55
|
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,25 @@ 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
|
-
define_cfgs :database_name,
|
45
|
-
define_cfgs :
|
46
|
-
define_cfgs :
|
33
|
+
define_cfgs :database_name, default_env: "MYSQL_DATABASE", configuration_name: :database_name
|
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'
|
47
36
|
|
48
|
-
|
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}"
|
37
|
+
define_cfgs :database_version, default_env: "MYSQL_MAJOR", configuration_name: :mysql_version
|
66
38
|
|
67
39
|
end
|
68
40
|
|
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
41
|
|
76
42
|
end
|
77
43
|
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
module SwarmClusterCliOpe
|
2
|
+
module SyncConfigs
|
3
|
+
class PostGres < BaseDatabase
|
4
|
+
|
5
|
+
|
6
|
+
def pull
|
7
|
+
resume('pull')
|
8
|
+
|
9
|
+
if yes?("Confermare il comando?[y,yes]")
|
10
|
+
|
11
|
+
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
12
|
+
dump_cmd(remote, tmp_file)
|
13
|
+
local.container.copy_in(tmp_file, tmp_file)
|
14
|
+
|
15
|
+
# drop old db and recreate
|
16
|
+
if Gem::Version.new(local.database_version) <= Gem::Version.new("12")
|
17
|
+
close_connections_and_drop_cmd(local)
|
18
|
+
else
|
19
|
+
raise "DA ANALIZZARE QUANDO LA 13 disponibile....dropdb ha un force come parametro"
|
20
|
+
end
|
21
|
+
|
22
|
+
create_cmd(local)
|
23
|
+
|
24
|
+
restore_cmd(local, tmp_file)
|
25
|
+
|
26
|
+
end
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [TrueClass, FalseClass]
|
31
|
+
def push
|
32
|
+
resume('PUSH')
|
33
|
+
|
34
|
+
if yes?("ATTENZIONE !!!!!!PUSH!!!!! - Confermare il comando?[y,yes]")
|
35
|
+
|
36
|
+
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
37
|
+
dump_cmd(local, tmp_file)
|
38
|
+
remote.container.copy_in(tmp_file, tmp_file)
|
39
|
+
|
40
|
+
# drop old db and recreate
|
41
|
+
if Gem::Version.new(local.database_version) <= Gem::Version.new("12")
|
42
|
+
close_connections_and_drop_cmd(remote)
|
43
|
+
else
|
44
|
+
raise "DA ANALIZZARE QUANDO LA 13 disponibile....dropdb ha un force come parametro"
|
45
|
+
end
|
46
|
+
create_cmd(remote)
|
47
|
+
|
48
|
+
restore_cmd(remote, tmp_file)
|
49
|
+
|
50
|
+
end
|
51
|
+
true
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Classe interna che rappresenta le configurazioni del DB
|
56
|
+
class EnvConfigs < BaseDatabase::EnvConfigs
|
57
|
+
|
58
|
+
define_cfgs :database_name, default_env: "POSTGRES_DB", configuration_name: :database_name
|
59
|
+
define_cfgs :username, default_env: "POSTGRES_USER", configuration_name: :pg_user, default_value: 'postgres'
|
60
|
+
define_cfgs :password, default_env: "POSTGRES_PASSWORD", configuration_name: :pg_password
|
61
|
+
|
62
|
+
define_cfgs :database_version, default_env: "PG_MAJOR", configuration_name: :pg_version
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
# @param [EnvConfigs] config
|
69
|
+
def create_cmd(config)
|
70
|
+
create_cmd = []
|
71
|
+
create_cmd << "PGPASSWORD=\"#{config.password}\""
|
72
|
+
create_cmd << 'createdb'
|
73
|
+
create_cmd << "--username=#{config.username}"
|
74
|
+
create_cmd << config.database_name
|
75
|
+
|
76
|
+
logger.info { "CREATE COMMAND: #{create_cmd.join(' ')}" }
|
77
|
+
config.container.exec("bash -c '#{create_cmd.join(' ')} || true'")
|
78
|
+
end
|
79
|
+
|
80
|
+
# @param [EnvConfigs] config
|
81
|
+
# def drop_cmd(config)
|
82
|
+
# drop_cmd = []
|
83
|
+
# drop_cmd << "PGPASSWORD=\"#{config.password}\""
|
84
|
+
# drop_cmd << 'dropdb'
|
85
|
+
# drop_cmd << '--if-exists'
|
86
|
+
# drop_cmd << "--username=#{config.username}"
|
87
|
+
# drop_cmd << config.database_name
|
88
|
+
# drop_cmd
|
89
|
+
#
|
90
|
+
# logger.info { "DROP COMMAND: #{drop_cmd.join(' ')}" }
|
91
|
+
# config.container.exec("bash -c '#{drop_cmd.join(' ')}'")
|
92
|
+
# end
|
93
|
+
|
94
|
+
# @param [EnvConfigs] config
|
95
|
+
def restore_cmd(config, tmp_file)
|
96
|
+
restore_cmd = []
|
97
|
+
restore_cmd << "PGPASSWORD=\"#{config.password}\""
|
98
|
+
restore_cmd << 'pg_restore'
|
99
|
+
restore_cmd << '--no-acl'
|
100
|
+
restore_cmd << '--no-owner'
|
101
|
+
restore_cmd << "--username=#{config.username}"
|
102
|
+
restore_cmd << "--dbname=#{config.database_name}"
|
103
|
+
restore_cmd << tmp_file
|
104
|
+
restore_cmd
|
105
|
+
|
106
|
+
logger.info { "RESTORE COMMAND: #{restore_cmd.join(' ')}" }
|
107
|
+
config.container.exec("bash -c '#{restore_cmd.join(' ')}'")
|
108
|
+
end
|
109
|
+
|
110
|
+
# @param [EnvConfigs] config
|
111
|
+
def dump_cmd(config, file)
|
112
|
+
dump_cmd = []
|
113
|
+
dump_cmd << "PGPASSWORD=\"#{config.password}\""
|
114
|
+
dump_cmd << 'pg_dump'
|
115
|
+
dump_cmd << '--no-acl'
|
116
|
+
dump_cmd << '--no-owner'
|
117
|
+
dump_cmd << "--username=#{config.username}"
|
118
|
+
dump_cmd << '--format=custom'
|
119
|
+
dump_cmd << '--compress=9'
|
120
|
+
dump_cmd << config.database_name
|
121
|
+
dump_cmd
|
122
|
+
|
123
|
+
logger.info { "DUMP COMMAND: #{dump_cmd.join(' ')}" }
|
124
|
+
config.container.exec("bash -c '#{dump_cmd.join(' ')}' > #{file}")
|
125
|
+
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
# @param [EnvConfigs] config
|
130
|
+
def close_connections_and_drop_cmd(config)
|
131
|
+
cmd = []
|
132
|
+
cmd << "PGPASSWORD=\"#{config.password}\""
|
133
|
+
|
134
|
+
sql = []
|
135
|
+
sql << "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '\"'\"'#{config.database_name}'\"'\"' AND pid <> pg_backend_pid();;"
|
136
|
+
sql << "DROP DATABASE IF EXISTS #{config.database_name};"
|
137
|
+
|
138
|
+
cmd << "echo \"#{sql.join(" ")}\" "
|
139
|
+
cmd << '|'
|
140
|
+
cmd << 'psql'
|
141
|
+
cmd << "-U #{config.username}"
|
142
|
+
cmd << "postgres"
|
143
|
+
cmd
|
144
|
+
|
145
|
+
logger.info { "CLOSE CONNECTIONS COMMAND: #{cmd.join(' ')}" }
|
146
|
+
config.container.exec("bash -c '#{cmd.join(' ')}'")
|
147
|
+
end
|
148
|
+
|
149
|
+
# quello che fa capistrano quando copia in locale - utenze inventate
|
150
|
+
# gzip -d cortobio_production_new_2020-09-10-171742.sql.gz &&
|
151
|
+
# PGPASSWORD='root' psql -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'development' AND pid <> pg_backend_pid();;" -U root -h 0.0.0.0 -p 32790 development;
|
152
|
+
# PGPASSWORD='root' dropdb -U root -h 0.0.0.0 -p 32790 development;
|
153
|
+
# PGPASSWORD='root' createdb -U root -h 0.0.0.0 -p 32790 development;
|
154
|
+
# PGPASSWORD='root' psql -U root -h 0.0.0.0 -p 32790 -d development < ./cortobio_production_new_2020-09-10-171742.sql
|
155
|
+
|
156
|
+
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: swarm_cluster_cli_ope
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marino Bonetti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -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
|