swarm_cluster_cli_ope 0.5.4 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec7a0b3dd9f3253a1e18bcb091b4e31a5de19b6304a91fe77ed4d704c9a9f109
4
- data.tar.gz: b552969314967d155c2ddba35de7f4f809b5905830c1fb2edc20a0542879005f
3
+ metadata.gz: 073c69aa2b2317c5c8148497cbe718c6a48f32e574f94fd98c686d8bf9645235
4
+ data.tar.gz: f37f6a1e5c9206262d0b38eaaff4583f4db31991e7715203424077b7a583c418
5
5
  SHA512:
6
- metadata.gz: 64f80000c7b36cd5ef521f41939fb93060b3e94520081b33522d44ecf52cba7754022881089a981a206b230287289e19735efd9b6fdcb1acdb60e26563c2f14a
7
- data.tar.gz: e7943eaad259b10bc0e3c6879aca2910d6a864e843d0235844eca2ef90c10c8ba7fa671b1fa2bbcbdec5041a0946abae4d96b6a49d311636f8a52dd5717a7a7a
6
+ metadata.gz: 84863ff0ac40ba91ab79b191558a501aed8d709b63109310791d3475909ac4571f1bb45afbb2b0a3ede1875de2d67699e672d4d9d2f6282b7d6a0acdb39cb45c
7
+ data.tar.gz: 50af6cdd12aae5210bdb98bf96face8b61f532c89adac2b1581e9b35e356e6ada191f2614f6a26ef4a5bcc6c0842ab1c2f43bae5c5cc7028f795cbbde8ce8059
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## Changelog
2
2
 
3
+ # 0.7
4
+ - riconoscimento delle immagini durante rsync per installare correttamente pacchetto,
5
+ viene riconosciuto ubuntu ed alpine
6
+
7
+ # 0.6
8
+ - sync con MongoDB
9
+
10
+ # 0.5.6
11
+ - correzione parsing variabili ambiente con '=='
12
+
13
+ # 0.5.5
14
+ - correzione utilizzo e configurazione variabili ambiente nelle configurazioni.
15
+ - controllo di non installare rsync e killall nel caso siano già presenti.
16
+
3
17
  # 0.5.4
4
18
  - bug permessi sul file password dell'rsync
5
19
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swarm_cluster_cli_ope (0.5.4)
4
+ swarm_cluster_cli_ope (0.7)
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.1)
13
+ activesupport (6.1.4.1)
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
- concurrent-ruby (1.1.8)
20
- i18n (1.8.7)
19
+ concurrent-ruby (1.1.9)
20
+ i18n (1.8.10)
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
 
