swarm_cluster_cli_ope 0.2.2 → 0.3

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: e7a0c1998bd04fc1f315b316b47d094352f24d07ca19cd0da18254a59bd4cd68
4
- data.tar.gz: ef6f7962bf661d9839ace5820485896db7c4066194632d865c54e5b667c05957
3
+ metadata.gz: 064a57b8e710fbf99c41b7bc29f1db95fdef7d8e3d39e817159c060b044cf06e
4
+ data.tar.gz: 7418cd71dd7e1fced025983fe6e3d08b141b1e0440216d36685036533c09cd52
5
5
  SHA512:
6
- metadata.gz: 514cca4b32bf7fa10adc40a0579a5db538a0601733ea78a0bb9514ff6b6cf006cf7bd2d6d744b508048d9a351fd79c2a5fd290b18a28c7b2fc376d19f9eed47c
7
- data.tar.gz: f174ac4bff4447acb66218ca7d20319caf47b8cd7e67d6a52a0974cfa940eaa5888f6a649e2ccf4b4b7283840c75c1b3ebcd03d17ebe4ef0b082119087782668
6
+ metadata.gz: d97192caba2f7aa3c4572e54e8aa4800f7d7aaff178e0fbc51043386209dc617ac802b9cbcdfd67e16393948d60081684f153f367476e9ccffef426723356765
7
+ data.tar.gz: e76e048acc106143d9f817ccc79a25fdacd6bfa7dc521a187013f2ef952a486d6e318b30d483611b545bfbef743d50facabc9eed8004f6ae9beed80dcd6449b0
@@ -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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swarm_cluster_cli_ope (0.2.2)
4
+ swarm_cluster_cli_ope (0.3)
5
5
  activesupport
6
6
  open4
7
7
  thor (~> 1.0)
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: "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: "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 test1
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).
@@ -255,7 +255,7 @@ module SwarmClusterCliOpe
255
255
  - service è il nome del servizio
256
256
  - how è il come sincronizzare, definendo la tipologia:
257
257
  ---- pg -> DB TODO
258
- ---- mysql -> DB TODO
258
+ ---- mysql -> DB dump con mysql
259
259
  ---- sqlite3 -> DB: viene eseguita una copia del file
260
260
  ---- rsync -> RSYNC
261
261
  - configs: è un hash con le configurazioni per ogni tipo di sincronizzazione
@@ -269,6 +269,19 @@ module SwarmClusterCliOpe
269
269
  --local: -> path al file
270
270
  --remote: -> path al file remoto (contesto del container)
271
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": "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": "MYSQL_DATABASE" -> variabile ambiente interna al servizio contenente NOME DB, DEFAULT: MYSQL_DATABASE
283
+
284
+
272
285
  EXAMPLE:
273
286
  Esempio di sincronizzazione di un file sqlite3 e una cartella
274
287
  {
@@ -0,0 +1,12 @@
1
+ module SwarmClusterCliOpe
2
+ module Commands
3
+ class ComposeContainer < Container
4
+
5
+ # Siamo in locale
6
+ def docker_host
7
+ ""
8
+ end
9
+
10
+ end
11
+ end
12
+ 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
@@ -163,6 +163,15 @@ module SwarmClusterCliOpe
163
163
  nodes.find { |c| c.id == node_id }
164
164
  end
165
165
 
166
+ ##
167
+ # Indica il nome del progetto locale compose, quella parte di nome che viene attaccata in fronte
168
+ # ad ogni nome di servizio locale, e che come default è il nome della cartella in cui risiede
169
+ # il docker-compose.yml file
170
+ # @return [String]
171
+ def local_compose_project_name
172
+ File.basename(FileUtils.pwd)
173
+ end
174
+
166
175
  ##
167
176
  # Elenco di tutte le configurazioni di sincronizzazione
168
177
  def sync_configurations
@@ -173,8 +182,11 @@ module SwarmClusterCliOpe
173
182
  SyncConfigs::Sqlite3.new(self, c)
174
183
  when 'rsync'
175
184
  SyncConfigs::Rsync.new(self, c)
185
+ when 'mysql'
186
+ SyncConfigs::Mysql.new(self, c)
176
187
  else
177
188
  logger.error { "CONFIGURAIONE NON PREVISTA: #{c[:how]}" }
189
+ nil
178
190
  end
179
191
 
180
192
  end.compact
@@ -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]
@@ -19,7 +19,6 @@ module SwarmClusterCliOpe
19
19
 
20
20
  ##
21
21
  # Containers del servizio
22
-
23
22
  # @return [Array<SwarmClusterCliOpe::Container>]
24
23
  def containers
25
24
  tasks.collect { |t| t.container }
@@ -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
@@ -23,11 +23,7 @@ module SwarmClusterCliOpe
23
23
  end
24
24
 
25
25
 
26
- private
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,62 @@
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
+ def self.define_cfgs(name, default: nil, configuration_name: nil)
31
+ configuration_name ||= name
32
+
33
+ define_method(name) do
34
+ return self.instance_variable_get("@#{name}") if self.instance_variable_defined?("@#{name}")
35
+ env_var = @configs[configuration_name.to_sym] || default
36
+ self.instance_variable_set("@#{name}", find_env_file_variable(env_var))
37
+ end
38
+
39
+ end
40
+
41
+
42
+ private
43
+
44
+ ##
45
+ # Estrae l'env dal container e ne tiene in memoria una copia, in modo da non fare multiple chiamate
46
+ def env
47
+ @env ||= container.exec("env").raw_result[:stdout].split("\n").collect { |v| v.split('=') }.to_h
48
+ end
49
+
50
+ def find_env_file_variable(env_var)
51
+ if env_var.match?(/_FILE$/)
52
+ # dobbiamo controllare la presenza del file e salvarci il contenuto
53
+ nome_file = env[env_var.to_s]
54
+ container.exec("cat #{nome_file}").raw_result[:stdout]
55
+ else
56
+ # env normale, dobbiamo ricavarlo dal container
57
+ env[env_var.to_s]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ 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: "MYSQL_DATABASE"
45
+ define_cfgs :mysql_user, default: "MYSQL_USER", configuration_name: :mysql_user_env
46
+ define_cfgs :mysql_password, default: "MYSQL_PASSWORD", configuration_name: :mysql_password_env
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
@@ -1,3 +1,3 @@
1
1
  module SwarmClusterCliOpe
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3"
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.2.2
4
+ version: '0.3'
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-07-03 00:00:00.000000000 Z
11
+ date: 2020-09-09 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