Improved `service del`, `service stop`, grooming.

master
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/*" require "./service/*"
parser = uninitialized OptionParser
args = [] of String args = [] of String
parser = OptionParser.parse do |parser| force = false
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
alias Command = Proc(Array(String), Nil) alias Command = Proc(Array(String), Nil)
alias CommandTuple = Tuple(String, String, Command) alias CommandTuple = Tuple(String, String, Command)
@ -95,9 +86,37 @@ commands.push "del", "Removes a service from an environment." do |args|
exit 1 exit 1
end end
rvalue = 0
args.each do |id| 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 end
exit rvalue
end end
commands.push "start", "Starts a service." do commands.push "start", "Starts a service." do
@ -133,11 +152,9 @@ commands.push "stop", "Stops a running service." do |args|
end end
services.each do |service| services.each do |service|
# FIXME: Build revdep tree and stop services started as dependencies?
next if ! service.running? PID_DIRECTORY next if ! service.running? PID_DIRECTORY
# FIXME: Should we remove duplicate services from the
# tree once flattened? service.reverse_dependencies.reverse.each do |service|
service.reverse_dependency_tree.flatten.reverse.each do |service|
next if ! service.running? PID_DIRECTORY next if ! service.running? PID_DIRECTORY
puts "stopping #{service.to_s}" puts "stopping #{service.to_s}"
@ -228,6 +245,25 @@ commands.push "help", "Prints this help message." do
puts commands.to_s puts commands.to_s
end 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]?) command = commands.find &.[0].==(args[0]?)
if command.nil? if command.nil?
STDERR << parser << "\n" STDERR << parser << "\n"

View File

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