@@ -217,6 +231,8 @@ docker-compose -f test_folder/test_k8s/docker-compose-local.yml up -d
217
231
  ```
218
232
 
219
233
 
234
+ ## Release
235
+
220
236
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version
221
237
  number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git
222
238
  commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
@@ -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
@@ -31,8 +31,9 @@ module SwarmClusterCliOpe
31
31
 
32
32
  # @param [String,Array<String>] cmd -> comando da passare a kubectl exec -- CMD
33
33
  # @return [SwarmClusterCliOpe::ShellCommandResponse]
34
- def exec(cmd)
35
- base_cmd(["exec", name, "--", cmd].flatten).execute
34
+ # @param [FalseClass] allow_failure -> parametro per lasciar fallire o meno il comando senza bloccarsi e poi gestirlo
35
+ def exec(cmd, allow_failure: false)
36
+ base_cmd(["exec", name, "--", cmd].flatten).execute(allow_failure: allow_failure)
36
37
  end
37
38
 
38
39
  ##
@@ -15,7 +15,6 @@ module SwarmClusterCliOpe
15
15
  @configs[:configs][:remote]
16
16
  end
17
17
 
18
-
19
18
  # @return [SwarmClusterCliOpe::ShellCommandResponse]
20
19
  def push
21
20
  execute(direction: :up)
@@ -26,7 +25,6 @@ module SwarmClusterCliOpe
26
25
  execute(direction: :down)
27
26
  end
28
27
 
29
-
30
28
  private
31
29
 
32
30
  def execute(direction: :down)
@@ -36,7 +34,6 @@ module SwarmClusterCliOpe
36
34
  exit
37
35
  end
38
36
 
39
-
40
37
  if yes? "Attenzione, i dati locali o remoti verranno sovrascritti/cancellati?[y,yes]"
41
38
 
42
39
  podname = container.name
@@ -46,10 +43,29 @@ module SwarmClusterCliOpe
46
43
  exit
47
44
  end
48
45
 
49
- cmd = container.exec(['bash -c "apt update && apt install -yq rsync psmisc"'])
50
- if cmd.failed?
51
- puts "Problemi nell'installazione di rsync nel pod"
46
+ # controllo presenza comandi necessari
47
+ command_installed = false
48
+ install_rsync = false
49
+ install_psmisc = false
50
+ if container.exec(['sh -c "command -v apt"'], allow_failure: true).success?
51
+ puts "Container Ubuntu"
52
+ install_rsync = container.exec(['sh -c "command -v rsync || apt update && apt install -yq rsync "'], allow_failure: true).success?
53
+ install_psmisc = container.exec(['sh -c "command -v killall || apt update && apt install -yq psmisc"'], allow_failure: true).success?
54
+ end
55
+
56
+ if container.exec(['sh -c "command -v apk"'], allow_failure: true).success?
57
+ puts "Container Alpine"
58
+ install_rsync = container.exec(['sh -c "command -v rsync || apk add rsync"'], allow_failure: true).success?
59
+ install_psmisc = true
60
+ end
61
+
62
+ if install_rsync and install_psmisc
63
+ command_installed = true
52
64
  else
65
+ puts "Problemi nell'installazione di rsync nel pod"
66
+ end
67
+
68
+ if command_installed
53
69
  cmd = container.cp_in(configs_path("rsyncd.conf"), "/tmp/.")
54
70
  copy_1 = cmd.execute.failed?
55
71
  cmd = container.cp_in(configs_path("rsyncd.secrets"), "/tmp/.")
@@ -66,7 +66,19 @@ module SwarmClusterCliOpe
66
66
  - pg_user: "postgres" -> valore in chiaro, in sostituzione della variabile ambiente, DEFAULT: postgres
67
67
  - database_name_env: "POSTGRES_DB" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: POSTGRES_DB
68
68
  - database_name: "nome_db" -> valore in chiaro, in sostituzione della variabile ambiente
69
-
69
+ mongo:
70
+ -- local: -> hash di configurazioni per il DB locale
71
+ - service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
72
+ - password: "" -> password per DB
73
+ - username: "" -> username per DB
74
+ - database_name: "nome_db" -> nome del DB
75
+ - exclude_from_sync: "coll1,coll2" -> elenco di collections di cui non eseguire il dump quando facciamo PUSH, DEFAULT: ''
76
+ -- remote: -> hash di configurazioni per il DB remoto
77
+ - service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
78
+ - password: "" -> password per DB
79
+ - username: "" -> username per DB
80
+ - database_name: "nome_db" -> nome del DB
81
+ - exclude_from_sync: "coll1,coll2" -> elenco di collections di cui non eseguire il dump quando facciamo PULL, DEFAULT: ''
70
82
 
71
83
  EXAMPLE:
72
84
  Esempio di sincronizzazione di un file sqlite3 e una cartella
@@ -17,6 +17,7 @@ module SwarmClusterCliOpe
17
17
  ##
18
18
  # Metodo che richiama la lambda della generazione del container al momento che ne
19
19
  # è proprio necessario
20
+ # @return [SwarmClusterCliOpe::Commands::Container,SwarmClusterCliOpe::Kubernetes::Pod]
20
21
  def container
21
22
  @container ||= @lambda_container.call
22
23
  end
@@ -26,17 +27,17 @@ module SwarmClusterCliOpe
26
27
  @configs[:service] || @sync_configs.service
27
28
  end
28
29
 
29
-
30
30
  ##
31
31
  # Costruisce i metodi che restituiscono i valori delle configurazioni
32
32
  #
33
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
34
+ # @param [String,Symbol,Array<String,Symbol>] default_env -> nome env default nel caso non sia passato
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
+ # @param [Block] from_proc -> proc da utilizzare per ricavare il valore
38
39
  # @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)
40
+ def self.define_cfgs(name, default_env: nil, configuration_name:, default_value: nil, from_proc: nil)
40
41
  configuration_name ||= name
41
42
 
42
43
  define_method(name) do
@@ -44,23 +45,42 @@ module SwarmClusterCliOpe
44
45
 
45
46
  #valore restituito direttamente dalla configurazione
46
47
  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
48
+ value = @configs["#{configuration_name}".to_sym]
49
+ end
50
+ # se non abbiamo nulla dalle configurazioni utilizziamo le variabili d'ambiente
51
+ if value.nil? and default_env
52
+
53
+ #cerchiamo nella lista delle variabili ambiente possibili, la prima che ha un valore la tengo per buona
54
+ env_vars = []
55
+ env_vars << @configs["#{configuration_name}_env".to_sym]
56
+ env_vars << default_env
57
+ env_vars.flatten!
58
+ env_vars.compact!
59
+
60
+ env_vars.each do |env_var|
61
+ value = find_env_file_variable(env_var)
62
+ break unless value.nil?
63
+ end
64
+
51
65
  end
66
+ # se non abbiamo ancora nulla e abbiamo una proc proseguiamo
67
+ if value.nil? and from_proc
68
+ value = from_proc.call(container)
69
+ end
70
+
71
+ value = value || default_value
72
+
52
73
  self.instance_variable_set("@#{name}", value)
53
74
  end
54
75
 
55
76
  end
56
77
 
57
-
58
78
  private
59
79
 
60
80
  ##
61
81
  # Estrae l'env dal container e ne tiene in memoria una copia, in modo da non fare multiple chiamate
62
82
  def env
63
- @env ||= container.exec("env").raw_result[:stdout].split("\n").collect { |v| v.split('=') }.to_h
83
+ @env ||= Hash[ container.exec("env").raw_result[:stdout].scan(/-{0,2}([^=\s]+)(?:[=\s](\S+))?/) ]
64
84
  end
65
85
 
66
86
  def find_env_file_variable(env_var)
@@ -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
@@ -50,11 +50,14 @@ module SwarmClusterCliOpe
50
50
  # Classe interna che rappresenta le configurazioni del DB
51
51
  class EnvConfigs < BaseDatabase::EnvConfigs
52
52
 
53
- define_cfgs :database_name, default_env: "POSTGRES_DB", configuration_name: :database_name
54
- define_cfgs :username, default_env: "POSTGRES_USER", configuration_name: :pg_user, default_value: 'postgres'
55
- define_cfgs :password, default_env: "POSTGRES_PASSWORD", configuration_name: :pg_password
56
-
57
- define_cfgs :database_version, default_env: "PG_MAJOR", configuration_name: :pg_version
53
+ define_cfgs :database_name, default_env: ["POSTGRES_DB", "POSTGRESQL_DATABASE"], configuration_name: :database_name
54
+ define_cfgs :username, default_env: ["POSTGRES_USER", "POSTGRESQL_USERNAME"], configuration_name: :pg_user, default_value: 'postgres'
55
+ define_cfgs :password, default_env: ["POSTGRES_PASSWORD", "POSTGRESQL_PASSWORD"], configuration_name: :pg_password
56
+
57
+ define_cfgs :database_version, default_env: "PG_MAJOR", configuration_name: :pg_version,
58
+ from_proc: ->(container) {
59
+ container.exec("postgres -V").raw_result[:stdout].strip.match(/((\d+\.)?(\d+\.)?(\*|\d+))$/)[1] rescue nil
60
+ }
58
61
 
59
62
  end
60
63
 
@@ -1,3 +1,3 @@
1
1
  module SwarmClusterCliOpe
2
- VERSION = "0.5.4"
2
+ VERSION = "0.7"
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.4
4
+ version: '0.7'
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-01-29 00:00:00.000000000 Z
11
+ date: 2021-10-06 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