From cf1cb497d81aa7ebac6784c6a2c4513c74346232 Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Tue, 17 Jul 2018 10:55:06 +0200 Subject: [PATCH 1/7] Fix shellcheck warnings This should fix the build failing. For reference, the warnings fixed are: * https://github.com/koalaman/shellcheck/wiki/SC2181 * https://github.com/koalaman/shellcheck/wiki/SC1117 --- create-lx-image | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/create-lx-image b/create-lx-image index 3a43d27..6e6e218 100755 --- a/create-lx-image +++ b/create-lx-image @@ -82,17 +82,17 @@ if [[ -z $TARBALL ]]; then fi if [[ ! -a "$TARBALL" ]]; then - printf "==> ERROR: %s: file or directory not found\n" $TARBALL + printf '==> ERROR: %s: file or directory not found\n' "$TARBALL" exit 1 fi if [[ "$(echo $TARBALL | cut -c 1)" != "/" ]]; then - printf "==> Pathname '%s' specified to -t must be absolute\n" $TARBALL + printf '==> Pathname '%s' specified to -t must be absolute\n' "$TARBALL" exit 1 fi if [[ ! -r "$TARBALL" ]]; then - printf "==> Cannot read file '%s'\n" $TARBALL + printf '==> Cannot read file '%s'\n' "$TARBALL" exit 1 fi @@ -164,8 +164,7 @@ create_dataset() { install_tar() { echo "==> Installing the tar archive, this will take a few minutes..." - ( cd "$zroot" && gtar --strip-components=2 "$gtaropts" "$TARBALL" ) - if [[ "$?" -ne "0" ]] ; then + if [[ ! $(cd "$zroot" && gtar --strip-components=2 "$gtaropts" "$TARBALL") ]] ; then echo "==> Error: extraction from tar archive failed." zfs destroy -r zones/$IUUID fi From 7124451f88d044a48a51ab74f4463b0483e6257d Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Tue, 17 Jul 2018 13:25:20 +0200 Subject: [PATCH 2/7] Cleanup -h and -? Lots of small cleanup around here. Added '?' to the getopts loop list, escaped '?' appropriately, and no longer exit with error when the user requests the help text to print. --- create-lx-image | 10 +++------- create-manifest | 4 +++- install | 8 ++------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/create-lx-image b/create-lx-image index 6e6e218..2c30184 100755 --- a/create-lx-image +++ b/create-lx-image @@ -43,12 +43,12 @@ OPTIONS: EOF } -while getopts "ht:k:m:i:d:u:" OPTION +while getopts "ht:k:m:i:d:u:?" OPTION do case $OPTION in - h) + h|\?) usage - exit 1 + exit ;; t) TARBALL=${OPTARG} @@ -68,10 +68,6 @@ do u) DOCS=${OPTARG} ;; - ?) - usage - exit - ;; esac done diff --git a/create-manifest b/create-manifest index 080332d..98f5ceb 100755 --- a/create-manifest +++ b/create-manifest @@ -47,7 +47,6 @@ Example: $0 -f debian-7-20150319.zfs.gz -k 3.4.105 -m 20150316T201553Z -n lx-debian-7 -v 20150319 USAGE - exit 1 } while getopts d:f:k:m:n:o:v:h:? OPTION @@ -79,6 +78,7 @@ do ;; \?) usage + exit ;; esac done @@ -86,6 +86,7 @@ done if [[ -z ${filename} || -z ${kernel} || -z ${min_platform} || -z ${name} || -z ${version} ]]; then echo "FATAL: All of -f, -k, -m, -n and -v are required." usage + exit 1 fi if [[ -z ${description} ]]; then @@ -103,6 +104,7 @@ fi if [[ ! -f ${filename} ]]; then echo "FATAL: ${filename} does not exist." usage + exit 1 fi shasum=$($SUM -x sha1 ${filename} | cut -d' ' -f1) diff --git a/install b/install index 3b28e23..2eb9e43 100755 --- a/install +++ b/install @@ -48,10 +48,10 @@ NAME= DESC= DOCS= -while getopts "hr:d:m:i:p:D:u:" OPTION +while getopts "hr:d:m:i:p:D:u:?" OPTION do case $OPTION in - h) + h|\?) usage exit ;; @@ -76,10 +76,6 @@ do u) DOCS=${OPTARG} ;; - \?) - usage - exit - ;; esac done From 6f9b7af20059881bc447d8232f9d34e89ce17f12 Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Tue, 17 Jul 2018 13:32:44 +0200 Subject: [PATCH 3/7] Condense and unify checks for required flags The ':' in getops already ensures that these flags are passed arguments. This checks the argument vars (which will be set) to infer whether these flags were declared. --- create-lx-image | 31 +++++-------------------------- install | 28 ++-------------------------- 2 files changed, 7 insertions(+), 52 deletions(-) diff --git a/create-lx-image b/create-lx-image index 2c30184..dcad1df 100755 --- a/create-lx-image +++ b/create-lx-image @@ -71,12 +71,15 @@ do esac done -if [[ -z $TARBALL ]]; then - echo "==> ERROR: the install archive is required" +if [[ -z "$TARBALL" || -z "$KERNEL" || -z "$MIN_PLATFORM" || \ + -z "$IMAGE_NAME" || -z "$DESC" ]] ; then + printf 'FATAL: -t, -k, -m, -i, and -d options are all required.\n' usage exit 1 fi +[ -z "$DOCS" ] && DOCS="https://docs.joyent.com/images/container-native-linux" + if [[ ! -a "$TARBALL" ]]; then printf '==> ERROR: %s: file or directory not found\n' "$TARBALL" exit 1 @@ -97,30 +100,6 @@ if [[ ! -f "$TARBALL" ]]; then exit 1 fi -if [[ -z ${KERNEL} ]]; then - echo "Error: missing kernel version (-k) value" - exit 1 -fi - -if [[ -z ${MIN_PLATFORM} ]]; then - echo "Error: missing minimum platform (-m) value" - exit 1 -fi - -if [[ -z ${IMAGE_NAME} ]]; then - echo "Error: missing image name (-i) value" - exit 1 -fi - -if [[ -z ${DESC} ]]; then - echo "Error: missing image description (-d) value" - exit 1 -fi - -if [[ -z ${DOCS} ]]; then - DOCS="https://docs.joyent.com/images/container-native-linux" -fi - IUUID=${IMAGE_NAME}-$BUILD_DATE filetype=$({ LC_ALL=C file $TARBALL | awk '{print $2}' ; } 2>/dev/null) diff --git a/install b/install index 2eb9e43..f6464c2 100755 --- a/install +++ b/install @@ -79,41 +79,17 @@ do esac done -if [[ $# -eq 0 ]]; then +if [[ -z "$RELEASE" || "$INSTALL_DIR" || "$MIRROR" || "$IMAGE_NAME" || "$NAME" ]]; then + printf 'FATAL: -r, -d, -m, -i, and -p are all required.\n' usage exit 1 fi -if [[ -z ${RELEASE} ]]; then - echo "Error: missing release (-r) value" - exit 1 -fi - if [[ ! -e ${INSTALL_DIR} ]] ; then echo "Directory $INSTALL_DIR not found" exit 1 fi -if [[ -z ${INSTALL_DIR} ]]; then - echo "Error: missing install directory (-d) value" - exit 1 -fi - -if [[ -z ${MIRROR} ]]; then - echo "Error: missing mirror (-m) value" - exit 1 -fi - -if [[ -z ${IMAGE_NAME} ]]; then - echo "Error: missing image name (-i) value" - exit 1 -fi - -if [[ -z ${NAME} ]]; then - echo "Error: missing proper name (-p) value" - exit 1 -fi - TARGET="${IMAGE_NAME}-${BUILD_DATE}.tar.gz" if [[ -z ${DOCS} ]]; then From 28a0da85fcab14d519ce59652caf5d95140f33b2 Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Tue, 17 Jul 2018 13:12:27 +0200 Subject: [PATCH 4/7] -u (docs) is optional; help output should reflect that --- create-lx-image | 4 ++-- install | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/create-lx-image b/create-lx-image index dcad1df..747fb64 100755 --- a/create-lx-image +++ b/create-lx-image @@ -26,7 +26,7 @@ cat < -k -m -i -d -u + $0 -t -k -m -i -d [-u ] Example: $0 -t /var/tmp/lx-debian-7-20150408.tar.gz -k 3.13.0 -m 20150316T201553Z -i lx-debian-7 -d "Debian 7 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux @@ -37,7 +37,7 @@ OPTIONS: -m The minimum platform required for the image -i The name of the image as it would appear in the manifest -d The description of the image as it would appear in the manifest - -u The homepage link of the image as it would appear in the manifest + -u The homepage link of the image as it would appear in the manifest [optional] -h Show this message EOF diff --git a/install b/install index f6464c2..151410c 100755 --- a/install +++ b/install @@ -22,7 +22,7 @@ cat < -d -m -i -p -D -u + $0 -r -d -m -i -p -D [-u ] Example: $0 -r wheezy -d /data/chroot -m http://httpredir.debian.org/debian/ -i lx-debian-7 -p "Debian 7 LX Brand" -D "Debian 7 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux From b86410b308bae7dc8b2bfb77540f90eee04705e8 Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Tue, 17 Jul 2018 13:49:59 +0200 Subject: [PATCH 5/7] Condense create-lx-image's sanity checks for TARBALL A few notes: * [-a ] is deprecated in favor of the more common [ -e ] * Bash supports Substring Extraction, so even less of a need to invoke `cut` and a subshell to get one character. * use shell's pattern matching rather than invoking awk; use file's -b option for convenience. --- create-lx-image | 51 ++++++++++++++----------------------------------- 1 file changed, 14 insertions(+), 37 deletions(-) diff --git a/create-lx-image b/create-lx-image index 747fb64..9ceac52 100755 --- a/create-lx-image +++ b/create-lx-image @@ -52,6 +52,20 @@ do ;; t) TARBALL=${OPTARG} + + [ ! -e "$TARBALL" ] && echo "==> ERROR: ${TARBALL}: file or directory not found" && exit 1 + [ ! -f "$TARBALL" ] && echo "==> ERROR: $TARBALL must be a file" && exit 1 + [ ! -r "$TARBALL" ] && echo "==> ERROR: $TARBALL is not readable" && exit 1 + [ "${TARBALL:0:1}" != "/" ] && echo "==> ERROR: the pathname '$TARBALL' specified to '-t' must be absolute" && exit 1 + + filetype=$(LC_ALL=C file -b "$TARBALL" 2> /dev/null) + case "$filetype" in + gzip*) gtaropts="-xzf" ;; + bzip2*) gtaropts="-xjf" ;; + compressed*) gtaropts="-xZf" ;; + USTAR*) gtaropts="-xf" ;; + *) echo "==> ERROR: $TARBALL must be a gzip, bzip2, .Z, or uncompressed tar archive." && exit 1 ;; + esac ;; k) KERNEL=${OPTARG} @@ -80,45 +94,8 @@ fi [ -z "$DOCS" ] && DOCS="https://docs.joyent.com/images/container-native-linux" -if [[ ! -a "$TARBALL" ]]; then - printf '==> ERROR: %s: file or directory not found\n' "$TARBALL" - exit 1 -fi - -if [[ "$(echo $TARBALL | cut -c 1)" != "/" ]]; then - printf '==> Pathname '%s' specified to -t must be absolute\n' "$TARBALL" - exit 1 -fi - -if [[ ! -r "$TARBALL" ]]; then - printf '==> Cannot read file '%s'\n' "$TARBALL" - exit 1 -fi - -if [[ ! -f "$TARBALL" ]]; then - echo "==> ERROR: must be a gzip, bzip2, .Z or uncompressed tar archive" - exit 1 -fi - IUUID=${IMAGE_NAME}-$BUILD_DATE -filetype=$({ LC_ALL=C file $TARBALL | awk '{print $2}' ; } 2>/dev/null) - -if [[ "$filetype" = "gzip" ]]; then - gtaropts="-xz" -elif [[ "$filetype" = "bzip2" ]]; then - gtaropts="-xj" -elif [[ "$filetype" = "compressed" ]]; then - gtaropts="-xZ" -elif [[ "$filetype" = "USTAR" ]]; then - gtaropts="-x" -else - printf "==> ERROR: must be a gzip, bzip2, .Z or uncompressed tar archive" - exit 1 -fi - -gtaropts="${gtaropts}f" - zpath="/zones/$IUUID" zroot="/zones/$IUUID/root" From 86ffa1ced264230b2946770df44bfa6d8e5ba59a Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Wed, 18 Jul 2018 15:52:02 +0200 Subject: [PATCH 6/7] typo: shecllcheck -> shellcheck --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7db9816..da50464 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Debian lx-brand Image Builder -[![Build Status](https://travis-ci.org/joyent/debian-lx-brand-image-builder.svg?branch=master)](https://travis-ci.org/joyent/debian-lx-brand-image-builder) (shecllcheck) +[![Build Status](https://travis-ci.org/joyent/debian-lx-brand-image-builder.svg?branch=master)](https://travis-ci.org/joyent/debian-lx-brand-image-builder) (shellcheck) This is a collection of scripts used for creating an lx-brand Debian image. From 69610be8d7eade086c737175785ffdc35aab4971 Mon Sep 17 00:00:00 2001 From: Alex Waite Date: Fri, 20 Jul 2018 14:47:11 +0200 Subject: [PATCH 7/7] Use trusty backports; not Debian sid Travis's stuff is... odd. They white list a bajillion external repos, but not Canonical's official backports for Trusty. One of many unfortunate oddities on the service. Because it's a new repo, I have to use "require: sudo", which gets us on the VM infra rather than the container infra. It's worth it though, to actually get this working. --- .travis.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8916a91..56fa3b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ -language: bash -addons: - apt: - sources: - - debian-sid # Grab ShellCheck from the Debian repo - packages: - - shellcheck +sudo: required +dist: trusty +before_install: + - sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse' + - sudo apt-get update -q + - sudo apt-get install shellcheck -y script: shellcheck -e SC2086 -s bash create-lx-image create-manifest install