summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2018-03-19 09:46:20 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-03-19 17:15:27 +0000
commit01717d7dfc516989f483ad729111793dce934789 (patch)
treef247645a016559168c6526f6b5e2f7dd5b9659fd
parentd4d193495f488e71ea89f97f17bd030a6669c61e (diff)
downloadostree-01717d7dfc516989f483ad729111793dce934789.tar.gz
main, status: Factor out deployment printing into helper
Prep for staged deployments; they won't be in the primary deployment list, and we want to print them first. Also pull in some code from rpm-ostree for the red/bold bits and use that tree-wide. Update submodule: libglnx Closes: #1504 Approved by: jlebon
m---------libglnx0
-rw-r--r--src/ostree/main.c13
-rw-r--r--src/ostree/ostree-trivial-httpd.c13
-rw-r--r--src/ostree/ot-admin-builtin-status.c211
-rw-r--r--src/ostree/ot-main.h15
5 files changed, 138 insertions, 114 deletions
diff --git a/libglnx b/libglnx
-Subproject 96b1fd9578b3d6ff2d8e0707068f5ef450eba98
+Subproject 0c82203cd459a35cc3f471e3205355e9fb79160
diff --git a/src/ostree/main.c b/src/ostree/main.c
index ddf531d1..9ed0f880 100644
--- a/src/ostree/main.c
+++ b/src/ostree/main.c
@@ -141,15 +141,10 @@ main (int argc,
if (error != NULL)
{
- int is_tty = isatty (1);
- const char *prefix = "";
- const char *suffix = "";
- if (is_tty)
- {
- prefix = "\x1b[31m\x1b[1m"; /* red, bold */
- suffix = "\x1b[22m\x1b[0m"; /* bold off, color reset */
- }
- g_printerr ("%serror: %s%s\n", prefix, suffix, error->message);
+ g_printerr ("%s%serror:%s%s %s\n",
+ ot_get_red_start (), ot_get_bold_start (),
+ ot_get_bold_end (), ot_get_red_end (),
+ error->message);
}
return ret;
diff --git a/src/ostree/ostree-trivial-httpd.c b/src/ostree/ostree-trivial-httpd.c
index 377de525..5da5a6ab 100644
--- a/src/ostree/ostree-trivial-httpd.c
+++ b/src/ostree/ostree-trivial-httpd.c
@@ -680,15 +680,10 @@ main (int argc,
if (!run (argc, argv, cancellable, &error))
{
- int is_tty = isatty (1);
- const char *prefix = "";
- const char *suffix = "";
- if (is_tty)
- {
- prefix = "\x1b[31m\x1b[1m"; /* red, bold */
- suffix = "\x1b[22m\x1b[0m"; /* bold off, color reset */
- }
- g_printerr ("%serror: %s%s\n", prefix, suffix, error->message);
+ g_printerr ("%s%serror:%s%s %s\n",
+ ot_get_red_start (), ot_get_bold_start (),
+ ot_get_bold_end (), ot_get_red_end (),
+ error->message);
return 1;
}
diff --git a/src/ostree/ot-admin-builtin-status.c b/src/ostree/ot-admin-builtin-status.c
index a2e24ef3..096155c6 100644
--- a/src/ostree/ot-admin-builtin-status.c
+++ b/src/ostree/ot-admin-builtin-status.c
@@ -27,6 +27,7 @@
#include "ot-admin-builtins.h"
#include "ot-admin-functions.h"
#include "ostree.h"
+#include "libglnx.h"
#include <glib/gi18n.h>
@@ -61,13 +62,116 @@ deployment_get_gpg_verify (OstreeDeployment *deployment,
return gpg_verify;
}
+
+static gboolean
+deployment_print_status (OstreeSysroot *sysroot,
+ OstreeRepo *repo,
+ OstreeDeployment *deployment,
+ gboolean is_booted,
+ gboolean is_pending,
+ gboolean is_rollback,
+ GCancellable *cancellable,
+ GError **error)
+{
+ const char *ref = ostree_deployment_get_csum (deployment);
+
+ /* Load the backing commit; shouldn't normally fail, but if it does,
+ * we stumble on.
+ */
+ g_autoptr(GVariant) commit = NULL;
+ (void)ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, ref,
+ &commit, NULL);
+ g_autoptr(GVariant) commit_metadata = NULL;
+ if (commit)
+ commit_metadata = g_variant_get_child_value (commit, 0);
+
+ const char *version = NULL;
+ const char *source_title = NULL;
+ if (commit_metadata)
+ {
+ (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_VERSION, "&s", &version);
+ (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_SOURCE_TITLE, "&s", &source_title);
+ }
+
+ GKeyFile *origin = ostree_deployment_get_origin (deployment);
+
+ const char *deployment_status = "";
+ if (is_pending)
+ deployment_status = " (pending)";
+ else if (is_rollback)
+ deployment_status = " (rollback)";
+ g_print ("%c %s %s.%d%s\n",
+ is_booted ? '*' : ' ',
+ ostree_deployment_get_osname (deployment),
+ ostree_deployment_get_csum (deployment),
+ ostree_deployment_get_deployserial (deployment),
+ deployment_status);
+ if (version)
+ g_print (" Version: %s\n", version);
+
+ OstreeDeploymentUnlockedState unlocked = ostree_deployment_get_unlocked (deployment);
+ switch (unlocked)
+ {
+ case OSTREE_DEPLOYMENT_UNLOCKED_NONE:
+ break;
+ default:
+ g_print (" %s%sUnlocked: %s%s%s\n", ot_get_red_start (), ot_get_bold_start (),
+ ostree_deployment_unlocked_state_to_string (unlocked),
+ ot_get_bold_end (), ot_get_red_end ());
+ }
+ if (ostree_deployment_is_pinned (deployment))
+ g_print (" Pinned: yes\n");
+ if (!origin)
+ g_print (" origin: none\n");
+ else
+ {
+ g_autofree char *origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
+ if (!origin_refspec)
+ g_print (" origin: <unknown origin type>\n");
+ else
+ g_print (" origin refspec: %s\n", origin_refspec);
+ if (source_title)
+ g_print (" `- %s\n", source_title);
+ }
+
+ if (deployment_get_gpg_verify (deployment, repo))
+ {
+ g_autoptr(GString) output_buffer = g_string_sized_new (256);
+ /* Print any digital signatures on this commit. */
+
+ g_autoptr(GError) local_error = NULL;
+ g_autoptr(OstreeGpgVerifyResult) result =
+ ostree_repo_verify_commit_ext (repo, ref, NULL, NULL,
+ cancellable, &local_error);
+
+ /* G_IO_ERROR_NOT_FOUND just means the commit is not signed. */
+ if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+ {
+ g_clear_error (&local_error);
+ return TRUE;
+ }
+ else if (local_error != NULL)
+ {
+ g_propagate_error (error, g_steal_pointer (&local_error));
+ return FALSE;
+ }
+
+ const guint n_signatures = ostree_gpg_verify_result_count_all (result);
+ for (guint jj = 0; jj < n_signatures; jj++)
+ {
+ ostree_gpg_verify_result_describe (result, jj, output_buffer, " GPG: ",
+ OSTREE_GPG_SIGNATURE_FORMAT_DEFAULT);
+ }
+
+ g_print ("%s", output_buffer->str);
+ }
+
+ return TRUE;
+}
+
gboolean
ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
- const int is_tty = isatty (1);
- const char *red_bold_prefix = is_tty ? "\x1b[31m\x1b[1m" : "";
- const char *red_bold_suffix = is_tty ? "\x1b[22m\x1b[0m" : "";
-
g_autoptr(GOptionContext) context = g_option_context_new ("");
g_autoptr(OstreeSysroot) sysroot = NULL;
@@ -98,98 +202,13 @@ ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocat
for (guint i = 0; i < deployments->len; i++)
{
OstreeDeployment *deployment = deployments->pdata[i];
- const char *ref = ostree_deployment_get_csum (deployment);
-
- /* Load the backing commit; shouldn't normally fail, but if it does,
- * we stumble on.
- */
- g_autoptr(GVariant) commit = NULL;
- (void)ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, ref,
- &commit, NULL);
- g_autoptr(GVariant) commit_metadata = NULL;
- if (commit)
- commit_metadata = g_variant_get_child_value (commit, 0);
-
- const char *version = NULL;
- const char *source_title = NULL;
- if (commit_metadata)
- {
- (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_VERSION, "&s", &version);
- (void) g_variant_lookup (commit_metadata, OSTREE_COMMIT_META_KEY_SOURCE_TITLE, "&s", &source_title);
- }
-
- GKeyFile *origin = ostree_deployment_get_origin (deployment);
-
- const char *deployment_status = "";
- if (deployment == pending_deployment)
- deployment_status = " (pending)";
- else if (deployment == rollback_deployment)
- deployment_status = " (rollback)";
- g_print ("%c %s %s.%d%s\n",
- deployment == booted_deployment ? '*' : ' ',
- ostree_deployment_get_osname (deployment),
- ostree_deployment_get_csum (deployment),
- ostree_deployment_get_deployserial (deployment),
- deployment_status);
- if (version)
- g_print (" Version: %s\n", version);
-
- OstreeDeploymentUnlockedState unlocked = ostree_deployment_get_unlocked (deployment);
- switch (unlocked)
- {
- case OSTREE_DEPLOYMENT_UNLOCKED_NONE:
- break;
- default:
- g_print (" %sUnlocked: %s%s\n", red_bold_prefix,
- ostree_deployment_unlocked_state_to_string (unlocked),
- red_bold_suffix);
- }
- if (ostree_deployment_is_pinned (deployment))
- g_print (" Pinned: yes\n");
- if (!origin)
- g_print (" origin: none\n");
- else
- {
- g_autofree char *origin_refspec = g_key_file_get_string (origin, "origin", "refspec", NULL);
- if (!origin_refspec)
- g_print (" origin: <unknown origin type>\n");
- else
- g_print (" origin refspec: %s\n", origin_refspec);
- if (source_title)
- g_print (" `- %s\n", source_title);
- }
-
- if (deployment_get_gpg_verify (deployment, repo))
- {
- g_autoptr(GString) output_buffer = g_string_sized_new (256);
- /* Print any digital signatures on this commit. */
-
- g_autoptr(GError) local_error = NULL;
- g_autoptr(OstreeGpgVerifyResult) result =
- ostree_repo_verify_commit_ext (repo, ref, NULL, NULL,
- cancellable, &local_error);
-
- /* G_IO_ERROR_NOT_FOUND just means the commit is not signed. */
- if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
- {
- g_clear_error (&local_error);
- continue;
- }
- else if (local_error != NULL)
- {
- g_propagate_error (error, g_steal_pointer (&local_error));
- return FALSE;
- }
-
- const guint n_signatures = ostree_gpg_verify_result_count_all (result);
- for (guint jj = 0; jj < n_signatures; jj++)
- {
- ostree_gpg_verify_result_describe (result, jj, output_buffer, " GPG: ",
- OSTREE_GPG_SIGNATURE_FORMAT_DEFAULT);
- }
-
- g_print ("%s", output_buffer->str);
- }
+ if (!deployment_print_status (sysroot, repo, deployment,
+ deployment == booted_deployment,
+ deployment == pending_deployment,
+ deployment == rollback_deployment,
+ cancellable,
+ error))
+ return FALSE;
}
}
diff --git a/src/ostree/ot-main.h b/src/ostree/ot-main.h
index c5ffeb7f..ac75118c 100644
--- a/src/ostree/ot-main.h
+++ b/src/ostree/ot-main.h
@@ -91,3 +91,18 @@ gboolean ostree_ensure_repo_writable (OstreeRepo *repo, GError **error);
void ostree_print_gpg_verify_result (OstreeGpgVerifyResult *result);
gboolean ot_enable_tombstone_commits (OstreeRepo *repo, GError **error);
+
+/* Copied from rpm-ostree's rpmostree-libbuiltin.h */
+#define TERM_ESCAPE_SEQUENCE(type,seq) \
+ static inline const char* ot_get_##type (void) { \
+ if (glnx_stdout_is_tty ()) \
+ return seq; \
+ return ""; \
+ }
+
+TERM_ESCAPE_SEQUENCE(red_start, "\x1b[31m")
+TERM_ESCAPE_SEQUENCE(red_end, "\x1b[22m")
+TERM_ESCAPE_SEQUENCE(bold_start, "\x1b[1m")
+TERM_ESCAPE_SEQUENCE(bold_end, "\x1b[0m")
+
+#undef TERM_ESCAPE_SEQUENCE