blackstack-nodes 1.2.12 → 1.2.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/blackstack-nodes.rb +40 -34
  3. metadata +2 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc21a5ad0c4b17836d8f63f5adf7b376ae8e274135b6371e40ee263cc7181aa7
4
- data.tar.gz: 4bfe763d33a8bded730860508dc66ce97177d8718f13a80a998f90f09929e7e1
3
+ metadata.gz: baa210daeea5b1e58fb15147e50003cef8e154085926c2b85cc80701965d3c92
4
+ data.tar.gz: d68c0e01dcd790b4bc3032f25bc7b8159cb1287260ce7c41b00eed02ee176553
5
5
  SHA512:
6
- metadata.gz: bc436cf1f7378481b0064cd8fb1b464e4c3a833b682cbcc1bfe72cd6dce85b839f7b09318325ec4bc1da26e64e947873755a05d161b8cca9083f2cc68f21e690
7
- data.tar.gz: 4886dbf65cdbed4e5a7eb31eccf4f216b83163f5dd18c25ad6f069e672b4c4af8f3d0953d3a9ff174ad10a28b3dff5938e5544c501d2421b5e7a16dab74c9cfd
6
+ metadata.gz: 74e04021b9f39eb587a0a2471e2bf1406dca2be895e65af0316c0dc89a94652148ac1a16c6ab5b16cc073f5b4cfa0bbf0414e13961357e5695e7a834e98eeec8
7
+ data.tar.gz: ab8d5169af5376d2c6e52251a4b1e4b76c9e45187ec1db42dd18741a8eaf1a2865293bfda6f68bdb279f42ed9ea75b617240dfe70eab04ff09992ef3182c6dea
@@ -76,7 +76,7 @@ module BlackStack
76
76
  self.tags = []
77
77
  end
78
78
  # create a logger
79
- self.logger = !i_logger.nil? ? i_logger : BlackStack::BaseLogger.new(nil)
79
+ self.logger = !i_logger.nil? ? i_logger : BlackStack::DummyLogger.new(nil)
80
80
  end # def self.create(h)
81
81
 
82
82
  def to_hash
@@ -117,27 +117,44 @@ module BlackStack
117
117
  self.ssh.close
118
118
  end
119
119
 
120
- def code(command, sudo=true)
121
- s = nil
122
- if sudo
123
- command.gsub!(/'/, "\\\\'")
124
- if self.using_password?
125
- s = "echo '#{self.ssh_password.gsub(/'/, "\\\\'")}' | sudo -S su root -c '#{command}'"
126
- elsif self.using_private_key_file?
127
- s = "sudo -S su root -c '#{command}'"
128
- end
129
- else
130
- s = command
120
+ # Execute a command on the remote server and return the output.
121
+ # If the exit code of the command is not zero, an exception is raised, with the messages pushed into the stderr.
122
+ # If the exit code of the command is zero, the messages pushed into the stdout are returned.
123
+ #
124
+ # IMPORTANT: Messages pushed into stderr re not always error.
125
+ # Reference:
126
+ # - How to stop git from writing non-errors to stderr ?
127
+ # - https://stackoverflow.com/questions/57016157/how-to-stop-git-from-writing-non-errors-to-stderr
128
+ #
129
+ def exec(
130
+ command,
131
+ output_file: "~/.bash-command-stdout-buffer",
132
+ error_file: "~/.bash-command-stderr-buffer",
133
+ exit_code_file: "~/.bash-command-exit-code"
134
+ )
135
+ # Construct the remote command with redirection for stdout, stderr, and capturing exit code
136
+ remote_command = "#{command} > #{output_file} 2> #{error_file}; echo $? > #{exit_code_file}"
137
+
138
+ # Execute the command on the remote server, truncating output, error, and exit code files
139
+ self.ssh.exec!("truncate -s 0 #{output_file} #{error_file} #{exit_code_file} && #{remote_command}")
140
+
141
+ # Retrieve the exit code
142
+ exit_code = self.ssh.exec!("cat #{exit_code_file}").to_s.chomp.to_i
143
+
144
+ # Retrieve the content of the error file from the remote server
145
+ error_content = self.ssh.exec!("cat #{error_file}").to_s.chomp
146
+
147
+ # Check if the exit code is not zero
148
+ if exit_code != 0
149
+ # Raise an exception with the error message
150
+ raise "Command failed with exit code #{exit_code}:\n#{error_content}"
131
151
  end
132
- s
133
- end
134
-
135
- def exec(command, sudo=true)
136
- code = self.code(command, sudo)
137
- s = self.ssh.exec!(code)
138
- s
152
+
153
+ # Retrieve and return the content of the output file from the remote server
154
+ # Truncate any trailing newline character
155
+ self.ssh.exec!("cat #{output_file}").to_s.chomp
139
156
  end # def exec
140
-
157
+
141
158
  def reboot()
142
159
  tries = 0
143
160
  max_tries = 20
@@ -178,7 +195,7 @@ module BlackStack
178
195
  def usage()
179
196
  ret = {}
180
197
 
181
- self.connect
198
+ #self.connect
182
199
 
183
200
  ret[:b_total_memory] = self.ssh.exec!('cat /proc/meminfo | grep MemTotal').delete('^0-9').to_i*1024
184
201
  ret[:kb_total_memory] = ret[:b_total_memory] / 1024
@@ -221,17 +238,14 @@ module BlackStack
221
238
  # mapping lan attributes
222
239
  ret[:net_mac_address] = self.ssh.exec!('ifconfig | grep ether').split[1].upcase.strip.gsub(':', '-')
223
240
 
224
- self.disconnect
241
+ #self.disconnect
225
242
 
226
243
  ret
227
244
  end # def usage
228
245
 
229
246
  # return the latest `n`` lines of the file specified by the `filename` parameter
230
247
  def tail(filename, n=10)
231
- self.connect
232
- s = self.ssh.exec!("tail -n #{n.to_s} #{filename}")
233
- self.disconnect
234
- s
248
+ self.ssh.exec!("tail -n #{n.to_s} #{filename}")
235
249
  end
236
250
 
237
251
  end # module NodeModule
@@ -244,13 +258,5 @@ module BlackStack
244
258
  class Node
245
259
  include NodeModule
246
260
  end # class Node
247
- =begin
248
- # Node Skeleton
249
- # This class represents a node, with connection to the database.
250
- # Use this class at the server side.
251
- class Node < Sequel::Model(:node)
252
- include NodeModule
253
- end
254
- =end
255
261
  end # module Infrastructure
256
262
  end # module BlackStack
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blackstack-nodes
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.12
4
+ version: 1.2.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Daniel Sardi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-11 00:00:00.000000000 Z
11
+ date: 2024-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 6.1.0
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 6.1.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,6 @@ dependencies:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 6.1.0
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 6.1.0
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: simple_cloud_logging
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -37,9 +31,6 @@ dependencies:
37
31
  - - "~>"
38
32
  - !ruby/object:Gem::Version
39
33
  version: 1.2.2
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 1.2.2
43
34
  type: :runtime
44
35
  prerelease: false
45
36
  version_requirements: !ruby/object:Gem::Requirement
@@ -47,9 +38,6 @@ dependencies:
47
38
  - - "~>"
48
39
  - !ruby/object:Gem::Version
49
40
  version: 1.2.2
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 1.2.2
53
41
  description: "BlackStack Nodes is a simple library to managing a computer remotely
54
42
  via SSH, and perform some common operations.\nThis library is used and extended
55
43
  by many others like: \n- [BlackStack Deployer](https://github.com/leandrosardi/blackstack-deployer)\n-