Class: Yast::ServiceClass
- Inherits:
-
Module
- Object
- Module
- Yast::ServiceClass
- Includes:
- Logger
- Defined in:
- library/systemd/src/modules/Service.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#Active(service_name) ⇒ Object
(also: #active?)
Check if service is active/running.
-
#Adjust(name, action) ⇒ Boolean
deprecated
Deprecated.
Use the specific methods:
Enable
orDisable
-
#call(command_name, service_name) ⇒ Boolean
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.
-
#checkExists(name) ⇒ Object
deprecated
Deprecated.
Use Yast2::Systemd::Service.find
-
#Disable(service_name) ⇒ Object
(also: #disable)
Disable service Logs error with output from systemctl if the command fails.
-
#Enable(service_name) ⇒ Object
(also: #enable)
Enable service Logs error with output from systemctl if the command fails.
-
#Enabled(name) ⇒ Object
(also: #enabled?)
Check if service is enabled (in any runlevel).
-
#EnabledServices(_runlevel) ⇒ Array<String>
deprecated
Deprecated.
Runlevel features are not supported by systemd
-
#Error ⇒ Object
Error Message.
-
#Find(services) ⇒ String
deprecated
Deprecated.
Use Yast2::Systemd::Service.find instead
-
#Finetune(name, list) ⇒ Object
deprecated
Deprecated.
Use
Enable
orDisable
instead -
#FullInfo(name) ⇒ Object
deprecated
Deprecated.
Not supported by systemd
-
#GetServiceId(name) ⇒ resolved
deprecated
Deprecated.
Use Yast2::Systemd::Service.find('service_name').name
-
#GetUnitId(unit) ⇒ resolved
deprecated
Deprecated.
Use Yast2::Systemd::Service.find('service_name').id
-
#Info(name) ⇒ Object
deprecated
Deprecated.
Not supported by systemd
-
#initialize ⇒ ServiceClass
constructor
A new instance of ServiceClass.
-
#Reload(service_name) ⇒ Object
(also: #reload)
Reload service Logs error with output from systemctl if the command fails.
-
#Restart(service_name) ⇒ Object
(also: #restart)
Restart service Logs error with output from systemctl if the command fails.
-
#RunInitScript(name, param) ⇒ Fixnum
deprecated
Deprecated.
Use specific method for service configuration
-
#RunInitScriptOutput(name, param) ⇒ Object
deprecated
Deprecated.
Use a specific method instread
-
#RunInitScriptWithTimeOut(name, param) ⇒ Fixnum
deprecated
Deprecated.
Use specific unit methods for service configuration
-
#serviceDisable(name, _force) ⇒ Object
deprecated
Deprecated.
Use
Disable
instead -
#Start(service_name) ⇒ Object
(also: #start)
Start service Logs error with output from systemctl if the command fails.
-
#Status(name) ⇒ Object
deprecated
Deprecated.
Use
Active
instead -
#Stop(service_name) ⇒ Object
(also: #stop)
Stop service Logs error with output from systemctl if the command fails.
Constructor Details
#initialize ⇒ ServiceClass
Returns a new instance of ServiceClass.
48 49 50 51 52 53 |
# File 'library/systemd/src/modules/Service.rb', line 48 def initialize super textdomain "base" @error = "" end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
46 47 48 |
# File 'library/systemd/src/modules/Service.rb', line 46 def error @error end |
Instance Method Details
#Active(service_name) ⇒ Object Also known as: active?
Check if service is active/running
88 89 90 91 |
# File 'library/systemd/src/modules/Service.rb', line 88 def Active(service_name) service = Yast2::Systemd::Service.find(service_name) !!(service && service.active?) end |
#Adjust(name, action) ⇒ Boolean
Use the specific methods: Enable
or Disable
Adjusts runlevels in which the service runs.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'library/systemd/src/modules/Service.rb', line 317 def Adjust(name, action) deprecate("use `enable` or `disable` instead") service = Yast2::Systemd::Service.find(name) return failure(:not_found, name) unless service case action when "disable" service.disable when "enable", "default" service.enable else log.error "Unknown action '#{action}' for service '#{name}'" false end end |
#call(command_name, service_name) ⇒ Boolean
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
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'library/systemd/src/modules/Service.rb', line 59 def call(command_name, service_name) service = Yast2::Systemd::Service.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 |
#checkExists(name) ⇒ Object
Use Yast2::Systemd::Service.find
Check that a service exists. If not, set error_msg.
213 214 215 216 217 218 219 |
# File 'library/systemd/src/modules/Service.rb', line 213 def checkExists(name) deprecate("use `Yast2::Systemd::Service.find` instead") return failure(:not_found, name) unless Yast2::Systemd::Service.find(name) true end |
#Disable(service_name) ⇒ Object Also known as: disable
Disable service Logs error with output from systemctl if the command fails
127 128 129 130 131 132 133 134 |
# File 'library/systemd/src/modules/Service.rb', line 127 def Disable(service_name) log.info "Disabling service '#{service_name}'" service = Yast2::Systemd::Service.find(service_name) return failure(:not_found, service_name) unless service return failure(:disable, service_name, service.error) unless service.disable true end |
#Enable(service_name) ⇒ Object Also known as: enable
Enable service Logs error with output from systemctl if the command fails
112 113 114 115 116 117 118 119 |
# File 'library/systemd/src/modules/Service.rb', line 112 def Enable(service_name) log.info "Enabling service '#{service_name}'" service = Yast2::Systemd::Service.find(service_name) return failure(:not_found, service_name) unless service return failure(:enable, service_name, service.error) unless service.enable true end |
#Enabled(name) ⇒ Object Also known as: enabled?
Check if service is enabled (in any runlevel)
Forwards to chkconfig -l which decides between init and systemd
101 102 103 104 |
# File 'library/systemd/src/modules/Service.rb', line 101 def Enabled(name) service = Yast2::Systemd::Service.find(name) !!(service && service.enabled?) end |
#EnabledServices(_runlevel) ⇒ Array<String>
Runlevel features are not supported by systemd
Get list of enabled services in a runlevel
427 428 429 430 431 |
# File 'library/systemd/src/modules/Service.rb', line 427 def EnabledServices(_runlevel) deprecate("use `Yast2::Systemd::Service.all.select(&:enabled?)`") Yast2::Systemd::Service.all.select(&:enabled?).map(&:name) end |
#Error ⇒ Object
Error Message
If a Service function returns an error, this function would return an error message, possibly containing newlines.
204 205 206 |
# File 'library/systemd/src/modules/Service.rb', line 204 def Error error end |
#Find(services) ⇒ String
Use Yast2::Systemd::Service.find instead
Return the first of the list of services which is available (has init script) or "" if none is.
438 439 440 441 442 |
# File 'library/systemd/src/modules/Service.rb', line 438 def Find(services) deprecate("use `Yast2::Systemd::Service.find` instead") services.find { |service_name| Yast2::Systemd::Service.find(service_name) } end |
#Finetune(name, list) ⇒ Object
Use Enable
or Disable
instead
Set service to run in selected runlevels. Obsoleted - enables or disables the given service depending on the list of runlevels to start. If any runlevel is present, service is enabled, otherwise disabled.
343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'library/systemd/src/modules/Service.rb', line 343 def Finetune(name, list) deprecate("use `enable` or `disable` instead") service = Yast2::Systemd::Service.find(name) return failure(:not_found, name) unless service if list.empty? service.disable else log.warn "Cannot enable service '#{name}' in selected runlevels, enabling for all" service.enable end end |
#FullInfo(name) ⇒ Object
Not supported by systemd
Get service info and find out whether service is running.
287 288 289 290 291 292 293 |
# File 'library/systemd/src/modules/Service.rb', line 287 def FullInfo(name) deprecate("not supported by systemd") return {} if !checkExists(name) Builtins.add(Info(name), "started", Status(name)) end |
#GetServiceId(name) ⇒ resolved
Use Yast2::Systemd::Service.find('service_name').name
Get the name from a systemd service unit id without the .service suffix
259 260 261 262 263 264 265 266 |
# File 'library/systemd/src/modules/Service.rb', line 259 def GetServiceId(name) deprecate("use Yast2::Systemd::Service.find('service_name').name") unit = Yast2::Systemd::Service.find(name) return nil unless unit unit.name end |
#GetUnitId(unit) ⇒ resolved
Use Yast2::Systemd::Service.find('service_name').id
Get complete systemd unit id
246 247 248 249 250 251 252 253 |
# File 'library/systemd/src/modules/Service.rb', line 246 def GetUnitId(unit) deprecate("use Yast2::Systemd::Service.find('service_name').id") unit = Yast2::Systemd::Service.find(unit) return nil unless unit unit.id end |
#Info(name) ⇒ Object
Not supported by systemd
Get service info without peeking if service runs.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'library/systemd/src/modules/Service.rb', line 225 def Info(name) deprecate("not supported by systemd") unit = Yast2::Systemd::Service.find(name) return {} unless unit read = Convert.to_map(SCR.Read(path(".init.scripts.runlevel"), name)) detail = Ops.get_map(read, name, {}) read = Convert.to_map(SCR.Read(path(".init.scripts.comment"), name)) service = Ops.get_map(read, name, {}) Builtins.add( Builtins.add(service, "start", Ops.get_list(detail, "start", [])), "stop", Ops.get_list(detail, "stop", []) ) end |
#Reload(service_name) ⇒ Object Also known as: reload
Reload service Logs error with output from systemctl if the command fails
172 173 174 175 176 177 178 179 |
# File 'library/systemd/src/modules/Service.rb', line 172 def Reload(service_name) log.info "Reloading service '#{service_name}'" service = Yast2::Systemd::Service.find(service_name) return failure(:not_found, service_name) unless service return failure(:reload, service_name, service.error) unless service.reload true end |
#Restart(service_name) ⇒ Object Also known as: restart
Restart service Logs error with output from systemctl if the command fails
157 158 159 160 161 162 163 164 |
# File 'library/systemd/src/modules/Service.rb', line 157 def Restart(service_name) log.info "Restarting service '#{service_name}'" service = Yast2::Systemd::Service.find(service_name) return failure(:not_found, service_name) unless service return failure(:restart, service_name, service.error) unless service.restart true end |
#RunInitScript(name, param) ⇒ Fixnum
Use specific method for service configuration
Run init script.
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'library/systemd/src/modules/Service.rb', line 362 def RunInitScript(name, param) deprecate("use the specific unit command instead") service = Yast2::Systemd::Service.find(name) if !service failure(:not_found, name) return -1 end result = case param when "start", "stop", "status", "reload", "restart", "enable", "disable" service.send(param) when "try-restart" service.try_restart when "reload-or-restart" service.reload_or_restart when "reload-or-try-restart" service.reload_or_try_restart else log.error "Unknown action '#{param}' for service '#{name}'" false end result ? 0 : -1 end |
#RunInitScriptOutput(name, param) ⇒ Object
Use a specific method instread
Run init script and also return its output (stdout and stderr merged).
409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'library/systemd/src/modules/Service.rb', line 409 def RunInitScriptOutput(name, param) deprecate("use `start` or `stop` instead") service = Yast2::Systemd::Service.find(name) if service success = service.send(param) self.error = service.error else failure(:not_found, name) success = false end { "stdout" => "", "stderr" => error, "exit" => success ? 0 : 1 } end |
#RunInitScriptWithTimeOut(name, param) ⇒ Fixnum
Use specific unit methods for service configuration
Run init script with a time-out.
393 394 395 396 397 398 399 400 401 402 |
# File 'library/systemd/src/modules/Service.rb', line 393 def RunInitScriptWithTimeOut(name, param) deprecate("use `start` or `stop` instead") service = Yast2::Systemd::Service.find(name) if !service failure(:not_found, name) return 1 end service.send(param) ? 0 : 1 end |
#serviceDisable(name, _force) ⇒ Object
Use Disable
instead
Disables a given service and records errors. Does not check if it exists.
302 303 304 305 306 307 |
# File 'library/systemd/src/modules/Service.rb', line 302 def serviceDisable(name, _force) deprecate("use `disable` instead") unit = Yast2::Systemd::Service.find(name) !!(unit && unit.disable) end |
#Start(service_name) ⇒ Object Also known as: start
Start service Logs error with output from systemctl if the command fails
142 143 144 145 146 147 148 149 |
# File 'library/systemd/src/modules/Service.rb', line 142 def Start(service_name) log.info "Starting service '#{service_name}'" service = Yast2::Systemd::Service.find(service_name) return failure(:not_found, service_name) unless service return failure(:start, service_name, service.error) unless service.start true end |
#Status(name) ⇒ Object
Use Active
instead
Get service status. The status is the output from "service status". It should conform to LSB. 0 means the service is running.
274 275 276 277 278 279 280 281 |
# File 'library/systemd/src/modules/Service.rb', line 274 def Status(name) deprecate("use `active?` instead") unit = Yast2::Systemd::Service.find(name) failure(:not_found, name) unless unit unit&.active? ? 0 : -1 end |
#Stop(service_name) ⇒ Object Also known as: stop
Stop service Logs error with output from systemctl if the command fails
187 188 189 190 191 192 193 194 |
# File 'library/systemd/src/modules/Service.rb', line 187 def Stop(service_name) log.info "Stopping service '#{service_name}'" service = Yast2::Systemd::Service.find(service_name) return failure(:not_found, service_name) unless service return failure(:stop, service_name, service.error) unless service.stop true end |