Skip to content

Commit 78c87b7

Browse files
committed
CP-45921: Use dnf as package manager for XS9
Given XS9 has updated to dnf and no yum is available, xapi will choose package manager basing on following - If dnf exists, use dnf - otherwise, fallback to yum xapi just presume dnf or yum is available in the system. Because xapi decides to use dnf or yum according to the running environment, this commit is compatible with yum/xs8 and dnf/xs9 Signed-off-by: Lin Liu <[email protected]>
1 parent 6626e2c commit 78c87b7

File tree

6 files changed

+338
-114
lines changed

6 files changed

+338
-114
lines changed

ocaml/xapi/pkg_mgr.ml

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
type t = Yum | Dnf
2+
3+
let _cache = ref None
4+
5+
let active () =
6+
match !_cache with
7+
| Some v ->
8+
v
9+
| None -> (
10+
match Sys.file_exists !Xapi_globs.dnf_cmd with
11+
| true ->
12+
_cache := Some Dnf ;
13+
Dnf
14+
| false ->
15+
_cache := Some Yum ;
16+
Yum
17+
)
18+
19+
module type S = sig
20+
val repoquery_installed : unit -> string * string list
21+
22+
val clean_cache : string -> string * string list
23+
24+
val get_pkgs_from_updateinfo : string -> string list -> string * string list
25+
26+
val get_updates_from_upgrade_dry_run : string list -> string * string list
27+
28+
val is_obsoleted : string -> string list -> string * string list
29+
30+
val get_pkgs_from_repoquery : string -> string list -> string * string list
31+
32+
val config_repo : string -> string list -> string * string list
33+
34+
val get_repo_config : string -> string * string list
35+
36+
val make_cache : string -> string * string list
37+
38+
val sync_repo : string -> string * string list
39+
end
40+
41+
module type Args = sig
42+
val pkg_cmd : string
43+
44+
val repoquery_cmd : string
45+
46+
val repoconfig_cmd : string
47+
48+
val reposync_cmd : string
49+
50+
val repoquery_installed : unit -> string list
51+
52+
val clean_cache : string -> string list
53+
54+
val get_pkgs_from_updateinfo : string -> string list -> string list
55+
56+
val get_updates_from_upgrade_dry_run : string list -> string list
57+
58+
val is_obsoleted : string -> string list -> string list
59+
60+
val get_pkgs_from_repoquery : string -> string list -> string list
61+
62+
val config_repo : string -> string list -> string list
63+
64+
val get_repo_config : string -> string list
65+
66+
val make_cache : string -> string list
67+
68+
val sync_repo : string -> string list
69+
end
70+
71+
let repoquery_sep = ":|"
72+
73+
let fmt =
74+
["name"; "epoch"; "version"; "release"; "arch"; "repoid"]
75+
|> List.map (fun field -> "%{" ^ field ^ "}")
76+
|> String.concat repoquery_sep
77+
78+
module Common_args = struct
79+
let clean_cache = function
80+
| "*" ->
81+
["clean"; "all"]
82+
| name ->
83+
[
84+
"--disablerepo=*"
85+
; Printf.sprintf "--enablerepo=%s" name
86+
; "clean"
87+
; "all"
88+
]
89+
90+
let get_pkgs_from_updateinfo sub_command repositories =
91+
[
92+
"-q"
93+
; "--disablerepo=*"
94+
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
95+
; "updateinfo"
96+
; "list"
97+
; sub_command
98+
]
99+
100+
let is_obsoleted pkg_name repositories =
101+
[
102+
"-a"
103+
; "--disablerepo=*"
104+
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
105+
; "--whatobsoletes"
106+
; pkg_name
107+
; "--qf"
108+
; "%{name}"
109+
]
110+
111+
let get_updates_from_upgrade_dry_run repositories =
112+
[
113+
"--disablerepo=*"
114+
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
115+
; "--assumeno"
116+
; "upgrade"
117+
]
118+
119+
let get_pkgs_from_repoquery repositories =
120+
[
121+
"-a"
122+
; "--disablerepo=*"
123+
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
124+
; "--qf"
125+
; fmt
126+
]
127+
128+
let config_repo repo_name config = config @ [repo_name]
129+
130+
let make_cache repo_name =
131+
[
132+
"--disablerepo=*"
133+
; Printf.sprintf "--enablerepo=%s" repo_name
134+
; "-y"
135+
; "makecache"
136+
]
137+
138+
let sync_repo repo_name =
139+
[
140+
"-p"
141+
; !Xapi_globs.local_pool_repo_dir
142+
; "--downloadcomps"
143+
; "--download-metadata"
144+
; "--delete"
145+
; "--newest-only"
146+
; Printf.sprintf "--repoid=%s" repo_name
147+
]
148+
end
149+
150+
module Yum_args : Args = struct
151+
include Common_args
152+
153+
let repoquery_installed () = ["-a"; "--pkgnarrow=installed"; "--qf"; fmt]
154+
155+
let pkg_cmd = !Xapi_globs.yum_cmd
156+
157+
let repoquery_cmd = !Xapi_globs.repoquery_cmd
158+
159+
let repoconfig_cmd = !Xapi_globs.yum_config_manager_cmd
160+
161+
let reposync_cmd = !Xapi_globs.reposync_cmd
162+
163+
let get_updates_from_upgrade_dry_run repositories =
164+
["--quiet"] @ Common_args.get_updates_from_upgrade_dry_run repositories
165+
166+
let is_obsoleted pkg_name repositories =
167+
Common_args.is_obsoleted pkg_name repositories @ ["--plugins"]
168+
169+
let get_pkgs_from_repoquery pkg_narrow repositories =
170+
Common_args.get_pkgs_from_repoquery repositories
171+
@ [Printf.sprintf "--pkgnarrow=%s" pkg_narrow; "--plugins"]
172+
173+
let sync_repo repo_name = Common_args.sync_repo repo_name @ ["--plugins"]
174+
175+
let get_repo_config repo_name = [repo_name]
176+
end
177+
178+
module Dnf_args : Args = struct
179+
let pkg_cmd = !Xapi_globs.dnf_cmd
180+
181+
let repoquery_cmd = !Xapi_globs.dnf_cmd
182+
183+
let repoconfig_cmd = !Xapi_globs.dnf_cmd
184+
185+
let reposync_cmd = !Xapi_globs.dnf_cmd
186+
187+
type sub_cmd = Repoquery | Repoconfig | Reposync
188+
189+
let sub_cmd_to_string = function
190+
| Reposync ->
191+
"reposync"
192+
| Repoconfig ->
193+
"config-manager"
194+
| Repoquery ->
195+
"repoquery"
196+
197+
let add_sub_cmd sub_cmd params = [sub_cmd_to_string sub_cmd] @ params
198+
199+
include Common_args
200+
201+
let repoquery_installed () =
202+
["-a"; "--qf"; fmt; "--installed"] |> add_sub_cmd Repoquery
203+
204+
let is_obsoleted pkg_name repositories =
205+
Common_args.is_obsoleted pkg_name repositories |> add_sub_cmd Repoquery
206+
207+
let get_pkgs_from_repoquery pkg_narrow repositories =
208+
Common_args.get_pkgs_from_repoquery repositories
209+
@ [Printf.sprintf "--%s" pkg_narrow]
210+
|> add_sub_cmd Repoquery
211+
212+
let config_repo repo_name config =
213+
Common_args.config_repo repo_name config |> add_sub_cmd Repoconfig
214+
215+
let sync_repo repo_name =
216+
Common_args.sync_repo repo_name |> add_sub_cmd Reposync
217+
218+
let get_repo_config repo_name =
219+
["--dump"; repo_name] |> add_sub_cmd Repoconfig
220+
end
221+
222+
module Cmd_line (M : Args) : S = struct
223+
(* functor to construct comand line and arguments *)
224+
let repoquery_installed () = (M.repoquery_cmd, M.repoquery_installed ())
225+
226+
let clean_cache name = (M.pkg_cmd, M.clean_cache name)
227+
228+
let get_pkgs_from_updateinfo sub_command repositories =
229+
(M.pkg_cmd, M.get_pkgs_from_updateinfo sub_command repositories)
230+
231+
let get_updates_from_upgrade_dry_run repositories =
232+
(M.pkg_cmd, M.get_updates_from_upgrade_dry_run repositories)
233+
234+
let is_obsoleted pkg_name repositories =
235+
(M.repoquery_cmd, M.is_obsoleted pkg_name repositories)
236+
237+
let get_pkgs_from_repoquery pkg_narrow repositories =
238+
(M.repoquery_cmd, M.get_pkgs_from_repoquery pkg_narrow repositories)
239+
240+
let config_repo repo_name config =
241+
(M.repoconfig_cmd, M.config_repo repo_name config)
242+
243+
let get_repo_config repo_name = (M.repoconfig_cmd, M.get_repo_config repo_name)
244+
245+
let make_cache repo_name = (M.pkg_cmd, M.make_cache repo_name)
246+
247+
let sync_repo repo_name = (M.reposync_cmd, M.sync_repo repo_name)
248+
end
249+
250+
module Yum_cmd = Cmd_line (Yum_args)
251+
module Dnf_cmd = Cmd_line (Dnf_args)
252+
253+
let get_pkg_mgr : (module S) =
254+
match active () with Dnf -> (module Dnf_cmd) | Yum -> (module Yum_cmd)

