RPMOSTreeSysroot: add "booted" entry to deployment variant

Now that the `status` command learned a `--json` option, we can pretty
much avoid parsing human-readable output. The only piece of information
that is missing from the JSON output compared to the output for humans
is *which* deployment we're currently booted in.

This patch fixes that shortcoming by adding a "booted" boolean variant
to the deployment variant.

Closes: #350
Approved by: cgwalters
This commit is contained in:
Jonathan Lebon 2016-06-24 17:40:46 -04:00 committed by Atomic Bot
parent f35f5cf468
commit 225be81c40
6 changed files with 37 additions and 34 deletions

View File

@ -80,12 +80,10 @@ static gboolean
status_generic (RPMOSTreeSysroot *sysroot_proxy,
RPMOSTreeOS *os_proxy,
GVariant *deployments,
GVariant *booted_deployment,
GCancellable *cancellable,
GError **error)
{
GVariantIter iter;
const char *booted_id = NULL;
gboolean first = TRUE;
const int is_tty = isatty (1);
const char *bold_prefix = is_tty ? "\x1b[1m" : "";
@ -104,9 +102,6 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy,
g_print ("State: idle\n");
g_print ("Deployments:\n");
if (booted_deployment)
g_assert (g_variant_lookup (booted_deployment, "id", "&s", &booted_id));
g_variant_iter_init (&iter, deployments);
while (TRUE)
@ -174,7 +169,8 @@ status_generic (RPMOSTreeSysroot *sysroot_proxy,
else
g_print ("\n");
is_booted = g_strcmp0 (booted_id, id) == 0;
if (!g_variant_dict_lookup (dict, "booted", "b", &is_booted))
is_booted = FALSE;
g_print ("%s ", is_booted ? libsd_special_glyph (BLACK_CIRCLE) : " ");
@ -248,7 +244,6 @@ rpmostree_builtin_status (int argc,
GOptionContext *context = g_option_context_new ("- Get the version of the booted system");
glnx_unref_object RPMOSTreeOS *os_proxy = NULL;
glnx_unref_object RPMOSTreeSysroot *sysroot_proxy = NULL;
g_autoptr(GVariant) booted_deployment = NULL;
g_autoptr(GVariant) deployments = NULL;
if (!rpmostree_option_context_parse (context,
@ -265,7 +260,6 @@ rpmostree_builtin_status (int argc,
goto out;
deployments = rpmostree_sysroot_dup_deployments (sysroot_proxy);
booted_deployment = rpmostree_os_dup_booted_deployment (os_proxy);
if (opt_json)
{
@ -297,7 +291,6 @@ rpmostree_builtin_status (int argc,
else
{
if (!status_generic (sysroot_proxy, os_proxy, deployments,
booted_deployment,
cancellable, error))
goto out;
}

View File

@ -13,6 +13,7 @@
'origin' (type 's')
'signatures' (type 'av')
'packages' (type 'as')
'booted' (type 'b')
-->
<interface name="org.projectatomic.rpmostree1.Sysroot">

View File

@ -188,6 +188,7 @@ variant_add_commit_details (GVariantDict *dict,
GVariant *
rpmostreed_deployment_generate_variant (OstreeDeployment *deployment,
const char *booted_id,
OstreeRepo *repo,
GError **error)
{
@ -244,6 +245,9 @@ rpmostreed_deployment_generate_variant (OstreeDeployment *deployment,
g_variant_dict_insert (&dict, "unlocked", "s",
ostree_deployment_unlocked_state_to_string (ostree_deployment_get_unlocked (deployment)));
if (booted_id != NULL)
g_variant_dict_insert (&dict, "booted", "b", g_strcmp0 (booted_id, id) == 0);
return g_variant_dict_end (&dict);
}

View File

@ -37,6 +37,7 @@ void rpmostreed_deployment_get_refspec_packages (OstreeDeployment *de
GVariant * rpmostreed_deployment_generate_blank_variant (void);
GVariant * rpmostreed_deployment_generate_variant (OstreeDeployment *deployment,
const char *booted_id,
OstreeRepo *repo,
GError **error);

View File

@ -1089,6 +1089,7 @@ rpmostreed_os_load_internals (RpmostreedOS *self, GError **error)
const gchar *name;
OstreeDeployment *booted = NULL; /* owned by sysroot */
g_autofree gchar* booted_id = NULL;
glnx_unref_object OstreeDeployment *merge_deployment = NULL; /* transfered */
g_autoptr(GPtrArray) deployments = NULL;
@ -1110,13 +1111,22 @@ rpmostreed_os_load_internals (RpmostreedOS *self, GError **error)
ot_sysroot = rpmostreed_sysroot_get_root (rpmostreed_sysroot_get ());
ot_repo = rpmostreed_sysroot_get_repo (rpmostreed_sysroot_get ());
deployments = ostree_sysroot_get_deployments (ot_sysroot);
booted = ostree_sysroot_get_booted_deployment (ot_sysroot);
if (booted && g_strcmp0 (ostree_deployment_get_osname (booted), name) == 0)
{
booted_variant = rpmostreed_deployment_generate_variant (booted, booted_id, ot_repo, error);
if (!booted_variant)
return FALSE;
booted_id = rpmostreed_deployment_generate_id (booted);
}
deployments = ostree_sysroot_get_deployments (ot_sysroot);
for (i=0; i<deployments->len; i++)
{
if (g_strcmp0 (ostree_deployment_get_osname (deployments->pdata[i]), name) == 0)
{
default_variant = rpmostreed_deployment_generate_variant (deployments->pdata[i],
booted_id,
ot_repo, error);
if (default_variant == NULL)
return FALSE;
@ -1124,21 +1134,12 @@ rpmostreed_os_load_internals (RpmostreedOS *self, GError **error)
}
}
booted = ostree_sysroot_get_booted_deployment (ot_sysroot);
if (booted && g_strcmp0 (ostree_deployment_get_osname (booted),
name) == 0)
{
booted_variant = rpmostreed_deployment_generate_variant (booted, ot_repo, error);
if (!booted_variant)
return FALSE;
}
if (deployments->len >= 2)
{
rollback_index = rpmostreed_rollback_deployment_index (name, ot_sysroot, NULL);
if (rollback_index >= 0)
{
rollback_variant = rpmostreed_deployment_generate_variant (deployments->pdata[rollback_index],
rollback_variant = rpmostreed_deployment_generate_variant (deployments->pdata[rollback_index], booted_id,
ot_repo, error);
if (!rollback_variant)
return FALSE;

View File

@ -431,6 +431,7 @@ sysroot_populate_deployments_unlocked (RpmostreedSysroot *self,
{
gboolean ret = FALSE;
OstreeDeployment *booted = NULL; /* owned by sysroot */
g_autofree gchar *booted_id = NULL;
g_autoptr(GPtrArray) deployments = NULL;
g_autoptr(GHashTable) seen_osnames = NULL;
GHashTableIter iter;
@ -456,6 +457,21 @@ sysroot_populate_deployments_unlocked (RpmostreedSysroot *self,
seen_osnames = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
/* Updated booted property */
booted = ostree_sysroot_get_booted_deployment (self->ot_sysroot);
if (booted)
{
const gchar *os = ostree_deployment_get_osname (booted);
g_autofree gchar *path = rpmostreed_generate_object_path (BASE_DBUS_PATH,
os, NULL);
rpmostree_sysroot_set_booted (RPMOSTREE_SYSROOT (self), path);
booted_id = rpmostreed_deployment_generate_id (booted);
}
else
{
rpmostree_sysroot_set_booted (RPMOSTREE_SYSROOT (self), "/");
}
/* Add deployment interfaces */
deployments = ostree_sysroot_get_deployments (self->ot_sysroot);
@ -465,7 +481,7 @@ sysroot_populate_deployments_unlocked (RpmostreedSysroot *self,
OstreeDeployment *deployment = deployments->pdata[i];
const char *deployment_os;
variant = rpmostreed_deployment_generate_variant (deployment, self->repo, error);
variant = rpmostreed_deployment_generate_variant (deployment, booted_id, self->repo, error);
if (!variant)
goto out;
g_variant_builder_add_value (&builder, variant);
@ -486,19 +502,6 @@ sysroot_populate_deployments_unlocked (RpmostreedSysroot *self,
g_hash_table_add (seen_osnames, (char*)deployment_os);
}
booted = ostree_sysroot_get_booted_deployment (self->ot_sysroot);
if (booted)
{
const gchar *os = ostree_deployment_get_osname (booted);
g_autofree gchar *path = rpmostreed_generate_object_path (BASE_DBUS_PATH,
os, NULL);
rpmostree_sysroot_set_booted (RPMOSTREE_SYSROOT (self), path);
}
else
{
rpmostree_sysroot_set_booted (RPMOSTREE_SYSROOT (self), "/");
}
/* Remove dead os paths */
g_hash_table_iter_init (&iter, self->os_interfaces);
while (g_hash_table_iter_next (&iter, &hashkey, &value))