forked from xapi-project/xen-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkg_mgr.ml
More file actions
356 lines (258 loc) · 9.75 KB
/
pkg_mgr.ml
File metadata and controls
356 lines (258 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
96
97
98
99
100
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
140
141
142
143
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
284
285
286
287
288
289
290
291
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
(*
* Copyright (c) Cloud Software Group, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; version 2.1 only. with the special
* exception on linking described in file LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*)
type mgr = Yum | Dnf
type cmd_line = {cmd: string; params: string list}
module Updateinfo = struct
type t = Available | Updates
let to_string = function Available -> "available" | Updates -> "updates"
end
let active () =
match Sys.file_exists !Xapi_globs.dnf_cmd with true -> Dnf | false -> Yum
module type S = sig
val manager : mgr
val repoquery_installed : unit -> cmd_line
val clean_cache : repo_name:string -> cmd_line
val get_pkgs_from_updateinfo :
Updateinfo.t -> repositories:string list -> cmd_line
val get_updates_from_upgrade_dry_run : repositories:string list -> cmd_line
val get_updates_from_group_upgrade_dry_run :
repositories:string list -> cmd_line
val is_obsoleted : pkg_name:string -> repositories:string list -> cmd_line
val repoquery_updates : repositories:string list -> cmd_line
val repoquery_available : repositories:string list -> cmd_line
val config_repo : repo_name:string -> config:string list -> cmd_line
val get_repo_config : repo_name:string -> cmd_line
val make_cache : repo_name:string -> cmd_line
val sync_repo : repo_name:string -> cmd_line
val apply_upgrade : repositories:string list -> cmd_line
val apply_group_upgrade : repositories:string list -> cmd_line
end
module type Args = sig
val pkg_cmd : string
val repoquery_cmd : string
val repoconfig_cmd : string
val reposync_cmd : string
val repoquery_installed : unit -> string list
val clean_cache : string -> string list
val get_pkgs_from_updateinfo : Updateinfo.t -> string list -> string list
val get_updates_from_upgrade_dry_run : string list -> string list
val get_updates_from_group_upgrade_dry_run : string list -> string list
val is_obsoleted : string -> string list -> string list
val repoquery_updates : string list -> string list
val repoquery_available : string list -> string list
val config_repo : string -> string list -> string list
val get_repo_config : string -> string list
val make_cache : string -> string list
val sync_repo : string -> string list
val apply_upgrade : string list -> string list
val apply_group_upgrade : string list -> string list
end
let repoquery_sep = ":|"
module Common_args = struct
let fmt =
["name"; "epoch"; "version"; "release"; "arch"; "repoid"]
|> List.map (fun field -> Printf.sprintf "%%{%s}" field) (* %{field} *)
|> String.concat repoquery_sep
let clean_cache = function
| "*" ->
["clean"; "all"]
| name ->
[
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" name
; "clean"
; "all"
]
let is_obsoleted pkg_name repositories =
[
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "--whatobsoletes"
; pkg_name
; "--qf"
; "%{name}"
]
let get_updates_from_upgrade_dry_run repositories =
[
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "--assumeno"
; "upgrade"
]
let get_updates_from_group_upgrade_dry_run repositories =
[
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "--assumeno"
; "group"
; "upgrade"
; "*"
]
let repoquery repositories =
[
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
]
let config_repo repo_name config =
(* Add --setopt to repo options *)
List.map (fun x -> Printf.sprintf "--setopt=%s.%s" repo_name x) config
|> fun x -> ["--save"] @ x @ [repo_name]
let make_cache repo_name =
[
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" repo_name
; "-y"
; "makecache"
]
let sync_repo repo_name =
[
"-p"
; !Xapi_globs.local_pool_repo_dir
; "--download-metadata"
; "--delete"
; "--newest-only"
; Printf.sprintf "--repoid=%s" repo_name
]
let apply_upgrade repositories =
[
"-y"
; "--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "upgrade"
]
let apply_group_upgrade repositories =
[
"-y"
; "--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "group"
; "upgrade"
; "*"
]
end
module Yum_args : Args = struct
include Common_args
let repoquery_installed () = ["--all"; "--pkgnarrow=installed"; "--qf"; fmt]
let pkg_cmd = !Xapi_globs.yum_cmd
let repoquery_cmd = !Xapi_globs.repoquery_cmd
let repoconfig_cmd = !Xapi_globs.yum_config_manager_cmd
let reposync_cmd = !Xapi_globs.reposync_cmd
let get_updates_from_upgrade_dry_run repositories =
["--quiet"] @ Common_args.get_updates_from_upgrade_dry_run repositories
let get_updates_from_group_upgrade_dry_run repositories =
["--quiet"]
@ Common_args.get_updates_from_group_upgrade_dry_run repositories
let is_obsoleted pkg_name repositories =
["--all"] @ Common_args.is_obsoleted pkg_name repositories @ ["--plugins"]
let repoquery_available repositories =
Common_args.repoquery repositories
@ ["--qf"; fmt; "--all"; "--pkgnarrow"; "available"; "--plugins"]
let repoquery_updates repositories =
Common_args.repoquery repositories
@ ["--qf"; fmt; "--all"; "--pkgnarrow"; "updates"; "--plugins"]
let sync_repo repo_name =
Common_args.sync_repo repo_name @ ["--downloadcomps"; "--plugins"]
let get_repo_config repo_name = [repo_name]
let get_pkgs_from_updateinfo updateinfo repositories =
[
"-q"
; "--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "updateinfo"
; "list"
; Updateinfo.to_string updateinfo
]
end
module Dnf_args : Args = struct
include Common_args
let pkg_cmd = !Xapi_globs.dnf_cmd
let repoquery_cmd = !Xapi_globs.dnf_cmd
let repoconfig_cmd = !Xapi_globs.dnf_cmd
let reposync_cmd = !Xapi_globs.dnf_cmd
(* dnf5 removed ending line breaker, we need to add it back *)
let fmt = Printf.sprintf "%s\n" Common_args.fmt
type sub_cmd = Repoquery | Repoconfig | Reposync
let sub_cmd_to_string = function
| Reposync ->
"reposync"
| Repoconfig ->
"config-manager"
| Repoquery ->
"repoquery"
let add_sub_cmd sub_cmd params = [sub_cmd_to_string sub_cmd] @ params
let repoquery_installed () =
["--qf"; fmt; "--installed"] |> add_sub_cmd Repoquery
let config_repo repo_name config =
config |> List.map (fun x -> Printf.sprintf "%s.%s" repo_name x) |> fun x ->
["setopt"] @ x |> add_sub_cmd Repoconfig
let is_obsoleted pkg_name repositories =
Common_args.is_obsoleted pkg_name repositories |> add_sub_cmd Repoquery
let repoquery_available repositories =
Common_args.repoquery repositories @ ["--qf"; fmt; "--available"]
|> add_sub_cmd Repoquery
let repoquery_updates repositories =
Common_args.repoquery repositories @ ["--qf"; fmt; "--upgrades"]
|> add_sub_cmd Repoquery
let sync_repo repo_name =
Common_args.sync_repo repo_name |> add_sub_cmd Reposync
let get_repo_config repo_name =
["--dump"; repo_name] |> add_sub_cmd Repoconfig
let get_pkgs_from_updateinfo updateinfo repositories =
[
"-q"
; "--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "advisory"
; "list"
; (updateinfo |> Updateinfo.to_string |> fun x -> Printf.sprintf "--%s" x)
]
end
module Cmd_line (M : Args) : S = struct
let manager =
match M.pkg_cmd with cmd when cmd = !Xapi_globs.dnf_cmd -> Dnf | _ -> Yum
(* functor to construct comand line and arguments *)
let repoquery_installed () =
{cmd= M.repoquery_cmd; params= M.repoquery_installed ()}
let clean_cache ~repo_name = {cmd= M.pkg_cmd; params= M.clean_cache repo_name}
let get_pkgs_from_updateinfo updateinfo ~repositories =
{cmd= M.pkg_cmd; params= M.get_pkgs_from_updateinfo updateinfo repositories}
let get_updates_from_upgrade_dry_run ~repositories =
{cmd= M.pkg_cmd; params= M.get_updates_from_upgrade_dry_run repositories}
let get_updates_from_group_upgrade_dry_run ~repositories =
{
cmd= M.pkg_cmd
; params= M.get_updates_from_group_upgrade_dry_run repositories
}
let is_obsoleted ~pkg_name ~repositories =
{cmd= M.repoquery_cmd; params= M.is_obsoleted pkg_name repositories}
let repoquery_updates ~repositories =
{cmd= M.repoquery_cmd; params= M.repoquery_updates repositories}
let repoquery_available ~repositories =
{cmd= M.repoquery_cmd; params= M.repoquery_available repositories}
let config_repo ~repo_name ~config =
{cmd= M.repoconfig_cmd; params= M.config_repo repo_name config}
let get_repo_config ~repo_name =
{cmd= M.repoconfig_cmd; params= M.get_repo_config repo_name}
let make_cache ~repo_name = {cmd= M.pkg_cmd; params= M.make_cache repo_name}
let sync_repo ~repo_name =
{cmd= M.reposync_cmd; params= M.sync_repo repo_name}
let apply_upgrade ~repositories =
{cmd= M.pkg_cmd; params= M.apply_upgrade repositories}
let apply_group_upgrade ~repositories =
{cmd= M.pkg_cmd; params= M.apply_group_upgrade repositories}
end
module Yum_cmd = Cmd_line (Yum_args)
module Dnf_cmd = Cmd_line (Dnf_args)
let get_pkg_mgr : (module S) =
match active () with Dnf -> (module Dnf_cmd) | Yum -> (module Yum_cmd)