swarm_cluster_cli_ope 0.5.6 → 0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93771440f5d2a285cd10d390fe8983d8efcce1a5e3ef323d15de38ade748cfba
4
- data.tar.gz: 61d5654dcf67657bab0313da131292c930a5008dcb60359eef3eb0c9f334ffc1
3
+ metadata.gz: 956724efc48101ced01f9b90733cb45f4e7278a7e8da2d54edf7e85a1b103cab
4
+ data.tar.gz: e3d20867dcd4bc2cafa42b9997bfdbbf4cfeea4cbeb50881bdbebf615320056a
5
5
  SHA512:
6
- metadata.gz: 5b6d4dd4be603b2ce5648bfcd345d3a2f0f415b370ec0834816272480faade213c18fdc17672cbe6c806c438ff2846e40458683d7fc5e48306189a230ca81d99
7
- data.tar.gz: 38ae9e73b07af96a439a2f624d0851f0de4686447694a41eb0dd86d63126926b626e9c91b64699cec2df331eee5361098a08e894969401183edd23bbb28ccba0
6
+ metadata.gz: 898033f68020b639df391e38728e8ac63fc284b29dbb7ead531578f10fc11aaa102d2109ebe4d43db9254a1ca2df63149ff8d50341bf7bf509a2d1e0dea5f611
7
+ data.tar.gz: 85f316b0cef0d08ce7f768d7d35a67b023b728a86315433cb4bcaaf45fdaf1c71e46d609f7255a593a2aea61860d037f84bf259a5e39d417f8b64a9850c610b0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  ## Changelog
2
2
 
3
+ # 0.6
4
+ - sync con MongoDB
5
+
3
6
  # 0.5.6
4
7
  - correzione parsing variabili ambiente con '=='
5
8
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swarm_cluster_cli_ope (0.5.6)
4
+ swarm_cluster_cli_ope (0.6)
5
5
  activesupport
6
6
  open4
7
7
  thor (~> 1.0)
@@ -10,16 +10,16 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- activesupport (6.1.2)
13
+ activesupport (6.1.3)
14
14
  concurrent-ruby (~> 1.0, >= 1.0.2)
15
15
  i18n (>= 1.6, < 2)
16
16
  minitest (>= 5.1)
17
17
  tzinfo (~> 2.0)
18
18
  zeitwerk (~> 2.3)
19
19
  concurrent-ruby (1.1.8)
20
- i18n (1.8.8)
20
+ i18n (1.8.9)
21
21
  concurrent-ruby (~> 1.0)
22
- minitest (5.14.3)
22
+ minitest (5.14.4)
23
23
  open4 (1.3.4)
24
24
  rake (12.3.3)
25
25
  thor (1.1.0)
data/README.md CHANGED
@@ -112,10 +112,11 @@ ogni configurazione è composta da:
112
112
 
113
113
  - service è il nome del servizio (o nel caso di k8s una stringa da utilizzare come selettore labels)
114
114
  - how è il come sincronizzare, definendo la tipologia:
115
- - pg -> DB TODO
115
+ - pg -> DB: dump del DB con pgdump
116
116
  - mysql -> DB: dump del db con mysqldump
117
117
  - sqlite3 -> DB: viene eseguita una copia del file
118
118
  - rsync -> RSYNC
119
+ - mongodb -> mongodump e mongorestore
119
120
  - configs: è un hash con le configurazioni per ogni tipo di sincronizzazione
120
121
 
121
122
  Possibili CFGS per tipologia:
@@ -159,8 +160,21 @@ Possibili CFGS per tipologia:
159
160
  - pg_user_env: "POSTGRES_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: POSTGRES_USER
160
161
  - pg_user: "postgres" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: postgres
161
162
  - 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
163
-
163
+ - database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
164
+ - mongo:
165
+ - local: -> hash di configurazioni per il DB locale
166
+ - service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
167
+ - password: "" -> password per DB
168
+ - username: "" -> username per DB
169
+ - database_name: "nome_db" -> nome del DB
170
+ - exclude_from_sync: "coll1,coll2" -> elenco di collections di cui non eseguire il dump quando facciamo PUSH, DEFAULT: ''
171
+ - remote: -> hash di configurazioni per il DB remoto
172
+ - service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
173
+ - password: "" -> password per DB
174
+ - username: "" -> username per DB
175
+ - database_name: "nome_db" -> nome del DB
176
+ - exclude_from_sync: "coll1,coll2" -> elenco di collections di cui non eseguire il dump quando facciamo PULL, DEFAULT: ''
177
+
164
178
  #### EXAMPLE:
165
179
  Esempio di sincronizzazione di un file sqlite3 e una cartella
166
180
 
@@ -102,6 +102,8 @@ module SwarmClusterCliOpe
102
102
  SyncConfigs::Mysql
103
103
  when 'pg'
104
104
  SyncConfigs::PostGres
105
+ when 'mongodb'
106
+ SyncConfigs::MongoDb
105
107
  else
106
108
  logger.error { "CONFIGURAIONE NON PREVISTA: #{name}" }
107
109
  nil
@@ -53,7 +53,7 @@ module SwarmClusterCliOpe
53
53
  when 'pg'
54
54
  SyncConfigs::PostGres
55
55
  else
56
- logger.error { "CONFIGURAIONE NON PREVISTA: #{name}" }
56
+ logger.error { "CONFIGURAIONE NON PREVISTA in K8S: #{name}" }
57
57
  nil
58
58
  end
59
59
  end
@@ -37,7 +37,7 @@ module SwarmClusterCliOpe
37
37
  # con _env o senza....precedenza SENZA
38
38
  # @param [Block] from_proc -> proc da utilizzare per ricavare il valore
