Skip to content

Commit

Permalink
Merge pull request #1063 from yast/reduce-exported-profile
Browse files Browse the repository at this point in the history
Reduce exported profile
  • Loading branch information
imobachgs committed Jun 17, 2020
2 parents 5902d44 + 813a315 commit 86774ec
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 42 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Metrics/ParameterLists:
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 877
Exclude:
- "library/*/test/**/*_test.rb"

Metrics/PerceivedComplexity:
Max: 65
Expand Down
8 changes: 4 additions & 4 deletions library/systemd/doc/services_and_sockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cups.socket).

When a service is configured by using YaST (e.g., with the Services Manager), all units related to
each service must be taken into account. For example, when a service is stopped, the socket
associated to such service should be also stopped. Otherwise, the sevice could be automatically
associated to such service should be also stopped. Otherwise, the service could be automatically
activated again via its socket.

This file extends the {Yast2::SystemService} class documentation describing how it works with the
Expand All @@ -24,8 +24,8 @@ to:
on demand or manually)
* apply all changes in the "real system"

One goal in this class is to offer an agnostic API. At this moment it uses Systemd in low levels
layers, but in future this could change and the API should remain as much as possible.
One of the goals of this class is to offer an agnostic API. At this moment it uses Systemd in low
levels layers, but in future this could change and the API should remain as much as possible.

## Actions over Systemd units

Expand All @@ -41,7 +41,7 @@ socket (if any) when we try to start, stop, restart or reload a `SystemService`.

## Detailed actions

Here each `SystemService` action is decribed in a more detailed way.
Here each `SystemService` action is described in a more detailed way.

First of all, we are going to consider that a `SystemService` is stopped/running as follows:

Expand Down
45 changes: 37 additions & 8 deletions library/systemd/src/lib/yast2/system_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ def substate
def current_start_mode
return @current_start_mode unless @current_start_mode.nil?

@current_start_mode =
if service.enabled?
:on_boot
elsif socket&.enabled?
:on_demand
else
:manual
end
@current_start_mode = start_mode_from(service, socket, :enabled?)
end

# Determines the default start mode for this service
#
# @return [Symbol] :on_boot, :on_demand, :manual
def default_start_mode
return @default_start_mode unless @default_start_mode.nil?

@default_start_mode = start_mode_from(service, socket, :preset_enabled?)
end

# Whether the service is currently active in the system
Expand Down Expand Up @@ -257,6 +259,14 @@ def start_mode
new_value_for(:start_mode) || current_start_mode
end

# Determines whether the start mode has been changed from system's default
#
# @see #start_mode
# @see #default_start_mode
def default_start_mode?
default_start_mode == start_mode
end

# Sets the service start mode
#
# See {#start_modes} to find out the supported modes for a given service (usually :on_boot,
Expand Down Expand Up @@ -601,5 +611,24 @@ def disable_service

service.disable
end

# Helper method to calculate the start mode using a given method
#
# This method offers a mechanism to get the current and the
# default start_modes.
#
# @param service [Service] Systemd service
# @param socket [Socket,nil] Systemd socket
# @param enabled_method [Symbol] Method to use to determine whether service and socket
# are enabled or not.
def start_mode_from(service, socket, enabled_method)
if service.public_send(enabled_method)
:on_boot
elsif socket&.public_send(enabled_method)
:on_demand
else
:manual
end
end
end
end
3 changes: 2 additions & 1 deletion library/systemd/src/lib/yast2/systemd/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Unit
:active_state,
:sub_state,
:can_reload?,
:not_found?
:not_found?,
:preset_enabled?
].freeze

