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