swarm_cluster_cli_ope 0.5.1 → 0.5.6

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: deeeb31f5b17f54bd476bb6cc8100c91dbba91386188ee468e14b1d50c36b99a
4
- data.tar.gz: d6cd6932874bd1d393b4c975e1978f64db2870f3705bb3e243f4792114c60a42
3
+ metadata.gz: 93771440f5d2a285cd10d390fe8983d8efcce1a5e3ef323d15de38ade748cfba
4
+ data.tar.gz: 61d5654dcf67657bab0313da131292c930a5008dcb60359eef3eb0c9f334ffc1
5
5
  SHA512:
6
- metadata.gz: 694ec83f00a6c979fbb1f525d02194d3bdb58cd6a91239a511774d1d3ef9a3a38cd9068edcd3757a2318cc3f863b6c362692519500cd4d4436f431fa3a091320
7
- data.tar.gz: 3bcb05e5402cf1750908310b29fa930424da6afb74f2157978989e0a7872f94b6090122e0d7bd1568b8ef408bf75d1dc0cc1ac12abadda1416b1bc372985346e
6
+ metadata.gz: 5b6d4dd4be603b2ce5648bfcd345d3a2f0f415b370ec0834816272480faade213c18fdc17672cbe6c806c438ff2846e40458683d7fc5e48306189a230ca81d99
7
+ data.tar.gz: 38ae9e73b07af96a439a2f624d0851f0de4686447694a41eb0dd86d63126926b626e9c91b64699cec2df331eee5361098a08e894969401183edd23bbb28ccba0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  ## Changelog
2
2
 
3
+ # 0.5.6
4
+ - correzione parsing variabili ambiente con '=='
5
+
6
+ # 0.5.5
7
+ - correzione utilizzo e configurazione variabili ambiente nelle configurazioni.
8
+ - controllo di non installare rsync e killall nel caso siano già presenti.
9
+
10
+ # 0.5.4
11
+ - bug permessi sul file password dell'rsync
12
+
13
+ # 0.5.3
14
+ - bug selezione pod, ora filtra solamente per i containers che sono attivi
15
+
3
16
  # 0.4
4
17
  - implementazione push pull con il comando **stacksync** di pg
5
18
  - controllo di versione sul file caricato rispeto a gemma, con conseguente warning
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swarm_cluster_cli_ope (0.5.1)
4
+ swarm_cluster_cli_ope (0.5.6)
5
5
  activesupport
6
6
  open4
7
7
  thor (~> 1.0)
@@ -10,23 +10,22 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- activesupport (6.0.3.4)
13
+ activesupport (6.1.2)
14
14
  concurrent-ruby (~> 1.0, >= 1.0.2)
15
- i18n (>= 0.7, < 2)
16
- minitest (~> 5.1)
17
- tzinfo (~> 1.1)
18
- zeitwerk (~> 2.2, >= 2.2.2)
19
- concurrent-ruby (1.1.7)
20
- i18n (1.8.5)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ zeitwerk (~> 2.3)
19
+ concurrent-ruby (1.1.8)
20
+ i18n (1.8.8)
21
21
  concurrent-ruby (~> 1.0)
22
- minitest (5.14.2)
22
+ minitest (5.14.3)
23
23
  open4 (1.3.4)
24
24
  rake (12.3.3)
25
- thor (1.0.1)
26
- thread_safe (0.3.6)
27
- tzinfo (1.2.8)
28
- thread_safe (~> 0.1)
29
- zeitwerk (2.4.1)
25
+ thor (1.1.0)
26
+ tzinfo (2.0.4)
27
+ concurrent-ruby (~> 1.0)
28
+ zeitwerk (2.4.2)
30
29
 
31
30
  PLATFORMS
32
31
  ruby
@@ -90,6 +90,7 @@ module SwarmClusterCliOpe
90
90
  base_cmd << "--context=#{context}" unless context.blank?
91
91
  base_cmd << "get pod"
92
92
  base_cmd << "--selector=#{selector}"
