[cowdancer] 03/03: Use pbuilder-style coloured logging for qemubuilder

James Clarke jrtc27-guest at moszumanska.debian.org
Thu Sep 8 21:22:47 UTC 2016


This is an automated email from the git hooks/post-receive script.

jrtc27-guest pushed a commit to branch master
in repository cowdancer.

commit d6e400e6dd4ed2bfe8fcd4023a986281fd635021
Author: James Clarke <jrtc27 at jrtc27.com>
Date:   Thu Sep 8 21:44:56 2016 +0100

    Use pbuilder-style coloured logging for qemubuilder
---
 qemubuilder.c | 177 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 127 insertions(+), 50 deletions(-)

diff --git a/qemubuilder.c b/qemubuilder.c
index aa2fd83..e018815 100755
--- a/qemubuilder.c
+++ b/qemubuilder.c
@@ -52,23 +52,23 @@ indent"if [ -d \""CHROOT_HOOKDIR"\" ]; then\n" \
 indent"    for fn in \""CHROOT_HOOKDIR"/"prefix"\"[0-9][0-9]* ; do\n" \
 indent"        case \"$fn\" in\n" \
 indent"            \""CHROOT_HOOKDIR"/"prefix"\"'[0-9][0-9]*')\n" \
-indent"                echo \"W: no hooks of type "prefix" found -- ignoring\"\n" \
+indent"                log.d \"no hooks of type "prefix" found -- ignoring\"\n" \
 indent"                ;;\n" \
 indent"            *~)\n" \
-indent"                echo \"W: skipping an editor backup file $fn\"\n" \
+indent"                log.w \"skipping an editor backup file $fn\"\n" \
 indent"                ;;\n" \
 indent"            *)\n" \
 indent"                if [ -x \"$fn\" ]; then\n" \
-indent"                    echo \"I: user script $fn starting\"\n" \
+indent"                    log.i \"user script $fn starting\"\n" \
 indent"                    \""CHROOT_HOOKDIR"/$(basename \"$fn\")\"\n" \
-indent"                    echo \"I: user script $fn finished\"\n" \
+indent"                    log.i \"user script $fn finished\"\n" \
 indent"                else\n" \
 indent"                    if [ -f \"$fn\" ]; then\n" \
 indent"                        filetype=$(basename \"$fn\")\n" \
-indent"                        echo \"W: execute priv not set on file $filetype, not executing.\"\n" \
+indent"                        log.w \"execute priv not set on file $filetype, not executing.\"\n" \
 indent"                    else\n" \
 indent"                        # Should it reach here ? This case should be caught in the above case.\n" \
-indent"                        echo \"W: no hooks of type ${prefix} found -- internal error in logic\"\n" \
+indent"                        log.w \"no hooks of type ${prefix} found -- internal error in logic\"\n" \
 indent"                    fi\n" \
 indent"                fi\n" \
 indent"                ;;\n" \
