Class: RCS::Updater::Client
- Inherits:
-
Object
- Object
- RCS::Updater::Client
show all
- Extended by:
- Resolver
- Includes:
- Tracer, TmpDir
- Defined in:
- lib/rcs-common/updater/client.rb
Constant Summary
Constants included
from Tracer
Tracer::TRACE_YAML_NAME
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_firewall_rule(rule_name, params = {}) ⇒ Object
-
#add_to_hosts_file(hash) ⇒ Object
-
#add_to_path(*paths) ⇒ Object
TODO: ensure no duplication.
-
#connected? ⇒ Boolean
-
#cp(from, to) ⇒ Object
-
#cp_r(from, to) ⇒ Object
-
#database_exists?(name, mongo: nil) ⇒ Boolean
-
#delete_service(service_name) ⇒ Object
-
#execute(cmd) ⇒ Object
-
#extract_sfx(sfx_path, destination_path) ⇒ Object
-
#file_exists?(path) ⇒ Boolean
-
#initialize(address, port: 6677) ⇒ Client
constructor
A new instance of Client.
-
#local_command(cmd, options = {}) ⇒ Object
-
#localhost? ⇒ Boolean
-
#ls(glob) ⇒ Object
-
#mkdir_p(path) ⇒ Object
-
#mv(from, to) ⇒ Object
-
#read_file(path) ⇒ Object
-
#registry_add(key_path, value_name, value_data) ⇒ Object
-
#request(payload, options = {}, retry_count = self.max_retries) ⇒ Object
-
#restart_service(name) ⇒ Object
-
#rm_f(path) ⇒ Object
-
#rm_rf(path, allow: [], check: true) ⇒ Object
-
#service_config(service_name, param_name, param_value) ⇒ Object
-
#service_exists?(name) ⇒ Boolean
-
#service_failure(service_name, reset = 0, action1 = "restart/60000", action2 = "restart/60000", action3 = "restart/60000") ⇒ Object
-
#start(payload) ⇒ Object
(also: #detached)
-
#start_service(name) ⇒ Object
-
#stop_service(name) ⇒ Object
-
#store(payload, filename: nil) ⇒ Object
-
#store_file(path, remote_path = nil) ⇒ Object
-
#unixpath(path) ⇒ Object
-
#winpath(path) ⇒ Object
-
#write_file(path, content) ⇒ Object
Methods included from Resolver
resolve_dns, resolved_dns_cache
Methods included from TmpDir
#tmpdir, #windows?
Methods included from Tracer
#thread_name, #trace, #trace_ensure_log_folders, #trace_init, #trace_named_put, #trace_named_remove, #trace_nested_pop, #trace_nested_push, #trace_setup
Constructor Details
#initialize(address, port: 6677) ⇒ Client
Returns a new instance of Client.
25
26
27
28
29
30
31
32
33
|
# File 'lib/rcs-common/updater/client.rb', line 25
def initialize(address, port: 6677)
@address = address
@port = port
@shared_key = SharedKey.new
self.max_retries = 3
self.retry_interval = 4 self.open_timeout = 10 end
|
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
21
22
23
|
# File 'lib/rcs-common/updater/client.rb', line 21
def address
@address
end
|
#max_retries ⇒ Object
Returns the value of attribute max_retries.
22
23
24
|
# File 'lib/rcs-common/updater/client.rb', line 22
def max_retries
@max_retries
end
|
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
22
23
24
|
# File 'lib/rcs-common/updater/client.rb', line 22
def open_timeout
@open_timeout
end
|
#port ⇒ Object
Returns the value of attribute port.
21
22
23
|
# File 'lib/rcs-common/updater/client.rb', line 21
def port
@port
end
|
#pwd ⇒ Object
Returns the value of attribute pwd.
23
24
25
|
# File 'lib/rcs-common/updater/client.rb', line 23
def pwd
@pwd
end
|
#retry_interval ⇒ Object
Returns the value of attribute retry_interval.
22
23
24
|
# File 'lib/rcs-common/updater/client.rb', line 22
def retry_interval
@retry_interval
end
|
Class Method Details
.resolve_to_localhost?(name) ⇒ Boolean
85
86
87
88
89
|
# File 'lib/rcs-common/updater/client.rb', line 85
def self.resolve_to_localhost?(name)
return true if name == 'localhost'
addr = resolve_dns(name, use_cache: true) rescue nil
return addr == '127.0.0.1'
end
|
Instance Method Details
#add_firewall_rule(rule_name, params = {}) ⇒ Object
179
180
181
182
183
184
185
186
|
# File 'lib/rcs-common/updater/client.rb', line 179
def add_firewall_rule(rule_name, params = {})
if localhost?
WinFirewall.del_rule(rule_name)
WinFirewall.add_rule(params.merge(name: rule_name))
else
end
end
|
#add_to_hosts_file(hash) ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
|
# File 'lib/rcs-common/updater/client.rb', line 254
def add_to_hosts_file(hash)
ip, name = *hash.to_a.first
line = "\r\n#{ip}\t#{name}\r\n"
path = "C:\\Windows\\System32\\Drivers\\etc\\hosts"
if localhost?
File.open(path, 'ab') { |file| file.write(line) } unless File.read(path).include?(line.strip)
else
end
end
|
#add_to_path(*paths) ⇒ Object
TODO: ensure no duplication
279
280
281
282
283
284
285
286
287
288
|
# File 'lib/rcs-common/updater/client.rb', line 279
def add_to_path(*paths)
list = [paths].flatten.map{ |p| winpath(p) }.join(";")
if localhost?
ENV['PATH'] += ";#{list}" unless ENV['path'].include?(list)
return local_command("setx path \"%path%;#{list}\" /M && set PATH=\"%PATH%;#{list}\"")
else
return request("setx path \"%path%;#{list}\"", exec: 1)
end
end
|
#connected? ⇒ Boolean
345
346
347
348
349
350
351
|
# File 'lib/rcs-common/updater/client.rb', line 345
def connected?
if localhost?
return true
else
return !!(request("", {}, retry_count = 0) rescue false)
end
end
|
#cp(from, to) ⇒ Object
298
299
300
301
302
303
304
|
# File 'lib/rcs-common/updater/client.rb', line 298
def cp(from, to)
if localhost?
FileUtils.cp(unixpath(from), unixpath(to))
else
request("ruby -e 'require \"fileutils\"; FileUtils.cp(\"#{unixpath(from)}\", \"#{unixpath(to)}\");", exec: 1)
end
end
|
#cp_r(from, to) ⇒ Object
306
307
308
309
310
311
312
|
# File 'lib/rcs-common/updater/client.rb', line 306
def cp_r(from, to)
if localhost?
FileUtils.cp_r(unixpath(from), unixpath(to))
else
request("ruby -e 'require \"fileutils\"; FileUtils.cp_r(\"#{unixpath(from)}\", \"#{unixpath(to)}\");", exec: 1)
end
end
|
#database_exists?(name, mongo: nil) ⇒ Boolean
218
219
220
221
222
|
# File 'lib/rcs-common/updater/client.rb', line 218
def database_exists?(name, mongo: nil)
eval = "f=null; db.adminCommand({listDatabases: 1})['databases'].forEach(function(e){ if (e.name == '#{name}') { f = true } }); if (!f) { throw('not found') }"
cmd = "#{winpath(mongo)} 127.0.0.1 --eval \"#{eval}\""
return execute(cmd)
end
|
#delete_service(service_name) ⇒ Object
174
175
176
177
|
# File 'lib/rcs-common/updater/client.rb', line 174
def delete_service(service_name)
cmd = "sc delete #{service_name}"
return localhost? ? local_command(cmd) : request(cmd, exec: 1)
end
|
#execute(cmd) ⇒ Object
208
209
210
211
212
213
214
215
216
|
# File 'lib/rcs-common/updater/client.rb', line 208
def execute(cmd)
resp = localhost? ? local_command(cmd) : request(cmd, exec: 1)
if resp[:return_code] != 0
return nil
else
return resp[:output]
end
end
|
266
267
268
269
270
271
272
273
274
275
276
|
# File 'lib/rcs-common/updater/client.rb', line 266
def (sfx_path, destination_path)
mkdir_p(destination_path)
if localhost?
local_command("\"#{winpath(sfx_path)}\" -y -o\"#{winpath(destination_path)}\"")
else
remote_path = store_file(sfx_path)
request("\"#{winpath(remote_path)}\" -y -o\"#{winpath(destination_path)}\"", exec: 1)
rm_f(remote_path)
end
end
|
#file_exists?(path) ⇒ Boolean
331
332
333
|
# File 'lib/rcs-common/updater/client.rb', line 331
def file_exists?(path)
ls(path).any?
end
|
#local_command(cmd, options = {}) ⇒ Object
75
76
77
78
79
80
|
# File 'lib/rcs-common/updater/client.rb', line 75
def local_command(cmd, options = {})
payload = Payload.new(cmd, options.merge('exec' => true))
payload.store if payload.storable?
payload.run if payload.runnable?
return payload
end
|
#localhost? ⇒ Boolean
91
92
93
|
# File 'lib/rcs-common/updater/client.rb', line 91
def localhost?
self.class.resolve_to_localhost?(@address)
end
|
#ls(glob) ⇒ Object
322
323
324
325
326
327
328
329
|
# File 'lib/rcs-common/updater/client.rb', line 322
def ls(glob)
if localhost?
return Dir[unixpath(glob)]
else
resp = request('ruby -e \'require "base64"; require "json"; puts Base64.urlsafe_encode64(Dir["'+unixpath(glob)+'"].to_json)\'', exec: 1)
return JSON.parse(Base64.urlsafe_decode64(resp[:output].strip))
end
end
|
#mkdir_p(path) ⇒ Object
290
291
292
293
294
295
296
|
# File 'lib/rcs-common/updater/client.rb', line 290
def mkdir_p(path)
if localhost?
FileUtils.mkdir_p(winpath(path))
else
request("ruby -e 'require \"fileutils\"; FileUtils.mkdir_p(\"#{unixpath(path)}\");", exec: 1)
end
end
|
#mv(from, to) ⇒ Object
314
315
316
317
318
319
320
|
# File 'lib/rcs-common/updater/client.rb', line 314
def mv(from, to)
if localhost?
FileUtils.mv(unixpath(from), unixpath(to))
else
request("ruby -e 'require \"fileutils\"; FileUtils.mv(\"#{unixpath(from)}\", \"#{unixpath(to)}\");", exec: 1)
end
end
|
#read_file(path) ⇒ Object
169
170
171
172
|
# File 'lib/rcs-common/updater/client.rb', line 169
def read_file(path)
File.read(unixpath(path))
end
|
#registry_add(key_path, value_name, value_data) ⇒ Object
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/rcs-common/updater/client.rb', line 150
def registry_add(key_path, value_name, value_data)
value_type = if value_data.kind_of?(Fixnum)
:REG_DWORD
else
:REG_SZ
end
cmd = "reg add #{winpath(key_path)} /f /t #{value_type} /v #{value_name} /d #{value_data}"
return localhost? ? local_command(cmd) : request(cmd, exec: 1)
end
|
#request(payload, options = {}, retry_count = self.max_retries) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/rcs-common/updater/client.rb', line 35
def request(payload, options = {}, retry_count = self.max_retries)
msg = options[:store] ? [] : [payload]
msg = ["#{payload.size} B", options.inspect]
trace(:debug, "REQ #{msg.join(' | ')}")
http = Net::HTTP.new(address, port)
http.open_timeout = self.open_timeout
options.merge!(tm: Time.now.to_f, md5: Digest::MD5.hexdigest(payload))
req = Net::HTTP::Post.new('/', = {'Content-Type' =>'application/json'})
req['x-options'] = @shared_key.encrypt_hash(options)
raise("x-options header is too long") if req['x-options'].size > 4_096
req.body = payload
res = http.request(req)
status_code = res.code.to_i
trace :debug, "REP #{res.code} | #{res.body}"
raise("Internal server error") if res.code.to_i != 200
hash = JSON.parse(res.body)
hash.keys.each { |key| hash[key.to_sym] = hash.delete(key) }
return hash
rescue Exception => ex
trace(:error, "[#{ex.class}] #{ex.message}")
if retry_count > 0
trace(:warn, "Retrying in #{retry_interval} seconds, #{retry_count} attempt(s) left")
sleep(self.retry_interval)
return request(payload, options, retry_count-1)
else
raise(ex)
end
end
|
#restart_service(name) ⇒ Object
131
132
133
134
|
# File 'lib/rcs-common/updater/client.rb', line 131
def restart_service(name)
stop_service(name)
start_service(name)
end
|
#rm_f(path) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/rcs-common/updater/client.rb', line 240
def rm_f(path)
if localhost?
FileUtils.rm_f(unixpath(path))
else
request("ruby -e 'require \"fileutils\"; FileUtils.rm_f(\"#{unixpath(path)}\");'", exec: 1)
end
if ls(path).any?
raise("rm_f command failed on file #{path}")
else
return true
end
end
|
#rm_rf(path, allow: [], check: true) ⇒ Object
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/rcs-common/updater/client.rb', line 224
def rm_rf(path, allow: [], check: true)
if localhost?
FileUtils.rm_rf(unixpath(path))
else
request("ruby -e 'require \"fileutils\"; FileUtils.rm_rf(\"#{unixpath(path)}\");'", exec: 1)
end
if check
ls(unixpath(path)+"/*").each do |p|
raise("rm_rf command failed on folder #{path}") unless allow.find { |regexp| p =~ /#{regexp}/i }
end
end
return true
end
|
#service_config(service_name, param_name, param_value) ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/rcs-common/updater/client.rb', line 188
def service_config(service_name, param_name, param_value)
param_name = param_name.to_s
cmd = ""
if %w[type start error binPath group tag depend obj DisplayName password].include?(param_name)
cmd = "sc config #{service_name} #{param_name}= \"#{param_value}\""
elsif %[description].include?(param_name)
cmd = "sc description #{service_name} \"#{param_value}\""
else
raise "Invalid parameter #{param_name}"
end
return localhost? ? local_command(cmd) : request(cmd, exec: 1)
end
|
#service_exists?(name) ⇒ Boolean
146
147
148
|
# File 'lib/rcs-common/updater/client.rb', line 146
def service_exists?(name)
!!execute("SC QUERY #{name}")
end
|
#service_failure(service_name, reset = 0, action1 = "restart/60000", action2 = "restart/60000", action3 = "restart/60000") ⇒ Object
203
204
205
206
|
# File 'lib/rcs-common/updater/client.rb', line 203
def service_failure(service_name, reset = 0, action1 = "restart/60000", action2 = "restart/60000", action3 = "restart/60000")
cmd = "sc failure #{service_name} reset= #{reset.to_i} actions= "+[action1, action2, action3].compact.join("/")
return localhost? ? local_command(cmd) : request(cmd, exec: 1)
end
|
#start(payload) ⇒ Object
Also known as:
detached
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/rcs-common/updater/client.rb', line 113
def start(payload)
path = store(payload+"\nexit", filename: 'start.bat')
begin
if localhost?
resp = local_command("start #{path}", 'spawn' => 1)
else
resp = request("start #{path}", {spawn: 1}, retry_count = 0)
end
rescue Exception => ex
trace :error, "#start: #{ex.message}"
end
return nil
end
|
#start_service(name) ⇒ Object
136
137
138
139
|
# File 'lib/rcs-common/updater/client.rb', line 136
def start_service(name)
cmd = "NET START #{name}"
return localhost? ? local_command(cmd) : request(cmd, exec: 1)
end
|
#stop_service(name) ⇒ Object
141
142
143
144
|
# File 'lib/rcs-common/updater/client.rb', line 141
def stop_service(name)
cmd = "NET STOP #{name}"
return localhost? ? local_command(cmd) : request(cmd, exec: 1)
end
|
#store(payload, filename: nil) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/rcs-common/updater/client.rb', line 101
def store(payload, filename: nil)
raise("Missing filename") unless filename
if localhost?
path = unixpath("#{tmpdir}/#{filename}")
File.open(path, 'wb') { |f| f.write(payload) }
return path
else
return request(payload, filename: filename, store: 1)[:path]
end
end
|
#store_file(path, remote_path = nil) ⇒ Object
95
96
97
98
99
|
# File 'lib/rcs-common/updater/client.rb', line 95
def store_file(path, remote_path = nil)
path = unixpath(File.expand_path(path))
payload = File.open(path, 'rb') { |f| f.read }
return store(payload, filename: File.basename(path))
end
|
#unixpath(path) ⇒ Object
340
341
342
343
|
# File 'lib/rcs-common/updater/client.rb', line 340
def unixpath(path)
path = "#{self.pwd}/#{path}" if self.pwd and path !~ /\A[a-z]\:/i
path.gsub("\\", "/")
end
|
#winpath(path) ⇒ Object
335
336
337
338
|
# File 'lib/rcs-common/updater/client.rb', line 335
def winpath(path)
path = "#{self.pwd}\\#{path}" if self.pwd and path !~ /\A[a-z]\:/i
path.gsub("/", "\\")
end
|
#write_file(path, content) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/rcs-common/updater/client.rb', line 161
def write_file(path, content)
if localhost?
File.open(unixpath(path), 'wb') { |file| file.write(content) }
else
end
end
|