Skip to content

Commit

Permalink
Add register-driver option to UpdateDeployment D-Bus API
Browse files Browse the repository at this point in the history
Record the agent id and agent systemd unit and serialize it into a
JSON file at `/run/rpmostree/update-driver.json`.

Also add the companion `--register-driver` option to the `deploy`
CLI argument.

Closes coreos#1747.
  • Loading branch information
kelvinfan001 committed Jan 15, 2021
1 parent 2e417c4 commit 6f7414c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app/rpmostree-builtin-deploy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static gboolean opt_download_only;
static gboolean opt_lock_finalization;
static gboolean opt_disallow_downgrade;
static gboolean opt_unchanged_exit_77;
static gboolean opt_register_driver;

static GOptionEntry option_entries[] = {
{ "os", 0, 0, G_OPTION_ARG_STRING, &opt_osname, "Operate on provided OSNAME", "OSNAME" },
Expand All @@ -49,6 +50,7 @@ static GOptionEntry option_entries[] = {
{ "lock-finalization", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &opt_lock_finalization, "Prevent automatic deployment finalization on shutdown", NULL },
{ "disallow-downgrade", 0, 0, G_OPTION_ARG_NONE, &opt_disallow_downgrade, "Forbid deployment of chronologically older trees", NULL },
{ "unchanged-exit-77", 0, 0, G_OPTION_ARG_NONE, &opt_unchanged_exit_77, "If no new deployment made, exit 77", NULL },
{ "register-driver", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &opt_register_driver, "Register the calling agent as the driver for updates", NULL },
{ NULL }
};

Expand Down Expand Up @@ -125,6 +127,7 @@ rpmostree_builtin_deploy (int argc,
g_variant_dict_insert (&dict, "download-only", "b", opt_download_only);
g_variant_dict_insert (&dict, "lock-finalization", "b", opt_lock_finalization);
g_variant_dict_insert (&dict, "initiating-command-line", "s", invocation->command_line);
g_variant_dict_insert (&dict, "register-driver", "b", opt_register_driver);
g_autoptr(GVariant) options = g_variant_ref_sink (g_variant_dict_end (&dict));

/* Use newer D-Bus API only if we have to so we maintain coverage. */
Expand Down
2 changes: 2 additions & 0 deletions src/daemon/org.projectatomic.rpmostree1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@
Stop short of deploying the new tree. If layering packages,
the pkg diff is printed but packages are not downloaded or
imported.
"register-driver" (type 'b')
Register the caller as the update driver.
"no-layering" (type 'b')
Remove all package requests. Requests in "install-packages"
are still subsequently processed if specified.
Expand Down
3 changes: 3 additions & 0 deletions src/daemon/rpmostree-sysroot-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ G_BEGIN_DECLS
* /run/ostree/staged-deployment path and company. */
#define _OSTREE_SYSROOT_RUNSTATE_STAGED_LOCKED "/run/ostree/staged-deployment-locked"

/* File to store update driver info */
#define RPMOSTREE_DRIVER_STATE "/run/rpmostree/update-driver.json"

gboolean
rpmostree_syscore_cleanup (OstreeSysroot *sysroot,
OstreeRepo *repo,
Expand Down
53 changes: 53 additions & 0 deletions src/daemon/rpmostreed-transaction-types.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "config.h"
#include "ostree.h"

#include <json-glib/json-glib.h>
#include <gio/gunixoutputstream.h>
#include <libglnx.h>
#include <systemd/sd-journal.h>

Expand Down Expand Up @@ -787,6 +789,48 @@ deploy_has_bool_option (DeployTransaction *self,
return vardict_lookup_bool (self->options, option, FALSE);
}

static gboolean
record_driver_info (const char *agent,
const char *sd_unit,
GCancellable *cancellable,
GError **error)
{
g_auto(GVariantBuilder) caller_info_builder;
g_variant_builder_init (&caller_info_builder, G_VARIANT_TYPE ("a{sv}"));

if (agent_id)
g_variant_builder_add (&caller_info_builder, "{sv}", "agent-id",
g_variant_new_string (agent));
if (sd_unit)
g_variant_builder_add (&caller_info_builder, "{sv}", "agent-sd-unit",
g_variant_new_string (sd_unit));

g_autoptr(GVariant) composemeta_v = g_variant_builder_end (&caller_info_builder);
JsonNode *composemeta_node = json_gvariant_serialize (composemeta_v);
glnx_unref_object JsonGenerator *generator = json_generator_new ();
json_generator_set_root (generator, composemeta_node);

char *dnbuf = strdupa (RPMOSTREE_DRIVER_STATE);
const char *dn = dirname (dnbuf);
if (!glnx_shutil_mkdir_p_at (AT_FDCWD, dn,
0755, cancellable, error))
return FALSE;
g_auto(GLnxTmpfile) tmpf = { 0, };
if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, dn, O_WRONLY | O_CLOEXEC,
&tmpf, error))
return FALSE;
g_autoptr(GOutputStream) out = g_unix_output_stream_new (tmpf.fd, FALSE);
if (json_generator_to_stream (generator, out, cancellable, error) <= 0
|| (error != NULL && *error != NULL))
return FALSE;

if (!glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_REPLACE,
AT_FDCWD, RPMOSTREE_DRIVER_STATE, error))
return FALSE;

return TRUE;
}

static gboolean
deploy_transaction_execute (RpmostreedTransaction *transaction,
GCancellable *cancellable,
Expand All @@ -797,6 +841,7 @@ deploy_transaction_execute (RpmostreedTransaction *transaction,

const gboolean dry_run =
((self->flags & RPMOSTREE_TRANSACTION_DEPLOY_FLAG_DRY_RUN) > 0);
const gboolean register_driver = deploy_has_bool_option (self, "register-driver");
const gboolean no_overrides = deploy_has_bool_option (self, "no-overrides");
const gboolean no_layering = deploy_has_bool_option (self, "no-layering");
const gboolean no_initramfs = deploy_has_bool_option (self, "no-initramfs");
Expand Down Expand Up @@ -1332,6 +1377,14 @@ deploy_transaction_execute (RpmostreedTransaction *transaction,
return FALSE;
changed = changed || layering_changed;

if (register_driver)
{
if (!record_driver_info (rpmostreed_transaction_get_agent_id (transaction),
rpmostreed_transaction_get_sd_unit (transaction),
cancellable, error))
return FALSE;
}

if (dry_run)
/* Note early return here; we printed the transaction already */
return TRUE;
Expand Down

0 comments on commit 6f7414c

Please sign in to comment.