package_to_container: Add option for container name

Some packages may build multiple executables, but we
want to have the container name reflect the specific
binary the container was created for. This adds an
option to allow the user to select a container name
that is different from the package name.

BUG=chromium:624334
TEST=Run package_to_container with the --name option.

Change-Id: Ib6035d3704386e9a1e819b0155d2efed4be8dd2a
Reviewed-on: https://chromium-review.googlesource.com/363504
Commit-Ready: Keshav Santhanam <ksanthanam@google.com>
Tested-by: Keshav Santhanam <ksanthanam@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
diff --git a/package_to_container b/package_to_container
index 91072f0..6cab2c0 100755
--- a/package_to_container
+++ b/package_to_container
@@ -11,6 +11,8 @@
   "The board to build an image for."
 DEFINE_string package "" \
   "Package whose bare minimum deps the final container should have."
+DEFINE_string name "" \
+  "Name of the container."
 DEFINE_string extra "" \
   "Comma separated extra packages to be included in the final image."
 
@@ -21,6 +23,7 @@
 # Produce the container image.
 build_container_image() {
   local pkg_name="$1"
+  local container_name="$2"
   local ROOTDIR="out"
 
   info "Cleaning previously generated files ... "
@@ -43,7 +46,7 @@
   sudo mkdir -p "${ROOTDIR}"/{dev,proc,root,run/broker_service,sys}
 
   info "Generating squashfs file ... "
-  mksquashfs "${ROOTDIR}" "${pkg_name}.sqsh"
+  mksquashfs "${ROOTDIR}" "${container_name}.sqsh"
 
   info "Cleaning up ... "
   sudo rm -rf "${ROOTDIR}"
@@ -72,11 +75,16 @@
   . "${BUILD_LIBRARY_DIR}/base_image_util.sh" || exit 1
 
   if [[ -n "${FLAGS_package}" ]]; then
-    if [[ -f "${FLAGS_package}.sqsh" ]]; then
-      die_notrace "File already exists : ${FLAGS_package}.sqsh"
+    if [[ -n "${FLAGS_name}" ]]; then
+      local container_name="${FLAGS_name}"
+    else
+      local container_name="${FLAGS_package}"
     fi
-    info "Building container for ${FLAGS_package} ..."
-    build_container_image "${FLAGS_package}"
+    if [[ -f "${container_name}.sqsh" ]]; then
+      die_notrace "File already exists : ${container_name}.sqsh"
+    fi
+    info "Building container '${container_name}' for ${FLAGS_package} ..."
+    build_container_image "${FLAGS_package}" "${container_name}"
   else
     die_notrace "--package is needed."
   fi