Skip to content

Commit

Permalink
Merge pull request #242 from vmoravec/fix-restart
Browse files Browse the repository at this point in the history
Add Service.call method and improve restarting of services
  • Loading branch information
Vladimir Moravec committed May 30, 2014
2 parents fe5b7f7 + a44ef1c commit 33eb2a9
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
35 changes: 35 additions & 0 deletions library/runlevel/src/modules/Service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ def initialize
@error = ""
end

# Send whatever systemd command you need to call for a specific service
# If the command fails, log entry with output from systemctl is created in y2log
# @param [String,String] Command name and service name
# @return [Boolean] Result of the action, true means success
def call command_name, service_name
service = SystemdService.find(service_name)
return failure(:not_found, service_name) unless service

systemd_command =
case command_name
when 'show' then :show
when 'status' then :status
when 'start' then :start
when 'stop' then :stop
when 'enable' then :enable
when 'disable' then :disable
when 'restart' then :restart
when 'reload' then :reload
when 'try-restart' then :try_restart
when 'reload-or-restart' then :reload_or_restart
when 'reload-or-try-restart' then :reload_or_try_restart
else
raise "Command '#{command_name}' not supported"
end
result = service.send(systemd_command)
failure(command_name, service_name, service.error) unless result
result
end

# Check if service is active/running
#
# @param [String] name service name
Expand All @@ -75,6 +104,7 @@ def Enabled name
alias_method :enabled?, :Enabled

# Enable service
# Logs error with output from systemctl if the command fails
# @param [String] service service to be enabled
# @return true if operation is successful
def Enable service_name
Expand All @@ -88,6 +118,7 @@ def Enable service_name
alias_method :enable, :Enable

# Disable service
# Logs error with output from systemctl if the command fails
# @param [String] service service to be disabled
# @return true if operation is successful
def Disable service_name
Expand All @@ -101,6 +132,7 @@ def Disable service_name
alias_method :disable, :Disable

# Start service
# Logs error with output from systemctl if the command fails
# @param [String] service service to be started
# @return true if operation is successful
def Start service_name
Expand All @@ -114,6 +146,7 @@ def Start service_name
alias_method :start, :Start

# Restart service
# Logs error with output from systemctl if the command fails
# @param [String] service service to be restarted
# @return true if operation is successful
def Restart service_name
Expand All @@ -127,6 +160,7 @@ def Restart service_name
alias_method :restart, :Restart

# Reload service
# Logs error with output from systemctl if the command fails
# @param [String] service service to be reloaded
# @return true if operation is successful
def Reload service_name
Expand All @@ -140,6 +174,7 @@ def Reload service_name
alias_method :reload, :Reload

# Stop service
# Logs error with output from systemctl if the command fails
# @param [String] service service to be stopped
# @return true if operation is successful
def Stop service_name
Expand Down
22 changes: 22 additions & 0 deletions library/runlevel/test/service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ def stub_service_with method, result
stub_services
end

describe ".call" do
it "executes the command for the specified service" do
expect(Service.call('reload', 'sshd')).to be_true
end

it "returns false if the service has not been found" do
stub_services(:service=>'unknown')
expect(Service.call('restart', 'unknown')).to be_false
end

it "raises error if the command is not recognized" do
expect { Service.call('make-coffee', 'sshd') }.to raise_error
end

it "returns the result of the original result of the command call" do
expect(Service.call('status', 'sshd')).to be_kind_of(String)

stub_service_with(:"try_restart", false)
expect(Service.call('try-restart', "sshd")).to be_false
end
end

describe ".Active" do
it "returns true if a service is active" do
expect(Service.Active('sshd')).to be_true
Expand Down
3 changes: 3 additions & 0 deletions library/systemd/src/modules/systemd_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ def stop
end

def restart
# Delegate to SystemdUnit#restart if not within installation
return super unless installation_system?

stop
sleep(1)
start
Expand Down
6 changes: 6 additions & 0 deletions package/yast2.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Fri May 30 11:06:47 UTC 2014 - vmoravec@suse.com

- Add Service.call method to make available all systemctl commands
- 3.1.74

-------------------------------------------------------------------
Fri May 30 08:55:45 UTC 2014 - lslezak@suse.cz

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: 3.1.73
Version: 3.1.74
Release: 0
URL: https://github.com/yast/yast-yast2

Expand Down

0 comments on commit 33eb2a9

Please sign in to comment.