Skip to content

Commit b2c50e2

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 b2c50e2

File tree

6 files changed

+375
-115
lines changed

6 files changed

+375
-115
lines changed

ocaml/xapi/pkg_mgr.ml

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

ocaml/xapi/pkg_mgr.mli

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
val repoquery_sep : string
2+
3+
(* Type of package manager supported *)
4+
type mgr = Yum | Dnf
5+
6+
(*Which pakcage manager is actively using*)
7+
val active : unit -> mgr
8+
9+
(* Interfaces to build command line and params for the package manager
10+
* Each interface has following return result
11+
* (comandline * commandline params) with type (string, string list)
12+
* *)
13+
module type S = sig
14+
(* Command line and arguments to perform repoquery installed packages*)
15+
val repoquery_installed : unit -> string * string list
16+
17+
(* Command line and arguments to clean a repo cache *)
18+
val clean_cache : repo_name:string -> string * string list
19+
20+
(*Command line and arguments to get packages from updateinfo*)
21+
val get_pkgs_from_updateinfo :
22+
sub_command:string -> repositories:string list -> string * string list
23+
24+
(*Command line and arguments to dry run an upgrade, with repositories enabled*)
25+
val get_updates_from_upgrade_dry_run :
26+
repositories:string list -> string * string list
27+
28+
(*Command line and arguments to check whether a package is obsoleted by any other
29+
* package in given repositories*)
30+
val is_obsoleted :
31+
pkg_name:string -> repositories:string list -> string * string list
32+
33+
(*Command line and arguments to perform repoquery in repositories
34+
* Filter the output by pkg_narrow*)
35+
val get_pkgs_from_repoquery :
36+
pkg_narrow:string -> repositories:string list -> string * string list
37+
38+
(*Command line and arguments to perform repo configuration*)
39+
val config_repo :
40+
repo_name:string -> config:string list -> string * string list
41+
42+
(*Command line and arguments to query repo configuration*)
43+
val get_repo_config : repo_name:string -> string * string list
44+
45+
(* Command line and arguments to make cache for repo*)
46+
val make_cache : repo_name:string -> string * string list
47+
48+
(* Command line and arguments to sync with remote repo*)
49+
val sync_repo : repo_name:string -> string * string list
50+
51+
(* Command line and arguments to apply upgrades from repos*)
52+
val apply_upgrade : repositories:string list -> string * string list
53+
end
54+
55+
(*Exposed only for unittest, do not use the modules directly
56+
* Instead, use get_pkg_mgr to detect active package manager*)
57+
module Yum_cmd : S
58+
59+
module Dnf_cmd : S
60+
61+
(* Get active package manager module to provide command line and arguments*)
62+
val get_pkg_mgr : (module S)

0 commit comments

Comments
 (0)