From 22d5602a6b1a2ddf304666bdeeff896dd7cff1ec Mon Sep 17 00:00:00 2001 From: Robert Sturla Date: Mon, 22 Jan 2024 20:17:20 +0000 Subject: [PATCH] status: Add --booted-index, --pending-index and --rollback-index Add new commands to output the current, staged and previous deployment for use in automation and scripting. Right now, it's difficult to pin the current deployment without needing to look into the output of some other tooling (like rpm-ostree) to get the index of each deployment. This index also is not consistent - the current deployment could be 0 when you first boot the system then 1 shortly after. This change makes it easy to get the index of the current or future deployment so it can be piped into other commands (such as ostree admin pin). Huge thanks to Eric for authoring this patch for me! Co-authored-by: Eric Curtin Signed-off-by: Robert Sturla --- man/ostree-admin-status.xml | 27 ++++++++++++++++++++ src/ostree/ot-admin-builtin-status.c | 37 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/man/ostree-admin-status.xml b/man/ostree-admin-status.xml index aefa14db7a..9bad7a7da5 100644 --- a/man/ostree-admin-status.xml +++ b/man/ostree-admin-status.xml @@ -102,6 +102,33 @@ License along with this library. If not, see . + + + + + Print the index of the booted deployment. If we are not in a booted + OSTree system, an error is returned. + + + + + + + + Print the index of the pending deployment. If we are not in a booted + OSTree system, an error is returned. + + + + + + + + Print the index of the rollback deployment. If we are not in a booted + OSTree system, an error is returned. + + + diff --git a/src/ostree/ot-admin-builtin-status.c b/src/ostree/ot-admin-builtin-status.c index d05d992881..6e485bf050 100644 --- a/src/ostree/ot-admin-builtin-status.c +++ b/src/ostree/ot-admin-builtin-status.c @@ -31,6 +31,9 @@ static gboolean opt_verify; static gboolean opt_skip_signatures; static gboolean opt_is_default; +static gboolean opt_booted_index; +static gboolean opt_pending_index; +static gboolean opt_rollback_index; static GOptionEntry options[] = { { "verify", 'V', 0, G_OPTION_ARG_NONE, &opt_verify, "Print the commit verification status", @@ -40,6 +43,12 @@ static GOptionEntry options[] { "is-default", 'D', 0, G_OPTION_ARG_NONE, &opt_is_default, "Output \"default\" if booted into the default deployment, otherwise \"not-default\"", NULL }, + { "booted-index", 'b', 0, G_OPTION_ARG_NONE, &opt_booted_index, + "Print the index of the booted deployment", NULL }, + { "pending-index", 'p', 0, G_OPTION_ARG_NONE, &opt_pending_index, + "Print the index of the pending deployment", NULL }, + { "rollback-index", 'r', 0, G_OPTION_ARG_NONE, &opt_rollback_index, + "Print the index of the rollback deployment", NULL }, { NULL } }; static gboolean deployment_print_status (OstreeSysroot *sysroot, OstreeRepo *repo, OstreeDeployment *deployment, @@ -182,6 +191,22 @@ deployment_print_status (OstreeSysroot *sysroot, OstreeRepo *repo, OstreeDeploym return TRUE; } +static gboolean +find_deployment_index (const OstreeDeployment *target_deployment, const GPtrArray *deployments) +{ + for (guint i = 0; i < deployments->len; ++i) + { + const OstreeDeployment *deployment = deployments->pdata[i]; + if (deployment == target_deployment) + { + g_print ("%d\n", i); + return TRUE; + } + } + + return FALSE; +} + gboolean ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error) @@ -218,6 +243,18 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat { g_print ("No deployments.\n"); } + else if (opt_booted_index) + { + return find_deployment_index (booted_deployment, deployments); + } + else if (opt_pending_index) + { + return find_deployment_index (pending_deployment, deployments); + } + else if (opt_rollback_index) + { + return find_deployment_index (rollback_deployment, deployments); + } else { for (guint i = 0; i < deployments->len; i++)