private_constant :FORWARDED_METHODS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Yast2
module Systemd
# A replacement for {Yast2::Systemd::UnitPropertie} during installation
# A replacement for {Yast2::Systemd::UnitProperties} during installation
#
# Systemd `show` command (systemctl show) is not available during
# installation and will return error "Running in chroot, ignoring request."
Expand Down
19 changes: 10 additions & 9 deletions library/systemd/src/lib/yast2/systemd/unit_prop_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ module Systemd
class UnitPropMap < Hash
# @return [Yast2::Systemd::UniPropMap]
DEFAULT = UnitPropMap[{
id: "Id",
pid: "MainPID",
description: "Description",
load_state: "LoadState",
active_state: "ActiveState",
sub_state: "SubState",
unit_file_state: "UnitFileState",
path: "FragmentPath",
can_reload: "CanReload"
id: "Id",
pid: "MainPID",
description: "Description",
load_state: "LoadState",
active_state: "ActiveState",
sub_state: "SubState",
unit_file_state: "UnitFileState",
unit_file_preset: "UnitFilePreset",
path: "FragmentPath",
can_reload: "CanReload"
}].freeze
end
end
Expand Down
25 changes: 17 additions & 8 deletions library/systemd/src/lib/yast2/systemd/unit_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ def initialize(systemd_unit, property_text)
end

extract_properties
self[:active?] = ACTIVE_STATES.include?(active_state)
self[:running?] = sub_state == "running"
self[:loaded?] = load_state == "loaded"
self[:not_found?] = load_state == "not-found"
self[:static?] = unit_file_state == "static"
self[:enabled?] = read_enabled_state
self[:supported?] = SUPPORTED_STATES.include?(unit_file_state)
self[:can_reload?] = can_reload == "yes"
self[:active?] = ACTIVE_STATES.include?(active_state)
self[:running?] = sub_state == "running"
self[:loaded?] = load_state == "loaded"
self[:not_found?] = load_state == "not-found"
self[:static?] = unit_file_state == "static"
self[:preset_enabled?] = read_preset_enabled_state
self[:enabled?] = read_enabled_state
self[:supported?] = SUPPORTED_STATES.include?(unit_file_state)
self[:can_reload?] = can_reload == "yes"
end

private
Expand All @@ -76,6 +77,14 @@ def read_enabled_state
end
end

# Determines whether the unit should be enabled by default (preset)
# @return [Boolean] True if enabled by default, false otherwise.
def read_preset_enabled_state
return false unless unit_file_preset

state_name_enabled?(unit_file_preset.strip)
end

# Systemd service unit can have various states like enabled, enabled-runtime,
# linked, linked-runtime, masked, masked-runtime, static, disabled, invalid.
# We test for the return value 'enabled' and 'enabled-runtime' to consider
Expand Down
2 changes: 1 addition & 1 deletion library/systemd/test/data/cups_service_properties
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ LoadState=loaded
ActiveState=inactive
SubState=dead
FragmentPath=/usr/lib/systemd/system/cups.service
UnitFileState=disabled
UnitFileState=enabled
UnitFilePreset=enabled
StateChangeTimestamp=Mon 2018-06-25 13:29:46 CEST
StateChangeTimestampMonotonic=19149989334
Expand Down
1 change: 1 addition & 0 deletions library/systemd/test/data/sshd_service_properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ActiveState=active
SubState=running
FragmentPath=/usr/lib/systemd/system/sshd.service
UnitFileState=enabled
UnitFileState=disabled
InactiveExitTimestamp=St 2014-02-12 15:22:46 CET
InactiveExitTimestampMonotonic=22172625
ActiveEnterTimestamp=St 2014-02-12 15:22:46 CET
Expand Down
82 changes: 75 additions & 7 deletions library/systemd/test/yast2/system_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,31 @@

let(:service) do
instance_double(Yast2::Systemd::Service,
name: "cups",
enabled?: service_enabled,
active?: service_active,
not_found?: !service_found,
static?: service_static,
refresh!: true)
name: "cups",
enabled?: service_enabled,
active?: service_active,
not_found?: !service_found,
static?: service_static,
preset_enabled?: service_preset_enabled,
refresh!: true)
end

let(:service_enabled) { true }
let(:service_active) { true }
let(:service_found) { true }
let(:service_static) { false }
let(:service_preset_enabled) { true }

