blackstack-nodes 1.2.8 → 1.2.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/blackstack-nodes.rb +183 -188
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b29722929b2f21672086635198ce3503a0e4e07c89122a35837d05b4f342bb5
|
4
|
+
data.tar.gz: 88373aff2b6bb5f5e10f1d5456b79c8e90ea8716e1db2023cc20e447e43dc6de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96c3290ebdda88af9fbba57ed21c1238b88601121f129dadd95be2d32313fd55a797b2e741719976cd96d219eaf24bdafc8929687cba7316713f45ed6bab886b
|
7
|
+
data.tar.gz: 0dc4dcddf34317310ea677fbd04dbe57135f56683a39922d111167183616856fb095a5c09716e995473c9b18cfa460c54a62c0eb2e6cba22c8f06ca7e0feb356
|
data/lib/blackstack-nodes.rb
CHANGED
@@ -1,189 +1,184 @@
|
|
1
|
-
require 'net/ssh'
|
2
|
-
require 'shellwords'
|
3
|
-
require 'simple_cloud_logging'
|
4
|
-
#
|
5
|
-
module BlackStack
|
6
|
-
#
|
7
|
-
module Infrastructure
|
8
|
-
# this module has attributes an methods used by both classes Node and Node.
|
9
|
-
module NodeModule
|
10
|
-
# :name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
|
11
|
-
attr_accessor :name, :net_remote_ip, :ssh_username, :ssh_password, :ssh_port, :ssh_private_key_file
|
12
|
-
# non-database attributes, used for ssh connection and logging
|
13
|
-
attr_accessor :ssh, :logger
|
14
|
-
|
15
|
-
def self.descriptor_errors(h)
|
16
|
-
errors = []
|
17
|
-
|
18
|
-
# validate: the parameter h is a hash
|
19
|
-
errors << "The parameter h is not a hash" unless h.is_a?(Hash)
|
20
|
-
|
21
|
-
# validate: the parameter h has a key :name
|
22
|
-
errors << "The parameter h does not have a key :name" unless h.has_key?(:name)
|
23
|
-
|
24
|
-
# validate: the parameter h[:name] is a string
|
25
|
-
errors << "The parameter h[:name] is not a string" unless h[:name].is_a?(String)
|
26
|
-
|
27
|
-
# validate: the paramerer h has a key :net_remote_ip
|
28
|
-
errors << "The parameter h does not have a key :net_remote_ip" unless h.has_key?(:net_remote_ip)
|
29
|
-
|
30
|
-
# validate: the paramerer h has a key :ssh_username
|
31
|
-
errors << "The parameter h does not have a key :ssh_username" unless h.has_key?(:ssh_username)
|
32
|
-
|
33
|
-
# validate: the parameter h[:ssh_username] is a string
|
34
|
-
errors << "The parameter h[:ssh_username] is not a string" unless h[:ssh_username].is_a?(String)
|
35
|
-
|
36
|
-
# if the parameter h has a key :ssh_private_key_file
|
37
|
-
if h.has_key?(:ssh_private_key_file) && !h[:ssh_private_key_file].nil?
|
38
|
-
# validate: the parameter h[:ssh_private_key_file] is a string
|
39
|
-
errors << "The parameter h[:ssh_private_key_file] is not a string" unless h[:ssh_private_key_file].is_a?(String)
|
40
|
-
|
41
|
-
# validate: the parameter h[:ssh_private_key_file] is a string
|
42
|
-
errors << "The parameter h[:ssh_private_key_file] is not a string" unless h[:ssh_private_key_file].is_a?(String)
|
43
|
-
else
|
44
|
-
# validate: the parameter h has a key :ssh_password
|
45
|
-
errors << "The parameter h does not have a key :ssh_password nor :ssh_private_key_file" unless h.has_key?(:ssh_password)
|
46
|
-
|
47
|
-
# validate: the parameter h[:ssh_password] is a string
|
48
|
-
errors << "The parameter h[:ssh_password] is not a string" unless h[:ssh_password].is_a?(String)
|
49
|
-
end
|
50
|
-
|
51
|
-
# return
|
52
|
-
errors
|
53
|
-
end # def self.descriptor_errors(h)
|
54
|
-
|
55
|
-
def initialize(h, i_logger=nil)
|
56
|
-
errors = BlackStack::Infrastructure::NodeModule.descriptor_errors(h)
|
57
|
-
# raise an exception if any error happneed
|
58
|
-
raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
|
59
|
-
# map attributes
|
60
|
-
self.name = h[:name]
|
61
|
-
self.net_remote_ip = h[:net_remote_ip]
|
62
|
-
self.ssh_username = h[:ssh_username]
|
63
|
-
self.ssh_password = h[:ssh_password]
|
64
|
-
self.ssh_port = h[:ssh_port]
|
65
|
-
self.ssh_private_key_file = h[:ssh_private_key_file]
|
66
|
-
|
67
|
-
# create a logger
|
68
|
-
self.logger = !i_logger.nil? ? i_logger : BlackStack::BaseLogger.new(nil)
|
69
|
-
end # def self.create(h)
|
70
|
-
|
71
|
-
def to_hash
|
72
|
-
{
|
73
|
-
:name => self.name,
|
74
|
-
:net_remote_ip => self.net_remote_ip,
|
75
|
-
:ssh_username => self.ssh_username,
|
76
|
-
:ssh_password => self.ssh_password,
|
77
|
-
:ssh_port => self.ssh_port,
|
78
|
-
:ssh_private_key_file => self.ssh_private_key_file,
|
79
|
-
}
|
80
|
-
end # def to_hash
|
81
|
-
|
82
|
-
# return true if the node is all set to connect using ssh user and password.
|
83
|
-
def using_password?
|
84
|
-
!self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_password.nil?
|
85
|
-
end
|
86
|
-
|
87
|
-
# return true if the node is all set to connect using a private key file.
|
88
|
-
def using_private_key_file?
|
89
|
-
!self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_private_key_file.nil?
|
90
|
-
end
|
91
|
-
|
92
|
-
def connect
|
93
|
-
# connect
|
94
|
-
if self.using_password?
|
95
|
-
self.ssh = Net::SSH.start(self.net_remote_ip, self.ssh_username, :password => self.ssh_password, :port => self.ssh_port)
|
96
|
-
elsif self.using_private_key_file?
|
97
|
-
self.ssh = Net::SSH.start(self.net_remote_ip, self.ssh_username, :keys => self.ssh_private_key_file, :port => self.ssh_port)
|
98
|
-
else
|
99
|
-
raise "No ssh credentials available"
|
100
|
-
end
|
101
|
-
self.ssh
|
102
|
-
end # def connect
|
103
|
-
|
104
|
-
def disconnect
|
105
|
-
self.ssh.close
|
106
|
-
end
|
107
|
-
|
108
|
-
def
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
s = self.ssh.exec!(code)
|
125
|
-
|
126
|
-
#
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
#stdout
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
# Node
|
175
|
-
|
176
|
-
#
|
177
|
-
class
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
class Node < Sequel::Model(:node)
|
185
|
-
include NodeModule
|
186
|
-
end
|
187
|
-
=end
|
188
|
-
end # module Infrastructure
|
1
|
+
require 'net/ssh'
|
2
|
+
require 'shellwords'
|
3
|
+
require 'simple_cloud_logging'
|
4
|
+
#
|
5
|
+
module BlackStack
|
6
|
+
#
|
7
|
+
module Infrastructure
|
8
|
+
# this module has attributes an methods used by both classes Node and Node.
|
9
|
+
module NodeModule
|
10
|
+
# :name is this is just a descriptive name for the node. It is not the host name, nor the domain, nor any ip.
|
11
|
+
attr_accessor :name, :net_remote_ip, :ssh_username, :ssh_password, :ssh_port, :ssh_private_key_file
|
12
|
+
# non-database attributes, used for ssh connection and logging
|
13
|
+
attr_accessor :ssh, :logger
|
14
|
+
|
15
|
+
def self.descriptor_errors(h)
|
16
|
+
errors = []
|
17
|
+
|
18
|
+
# validate: the parameter h is a hash
|
19
|
+
errors << "The parameter h is not a hash" unless h.is_a?(Hash)
|
20
|
+
|
21
|
+
# validate: the parameter h has a key :name
|
22
|
+
errors << "The parameter h does not have a key :name" unless h.has_key?(:name)
|
23
|
+
|
24
|
+
# validate: the parameter h[:name] is a string
|
25
|
+
errors << "The parameter h[:name] is not a string" unless h[:name].is_a?(String)
|
26
|
+
|
27
|
+
# validate: the paramerer h has a key :net_remote_ip
|
28
|
+
errors << "The parameter h does not have a key :net_remote_ip" unless h.has_key?(:net_remote_ip)
|
29
|
+
|
30
|
+
# validate: the paramerer h has a key :ssh_username
|
31
|
+
errors << "The parameter h does not have a key :ssh_username" unless h.has_key?(:ssh_username)
|
32
|
+
|
33
|
+
# validate: the parameter h[:ssh_username] is a string
|
34
|
+
errors << "The parameter h[:ssh_username] is not a string" unless h[:ssh_username].is_a?(String)
|
35
|
+
|
36
|
+
# if the parameter h has a key :ssh_private_key_file
|
37
|
+
if h.has_key?(:ssh_private_key_file) && !h[:ssh_private_key_file].nil?
|
38
|
+
# validate: the parameter h[:ssh_private_key_file] is a string
|
39
|
+
errors << "The parameter h[:ssh_private_key_file] is not a string" unless h[:ssh_private_key_file].is_a?(String)
|
40
|
+
|
41
|
+
# validate: the parameter h[:ssh_private_key_file] is a string
|
42
|
+
errors << "The parameter h[:ssh_private_key_file] is not a string" unless h[:ssh_private_key_file].is_a?(String)
|
43
|
+
else
|
44
|
+
# validate: the parameter h has a key :ssh_password
|
45
|
+
errors << "The parameter h does not have a key :ssh_password nor :ssh_private_key_file" unless h.has_key?(:ssh_password)
|
46
|
+
|
47
|
+
# validate: the parameter h[:ssh_password] is a string
|
48
|
+
errors << "The parameter h[:ssh_password] is not a string" unless h[:ssh_password].is_a?(String)
|
49
|
+
end
|
50
|
+
|
51
|
+
# return
|
52
|
+
errors
|
53
|
+
end # def self.descriptor_errors(h)
|
54
|
+
|
55
|
+
def initialize(h, i_logger=nil)
|
56
|
+
errors = BlackStack::Infrastructure::NodeModule.descriptor_errors(h)
|
57
|
+
# raise an exception if any error happneed
|
58
|
+
raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
|
59
|
+
# map attributes
|
60
|
+
self.name = h[:name]
|
61
|
+
self.net_remote_ip = h[:net_remote_ip]
|
62
|
+
self.ssh_username = h[:ssh_username]
|
63
|
+
self.ssh_password = h[:ssh_password]
|
64
|
+
self.ssh_port = h[:ssh_port]
|
65
|
+
self.ssh_private_key_file = h[:ssh_private_key_file]
|
66
|
+
|
67
|
+
# create a logger
|
68
|
+
self.logger = !i_logger.nil? ? i_logger : BlackStack::BaseLogger.new(nil)
|
69
|
+
end # def self.create(h)
|
70
|
+
|
71
|
+
def to_hash
|
72
|
+
{
|
73
|
+
:name => self.name,
|
74
|
+
:net_remote_ip => self.net_remote_ip,
|
75
|
+
:ssh_username => self.ssh_username,
|
76
|
+
:ssh_password => self.ssh_password,
|
77
|
+
:ssh_port => self.ssh_port,
|
78
|
+
:ssh_private_key_file => self.ssh_private_key_file,
|
79
|
+
}
|
80
|
+
end # def to_hash
|
81
|
+
|
82
|
+
# return true if the node is all set to connect using ssh user and password.
|
83
|
+
def using_password?
|
84
|
+
!self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_password.nil?
|
85
|
+
end
|
86
|
+
|
87
|
+
# return true if the node is all set to connect using a private key file.
|
88
|
+
def using_private_key_file?
|
89
|
+
!self.net_remote_ip.nil? && !self.ssh_username.nil? && !self.ssh_private_key_file.nil?
|
90
|
+
end
|
91
|
+
|
92
|
+
def connect
|
93
|
+
# connect
|
94
|
+
if self.using_password?
|
95
|
+
self.ssh = Net::SSH.start(self.net_remote_ip, self.ssh_username, :password => self.ssh_password, :port => self.ssh_port)
|
96
|
+
elsif self.using_private_key_file?
|
97
|
+
self.ssh = Net::SSH.start(self.net_remote_ip, self.ssh_username, :keys => self.ssh_private_key_file, :port => self.ssh_port)
|
98
|
+
else
|
99
|
+
raise "No ssh credentials available"
|
100
|
+
end
|
101
|
+
self.ssh
|
102
|
+
end # def connect
|
103
|
+
|
104
|
+
def disconnect
|
105
|
+
self.ssh.close
|
106
|
+
end
|
107
|
+
|
108
|
+
def code(command, sudo=true)
|
109
|
+
s = nil
|
110
|
+
command = command.gsub(/'/, "\\\\'")
|
111
|
+
if sudo
|
112
|
+
if self.using_password?
|
113
|
+
s = "echo '#{self.ssh_password.gsub(/'/, "\\\\'")}' | sudo -S su root -c '#{command}'"
|
114
|
+
elsif self.using_private_key_file?
|
115
|
+
s = "sudo -S su root -c '#{command}'"
|
116
|
+
end
|
117
|
+
else
|
118
|
+
s = command
|
119
|
+
end
|
120
|
+
s
|
121
|
+
end
|
122
|
+
|
123
|
+
def exec(command, sudo=true)
|
124
|
+
s = self.ssh.exec!(self.code(command, sudo))
|
125
|
+
s
|
126
|
+
end # def exec
|
127
|
+
|
128
|
+
def reboot()
|
129
|
+
tries = 0
|
130
|
+
max_tries = 20
|
131
|
+
success = false
|
132
|
+
|
133
|
+
host = self
|
134
|
+
|
135
|
+
logger.logs 'reboot... '
|
136
|
+
#stdout = host.reboot
|
137
|
+
begin
|
138
|
+
stdout = self.exec("reboot")
|
139
|
+
rescue
|
140
|
+
end
|
141
|
+
logger.done #logf("done (#{stdout})")
|
142
|
+
|
143
|
+
while tries < max_tries && !success
|
144
|
+
begin
|
145
|
+
tries += 1
|
146
|
+
|
147
|
+
delay = 10
|
148
|
+
logger.logs "wait #{delay.to_s} seconds... "
|
149
|
+
sleep(delay)
|
150
|
+
logger.done
|
151
|
+
|
152
|
+
logger.logs "connecting (try #{tries.to_s})... "
|
153
|
+
host.connect
|
154
|
+
logger.done
|
155
|
+
|
156
|
+
success = true
|
157
|
+
rescue => e
|
158
|
+
logger.logf e.to_s #error e
|
159
|
+
end
|
160
|
+
end # while
|
161
|
+
raise 'reboot failed' if !success
|
162
|
+
end # def reboot
|
163
|
+
|
164
|
+
|
165
|
+
end # module NodeModule
|
166
|
+
|
167
|
+
# TODO: declare these classes (stub and skeleton) using blackstack-rpc
|
168
|
+
#
|
169
|
+
# Node Stub
|
170
|
+
# This class represents a node, without using connection to the database.
|
171
|
+
# Use this class at the client side.
|
172
|
+
class Node
|
173
|
+
include NodeModule
|
174
|
+
end # class Node
|
175
|
+
=begin
|
176
|
+
# Node Skeleton
|
177
|
+
# This class represents a node, with connection to the database.
|
178
|
+
# Use this class at the server side.
|
179
|
+
class Node < Sequel::Model(:node)
|
180
|
+
include NodeModule
|
181
|
+
end
|
182
|
+
=end
|
183
|
+
end # module Infrastructure
|
189
184
|
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.
|
4
|
+
version: 1.2.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leandro Daniel Sardi
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-06
|
11
|
+
date: 2022-09-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: net-ssh
|
@@ -66,7 +66,7 @@ homepage: https://github.com/leandrosardi/blackstack-nodes
|
|
66
66
|
licenses:
|
67
67
|
- MIT
|
68
68
|
metadata: {}
|
69
|
-
post_install_message:
|
69
|
+
post_install_message:
|
70
70
|
rdoc_options: []
|
71
71
|
require_paths:
|
72
72
|
- lib
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
84
|
rubygems_version: 3.3.7
|
85
|
-
signing_key:
|
85
|
+
signing_key:
|
86
86
|
specification_version: 4
|
87
87
|
summary: BlackStack Nodes is a simple library to managing a computer remotely via
|
88
88
|
SSH, and perform some common operations.
|