Skip to content

Commit

Permalink
status: Add --booted-index, --pending-index and --rollback-index
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Robert Sturla <[email protected]>
  • Loading branch information
p5 and ericcurtin committed Jan 23, 2024
1 parent d1d8f4a commit 5242e56
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
27 changes: 27 additions & 0 deletions man/ostree-admin-status.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ License along with this library. If not, see <https://www.gnu.org/licenses/>.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>-b, --booted-index</option></term>

<listitem><para>
Print the index of the booted deployment. If we are not in a booted
OSTree system, an error is returned.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>-p, --pending-index</option></term>

<listitem><para>
Print the index of the pending deployment. If we are not in a booted
OSTree system, an error is returned.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>-r, --rollback-index</option></term>

<listitem><para>
Print the index of the rollback deployment. If we are not in a booted
OSTree system, an error is returned.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>-v, --verbose</option></term>

Expand Down
38 changes: 38 additions & 0 deletions src/ostree/ot-admin-builtin-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -201,6 +226,7 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat
g_autoptr (GPtrArray) deployments = ostree_sysroot_get_deployments (sysroot);
OstreeDeployment *booted_deployment = ostree_sysroot_get_booted_deployment (sysroot);

guint deploymentIndex = G_MAXUINT;
g_autoptr (OstreeDeployment) pending_deployment = NULL;
g_autoptr (OstreeDeployment) rollback_deployment = NULL;
if (booted_deployment)
Expand All @@ -218,6 +244,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++)
Expand Down

0 comments on commit 5242e56

Please sign in to comment.