Skip to content

Commit

Permalink
CP-52074: Add systemctl enable and disable API
Browse files Browse the repository at this point in the history
Signed-off-by: Bengang Yuan <[email protected]>
  • Loading branch information
BengangY committed Jan 9, 2025
1 parent 9eeb1f3 commit 5894036
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
8 changes: 6 additions & 2 deletions ocaml/forkexecd/lib/fe_systemctl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,19 @@ let stop ~service =
Xapi_stdext_unix.Unixext.unlink_safe destination ;
status

let is_active ~service =
let check_service_status ~command ~service =
let status =
Forkhelpers.safe_close_and_exec None None None [] systemctl
["is-active"; "--quiet"; service]
[command; "--quiet"; service]
|> Forkhelpers.waitpid
|> snd
in
Unix.WEXITED 0 = status

let is_active ~service = check_service_status ~command:"is-active" ~service

let is_enabled ~service = check_service_status ~command:"is-enabled" ~service

(** path to service file *)
let path service = Filename.concat run_path (service ^ ".service")

Expand Down
3 changes: 3 additions & 0 deletions ocaml/forkexecd/lib/fe_systemctl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ val start_transient :
val is_active : service:string -> bool
(** [is_active ~service] checks whether the [service] is still running *)

val is_enabled : service:string -> bool
(** [is_enabled ~service] checks whether the [service] is enabled *)

val show : service:string -> status
(** [shows ~service] retrieves the exitcodes and PIDs of the specified [service] *)

Expand Down
25 changes: 23 additions & 2 deletions ocaml/xapi/xapi_systemctl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module D = Debug.Make (struct let name = "xapi_systemctl" end)

open D

type t = Start | Stop | Restart
type t = Start | Stop | Restart | Enable | Disable

exception Systemctl_fail of string

Expand All @@ -30,6 +30,10 @@ let to_string = function
"stop"
| Restart ->
"restart"
| Enable ->
"enable"
| Disable ->
"disable"

let perform ~wait_until_success ~service ~timeout op =
let op_str = op |> to_string in
Expand All @@ -42,8 +46,17 @@ let perform ~wait_until_success ~service ~timeout op =
if wait_until_success then (
if op = Restart then Thread.delay 0.1 ;
let is_active = Fe_systemctl.is_active ~service in
let is_enabled = Fe_systemctl.is_enabled ~service in
let success_cond () =
match op with Start | Restart -> is_active | Stop -> is_active |> not
match op with
| Start | Restart ->
is_active
| Stop ->
is_active |> not
| Enable ->
is_enabled
| Disable ->
is_enabled |> not
in
try
Helpers.retry_until_timeout ~timeout
Expand All @@ -66,3 +79,11 @@ let stop ?(timeout = 5.) ~wait_until_success service =

let start ?(timeout = 5.) ~wait_until_success service =
perform ~wait_until_success ~service ~timeout Start

let disable ?(timeout = 5.) ~wait_until_success service =
if Fe_systemctl.is_enabled ~service:"sshd" then
perform ~wait_until_success ~service ~timeout Disable

let enable ?(timeout = 5.) ~wait_until_success service =
if not (Fe_systemctl.is_enabled ~service:"sshd") then
perform ~wait_until_success ~service ~timeout Enable
10 changes: 10 additions & 0 deletions ocaml/xapi/xapi_systemctl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
(* Exception about systemctl operation like start/stop failed *)
exception Systemctl_fail of string

type t = Start | Stop | Restart | Enable | Disable

val to_string : t -> string

(* start a service with systemctl *)
val start : ?timeout:float -> wait_until_success:bool -> string -> unit

Expand All @@ -23,3 +27,9 @@ val stop : ?timeout:float -> wait_until_success:bool -> string -> unit

(* restart a service with systemctl *)
val restart : ?timeout:float -> wait_until_success:bool -> string -> unit

(* enable a service with systemctl *)
val enable : ?timeout:float -> wait_until_success:bool -> string -> unit

(* disable a service with systemctl *)
val disable : ?timeout:float -> wait_until_success:bool -> string -> unit

0 comments on commit 5894036

Please sign in to comment.