Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions ocaml/tests/test_pkg_mgr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ let test_dnf_repo_query_installed =
; params=
[
"repoquery"
; "-a"
; "--qf"
; "%{name}:|%{epoch}:|%{version}:|%{release}:|%{arch}:|%{repoid}"
; "%{name}:|%{epoch}:|%{version}:|%{release}:|%{arch}:|%{repoid}\n"
; "--installed"
]
}
Expand All @@ -40,11 +39,10 @@ let test_dnf_repo_query_updates =
; params=
[
"repoquery"
; "-a"
; "--disablerepo=*"
; "--enablerepo=testrepo1,testrepo2"
; "--qf"
; "%{name}:|%{epoch}:|%{version}:|%{release}:|%{arch}:|%{repoid}"
; "%{name}:|%{epoch}:|%{version}:|%{release}:|%{arch}:|%{repoid}\n"
; "--upgrades"
]
}
Expand Down Expand Up @@ -78,7 +76,6 @@ let test_dnf_clean_repo_cache =
]

let test_dnf_get_pkgs_from_updateinfo =
let sub_command = "upgrades" in
let repositories = ["testrepo1"; "testrepo2"] in
[
( "<null>"
Expand All @@ -91,17 +88,19 @@ let test_dnf_get_pkgs_from_updateinfo =
"-q"
; "--disablerepo=*"
; "--enablerepo=testrepo1,testrepo2"
; "updateinfo"
; "advisory"
; "list"
; "upgrades"
; "--updates"
]
}
(Pkg_mgr.Dnf_cmd.get_pkgs_from_updateinfo ~sub_command ~repositories)
(Pkg_mgr.Dnf_cmd.get_pkgs_from_updateinfo Pkg_mgr.Updateinfo.Updates
~repositories
)
)
]

let test_dnf_config_repo =
let config = ["--setopt=testrepo.accesstoken=file:///some/path"] in
let config = ["accesstoken=file:///some/path"] in
[
( "<null>"
, `Quick
Expand All @@ -111,8 +110,8 @@ let test_dnf_config_repo =
; params=
[
"config-manager"
; "--setopt=testrepo.accesstoken=file:///some/path"
; "testrepo"
; "setopt"
; "testrepo.accesstoken=file:///some/path"
]
}
(Pkg_mgr.Dnf_cmd.config_repo ~repo_name:"testrepo" ~config)
Expand All @@ -131,7 +130,6 @@ let test_dnf_sync_repo =
"reposync"
; "-p"
; !Xapi_globs.local_pool_repo_dir
; "--downloadcomps"
; "--download-metadata"
; "--delete"
; "--newest-only"
Expand Down Expand Up @@ -170,7 +168,7 @@ let test_yum_repo_query_installed =
cmd= !Xapi_globs.repoquery_cmd
; params=
[
"-a"
"--all"
; "--pkgnarrow=installed"
; "--qf"
; "%{name}:|%{epoch}:|%{version}:|%{release}:|%{arch}:|%{repoid}"
Expand Down Expand Up @@ -204,7 +202,6 @@ let test_yum_clean_repo_cache =
]

let test_yum_get_pkgs_from_updateinfo =
let sub_command = "updates" in
let repositories = ["testrepo1"; "testrepo2"] in
[
( "<null>"
Expand All @@ -222,20 +219,26 @@ let test_yum_get_pkgs_from_updateinfo =
; "updates"
]
}
(Pkg_mgr.Yum_cmd.get_pkgs_from_updateinfo ~sub_command ~repositories)
(Pkg_mgr.Yum_cmd.get_pkgs_from_updateinfo Pkg_mgr.Updateinfo.Updates
~repositories
)
)
]

let test_yum_config_repo =
let config = ["--setopt=testrepo.accesstoken=file:///some/path"] in
let config = ["accesstoken=file:///some/path"] in
[
( "<null>"
, `Quick
, check
{
cmd= !Xapi_globs.yum_config_manager_cmd
; params=
["--setopt=testrepo.accesstoken=file:///some/path"; "testrepo"]
[
"--save"
; "--setopt=testrepo.accesstoken=file:///some/path"
; "testrepo"
]
}
(Pkg_mgr.Yum_cmd.config_repo ~repo_name:"testrepo" ~config)
)
Expand All @@ -252,11 +255,11 @@ let test_yum_sync_repo =
[
"-p"
; !Xapi_globs.local_pool_repo_dir
; "--downloadcomps"
; "--download-metadata"
; "--delete"
; "--newest-only"
; "--repoid=testrepo"
; "--downloadcomps"
; "--plugins"
]
}
Expand Down Expand Up @@ -292,11 +295,11 @@ let test_yum_repo_query_updates =
cmd= !Xapi_globs.repoquery_cmd
; params=
[
"-a"
; "--disablerepo=*"
"--disablerepo=*"
; "--enablerepo=testrepo1,testrepo2"
; "--qf"
; "%{name}:|%{epoch}:|%{version}:|%{release}:|%{arch}:|%{repoid}"
; "--all"
; "--pkgnarrow updates"
; "--plugins"
]
Expand Down
104 changes: 61 additions & 43 deletions ocaml/xapi/pkg_mgr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ 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

