swarm_cluster_cli_ope 0.5.6 → 0.7.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 +7 -0
- data/Gemfile.lock +8 -8
- data/README.md +23 -6
- data/lib/swarm_cluster_cli_ope/base_configuration.rb +2 -0
- data/lib/swarm_cluster_cli_ope/kubernetes/configuration.rb +1 -1
- data/lib/swarm_cluster_cli_ope/kubernetes/pod.rb +3 -2
- data/lib/swarm_cluster_cli_ope/kubernetes/sync_configs/rsync.rb +16 -14
- data/lib/swarm_cluster_cli_ope/models/compose_container.rb +8 -1
- data/lib/swarm_cluster_cli_ope/stack_sync_concern.rb +13 -1
- data/lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb +2 -2
- data/lib/swarm_cluster_cli_ope/sync_configs/mongo_db.rb +94 -0
- data/lib/swarm_cluster_cli_ope/version.rb +1 -1
- data/swarm_cluster_cli_ope.gemspec +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d51e0df2052c16b612f01ad3601fae2437cacdc0576934f9986ad9144cb56bd
|
4
|
+
data.tar.gz: aa35f3b0392254bac2e0279a4b03be48e117f7a30647579e853ee61d6b1e7b3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff8ba67141d48e2bf24ab2940c2567b2b48d8b1d92e8459dc14436813feee68c60886510a1812aafe340e2186a1a1bf11766c505d6a97ca8ee001a6cd016a16d
|
7
|
+
data.tar.gz: c34cfd520df2b7afcacfcd69827033fe12569eedff9f482053bf9b58e97f288632a92cc573aae71c02d7e457fb4f0ab307eeba07131c8dd03cc65a0283a4ccec
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
swarm_cluster_cli_ope (0.
|
5
|
-
activesupport
|
4
|
+
swarm_cluster_cli_ope (0.7.2)
|
5
|
+
activesupport (< 7)
|
6
6
|
open4
|
7
7
|
thor (~> 1.0)
|
8
8
|
zeitwerk (~> 2.3)
|
@@ -10,22 +10,22 @@ PATH
|
|
10
10
|
GEM
|
11
11
|
remote: https://rubygems.org/
|
12
12
|
specs:
|
13
|
-
activesupport (6.1.
|
13
|
+
activesupport (6.1.6)
|
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.
|
20
|
-
i18n (1.
|
19
|
+
concurrent-ruby (1.1.10)
|
20
|
+
i18n (1.10.0)
|
21
21
|
concurrent-ruby (~> 1.0)
|
22
|
-
minitest (5.
|
22
|
+
minitest (5.15.0)
|
23
23
|
open4 (1.3.4)
|
24
24
|
rake (12.3.3)
|
25
|
-
thor (1.1
|
25
|
+
thor (1.2.1)
|
26
26
|
tzinfo (2.0.4)
|
27
27
|
concurrent-ruby (~> 1.0)
|
28
|
-
zeitwerk (2.
|
28
|
+
zeitwerk (2.6.0)
|
29
29
|
|
30
30
|
PLATFORMS
|
31
31
|
ruby
|
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
|
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,9 +231,12 @@ docker-compose -f test_folder/test_k8s/docker-compose-local.yml up -d
|
|
217
231
|
```
|
218
232
|
|
219
233
|
|
220
|
-
|
221
|
-
|
222
|
-
|
234
|
+
## Release
|
235
|
+
|
236
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
237
|
+
|
238
|
+
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
239
|
+
which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
223
240
|
|
224
241
|
|
225
242
|
## License
|
@@ -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
|
-
|
35
|
-
|
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
|
##
|
@@ -45,22 +45,24 @@ module SwarmClusterCliOpe
|
|
45
45
|
|
46
46
|
# controllo presenza comandi necessari
|
47
47
|
command_installed = false
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
54
|
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
64
|
+
else
|
65
|
+
puts "Problemi nell'installazione di rsync nel pod"
|
64
66
|
end
|
65
67
|
|
66
68
|
if command_installed
|
@@ -4,7 +4,14 @@ module SwarmClusterCliOpe
|
|
4
4
|
|
5
5
|
# @return [SwarmClusterCliOpe::Models::ComposeContainer]
|
6
6
|
def self.find_by_service_name(service_name, stack_name: '')
|
7
|
-
|
7
|
+
# prima controlliamo se siamo con il vecchio sistema di docker-compose dove i nomi dei servizi venivano
|
8
|
+
# costruiti con _ oppure se siamo nella nuova versione di docker compose dove c'è il - che fà da spaziatore
|
9
|
+
res = nil
|
10
|
+
["_", "-"].each do |separatore|
|
11
|
+
res = ShellCommandExecution.new("docker inspect #{[stack_name, service_name, "1"].compact.join(separatore)}").execute(allow_failure: true)
|
12
|
+
break unless res.failed?
|
13
|
+
end
|
14
|
+
raise "Non siamo riusciti ad identificare il servizio in locale" if res.failed?
|
8
15
|
self.new(JSON.parse(res.raw_result[:stdout]).first)
|
9
16
|
end
|
10
17
|
|
@@ -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
|
@@ -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
|
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
|
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.
|
4
|
+
version: 0.7.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:
|
11
|
+
date: 2022-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -56,16 +56,16 @@ dependencies:
|
|
56
56
|
name: activesupport
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "<"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '7'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "<"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '7'
|
69
69
|
description: |-
|
70
70
|
Gestione di varie operazioni come sincronia con le cartelle bindate dei container (rsync) up o
|
71
71
|
down e possibilità di scaricare/caricare i file direttamente all'interno del cluster, in
|
@@ -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
|