39
39
  # @param [nil,String] default_value se non è estrapolato nessun valore, viene utilizzato il valore di default
40
- def self.define_cfgs(name, default_env:, configuration_name:, default_value: nil, from_proc: nil)
40
+ def self.define_cfgs(name, default_env: nil, configuration_name:, default_value: nil, from_proc: nil)
41
41
  configuration_name ||= name
42
42
 
43
43
  define_method(name) do
@@ -48,7 +48,7 @@ module SwarmClusterCliOpe
48
48
  value = @configs["#{configuration_name}".to_sym]
49
49
  end
50
50
  # se non abbiamo nulla dalle configurazioni utilizziamo le variabili d'ambiente
51
- if value.nil?
51
+ if value.nil? and default_env
52
52
 
53
53
  #cerchiamo nella lista delle variabili ambiente possibili, la prima che ha un valore la tengo per buona
54
54
  env_vars = []
@@ -0,0 +1,94 @@
1
+ module SwarmClusterCliOpe
2
+ module SyncConfigs
3
+ class MongoDb < BaseDatabase
4
+
5
+ # @return [TrueClass, FalseClass]
6
+ def pull
7
+ resume('pull')
8
+ if yes?("Confermare il comando?[y,yes]")
9
+
10
+ tmp_file = make_dump(remote, container)
11
+
12
+ local_container.copy_in(tmp_file, tmp_file)
13
+
14
+ restore(tmp_file, remote, local, local_container)
15
+ end
16
+ true
17
+ end
18
+
19
+ # @return [TrueClass, FalseClass]
20
+ def push
21
+ resume('PUSH')
22
+ if yes?("ATTENZIONE !!!!!!PUSH!!!!! - Confermare il comando?[y,yes]")
23
+ tmp_file = make_dump(local, local_container)
24
+ container.copy_in(tmp_file, tmp_file)
25
+ restore(tmp_file, local, remote, container)
26
+ end
27
+ true
28
+ end
29
+
30
+ ##
31
+ # Classe interna che rappresenta le configurazioni del DB
32
+ class EnvConfigs < BaseDatabase::EnvConfigs
33
+
34
+ define_cfgs :database_name, configuration_name: :database_name
35
+ define_cfgs :username, configuration_name: :username
36
+ define_cfgs :password, configuration_name: :password
37
+
38
+ define_cfgs :database_version, default_env: "MONGO_VERSION", configuration_name: :version
39
+
40
+ ##
41
+ # Possiamo definire una lista, comma-separated, per limitare le collections da non importare
42
+ define_cfgs :exclude_from_sync, default_env: "EXCLUDE_FROM_SYNC", configuration_name: :exclude_from_sync, default_value: ""
43
+
44
+ ##
45
+ # Helper per avere un array di collections da non sincronizzare, specifico per mongodb
46
+ # @return [Array<String>]
47
+ def excluded_collections
48
+ return [] if exclude_from_sync.nil?
49
+ exclude_from_sync.split(",").compact
50
+ end
51
+
52
+ end
53
+
54
+ private
55
+
56
+ # @param [String] tmp_file
57
+ # @param [EnvConfigs] from_env environment sorgente (per rinominare anche il nome del DB)
58
+ # @param [EnvConfigs] to_env environment di arrivo (per rinominare anche il nome del DB)
59
+ # @param [SwarmClusterCliOpe::Models::Container] cnt in cui eseguire l'import
60
+ def restore(tmp_file, from_env, to_env, cnt)
61
+ command = []
62
+ command << "bash -c '"
63
+ command << "mongorestore"
64
+ command << "--nsFrom '#{from_env.database_name}.*'"
65
+ command << "--nsTo '#{to_env.database_name}.*'"
66
+ command << "--drop --archive=#{tmp_file} --gzip"
67
+ command << "'"
68
+
69
+ cnt.exec(command.join " ")
70
+ end
71
+
72
+ # @param [EnvConfigs] environment
73
+ # @param [SwarmClusterCliOpe::Models::Container] cnt
74
+ def make_dump(environment, cnt)
75
+ tmp_file = "/tmp/dump.#{Time.now.to_i}.archive"
76
+ command = []
77
+ command << "bash -c '"
78
+
79
+ command << "mongodump --db #{environment.database_name} "
80
+ environment.excluded_collections.each do |collection|
81
+ command << " --excludeCollection \"#{collection}\" "
82
+ end
83
+ command << "--username \"#{environment.username}\" " if environment.username
84
+ command << "--password \"#{environment.password}\" " if environment.password
85
+ command << "--archive --gzip"
86
+
87
+ command << "' > #{tmp_file}"
88
+ cnt.exec(command.join " ")
89
+ tmp_file
90
+ end
91
+
92
+ end
93
+ end
94
+ end
@@ -1,3 +1,3 @@
1
1
  module SwarmClusterCliOpe
2
- VERSION = "0.5.6"
2
+ VERSION = "0.6"
3
3
  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: 0.5.6
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marino Bonetti
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-10 00:00:00.000000000 Z
11
+ date: 2021-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -126,6 +126,7 @@ files:
126
126
  - lib/swarm_cluster_cli_ope/sync_configs/base_database.rb
127
127
  - lib/swarm_cluster_cli_ope/sync_configs/copy.rb
128
128
  - lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb
129
+ - lib/swarm_cluster_cli_ope/sync_configs/mongo_db.rb
129
130
  - lib/swarm_cluster_cli_ope/sync_configs/mysql.rb
130
131
  - lib/swarm_cluster_cli_ope/sync_configs/post_gres.rb
131
132
  - lib/swarm_cluster_cli_ope/sync_configs/rsync.rb