Expand All @@ -27,7 +33,7 @@ module type S = sig
val clean_cache : repo_name:string -> cmd_line

val get_pkgs_from_updateinfo :
sub_command:string -> repositories:string list -> cmd_line
Updateinfo.t -> repositories:string list -> cmd_line

val get_updates_from_upgrade_dry_run : repositories:string list -> cmd_line

Expand Down Expand Up @@ -61,7 +67,7 @@ module type Args = sig

val clean_cache : string -> string list

val get_pkgs_from_updateinfo : string -> string list -> string list
val get_pkgs_from_updateinfo : Updateinfo.t -> string list -> string list

val get_updates_from_upgrade_dry_run : string list -> string list

Expand All @@ -84,12 +90,12 @@ end

let repoquery_sep = ":|"

let fmt =
["name"; "epoch"; "version"; "release"; "arch"; "repoid"]
|> List.map (fun field -> Printf.sprintf "%%{%s}" field) (* %{field} *)
|> String.concat 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"]
Expand All @@ -101,20 +107,9 @@ module Common_args = struct
; "all"
]

let get_pkgs_from_updateinfo sub_command repositories =
[
"-q"
; "--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "updateinfo"
; "list"
; sub_command
]

let is_obsoleted pkg_name repositories =
[
"-a"
; "--disablerepo=*"
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "--whatobsoletes"
; pkg_name
Expand All @@ -132,14 +127,14 @@ module Common_args = struct

let repoquery repositories =
[
"-a"
; "--disablerepo=*"
"--disablerepo=*"
; Printf.sprintf "--enablerepo=%s" (String.concat "," repositories)
; "--qf"
; fmt
]

let config_repo repo_name config = config @ [repo_name]
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 =
[
Expand All @@ -153,7 +148,6 @@ module Common_args = struct
[
"-p"
; !Xapi_globs.local_pool_repo_dir
; "--downloadcomps"
; "--download-metadata"
; "--delete"
; "--newest-only"
Expand All @@ -172,7 +166,7 @@ end
module Yum_args : Args = struct
include Common_args

let repoquery_installed () = ["-a"; "--pkgnarrow=installed"; "--qf"; fmt]
let repoquery_installed () = ["--all"; "--pkgnarrow=installed"; "--qf"; fmt]

let pkg_cmd = !Xapi_globs.yum_cmd

Expand All @@ -186,21 +180,35 @@ module Yum_args : Args = struct
["--quiet"] @ Common_args.get_updates_from_upgrade_dry_run repositories

let is_obsoleted pkg_name repositories =
Common_args.is_obsoleted pkg_name repositories @ ["--plugins"]
["--all"] @ Common_args.is_obsoleted pkg_name repositories @ ["--plugins"]

let repoquery_available repositories =
Common_args.repoquery repositories
@ ["--pkgnarrow"; "available"; "--plugins"]
@ ["--qf"; fmt; "--all"; "--pkgnarrow"; "available"; "--plugins"]

let repoquery_updates repositories =
Common_args.repoquery repositories @ ["--pkgnarrow"; "updates"; "--plugins"]
Common_args.repoquery repositories
@ ["--qf"; fmt; "--all"; "--pkgnarrow"; "updates"; "--plugins"]

let sync_repo repo_name = Common_args.sync_repo repo_name @ ["--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
Expand All @@ -209,6 +217,9 @@ module Dnf_args : Args = struct

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
Expand All @@ -221,29 +232,39 @@ module Dnf_args : Args = struct

let add_sub_cmd sub_cmd params = [sub_cmd_to_string sub_cmd] @ params

include Common_args

let repoquery_installed () =
["-a"; "--qf"; fmt; "--installed"] |> add_sub_cmd Repoquery
["--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 @ ["--available"]
Common_args.repoquery repositories @ ["--qf"; fmt; "--available"]
|> add_sub_cmd Repoquery

let repoquery_updates repositories =
Common_args.repoquery repositories @ ["--upgrades"] |> add_sub_cmd Repoquery

let config_repo repo_name config =
Common_args.config_repo repo_name config |> add_sub_cmd Repoconfig
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
Expand All @@ -256,11 +277,8 @@ module Cmd_line (M : Args) : S = struct

let clean_cache ~repo_name = {cmd= M.pkg_cmd; params= M.clean_cache repo_name}

let get_pkgs_from_updateinfo ~sub_command ~repositories =
{
cmd= M.pkg_cmd
; params= M.get_pkgs_from_updateinfo sub_command repositories
}
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}
Expand Down
8 changes: 7 additions & 1 deletion ocaml/xapi/pkg_mgr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type mgr = Yum | Dnf

type cmd_line = {cmd: string; params: string list}

module Updateinfo : sig
type t = Available | Updates

val to_string : t -> string
end

(** Interfaces to build command line and params for the package manager *)
module type S = sig
val manager : mgr
Expand All @@ -29,7 +35,7 @@ module type S = sig
(** Command line and arguments to clean a repo cache *)

val get_pkgs_from_updateinfo :
sub_command:string -> repositories:string list -> cmd_line
Updateinfo.t -> repositories:string list -> cmd_line
(** Command line and arguments to get packages from updateinfo *)

val get_updates_from_upgrade_dry_run : repositories:string list -> cmd_line
Expand Down
Loading
Loading