Class: Yast::ServiceClass
- Inherits:
-
Module
- Object
- Module
- Yast::ServiceClass
- Defined in:
- ../../src/modules/Service.rb
Instance Method Summary (collapse)
-
- (Boolean) Adjust(name, action)
Adjusts runlevels in which the service runs.
-
- (Object) checkExists(name)
Check that a service exists.
-
- (Object) Disable(service)
Disable service.
-
- (Object) Enable(service)
Enable service.
-
- (Object) Enabled(name)
Check if service is enabled (in any runlevel).
-
- (Array<String>) EnabledServices(runlevel)
Get list of enabled services in a runlevel.
-
- (Object) Error
Error Message.
-
- (String) Find(services)
Return the first of the list of services which is available (has init script) or “” if none is.
-
- (Object) Finetune(name, rl)
Set service to run in selected runlevels.
-
- (Object) FullInfo(name)
Get service info and find out whether service is running.
-
- (resolved) GetServiceId(name)
Get the name from a systemd service unit id without the .service suffix.
-
- (resolved) GetUnitId(unit)
Get complete systemd unit id.
-
- (Object) Info(name)
Get service info without peeking if service runs.
- - (Object) main
-
- (Object) Reload(service)
Reload service.
-
- (Object) Restart(service)
Restart service.
-
- (Fixnum) RunInitScript(name, param)
Run init script.
-
- (Object) RunInitScriptOutput(name, param)
Run init script and also return its output (stdout and stderr merged).
-
- (Fixnum) RunInitScriptWithTimeOut(name, param)
Run init script with a time-out.
-
- (Object) serviceDisable(name, force)
Disables a given service and records errors.
-
- (Object) Start(service)
Start service.
-
- (Object) Status(name)
Get service status.
-
- (Object) Stop(service)
Stop service.
Instance Method Details
- (Boolean) Adjust(name, action)
Adjusts runlevels in which the service runs.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File '../../src/modules/Service.rb', line 292 def Adjust(name, action) is_enabled = Enabled(name) if action == "disable" if is_enabled return serviceDisable(name, false) else return true end elsif action == "default" || action == "enable" if action == "enable" && is_enabled # nothing to do return true else cmd = Builtins.sformat("%1 enable %2.service", @invoker, name) ret = Convert.to_map(SCR.Execute(path(".target.bash_output"), cmd)) if Ops.get_integer(ret, "exit", -1) != 0 # Error message. # %1 is a name of an init script in /etc/init.d, # Enabling means that the service should start # in appropriate runlevels, eg. at boot time. # %2 is the stderr output of insserv(8) @error_msg = Builtins.sformat( _( "Unable to enable service %1\n" + "Command %2 returned\n" + "%3" ), name, cmd, Ops.get_string(ret, "stderr", "") ) Builtins.y2error(1, @error_msg) return false end end return true end # not translated @error_msg = Builtins.sformat("Invalid parameter: %1", action) Builtins.y2internal(1, @error_msg) false end |
- (Object) checkExists(name)
Check that a service exists. If not, set error_msg.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File '../../src/modules/Service.rb', line 101 def checkExists(name) if name == nil || name == "" # Error message. # %1 is a name of an init script in /usr/lib/systemd/system, # eg. nfsserver @error_msg = Builtins.sformat(_("Empty service name: %1."), name) Builtins.y2error(1, @error_msg) return false end possible_service_locations = Builtins.add( # all known $service.service locations Builtins.maplist(@systemd_dirs) do |directory| Builtins.sformat("%1/%2.service", directory, name) end, # init.d fallback, see bnc#795929 comment#20 Builtins.sformat("%1/%2", @INITD_DIR, name) ) target_dir = Builtins.find(possible_service_locations) do |service_file| FileUtils.Exists(service_file) end if target_dir != nil return true else possible_locations = Builtins.add(@systemd_dirs, @INITD_DIR) # Error message. # %1 is a name of an init script in /usr/lib/systemd/system, # eg. nfsserver @error_msg = Builtins.sformat( _("Service %1 does not exist in %2."), name, Builtins.mergestring(possible_locations, ", ") ) Builtins.y2milestone(1, @error_msg) return false end end |
- (Object) Disable(service)
Disable service
509 510 511 512 |
# File '../../src/modules/Service.rb', line 509 def Disable(service) Builtins.y2milestone("Disabling service %1", service) Adjust(service, "disable") end |
- (Object) Enable(service)
Enable service
501 502 503 504 |
# File '../../src/modules/Service.rb', line 501 def Enable(service) Builtins.y2milestone("Enabling service %1", service) Adjust(service, "enable") end |
- (Object) Enabled(name)
Check if service is enabled (in any runlevel)
Forwards to chkconfig -l which decides between init and systemd
202 203 204 205 206 207 |
# File '../../src/modules/Service.rb', line 202 def Enabled(name) SCR.Execute( path(".target.bash"), Builtins.sformat("%1 is-enabled %2.service", @invoker, name) ) == 0 end |
- (Array<String>) EnabledServices(runlevel)
Get list of enabled services in a runlevel
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
# File '../../src/modules/Service.rb', line 567 def EnabledServices(runlevel) if Ops.less_than(runlevel, -1) || Ops.greater_than(runlevel, 6) Builtins.y2error("ERROR: Invalid runlevel: %1", runlevel) return nil end # convert the integer to a string (-1 = S) runlevel_str = runlevel == -1 ? "S" : Builtins.sformat("%1", runlevel) ret = [] command = Builtins.sformat("ls -1 /etc/init.d/rc%1.d/", runlevel_str) Builtins.y2milestone("Executing: %1", command) out = Convert.to_map(SCR.Execute(path(".target.bash_output"), command)) Builtins.y2debug("Result: %1", out) if Ops.get_integer(out, "exit", -1) != 0 Builtins.y2error("ERROR: %1", out) return nil end Builtins.foreach( Builtins.splitstring(Ops.get_string(out, "stdout", ""), "\n") ) do |s| service = Builtins.regexpsub(s, "^S[0-9]+([^0-9]+.*)", "\\1") ret = Builtins.add(ret, service) if service != nil end Builtins.y2milestone("Enabled services in runlevel %1: %2", runlevel, ret) deep_copy(ret) end |
- (Object) Error
Error Message
If a Service function returns an error, this function would return an error message, including insserv stderr and possibly containing newlines.
560 561 562 |
# File '../../src/modules/Service.rb', line 560 def Error @error_msg end |
- (String) Find(services)
Return the first of the list of services which is available (has init script) or "" if none is.
606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File '../../src/modules/Service.rb', line 606 def Find(services) services = deep_copy(services) found_service = "" Builtins.foreach(services) do |service| if checkExists(service) found_service = service raise Break end end found_service end |
- (Object) Finetune(name, rl)
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.
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File '../../src/modules/Service.rb', line 347 def Finetune(name, rl) rl = deep_copy(rl) if !checkExists(name) Builtins.y2error("Unknown service: %1", name) return false end if rl != [] Builtins.y2warning( "Cannot enable service %1 (just) in selected runlevels, enabling in all default ones", name ) return Adjust(name, "enable") else return serviceDisable(name, true) end end |
- (Object) FullInfo(name)
Get service info and find out whether service is running.
245 246 247 248 |
# File '../../src/modules/Service.rb', line 245 def FullInfo(name) return {} if !checkExists(name) Builtins.add(Info(name), "started", Status(name)) end |
- (resolved) GetServiceId(name)
Get the name from a systemd service unit id without the .service suffix
186 187 188 189 190 191 192 193 194 |
# File '../../src/modules/Service.rb', line 186 def GetServiceId(name) id = GetUnitId(Builtins.sformat("%1.service", name)) return nil if id == nil # return without .service pos = Builtins.search(id, ".service") return nil if Ops.less_or_equal(pos, 0) Builtins.substring(id, 0, pos) end |
- (resolved) GetUnitId(unit)
Get complete systemd unit id
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File '../../src/modules/Service.rb', line 144 def GetUnitId(unit) cmd = Builtins.sformat("%1 --no-pager -p Id show %2", @invoker, unit) ret = Convert.convert( SCR.Execute(path(".target.bash_output"), cmd, { "TERM" => "raw" }), :from => "any", :to => "map <string, any>" ) if Ops.get_integer(ret, "exit", -1) != 0 Builtins.y2error( _("Unable to query '%1' unit Id\nCommand returned: %2\n"), unit, ret ) return nil end # extract first line _end = Builtins.findfirstof(Ops.get_string(ret, "stdout", ""), " \n") out = Builtins.substring( Ops.get_string(ret, "stdout", ""), 0, _end != nil ? _end : 0 ) # extract key anv value tmp = Builtins.splitstring(out, "=") if Builtins.size(tmp) != 2 || Ops.get_string(tmp, 0, "") != "Id" || Ops.get_string(tmp, 1, "") == "" Builtins.y2error( _("Unable to parse '%1' unit Id query output: '%2'\n"), unit, out ) return nil end Ops.get_string(tmp, 1, "") end |
- (Object) Info(name)
Get service info without peeking if service runs.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File '../../src/modules/Service.rb', line 212 def Info(name) Builtins.y2error("### Calling Service::Info is broken with systemd! ###") return {} if !checkExists(name) 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 |
- (Object) main
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File '../../src/modules/Service.rb', line 40 def main textdomain "base" Yast.import "FileUtils" #** # Services Manipulation # * @struct service # * One service is described by such map: <pre> # "servicename" : $[ # "defstart" : [ "2", "3", "5", ], // Default-Start comment # "defstop" : [ "0", "1", "6", ], // Default-Stop comment # # "reqstart" : [ "$network", "portmap" ], // Required-Start comment # "reqstop" : [ "$network", "portmap" ], // Required-Stop comment # # "description" : "text...", // Description comment # # "start" : [ "3", "5", ], // which runlevels service is really started/stopped in # "stop" : [ "3", "5", ], // read from /etc/init.d/rc?.d/* links # # "started" : 0, // return from rcservice status (integer) # # "dirty" : false, // was the entry changed? # ]</pre> # Program to invoke the service init scripts, or the systemd actions @invoker = "/bin/systemctl" # Unit locations for systemd @systemd_dirs = [ "/usr/lib/systemd/system", "/run/systemd/system", "/etc/systemd/system" ] # Init.d scripts location @INITD_DIR = "/etc/init.d" # After a function returns an error, this holds an error message, # including insserv stderr and possibly containing newlines. # # Set by # checkExists: [Full]Info, Status, Enabled, Adjust, Finetune # # Never cleared. @error_msg = "" # Time out for background agent - init script run @script_time_out = 60000 @sleep_step = 20 @lang = nil end |
- (Object) Reload(service)
Reload service
537 538 539 540 541 542 |
# File '../../src/modules/Service.rb', line 537 def Reload(service) Builtins.y2milestone("Reloading service %1", service) ret = RunInitScript(service, "reload") Builtins.y2debug("ret=%1", ret) ret == 0 end |
- (Object) Restart(service)
Restart service
527 528 529 530 531 532 |
# File '../../src/modules/Service.rb', line 527 def Restart(service) Builtins.y2milestone("Restarting service %1", service) ret = RunInitScript(service, "restart") Builtins.y2debug("ret=%1", ret) ret == 0 end |
- (Fixnum) RunInitScript(name, param)
Run init script.
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File '../../src/modules/Service.rb', line 369 def RunInitScript(name, param) Builtins.y2milestone("Running service initscript %1 %2", name, param) command = Builtins.sformat("%1 %2 %3.service", @invoker, param, name) output = Convert.convert( SCR.Execute(path(".target.bash_output"), command, { "TERM" => "raw" }), :from => "any", :to => "map <string, any>" ) if Ops.get_integer(output, "exit", -1) != 0 Builtins.y2error( "Error while running initscript %1 :\n%2", command, output ) end Ops.get_integer(output, "exit", -1) end |
- (Object) RunInitScriptOutput(name, param)
Run init script and also return its output (stdout and stderr merged).
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
# File '../../src/modules/Service.rb', line 464 def RunInitScriptOutput(name, param) env = { "TERM" => "raw" } # encoding problems - append .UTF-8 to LANG if @lang == nil ex = Convert.convert( SCR.Execute(path(".target.bash_output"), "echo -n $LANG"), :from => "any", :to => "map <string, any>" ) @lang = Ops.get_string(ex, "stdout", "") ll = Builtins.splitstring(@lang, ".") @lang = Ops.get_string(ll, 0, "") @lang = Ops.add(@lang, ".UTF-8") if @lang != "" Builtins.y2debug("LANG: %1", @lang) end env = Builtins.add(env, "LANG", @lang) if @lang != "" # looks like a bug in bash... locale_debug = "" # locale_debug = "; ls /nono 2>&1; /usr/bin/locale; /usr/bin/env"; Convert.to_map( SCR.Execute( path(".target.bash_output"), Ops.add( Builtins.sformat("%1 %2 %3.service 2>&1", @invoker, param, name), locale_debug ), env ) ) end |
- (Fixnum) RunInitScriptWithTimeOut(name, param)
Run init script with a time-out.
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File '../../src/modules/Service.rb', line 393 def RunInitScriptWithTimeOut(name, param) Builtins.y2milestone("Running service initscript %1 %2", name, param) command = Builtins.sformat( "TERM=dumb %1 %2 %3.service", @invoker, param, name ) # default return code return_code = nil # starting the process process_pid = Convert.to_integer( SCR.Execute(path(".process.start_shell"), command) ) if process_pid == nil || Ops.less_or_equal(process_pid, 0) Builtins.y2error("Cannot run '%1' -> %2", command, process_pid) return return_code end Builtins.y2debug("Running: %1", command) script_out = [] time_spent = 0 cont_loop = true # while continuing is needed and while it is possible while cont_loop && Convert.to_boolean(SCR.Read(path(".process.running"), process_pid)) if Ops.greater_or_equal(time_spent, @script_time_out) Builtins.y2error( "Command '%1' timed-out after %2 mces", command, time_spent ) cont_loop = false end # sleep a little while time_spent = Ops.add(time_spent, @sleep_step) Builtins.sleep(@sleep_step) end # fetching the return code if not timed-out if cont_loop return_code = Convert.to_integer( SCR.Read(path(".process.status"), process_pid) ) end Builtins.y2milestone( "Time spent: %1 msecs, retcode: %2", time_spent, return_code ) # killing the process (if it still runs) if Convert.to_boolean(SCR.Read(path(".process.running"), process_pid)) SCR.Execute(path(".process.kill"), process_pid) end # release the process from the agent SCR.Execute(path(".process.release"), process_pid) return_code end |
- (Object) serviceDisable(name, force)
Disables a given service and records errors. Does not check if it exists.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File '../../src/modules/Service.rb', line 256 def serviceDisable(name, force) cmd = Builtins.sformat( "%1 %2 disable %3.service", @invoker, force ? "--force" : "", name ) ret = Convert.to_map(SCR.Execute(path(".target.bash_output"), cmd)) if 0 != Ops.get_integer(ret, "exit", -1) # Error message. # %1 is a name of an init script in /etc/init.d, # Disabling means that the service should not start # in appropriate runlevels, eg. at boot time. # %2 is the stderr output of insserv(8) @error_msg = Builtins.sformat( _("Unable to disable service %1\nCommand '%2' returned:%3\n"), name, cmd, Ops.get_string(ret, "stderr", "") ) # the user is two levels up Builtins.y2error(2, @error_msg) return false end true end |
- (Object) Start(service)
Start service
517 518 519 520 521 522 |
# File '../../src/modules/Service.rb', line 517 def Start(service) Builtins.y2milestone("Starting service %1", service) ret = RunInitScript(service, "start") Builtins.y2debug("ret=%1", ret) ret == 0 end |
- (Object) Status(name)
Get service status.
The status is the output from “service status”. It should conform to LSB. 0 means the service is running.
232 233 234 235 236 237 238 239 240 |
# File '../../src/modules/Service.rb', line 232 def Status(name) Convert.to_integer( SCR.Execute( path(".target.bash"), Builtins.sformat("%1 status %2.service", @invoker, name), { "TERM" => "raw" } ) ) end |
- (Object) Stop(service)
Stop service
547 548 549 550 551 552 |
# File '../../src/modules/Service.rb', line 547 def Stop(service) Builtins.y2milestone("Stopping service %1", service) ret = RunInitScript(service, "stop") Builtins.y2debug("ret=%1", ret) ret == 0 end |