-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcli.rb
More file actions
130 lines (111 loc) · 2.72 KB
/
cli.rb
File metadata and controls
130 lines (111 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
121
122
123
124
125
126
127
128
129
130
require 'ipaddr'
require 'optparse'
module DeployAgent
class CLI
def dispatch(arguments)
methods = self.public_methods(false).delete_if { |n| n == :dispatch }.sort
@options = {}
OptionParser.new do |opts|
opts.on('-v', '--verbose', 'Log extra debug information') do
@options[:verbose] = true
end
end.parse!(arguments)
if arguments[0] && methods.include?(arguments[0].to_sym)
public_send(arguments[0])
else
puts "Usage: deploy-agent [#{methods.join('|')}]"
end
end
def setup
ConfigurationGenerator.new.configure
end
def restart
stop
while(is_running?)
sleep 0.5
end
start
end
def start
if is_running?
puts "Deploy agent already running. Process ID #{pid_from_file}"
Process.exit(1)
else
ensure_configured
pid = fork do
$background = true
write_pid
run
end
puts "Deploy agent started. Process ID #{pid}"
Process.detach(pid)
end
end
def stop
if is_running?
pid = pid_from_file
Process.kill('TERM', pid)
puts "Deploy agent stopped. Process ID #{pid}"
else
puts "Deploy agent is not running"
Process.exit(1)
end
end
def status
if is_running?
puts "Deploy agent is running. PID #{pid_from_file}"
else
puts "Deploy agent is not running."
Process.exit(1)
end
end
def run
ensure_configured
Agent.new(@options).run
end
def accesslist
puts "Access list:"
DeployAgent.allowed_destinations.each do |destination|
begin
IPAddr.new(destination)
puts " - " + destination
rescue IPAddr::InvalidAddressError
puts " - " + destination + " (INVALID)"
end
end
puts
puts "To edit the list of allowed servers, please modify " + ACCESS_PATH
end
def version
puts DeployAgent::VERSION
end
private
def ensure_configured
unless File.file?(CERTIFICATE_PATH) && File.file?(ACCESS_PATH)
puts 'Deploy agent is not configured. Please run "deploy-agent setup" first.'
Process.exit(1)
end
end
def is_running?
if pid = pid_from_file
Process.kill(0, pid)
true
else
false
end
rescue Errno::ESRCH
false
rescue Errno::EPERM
true
end
def pid_from_file
File.read(PID_PATH).to_i
rescue Errno::ENOENT
nil
end
def write_pid
File.open(PID_PATH, 'w') { |f| f.write Process.pid.to_s }
at_exit { File.delete(PID_PATH) if File.exist?(PID_PATH) }
end
end
end