93
+ base_cmd << "--field-selector=status.phase=Running" #solo pod che stanno girando teniamo sotto controllo
93
94
  base_cmd << "--output=json"
94
95
 
95
96
  cmd = ShellCommandExecution.new(base_cmd)
@@ -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,13 +43,30 @@ 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"
52
- else
53
- cmd = container.cp_in(File.expand_path("../../rsync_cfgs/rsyncd.conf", __FILE__), "/tmp/.")
46
+ # controllo presenza comandi necessari
47
+ command_installed = false
48
+ cmd_ricerca = container.exec(['bash -c "command -v rsync && command -v killall"'])
49
+ unless cmd_ricerca.failed?
50
+ num_commands = cmd_ricerca.raw_result[:stdout].split("\n").count rescue 0
51
+ if num_commands == 2
52
+ command_installed = true
53
+ end
54
+ end
55
+
56
+ unless command_installed
57
+ # tentiamo di installarlo
58
+ cmd = container.exec(['bash -c "apt update && apt install -yq rsync psmisc"'])
59
+ if cmd.failed?
60
+ puts "Problemi nell'installazione di rsync nel pod"
61
+ else
62
+ command_installed = true
63
+ end
64
+ end
65
+
66
+ if command_installed
67
+ cmd = container.cp_in(configs_path("rsyncd.conf"), "/tmp/.")
54
68
  copy_1 = cmd.execute.failed?
55
- cmd = container.cp_in(File.expand_path("../../rsync_cfgs/rsyncd.secrets", __FILE__), "/tmp/.")
69
+ cmd = container.cp_in(configs_path("rsyncd.secrets"), "/tmp/.")
56
70
  copy_2 = cmd.execute.failed?
57
71
  cmd = container.exec(['bash -c "chmod 600 /tmp/rsyncd.secrets && chown root /tmp/*"'])
58
72
  chmod = cmd.failed?
@@ -76,11 +90,15 @@ module SwarmClusterCliOpe
76
90
 
77
91
  sleep 1
78
92
 
93
+ # forzo i permessi sul file della password
94
+ cmd = ShellCommandExecution.new(["chmod 600 #{ configs_path("password")}"])
95
+ cmd.execute
96
+
79
97
  # lanciamo il comando quindi per far rsync
80
98
  rsync_command = [
81
99
  "rsync -az --no-o --no-g",
82
100
  "--delete",
83
- "--password-file=#{ File.expand_path("../../rsync_cfgs/password", __FILE__)}"
101
+ "--password-file=#{ configs_path("password")}"
84
102
  ]
85
103
 
86
104
  if direction == :up
@@ -119,6 +137,14 @@ module SwarmClusterCliOpe
119
137
 
120
138
  end
121
139
 
140
+ ##
141
+ # Estrapola la path al file di configurazione
142
+ # @param [String] file
143
+ # @return [String]
144
+ def configs_path(file)
145
+ File.expand_path("../../rsync_cfgs/#{file}", __FILE__)
146
+ end
147
+
122
148
  end
123
149
  end
124
150
  end
@@ -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:, 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?
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)
@@ -2,7 +2,6 @@ module SwarmClusterCliOpe
2
2
  module SyncConfigs
3
3
  class PostGres < BaseDatabase
4
4
 
5
-
6
5
  def pull
7
6
  resume('pull')
8
7
 
@@ -13,11 +12,11 @@ module SwarmClusterCliOpe
13
12
  local.container.copy_in(tmp_file, tmp_file)
14
13
 
15
14
  # drop old db and recreate
16
- if Gem::Version.new(local.database_version) <= Gem::Version.new("12")
17
- close_connections_and_drop_cmd(local)
18
- else
19
- raise "DA ANALIZZARE QUANDO LA 13 disponibile....dropdb ha un force come parametro"
20
- end
15
+ # if Gem::Version.new(local.database_version) <= Gem::Version.new("12")
16
+ close_connections_and_drop_cmd(local)
17
+ # else
18
+ # raise "DA ANALIZZARE QUANDO LA 13 disponibile....dropdb ha un force come parametro"
19
+ # end
21
20
 
22
21
  create_cmd(local)