ocaml/xapi/pkg_mgr.mli

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
val repoquery_sep : string
2+
3+
type t = Yum | Dnf
4+
5+
val active : unit -> t
6+
7+
module type S = sig
8+
val repoquery_installed : unit -> string * string list
9+
10+
val clean_cache : string -> string * string list
11+
12+
val get_pkgs_from_updateinfo : string -> string list -> string * string list
13+
14+
val get_updates_from_upgrade_dry_run : string list -> string * string list
15+
16+
val is_obsoleted : string -> string list -> string * string list
17+
18+
val get_pkgs_from_repoquery : string -> string list -> string * string list
19+
20+
val config_repo : string -> string list -> string * string list
21+
22+
val get_repo_config : string -> string * string list
23+
24+
val make_cache : string -> string * string list
25+
26+
val sync_repo : string -> string * string list
27+
end
28+
29+
val get_pkg_mgr : (module S)

ocaml/xapi/repository.ml

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ open Updateinfo
2121
open Repository_helpers
2222
module UpdateIdSet = Set.Make (String)
2323

24+
module Pkgs = (val Pkg_mgr.get_pkg_mgr)
25+
2426
let capacity_in_parallel = 16
2527

2628
(* The cache below is protected by pool's current_operations locking mechanism *)
@@ -128,7 +130,8 @@ let sync ~__context ~self ~token ~token_id =
128130
write_initial_yum_config () ;
129131
clean_yum_cache repo_name ;
130132
(* Remove imported YUM repository GPG key *)
131-
Xapi_stdext_unix.Unixext.rm_rec (get_repo_config repo_name "gpgdir") ;
133+
if Pkg_mgr.(active () = Yum) then
134+
Xapi_stdext_unix.Unixext.rm_rec (get_repo_config repo_name "gpgdir") ;
132135
Xapi_stdext_pervasives.Pervasiveext.finally
133136
(fun () ->
134137
with_access_token ~token ~token_id @@ fun token_path ->
@@ -143,49 +146,27 @@ let sync ~__context ~self ~token ~token_id =
143146
let proxy_url_param, proxy_username_param, proxy_password_param =
144147
get_proxy_params ~__context repo_name
145148
in
146-
let config_params =
149+
let cmd, params =
147150
[
148151
"--save"
149152
; proxy_url_param
150153
; proxy_username_param
151154
; proxy_password_param
152155
; token_param
153-
; repo_name
154156
]
157+
|> Pkgs.config_repo repo_name
155158
in
156-
ignore
157-
(Helpers.call_script ~log_output:Helpers.On_failure
158-
!Xapi_globs.yum_config_manager_cmd
159-
config_params
160-
) ;
159+
ignore (Helpers.call_script ~log_output:Helpers.On_failure cmd params) ;
161160

162161
(* Import YUM repository GPG key to check metadata in reposync *)
163-
let makecache_params =
164-
[
165-
"--disablerepo=*"
166-
; Printf.sprintf "--enablerepo=%s" repo_name
167-
; "-y"
168-
; "makecache"
169-
]
170-
in
171-
ignore (Helpers.call_script !Xapi_globs.yum_cmd makecache_params) ;
162+
let cmd, params = Pkgs.make_cache repo_name in
163+
ignore (Helpers.call_script cmd params) ;
172164

173165
(* Sync with remote repository *)
174-
let sync_params =
175-
[
176-
"-p"
177-
; !Xapi_globs.local_pool_repo_dir
178-
; "--downloadcomps"
179-
; "--download-metadata"
180-
; "--delete"
181-
; "--plugins"
182-
; "--newest-only"
183-
; Printf.sprintf "--repoid=%s" repo_name
184-
]
185-
in
166+
let cmd, params = Pkgs.sync_repo repo_name in
186167
Unixext.mkdir_rec !Xapi_globs.local_pool_repo_dir 0o700 ;
187168
clean_yum_cache repo_name ;
188-
ignore (Helpers.call_script !Xapi_globs.reposync_cmd sync_params)
169+
ignore (Helpers.call_script cmd params)
189170
)
190171
(fun () ->
191172
(* Rewrite repo conf file as initial content to remove credential related info,

0 commit comments

Comments
 (0)