let(:service_socket) do
instance_double(Yast2::Systemd::Socket, enabled?: socket_enabled, active?: socket_active)
instance_double(
Yast2::Systemd::Socket, enabled?: socket_enabled, active?: socket_active,
preset_enabled?: socket_preset_enabled
)
end

let(:socket_enabled) { true }
let(:socket_active) { true }
let(:socket_preset_enabled) { true }

let(:socket) { service_socket }

Expand Down Expand Up @@ -364,6 +370,68 @@
end
end

describe "#default_start_mode" do
let(:socket_preset_enabled) { true }
let(:service_preset_enabled) { true }

context "when service should be enabled by default" do
it "returns :on_boot" do
expect(system_service.default_start_mode).to eq(:on_boot)
end
end

context "when the service should not be enabled" do
let(:service_preset_enabled) { false }

context "but the socket should be enabled" do
it "returns :on_demand" do
expect(system_service.default_start_mode).to eq(:on_demand)
end
end

context "and the socket should not be disabled" do
let(:socket_preset_enabled) { false }

it "returns :manual" do
expect(system_service.default_start_mode).to eq(:manual)
end
end

context "and there is not socket" do
let(:socket) { nil }

it "returns :manual" do
expect(system_service.default_start_mode).to eq(:manual)
end
end
end
end

describe "#default_start_mode?" do
let(:start_mode) { :on_boot }

before do
allow(system_service).to receive(:default_start_mode).and_return(:on_boot)
allow(system_service).to receive(:start_mode).and_return(start_mode)
end

context "when the start mode is the same than the default one" do
let(:start_mode) { :on_boot }

it "returns true" do
expect(system_service.default_start_mode?).to eq(true)
end
end

context "when the start mode is not the default one" do
let(:start_mode) { :manual }

it "returns false" do
expect(system_service.default_start_mode?).to eq(false)
end
end
end

describe "#support_start_on_demand?" do
context "when the service has an associated socket" do
let(:socket) { service_socket }
Expand Down
4 changes: 2 additions & 2 deletions library/systemd/test/yast2/systemd_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ module Yast2

before do
allow(Yast2::Systemctl).to receive(:execute).with(
"show --property=Id,MainPID,Description,LoadState,ActiveState,SubState,UnitFileState," \
"FragmentPath,CanReload apparmor.service cups.service"
"show --property=Id,MainPID,Description,LoadState,ActiveState,SubState," \
"UnitFileState,UnitFilePreset,FragmentPath,CanReload apparmor.service cups.service"
).and_return(systemctl_show)
allow(Systemd::Service).to receive(:find).with("apparmor", {}).and_return(apparmor_double)
allow(Systemd::Service).to receive(:find).with("cups", {}).and_return(cups_double)
Expand Down
33 changes: 33 additions & 0 deletions library/systemd/test/yast2/unit_properties_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,37 @@
end
end
end

describe "#preset_enabled?" do
subject(:properties) { described_class.new(service, nil) }
let(:service) { Yast2::Systemd::Service.build(service_name) }

before do
stub_services(service: service_name)
end

context "when the service should be enabled by default" do
let(:service_name) { "cups" }

it "returns true" do
expect(properties.preset_enabled?).to eq(true)
end
end

context "when the service should be disabled by default" do
let(:service_name) { "sshd" }

it "returns false" do
expect(properties.preset_enabled?).to eq(false)
end
end

context "when the service is static" do
let(:service_name) { "tftp" }

it "returns false" do
expect(properties.preset_enabled?).to eq(false)
end
end
end
end
7 changes: 7 additions & 0 deletions package/yast2.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Tue Jun 16 14:01:51 UTC 2020 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

- Add a method to determine the default start mode for a system
service (related to bsc#1172749).
- 4.3.7

-------------------------------------------------------------------
Tue Jun 9 16:00:19 UTC 2020 - David Diaz <dgonzalez@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/yast2.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2
Version: 4.3.6
Version: 4.3.7
Release: 0
Summary: YaST2 Main Package
License: GPL-2.0-only
Expand Down

0 comments on commit 86774ec

Please sign in to comment.