@@ -246,9 +246,9 @@ static int copy_file_contents_in_temp(FILE *f,
   int trailing_slash = targetdir[strlen(targetdir)-1] == '/';
   const char* sep = trailing_slash ? "" : "/";
   fprintf(f,
-	  "echo \"I: copying %s%s%s from temporary location\"\n"
+	  "log.i \"copying %s%s%s from temporary location\"\n"
 	  "mkdir -p %s\n"
-	  "cp $BUILDDIR/%s %s%s%s || echo \"E: Copy failed\"\n",
+	  "cp $BUILDDIR/%s %s%s%s || log.e \"Copy failed\"\n",
 	  targetdir, sep, targetname,
 	  targetdir,
 	  tempname,
@@ -700,6 +700,8 @@ static void write_first_stage(FILE *f, const struct pbuilderconfig* pc)
   fprintf(f,
 	  "#!/bin/bash\n"
 	  "echo \n"
+	  /* Can't use log.i or check LOGLEVEL in first stage, since this lives
+	   * inside the base chroot, and has not yet mounted the input disk. */
 	  "echo 'I: qemu-pbuilder first-stage' \n"
 	  "export PBUILDER_INIT_VERSION="XSTR(PBUILDER_INIT_VERSION)"\n"
 	  "export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'\n"
@@ -725,6 +727,108 @@ static void write_first_stage(FILE *f, const struct pbuilderconfig* pc)
 	  );
 }
 
+static void write_second_stage_header(FILE *f, int debug_shell) {
+  const char *log_level;
+
+  switch (log_get_filter_level()) {
+    case log_debug: log_level = "D"; break;
+    case log_info:  log_level = "I"; break;
+    case log_warn:  log_level = "W"; break;
+    case log_error: log_level = "E"; break;
+    default:        log_level = "I"; break;
+  }
+
+  fprintf(f,
+	  "#!/bin/bash\n"
+
+	  /* Will not be auto (using log_get_use_colors, not _unresolved) */
+	  "export USECOLORS=\"%s\"\n"
+	  "export LOGLEVEL=\"%s\"\n"
+
+	  /* Bash logging taken from pbuilder */
+	  "# Log a message\n"
+	  "# message is of a format\n"
+	  "#  E: error message\n"
+	  "#  W: warning message\n"
+	  "#  I: informational message\n"
+	  "_log() {\n"
+	  "    set -u\n"
+	  "    local color=\"$1\" ; shift\n"
+	  "    local red='\\033[0;31m'\n"
+	  "    local yellow='\\033[1;33m'\n"
+	  "    local blue='\\033[0;34m'\n"
+	  "    local reset='\\033[0m'\n"
+	  "    case \"$USECOLORS\" in\n"
+	  "        yes)\n"
+	  "            printf \"${!color}${*}${reset}\\n\"\n"
+	  "            ;;\n"
+	  "        no)\n"
+	  "            printf \"${*}\\n\"\n"
+	  "            ;;\n"
+	  "        *)\n"
+	  "            printf \"malformed value of USECOLORS: [%%s]\\n\" \"$USECOLORS\" >&2\n"
+	  "            exit 1\n"
+	  "            ;;\n"
+	  "    esac\n"
+	  "    set +u\n"
+	  "}\n"
+	  "function log() {\n"
+	  "    case \"$*\" in\n"
+	  "        \"E: \"*)\n"
+	  "            _log 'red' \"$*\" >&2\n"
+	  "            ;;\n"
+	  "        \"W: \"*)\n"
+	  "            _log 'yellow' \"$*\" >&2\n"
+	  "            ;;\n"
+	  "        \"I: \"*)\n"
+	  "            _log 'reset' \"$*\"\n"
+	  "            ;;\n"
+	  "        \"D: \"*)\n"
+	  "            _log 'blue' \"$*\"\n"
+	  "            ;;\n"
+	  "        *)\n"
+	  "            echo \"malformed log message: $*\" >&2\n"
+	  "            exit 1\n"
+	  "            ;;\n"
+	  "    esac\n"
+	  "}\n"
+
+	  "log.e() {\n"
+	  "    case \"$LOGLEVEL\" in\n"
+	  "        D|I|W|E) log \"E: $*\" ;;\n"
+	  "    esac\n"
+	  "}\n"
+	  "log.w() {\n"
+	  "    case \"$LOGLEVEL\" in\n"
+	  "        D|I|W) log \"W: $*\" ;;\n"
+	  "    esac\n"
+	  "}\n"
+	  "log.i() {\n"
+	  "    case \"$LOGLEVEL\" in\n"
+	  "        D|I) log \"I: $*\" ;;\n"
+	  "    esac\n"
+	  "}\n"
+	  "log.d() {\n"
+	  "    case \"$LOGLEVEL\" in\n"
+	  "        D) log \"D: $*\" ;;\n"
+	  "    esac\n"
+	  "}\n"
+
+	  /* define function to terminate qemu */
+	  "function exit_from_qemu() {\n"
+	  "    %s\n"
+	  "    sync\n"
+	  "    sync\n"
+	  "    sleep 1s\n"		/* sleep before sending dying message */
+	  "    log.i 'qemu-pbuilder %s'\"$1\"\n"
+	  "    sleep 1s\n"
+	  "    halt -f -p\n"		/* just halt myself if possible */
+	  "}\n",
+	  log_get_use_colors() == log_use_colors_yes ? "yes" : "no",
+	  log_level,
+	  debug_shell?"log.i \"Debug shell\"; /bin/bash":"",
+	  qemu_keyword);
+}
 
 /**
  * Invoke qemu, and run the second-stage script within QEMU.
@@ -767,38 +871,25 @@ static int run_second_stage_script
   loop_mount(workblockdevicepath, pc->buildplace);
 
   f = create_script(pc->buildplace, "input/pbuilder-run");
-  fprintf(f,
-	  "#!/bin/bash\n"
-
-	  /* define function to terminate qemu */
-	  "function exit_from_qemu() {\n"
-	  "sync\n"
-	  "sync\n"
-	  "sleep 1s\n"		/* sleep before sending dying message */
-	  "echo 'I: qemu-pbuilder %s'\"$1\"\n"
-	  "sleep 1s\n"
-	  "halt -f -p\n"	/* just halt myself if possible */
-	  "}\n",
-	  qemu_keyword);
-
+  write_second_stage_header(f, 0);
   fprintf(f,
 	  /* main code */
 	  "echo \n"
-	  "echo 'I: qemu-pbuilder second-stage' \n"
+	  "log.i 'qemu-pbuilder second-stage' \n"
 	  // Remove compatibility symlink
 	  "rm \"$BUILDDIR\"/pbuilder-run\n"
 	  //"mount -n /proc /proc -t proc\n" // this is done in first stage.
 	  "if [ \"${PBUILDER_INIT_VERSION:-0}\" -lt "XSTR(PBUILDER_INIT_VERSION)" ]; then\n"
-	  "    echo \"E: qemubuilder init script is out of date (${PBUILDER_INIT_VERSION:-0} < "XSTR(PBUILDER_INIT_VERSION)")\"\n"
-	  "    echo \"E: Please run qemubuilder --update\"\n"
+	  "    log.e \"qemubuilder init script is out of date (${PBUILDER_INIT_VERSION:-0} < "XSTR(PBUILDER_INIT_VERSION)")\"\n"
+	  "    log.e \"Please run qemubuilder --update\"\n"
 	  "    exit_from_qemu 1\n"
 	  "elif [ \"${PBUILDER_INIT_VERSION:-0}\" -gt "XSTR(PBUILDER_INIT_VERSION)" ]; then\n"
-	  "    echo \"E: qemubuilder init script is newer than expected (${PBUILDER_INIT_VERSION:-0} < "XSTR(PBUILDER_INIT_VERSION)")\"\n"
+	  "    log.e \"qemubuilder init script is newer than expected (${PBUILDER_INIT_VERSION:-0} < "XSTR(PBUILDER_INIT_VERSION)")\"\n"
 	  "    exit_from_qemu 1\n"
 	  "fi\n"
-	  "echo 'I: setting time to %s' \n"
+	  "log.i 'setting time to %s' \n"
 	  "date --set=\"%s\"\n"
-	  "echo 'I: configuring network' \n"
+	  "log.i 'configuring network' \n"
 	  "ifconfig -a\n"
 	  "export IFNAME=`/sbin/ifconfig -a | grep eth | head -n1 | awk '{print $1}'`\n"
 	  "dhclient $IFNAME\n"
@@ -1076,32 +1167,18 @@ int cpbuilder_create(const struct pbuilderconfig* pc)
   timestring=get_current_time_string();
 
   f = create_script(pc->buildplace, "input/pbuilder-run");
-  fprintf(f,
-	  "#!/bin/bash\n"
-	  /* define function to terminate qemu */
-	  "function exit_from_qemu() {\n"
-	  "%s\n"
-	  "sync\n"
-	  "sync\n"
-	  "sleep 1s\n"		/* sleep before sending dying message */
-	  "echo 'I: qemu-pbuilder %s$1'\n"
-	  "sleep 1s\n"
-	  "halt -f -p\n"	/* just halt myself if possible */
-	  "}\n",
-	  pc->debug?"echo \"Debug shell\"; /bin/bash":"",
-	  qemu_keyword);
-
+  write_second_stage_header(f, pc->debug);
   fprintf(f,
 	  /* start of main code */
 	  "export RET=0\n"
 	  "echo \n"
-	  "echo 'I: qemu-pbuilder second-stage' \n"
-	  "echo 'I: setting time to %s' \n"
+	  "log.i 'qemu-pbuilder second-stage' \n"
+	  "log.i 'setting time to %s' \n"
 	  "date --set=\"%s\"\n"
-	  "echo 'I: Running debootstrap second-stage script' \n"
+	  "log.i 'Running debootstrap second-stage script' \n"
 	  "touch /etc/udev/disabled\n" // work-around for #520742
 	  "/debootstrap/debootstrap --second-stage || ( "
-	  "  echo dumping debootstrap log\n"
+	  "  log.i \"dumping debootstrap log\"\n"
 	  "  cat /debootstrap/debootstrap.log\n"
 	  "  exit_from_qemu\n"
 	  "\n )\n"
@@ -1214,7 +1291,7 @@ int cpbuilder_build(const struct pbuilderconfig* pc, const char* dscfile)
 	   "ALLOWUNTRUSTED=%s /usr/lib/pbuilder/pbuilder-satisfydepends --control $BUILDDIR/*.dsc --internal-chrootexec 'chroot . ' %s \n"
 	   "apt-get install %s -y %s\n"
 	   "cd $BUILDDIR; /usr/bin/dpkg-source -x $(basename %s) \n"
-	   "echo 'I: Building the package'\n"
+	   "log.i 'Building the package'\n"
 	   EXECUTE_HOOKS("A")
 	   "if ! (\n"
 	   "    cd $BUILDDIR/*-*/\n"
@@ -1248,7 +1325,7 @@ int cpbuilder_build(const struct pbuilderconfig* pc, const char* dscfile)
 	   "        fi\n"
 	   "    done\n"
 	   "else\n"
-	   "    echo \"E: BUILDRESULT=[$BUILDRESULT] is not a directory.\"\n"
+	   "    log.e \"BUILDRESULT=[$BUILDRESULT] is not a directory.\"\n"
 	   "fi\n",
 	   pc->buildplace, pc->buildresult,
 	   pc->buildresultuid, pc->buildresultgid);
@@ -1289,7 +1366,7 @@ int cpbuilder_execute(const struct pbuilderconfig* pc, char** av)
   int ret;
 
   asprintf(&hostcommand,
-	   "[ -d %s/input ] || mkdir %s/input\n || echo \"E: Failed to create directory '%s/input'\"\n"
+	   "[ -d %s/input ] || mkdir %s/input\n || log.e \"Failed to create directory '%s/input'\"\n"
 	   "cp %s %s/input/runscript\n",
 	   pc->buildplace, pc->buildplace, pc->buildplace,
 	   av[0], pc->buildplace);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pbuilder/cowdancer.git



More information about the Pbuilder-maint mailing list