CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/util/service_helper.rb
Views: 11780
1
require 'open3'
2
3
module Msf
4
module Util
5
class ServiceHelper
6
def self.run_cmd(cmd, input: nil, env: {}, debug: false)
7
exitstatus = 0
8
err = out = ""
9
10
$stdout.puts "run_cmd: cmd=#{cmd}, input=#{input}, env=#{env}" if debug
11
12
Open3.popen3(env, cmd) do |stdin, stdout, stderr, wait_thr|
13
stdin.puts(input) if input
14
if debug
15
err = stderr.read
16
out = stdout.read
17
end
18
exitstatus = wait_thr.value.exitstatus
19
end
20
21
if exitstatus != 0
22
if debug
23
$stdout.puts "'#{cmd}' returned #{exitstatus}"
24
$stdout.puts out
25
$stdout.puts err
26
end
27
end
28
29
exitstatus
30
end
31
32
def self.process_active?(pid)
33
begin
34
Process.kill(0, pid)
35
true
36
rescue Errno::ESRCH
37
false
38
end
39
end
40
41
def self.tail(file)
42
begin
43
File.readlines(file).last.to_s.strip
44
rescue
45
nil
46
end
47
end
48
49
def self.thin_cmd(conf:, address:, port:, ssl:, ssl_key:, ssl_cert:, ssl_disable_verify:,
50
env: 'production', daemonize:, log:, pid:, tag:)
51
server_opts = "--rackup #{conf} --address #{address} --port #{port}"
52
ssl_opts = ssl ? "--ssl --ssl-key-file #{ssl_key} --ssl-cert-file #{ssl_cert}" : ''
53
ssl_opts << ' --ssl-disable-verify' if ssl_disable_verify
54
adapter_opts = "--environment #{env}"
55
daemon_opts = daemonize ? "--daemonize --log #{log} --pid #{pid} --tag #{tag}" : ''
56
all_opts = [server_opts, ssl_opts, adapter_opts, daemon_opts].reject(&:empty?).join(' ')
57
58
"thin #{all_opts}"
59
end
60
end
61
end
62
end
63
64