23
22
 
@@ -37,12 +36,8 @@ module SwarmClusterCliOpe
37
36
  dump_cmd(local, tmp_file)
38
37
  remote.container.copy_in(tmp_file, tmp_file)
39
38
 
40
- # drop old db and recreate
41
- if Gem::Version.new(local.database_version) <= Gem::Version.new("12")
42
- close_connections_and_drop_cmd(remote)
43
- else
44
- raise "DA ANALIZZARE QUANDO LA 13 disponibile....dropdb ha un force come parametro"
45
- end
39
+ close_connections_and_drop_cmd(remote)
40
+
46
41
  create_cmd(remote)
47
42
 
48
43
  restore_cmd(remote, tmp_file)
@@ -55,11 +50,14 @@ module SwarmClusterCliOpe
55
50
  # Classe interna che rappresenta le configurazioni del DB
56
51
  class EnvConfigs < BaseDatabase::EnvConfigs
57
52
 
58
- define_cfgs :database_name, default_env: "POSTGRES_DB", configuration_name: :database_name
59
- define_cfgs :username, default_env: "POSTGRES_USER", configuration_name: :pg_user, default_value: 'postgres'
60
- define_cfgs :password, default_env: "POSTGRES_PASSWORD", configuration_name: :pg_password
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
61
56
 
62
- define_cfgs :database_version, default_env: "PG_MAJOR", configuration_name: :pg_version
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
+ }
63
61
 
64
62
  end
65
63
 
@@ -125,23 +123,31 @@ module SwarmClusterCliOpe
125
123
 
126
124
  end
127
125
 
128
-
129
126
  # @param [EnvConfigs] config
130
127
  def close_connections_and_drop_cmd(config)
128
+
131
129
  cmd = []
132
- cmd << "export PGPASSWORD=\"#{config.password}\" &&"
133
130
 
134
- sql = []
135
- sql << "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '\"'\"'#{config.database_name}'\"'\"' AND pid <> pg_backend_pid();;"
136
- sql << "DROP DATABASE IF EXISTS #{config.database_name};"
131
+ if Gem::Version.new(config.database_version) >= Gem::Version.new("13")
132
+ cmd << "export PGPASSWORD=\"#{config.password}\" &&"
133
+ cmd << 'dropdb --force --if-exists'
134
+ cmd << "-U #{config.username}"
135
+ cmd << config.database_name
137
136
 
138
- cmd << "echo \"#{sql.join(" ")}\" "
139
- cmd << '|'
140
- cmd << 'psql'
141
- cmd << "-U #{config.username}"
142
- cmd << "postgres"
143
- cmd
137
+ else
138
+ cmd << "export PGPASSWORD=\"#{config.password}\" &&"
144
139
 
140
+ sql = []
141
+ sql << "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '\"'\"'#{config.database_name}'\"'\"' AND pid <> pg_backend_pid();;"
142
+ sql << "DROP DATABASE IF EXISTS #{config.database_name};"
143
+
144
+ cmd << "echo \"#{sql.join(" ")}\" "
145
+ cmd << '|'
146
+ cmd << 'psql'
147
+ cmd << "-U #{config.username}"
148
+ cmd << "postgres"
149
+
150
+ end
145
151
  logger.info { "CLOSE CONNECTIONS COMMAND: #{cmd.join(' ')}" }
146
152
  config.container.exec("bash -c '#{cmd.join(' ')}'")
147
153
  end
@@ -153,7 +159,6 @@ module SwarmClusterCliOpe
153
159
  # PGPASSWORD='root' createdb -U root -h 0.0.0.0 -p 32790 development;
154
160
  # PGPASSWORD='root' psql -U root -h 0.0.0.0 -p 32790 -d development < ./cortobio_production_new_2020-09-10-171742.sql
155
161
 
156
-
157
162
  end
158
163
  end
159
164
  end
@@ -1,3 +1,3 @@
1
1
  module SwarmClusterCliOpe
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.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.1
4
+ version: 0.5.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: 2020-11-13 00:00:00.000000000 Z
11
+ date: 2021-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor