agama/service/bin/agamactl
José Iván López González c947a6340a Move ENV assignment
2026-06-23 12:59:56 +01:00

101 lines
3.4 KiB
Ruby
Executable file

#!/usr/bin/env ruby
# frozen_string_literal: true
# Copyright (c) [2022-2026] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
# Mock some YaST modules like Package.
agama_y2dir = File.expand_path("../lib/agama/y2dir", __dir__)
ENV["Y2DIR"] = [ENV.fetch("Y2DIR", nil), agama_y2dir].compact.join(":")
# Set the PATH to a known value
ENV["PATH"] = "/sbin:/usr/sbin:/usr/bin:/bin"
require "rubygems"
# find Gemfile when D-Bus activates a git checkout
Dir.chdir(__dir__) do
require "bundler/setup"
end
require "agama/dbus/service_runner"
require "agama/dbus/server_manager"
# Set up a {Logger} suitable either for terminal (started manually)
# or syslog (D-Bus activated)
# @return [Logger]
def logger_for(name)
if $stdout.tty?
formatter = Logger::Formatter.new # the default
else
# going via syslog which will provide time and progname already
formatter = ->(severity, _time, _progname, msg) { "[#{severity}]: #{name}: #{msg}\n" }
$stdout.sync = true
end
Logger.new($stdout, progname: name.to_s, formatter: formatter)
end
# Runs the service with the given name
#
# @param name [Symbol] Service name
# @see ORDERED_SERVICES
def start_service(name)
logger = logger_for(name)
service_runner = Agama::DBus::ServiceRunner.new(name, logger: logger)
service_runner.run
end
ORDERED_SERVICES = [:storage, :users, :manager].freeze
dbus_server_manager = Agama::DBus::ServerManager.new
if ARGV.empty?
puts "ERROR: Using 'agamactl' to start all services no longer works."
puts "NOTE: It had race conditions all along and now there's a Rust service it can't reach too."
puts "NOTE: Use `systemctl start agama.service` instead"
puts "NOTE: which is setup by a) RPMs b) ./setup-services.sh"
exit 1
elsif ["-h", "--help"].include?(ARGV[0])
me = $PROGRAM_NAME
puts "Usage:"
puts "#{me} -h|--help"
puts "#{me} -s|--status"
puts "#{me} -k|--kill"
puts "#{me} -d|--daemon"
puts "#{me} #{ORDERED_SERVICES.sort.join("|")}"
elsif ["-s", "--status"].include?(ARGV[0])
# The .[a-z] ensures matching the forked processes
# which can be distinguished by having a non-option argument
system "pgrep -fl bin/agamactl.[a-z]"
# busctl is prettier but will find nothing until the service files are installed,
# hence the pgrep above
system "busctl --address #{dbus_server_manager.address} | grep -E '^NAME|Agama'"
elsif ["-k", "--kill"].include?(ARGV[0])
system "pkill -f bin/agamactl.[a-z]"
# stop the d-bus daemon
dbus_server_manager.stop_server
elsif ["-d", "--daemon"].include?(ARGV[0])
# start the D-Bus server
dbus_server_manager.find_or_start_server
else
dbus_server_manager.find_or_start_server
name = ARGV[0]
start_service(name.to_sym)
end