swarm_cluster_cli_ope 0.2 → 0.3.2
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 +4 -4
- data/CHANGELOG.md +7 -1
- data/Gemfile.lock +6 -6
- data/README.md +80 -1
- data/lib/swarm_cluster_cli_ope/cli.rb +82 -20
- data/lib/swarm_cluster_cli_ope/commands/compose_container.rb +12 -0
- data/lib/swarm_cluster_cli_ope/commands/container.rb +7 -0
- data/lib/swarm_cluster_cli_ope/configuration.rb +18 -3
- data/lib/swarm_cluster_cli_ope/models/compose_container.rb +17 -0
- data/lib/swarm_cluster_cli_ope/models/container.rb +7 -1
- data/lib/swarm_cluster_cli_ope/models/service.rb +0 -1
- data/lib/swarm_cluster_cli_ope/sync_configs/base.rb +10 -1
- data/lib/swarm_cluster_cli_ope/sync_configs/copy.rb +3 -7
- data/lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb +77 -0
- data/lib/swarm_cluster_cli_ope/sync_configs/mysql.rb +78 -0
- data/lib/swarm_cluster_cli_ope/sync_configs/rsync.rb +0 -3
- data/lib/swarm_cluster_cli_ope/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7a3de16030f365ec9757a6572223614b5765118ef45ddbcf9a86840d1fd0b69
|
4
|
+
data.tar.gz: 2ab1028a809d3c9342be4f106617e50bfc50c09c40e86b1db20d2c3ed11f58bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d52ead2fd097d52550a792001b61d3de17ad0673cf5253f597793b1ed2d7ed5a8339318c98ca7159069af05efe7459829b55b677f9bd4823a81f415a0c64475
|
7
|
+
data.tar.gz: 057f8c54285d6e1d439c50ebdd77f6c8bdc32a839dc53412c23fc389675612a5eaa5c01bfcd4464cd0213de2ff2892f7008a397622e5d0b03fb3806a24db58f3
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
## Changelog
|
2
2
|
|
3
|
+
# 0.3
|
4
|
+
- implementazione push pull con il comando **stacksync** di mysql
|
5
|
+
|
6
|
+
# 0.2
|
7
|
+
- implementazione comando **stacksync** con configurazioni nello stack per eseguire rsync di files e dump di sqlite3
|
8
|
+
|
3
9
|
# 0.1.0
|
4
10
|
- rsync dei files da/verso cluster su cartella condivisa
|
5
11
|
- copia files direttamente in container
|
6
12
|
- shell in servizio
|
7
13
|
- mc in servizio
|
8
14
|
- configurazioni globali
|
9
|
-
- configurazioni di progetto
|
15
|
+
- configurazioni di progetto
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
swarm_cluster_cli_ope (0.2)
|
4
|
+
swarm_cluster_cli_ope (0.3.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
@@ -95,17 +95,96 @@ Utilizzare `rsync_binded_from` per scaricare e `rsync_binded_to` per caricare
|
|
95
95
|
swarm_cli_ope rsync_binded_from --stack-name=STACK_NAME --service_name NOME_SERVIZIO_SENZA_STACK --binded-container-folders CARTELLA_CONTAINER --local-folder CARTELLA_DESTINAZIONE
|
96
96
|
```
|
97
97
|
|
98
|
+
### stacksync
|
99
|
+
Si occupa di scaricare|caricare,utilizzando le configurazioni presenti, i dati dallo stack remoto
|
100
|
+
|
101
|
+
Le configurazioni sono contenute nell'array: sync_configs.
|
102
|
+
|
103
|
+
ogni configurazione è composta da:
|
104
|
+
|
105
|
+
```json
|
106
|
+
{
|
107
|
+
service:""
|
108
|
+
how:""
|
109
|
+
configs:{ }
|
110
|
+
}
|
111
|
+
```
|
112
|
+
|
113
|
+
- service è il nome del servizio
|
114
|
+
- how è il come sincronizzare, definendo la tipologia:
|
115
|
+
- pg -> DB TODO
|
116
|
+
- mysql -> DB: dump del db con mysqldump
|
117
|
+
- sqlite3 -> DB: viene eseguita una copia del file
|
118
|
+
- rsync -> RSYNC
|
119
|
+
- configs: è un hash con le configurazioni per ogni tipo di sincronizzazione
|
120
|
+
|
121
|
+
Possibili CFGS per tipologia:
|
122
|
+
|
123
|
+
- rsync:
|
124
|
+
- local: -> path cartella locale
|
125
|
+
- remote: -> path cartella remota (contesto del container)
|
126
|
+
- sqlite3:
|
127
|
+
- local: -> path al file
|
128
|
+
- remote: -> path al file remoto (contesto del container)
|
129
|
+
- mysql:
|
130
|
+
- local: -> hash di configurazioni per il DB locale
|
131
|
+
- service: "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
132
|
+
- mysql_password_env: "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
133
|
+
- mysql_user_env: "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
134
|
+
- database_name_env: "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
135
|
+
- remote: -> hash di configurazioni per il DB remoto
|
136
|
+
- 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
|
+
- mysql_user_env: "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
139
|
+
- database_name_env: "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
140
|
+
|
141
|
+
#### EXAMPLE:
|
142
|
+
Esempio di sincronizzazione di un file sqlite3 e una cartella
|
143
|
+
|
144
|
+
```json
|
145
|
+
{
|
146
|
+
"stack_name": "test1",
|
147
|
+
"sync_configs": [
|
148
|
+
{
|
149
|
+
"service": "second",
|
150
|
+
"how": "rsync",
|
151
|
+
"configs": {
|
152
|
+
"remote": "/test_bind",
|
153
|
+
"local": "./uploads"
|
154
|
+
}
|
155
|
+
},
|
156
|
+
{
|
157
|
+
"service": "test_sqlite3",
|
158
|
+
"how": "sqlite3",
|
159
|
+
"configs": {
|
160
|
+
"remote": "/cartella_sqlite3/esempio.sqlite3",
|
161
|
+
"local": "./development.sqlite3"
|
162
|
+
}
|
163
|
+
}
|
164
|
+
]
|
165
|
+
}
|
166
|
+
```
|
167
|
+
|
168
|
+
|
98
169
|
## Development
|
99
170
|
|
100
171
|
nel file di configurazione creato nella home aggiungere la chiave "dev_mode":1 per collegarsi localmente
|
101
172
|
|
102
173
|
### Abbiamo due tasks swarm di simulazione
|
103
174
|
```shell script
|
104
|
-
docker stack deploy -c test_folder/test_1/docker-compose.yml
|
175
|
+
docker stack deploy -c test_folder/test_1/docker-compose.yml test_1_stack
|
105
176
|
docker stack deploy -c test_folder/test_1/docker-compose.yml test1_staging
|
106
177
|
docker stack deploy -c test_folder/test_2/docker_compose.yml test2
|
107
178
|
```
|
108
179
|
|
180
|
+
Per simulare una sincronizzazione fra locale e remoto di un mysql, lanciamo lo stesso stack anche come compose, in modo
|
181
|
+
da trovarci sulla stessa macchina con tutte e due le situazioni
|
182
|
+
```shell script
|
183
|
+
docker-compose up -f test_folder/test_1/docker-compose-local.yml -d
|
184
|
+
```
|
185
|
+
|
186
|
+
|
187
|
+
|
109
188
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version
|
110
189
|
number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git
|
111
190
|
commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
@@ -181,7 +181,7 @@ module SwarmClusterCliOpe
|
|
181
181
|
}
|
182
182
|
})
|
183
183
|
|
184
|
-
puts "COMPLETATO" if
|
184
|
+
puts "COMPLETATO" if cmd.send(execute)
|
185
185
|
|
186
186
|
end
|
187
187
|
end
|
@@ -243,28 +243,90 @@ module SwarmClusterCliOpe
|
|
243
243
|
end
|
244
244
|
end
|
245
245
|
|
246
|
-
desc "
|
247
|
-
long_desc <<-LONGDESC
|
248
|
-
le configurazioni sono contenute nell'array: sync_configs.
|
249
|
-
ogni configurazione è composta da:
|
250
|
-
{
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
}
|
255
|
-
- service è il nome del servizio
|
256
|
-
- how è il come sincronizzare, definendo la tipologia
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
- configs: è un hash con le configurazioni per ogni tipo di sincronizzazione
|
246
|
+
desc "stacksync [DIRECTION:pull|push]", "Si occupa di scaricare|caricare,utilizzando le configurazioni presenti, i dati dallo stack remoto"
|
247
|
+
long_desc <<-LONGDESC.gsub("\n", "\x5")
|
248
|
+
le configurazioni sono contenute nell'array: sync_configs.
|
249
|
+
ogni configurazione è composta da:
|
250
|
+
{
|
251
|
+
service:""
|
252
|
+
how:""
|
253
|
+
configs:{ }
|
254
|
+
}
|
255
|
+
- service è il nome del servizio
|
256
|
+
- how è il come sincronizzare, definendo la tipologia:
|
257
|
+
---- pg -> DB TODO
|
258
|
+
---- mysql -> DB dump con mysql
|
259
|
+
---- sqlite3 -> DB: viene eseguita una copia del file
|
260
|
+
---- rsync -> RSYNC
|
261
|
+
- configs: è un hash con le configurazioni per ogni tipo di sincronizzazione
|
262
|
+
|
263
|
+
Possibili CFGS per tipologia:
|
264
|
+
rsync:
|
265
|
+
--local: -> path cartella locale
|
266
|
+
--remote: -> path cartella remota (contesto del container)
|
267
|
+
|
268
|
+
sqlite3:
|
269
|
+
--local: -> path al file
|
270
|
+
--remote: -> path al file remoto (contesto del container)
|
271
|
+
|
272
|
+
mysql:
|
273
|
+
-- local: -> hash di configurazioni per il DB locale
|
274
|
+
"service": "db" -> nome del servizio nel compose locale, DEFAULT: quello definito sopra
|
275
|
+
"mysql_password_env": "MYSQL_PASSWORD" -> variabile ambiente interna al servizio contenente PASSWORD, DEFAULT: MYSQL_PASSWORD
|
276
|
+
"mysql_user_env": "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
277
|
+
"database_name_env": "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
278
|
+
-- remote: -> hash di configurazioni per il DB remoto
|
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_user_env": "MYSQL_USER" -> variabile ambiente interna al servizio contenente USERNAME, DEFAULT: MYSQL_USER
|
282
|
+
"database_name_env": "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
|
283
|
+
|
284
|
+
|
285
|
+
EXAMPLE:
|
286
|
+
Esempio di sincronizzazione di un file sqlite3 e una cartella
|
287
|
+
{
|
288
|
+
"stack_name": "test1",
|
289
|
+
"sync_configs": [
|
290
|
+
{
|
291
|
+
"service": "second",
|
292
|
+
"how": "rsync",
|
293
|
+
"configs": {
|
294
|
+
"remote": "/test_bind",
|
295
|
+
"local": "./uploads"
|
296
|
+
}
|
297
|
+
},
|
298
|
+
{
|
299
|
+
"service": "test_sqlite3",
|
300
|
+
"how": "sqlite3",
|
301
|
+
"configs": {
|
302
|
+
"remote": "/cartella_sqlite3/esempio.sqlite3",
|
303
|
+
"local": "./development.sqlite3"
|
304
|
+
}
|
305
|
+
}
|
306
|
+
]
|
307
|
+
}
|
262
308
|
LONGDESC
|
263
309
|
|
264
|
-
def
|
310
|
+
def stacksync(direction)
|
311
|
+
direction = case direction
|
312
|
+
when 'push'
|
313
|
+
:push
|
314
|
+
when 'pull'
|
315
|
+
:pull
|
316
|
+
else
|
317
|
+
raise "ONLY [push|pull] action accepted"
|
318
|
+
end
|
265
319
|
cfgs.env(options[:environment]) do |cfgs|
|
266
|
-
cfgs.sync_configurations
|
267
|
-
|
320
|
+
sync_cfgs = cfgs.sync_configurations
|
321
|
+
if sync_cfgs.empty?
|
322
|
+
say "Attenzione, configurazioni di sincronizzazione vuoto. Leggere la documentazione"
|
323
|
+
else
|
324
|
+
sync_cfgs.each do |sync|
|
325
|
+
say "----------->>>>>>"
|
326
|
+
say "[ #{sync.class.name} ]"
|
327
|
+
sync.send(direction)
|
328
|
+
say "<<<<<<-----------"
|
329
|
+
end
|
268
330
|
end
|
269
331
|
end
|
270
332
|
end
|
@@ -9,6 +9,13 @@ module SwarmClusterCliOpe
|
|
9
9
|
end.execute
|
10
10
|
end
|
11
11
|
|
12
|
+
def exec(container_id,cmd_str)
|
13
|
+
self.base_suffix_command = []
|
14
|
+
command do |cmd|
|
15
|
+
cmd.add("exec #{container_id} #{cmd_str}")
|
16
|
+
end.execute
|
17
|
+
end
|
18
|
+
|
12
19
|
##
|
13
20
|
# Esegue il ps sui container, possibile filtrare passando nome stack e/o nome servizio
|
14
21
|
# @param [String] service_name
|
@@ -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
|
@@ -163,18 +164,33 @@ module SwarmClusterCliOpe
|
|
163
164
|
nodes.find { |c| c.id == node_id }
|
164
165
|
end
|
165
166
|
|
167
|
+
##
|
168
|
+
# Indica il nome del progetto locale compose, quella parte di nome che viene attaccata in fronte
|
169
|
+
# ad ogni nome di servizio locale, e che come default è il nome della cartella in cui risiede
|
170
|
+
# il docker-compose.yml file
|
171
|
+
# @return [String]
|
172
|
+
def local_compose_project_name
|
173
|
+
File.basename(FileUtils.pwd).downcase
|
174
|
+
end
|
175
|
+
|
166
176
|
##
|
167
177
|
# Elenco di tutte le configurazioni di sincronizzazione
|
178
|
+
# @return [Array]
|
168
179
|
def sync_configurations
|
169
|
-
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|
|
170
183
|
|
171
184
|
case c[:how]
|
172
185
|
when 'sqlite3'
|
173
186
|
SyncConfigs::Sqlite3.new(self, c)
|
174
187
|
when 'rsync'
|
175
188
|
SyncConfigs::Rsync.new(self, c)
|
189
|
+
when 'mysql'
|
190
|
+
SyncConfigs::Mysql.new(self, c)
|
176
191
|
else
|
177
192
|
logger.error { "CONFIGURAIONE NON PREVISTA: #{c[:how]}" }
|
193
|
+
nil
|
178
194
|
end
|
179
195
|
|
180
196
|
end.compact
|
@@ -230,7 +246,6 @@ module SwarmClusterCliOpe
|
|
230
246
|
project_cfgs.merge!(JSON.parse(File.read(enviroment_file)).deep_symbolize_keys)
|
231
247
|
end
|
232
248
|
|
233
|
-
logger.debug { "CONFIGS[#{@environment}]: #{project_cfgs.inspect}" }
|
234
249
|
hash[key] = self.class.read_base.merge(project_cfgs)
|
235
250
|
end
|
236
251
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module SwarmClusterCliOpe
|
2
|
+
module Models
|
3
|
+
class ComposeContainer < Container
|
4
|
+
|
5
|
+
# @return [SwarmClusterCliOpe::Models::ComposeContainer]
|
6
|
+
def self.find_by_service_name(service_name, stack_name: '')
|
7
|
+
res = ShellCommandExecution.new("docker inspect #{[stack_name, service_name, "1"].compact.join("_".strip)}").execute
|
8
|
+
self.new(JSON.parse(res.raw_result[:stdout]).first)
|
9
|
+
end
|
10
|
+
|
11
|
+
def mapped_uri_connection
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -4,7 +4,7 @@ module SwarmClusterCliOpe
|
|
4
4
|
|
5
5
|
#@return [String]
|
6
6
|
attr_accessor :name
|
7
|
-
#@return [String]
|
7
|
+
#@return [String] id del container
|
8
8
|
attr_accessor :id
|
9
9
|
#@return [String] nome dell'immagine
|
10
10
|
attr_accessor :image
|
@@ -51,6 +51,12 @@ module SwarmClusterCliOpe
|
|
51
51
|
docker_command.cp("#{id}:#{src}", dest).success?
|
52
52
|
end
|
53
53
|
|
54
|
+
##
|
55
|
+
# Esegue il comando passato
|
56
|
+
def exec(cmd)
|
57
|
+
docker_command.exec(id, cmd)
|
58
|
+
end
|
59
|
+
|
54
60
|
##
|
55
61
|
# Ritorna il connection_uri del nodo che ospita il container
|
56
62
|
# @return [String]
|
@@ -1,10 +1,13 @@
|
|
1
1
|
module SwarmClusterCliOpe
|
2
2
|
module SyncConfigs
|
3
3
|
class Base < Thor::Shell::Basic
|
4
|
-
|
4
|
+
include LoggerConcern
|
5
5
|
#@return [String] nome del servizio dello stack
|
6
6
|
attr_accessor :service
|
7
7
|
|
8
|
+
#@return [Hash] configurazioni di sincro
|
9
|
+
attr_accessor :configs
|
10
|
+
|
8
11
|
# @param [Hash] configs
|
9
12
|
# @param [Continuation] stack_cfgs
|
10
13
|
def initialize(stack_cfgs, configs)
|
@@ -30,6 +33,12 @@ module SwarmClusterCliOpe
|
|
30
33
|
raise "TO OVERRIDE"
|
31
34
|
end
|
32
35
|
|
36
|
+
private
|
37
|
+
# @return [SwarmClusterCliOpe::Models::Container]
|
38
|
+
def container
|
39
|
+
Models::Container.find_by_service_name(service, stack_name: stack_name)
|
40
|
+
end
|
41
|
+
|
33
42
|
end
|
34
43
|
end
|
35
44
|
end
|
@@ -14,20 +14,16 @@ module SwarmClusterCliOpe
|
|
14
14
|
|
15
15
|
# @return [TrueClass, FalseClass]
|
16
16
|
def push
|
17
|
-
container.copy_in(local_folder,remote_folder)
|
17
|
+
say "#{local_folder} -->> #{remote_folder}" if container.copy_in(local_folder,remote_folder)
|
18
18
|
end
|
19
19
|
|
20
20
|
# @return [TrueClass, FalseClass]
|
21
21
|
def pull
|
22
|
-
container.copy_out(remote_folder,local_folder)
|
22
|
+
say "#{remote_folder} -->> #{local_folder}" if container.copy_out(remote_folder,local_folder)
|
23
23
|
end
|
24
24
|
|
25
25
|
|
26
|
-
|
27
|
-
# @return [SwarmClusterCliOpe::Models::Container]
|
28
|
-
def container
|
29
|
-
Models::Container.find_by_service_name(service, stack_name: stack_name)
|
30
|
-
end
|
26
|
+
|
31
27
|
|
32
28
|
|
33
29
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module SwarmClusterCliOpe
|
2
|
+
module SyncConfigs
|
3
|
+
##
|
4
|
+
# Classe per la gestione dell'ambiente differente fra local e remote
|
5
|
+
# sono presenti le variabili di classe per definire una minima DSL per poter definire le variabili
|
6
|
+
# disponibili e i default da utilizzare
|
7
|
+
class EnvConfigs
|
8
|
+
# @param [Hash] configs
|
9
|
+
# @param [SwarmClusterCliOpe::SyncConfigs::Base] sync_configs
|
10
|
+
# @param [Lambda] lambda che ritorna il container Istanza container su cui la
|
11
|
+
def initialize(sync_configs, configs, container)
|
12
|
+
@configs = configs
|
13
|
+
@sync_configs = sync_configs
|
14
|
+
@lambda_container = container
|
15
|
+
end
|
16
|
+
|
17
|
+
##
|
18
|
+
# Metodo che richiama la lambda della generazione del container al momento che ne
|
19
|
+
# è proprio necessario
|
20
|
+
def container
|
21
|
+
@container ||= @lambda_container.call
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [String]
|
25
|
+
def service_name
|
26
|
+
@configs[:service] || @sync_configs.service
|
27
|
+
end
|
28
|
+
|
29
|
+
|
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
|
+
def self.define_cfgs(name, default_env:, configuration_name:)
|
39
|
+
configuration_name ||= name
|
40
|
+
|
41
|
+
define_method(name) do
|
42
|
+
return self.instance_variable_get("@#{name}") if self.instance_variable_defined?("@#{name}")
|
43
|
+
|
44
|
+
#valore restituito direttamente dalla configurazione
|
45
|
+
if @configs.key?(configuration_name)
|
46
|
+
value = @configs["#{configuration_name}".to_sym]
|
47
|
+
else
|
48
|
+
env_var = @configs["#{configuration_name}_env".to_sym] || default_env
|
49
|
+
value = find_env_file_variable(env_var)
|
50
|
+
end
|
51
|
+
self.instance_variable_set("@#{name}", value)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
##
|
60
|
+
# Estrae l'env dal container e ne tiene in memoria una copia, in modo da non fare multiple chiamate
|
61
|
+
def env
|
62
|
+
@env ||= container.exec("env").raw_result[:stdout].split("\n").collect { |v| v.split('=') }.to_h
|
63
|
+
end
|
64
|
+
|
65
|
+
def find_env_file_variable(env_var)
|
66
|
+
if env_var.match?(/_FILE$/)
|
67
|
+
# dobbiamo controllare la presenza del file e salvarci il contenuto
|
68
|
+
nome_file = env[env_var.to_s]
|
69
|
+
container.exec("cat #{nome_file}").raw_result[:stdout]
|
70
|
+
else
|
71
|
+
# env normale, dobbiamo ricavarlo dal container
|
72
|
+
env[env_var.to_s]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module SwarmClusterCliOpe
|
2
|
+
module SyncConfigs
|
3
|
+
class Mysql < Base
|
4
|
+
|
5
|
+
# @return [TrueClass, FalseClass]
|
6
|
+
def pull
|
7
|
+
resume('pull')
|
8
|
+
if yes?("Confermare il comando?[y,yes]")
|
9
|
+
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
10
|
+
container.exec("bash -c 'mysqldump -u #{remote.mysql_user} --password=#{remote.mysql_password} #{remote.database_name} | gzip -c -f' > #{tmp_file}")
|
11
|
+
local_container.copy_in(tmp_file, tmp_file)
|
12
|
+
local_container.exec("bash -c 'zcat #{tmp_file} | mysql -u #{local.mysql_user} --password=#{local.mysql_password} #{local.database_name}'")
|
13
|
+
end
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [TrueClass, FalseClass]
|
18
|
+
def push
|
19
|
+
resume('PUSH')
|
20
|
+
if yes?("ATTENZIONE !!!!!!PUSH!!!!! - Confermare il comando?[y,yes]")
|
21
|
+
tmp_file = "/tmp/#{Time.now.to_i}.sql.gz"
|
22
|
+
local_container.exec("bash -c 'mysqldump -u #{local.mysql_user} --password=#{local.mysql_password} #{local.database_name} | gzip -c -f' > #{tmp_file}")
|
23
|
+
container.copy_in(tmp_file, tmp_file)
|
24
|
+
container.exec("bash -c 'zcat #{tmp_file} | mysql -u #{remote.mysql_user} --password=#{remote.mysql_password} #{remote.database_name}'")
|
25
|
+
end
|
26
|
+
true
|
27
|
+
end
|
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
|
+
##
|
41
|
+
# Classe interna che rappresenta le configurazioni del DB
|
42
|
+
class EnvConfigs < SwarmClusterCliOpe::SyncConfigs::EnvConfigs
|
43
|
+
|
44
|
+
define_cfgs :database_name, default_env: "MYSQL_DATABASE", configuration_name: :database_name
|
45
|
+
define_cfgs :mysql_user, default_env: "MYSQL_USER", configuration_name: :mysql_user
|
46
|
+
define_cfgs :mysql_password, default_env: "MYSQL_PASSWORD", configuration_name: :mysql_password
|
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}"
|
66
|
+
|
67
|
+
end
|
68
|
+
|
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
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -28,9 +28,6 @@ module SwarmClusterCliOpe
|
|
28
28
|
private
|
29
29
|
def execute(direction: :down)
|
30
30
|
|
31
|
-
# trovo il container del servizio
|
32
|
-
container = Models::Container.find_by_service_name(service, stack_name: stack_name)
|
33
|
-
|
34
31
|
if container.nil?
|
35
32
|
say "Container non trovato con #{stack_name}@##{service}"
|
36
33
|
exit 0
|
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.3.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-
|
11
|
+
date: 2020-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- lib/swarm_cluster_cli_ope.rb
|
91
91
|
- lib/swarm_cluster_cli_ope/cli.rb
|
92
92
|
- lib/swarm_cluster_cli_ope/commands/base.rb
|
93
|
+
- lib/swarm_cluster_cli_ope/commands/compose_container.rb
|
93
94
|
- lib/swarm_cluster_cli_ope/commands/container.rb
|
94
95
|
- lib/swarm_cluster_cli_ope/commands/service.rb
|
95
96
|
- lib/swarm_cluster_cli_ope/commands/swarm.rb
|
@@ -99,6 +100,7 @@ files:
|
|
99
100
|
- lib/swarm_cluster_cli_ope/logger_concern.rb
|
100
101
|
- lib/swarm_cluster_cli_ope/manager.rb
|
101
102
|
- lib/swarm_cluster_cli_ope/models/base.rb
|
103
|
+
- lib/swarm_cluster_cli_ope/models/compose_container.rb
|
102
104
|
- lib/swarm_cluster_cli_ope/models/container.rb
|
103
105
|
- lib/swarm_cluster_cli_ope/models/mapped_volume.rb
|
104
106
|
- lib/swarm_cluster_cli_ope/models/service.rb
|
@@ -109,6 +111,8 @@ files:
|
|
109
111
|
- lib/swarm_cluster_cli_ope/shell_command_response.rb
|
110
112
|
- lib/swarm_cluster_cli_ope/sync_configs/base.rb
|
111
113
|
- lib/swarm_cluster_cli_ope/sync_configs/copy.rb
|
114
|
+
- lib/swarm_cluster_cli_ope/sync_configs/env_configs.rb
|
115
|
+
- lib/swarm_cluster_cli_ope/sync_configs/mysql.rb
|
112
116
|
- lib/swarm_cluster_cli_ope/sync_configs/rsync.rb
|
113
117
|
- lib/swarm_cluster_cli_ope/sync_configs/sqlite3.rb
|
114
118
|
- lib/swarm_cluster_cli_ope/version.rb
|