Improved service del, service stop, grooming.

This commit is contained in:
Luka Vandervelden 2019-10-25 18:40:27 +02:00
parent 21e6943868
commit 1f9264f97b
2 changed files with 67 additions and 24 deletions

View File

@ -9,20 +9,11 @@ require "./config.cr"
require "./service/*"
parser = uninitialized OptionParser
args = [] of String
parser = OptionParser.parse do |parser|
parser.banner = "usage: service <command> [options]\n" +
"options:\n"
parser.on "-h", "--help", "Prints this help message." do
puts parser
exit 0
end
parser.unknown_args do |x|
args = x
end
end
force = false
alias Command = Proc(Array(String), Nil)
alias CommandTuple = Tuple(String, String, Command)
@ -95,9 +86,37 @@ commands.push "del", "Removes a service from an environment." do |args|
exit 1
end
rvalue = 0
args.each do |id|
Service.get_by_id(id).try &.remove RC_DIRECTORY
service = Service.get_by_id(id)
if service.nil?
STDERR.puts "#{id}: no such service"
rvalue = 1
next
end
revdeps = service.reverse_dependencies
if revdeps.size > 1 && ! force
STDERR.puts "#{id}: has reverse dependencies, use -f to force"
rvalue = 2
next
end
revdeps.reverse.each do |service|
next if ! service.running? PID_DIRECTORY
puts "stopping #{service.to_s}"
service.stop PID_DIRECTORY
end
revdeps.reverse.each do |service|
service.remove RC_DIRECTORY
end
end
exit rvalue
end
commands.push "start", "Starts a service." do
@ -133,11 +152,9 @@ commands.push "stop", "Stops a running service." do |args|
end
services.each do |service|
# FIXME: Build revdep tree and stop services started as dependencies?
next if ! service.running? PID_DIRECTORY
# FIXME: Should we remove duplicate services from the
# tree once flattened?
service.reverse_dependency_tree.flatten.reverse.each do |service|
service.reverse_dependencies.reverse.each do |service|
next if ! service.running? PID_DIRECTORY
puts "stopping #{service.to_s}"
@ -228,6 +245,25 @@ commands.push "help", "Prints this help message." do
puts commands.to_s
end
parser = OptionParser.parse do |cli|
cli.banner = "usage: service <command> [options]\n" +
"options:\n"
cli.on "-h", "--help", "Prints this help message." do
puts cli
puts
puts commands.to_s
exit 0
end
cli.on "-f", "--force", "Ignores warnings and executes dangerous operations." do
force = true
end
cli.unknown_args do |x|
args = x
end
end
command = commands.find &.[0].==(args[0]?)
if command.nil?
STDERR << parser << "\n"

View File

@ -388,18 +388,25 @@ class Service
tree
end
def reverse_dependency_tree
tree = [self] of ServiceTree
def reverse_dependencies
rdeps = [self]
@@all.each do |service|
service.providers.any? do |token, id|
if self.is_id?(id) && ! tree.any? service
tree << service
i = 0
while i < rdeps.size
item = rdeps[i]
@@all.each do |service|
service.providers.any? do |token, id|
if item.is_id?(id) && ! rdeps.any? service
rdeps << service
end
end
end
i += 1
end
tree
rdeps
end
def get_default_provider(token) : String?