Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
Addresses all issues found by 'shellcheck'
Browse files Browse the repository at this point in the history
Signed-off-by: Murilo Belluzzo <[email protected]>
  • Loading branch information
mbelluzzo-intel committed Nov 27, 2018
1 parent 7b4cbac commit 51e6933
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 181 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ DSTREAM_DL_URL ?= http://${HOSTNAME}:8000/update

pipelines := common koji release watcher

common_CHECKOPTS := --exclude=2034,2164
common_SRC := $(wildcard *.sh)

koji_SRC := $(wildcard $(CURDIR)/koji/*.sh)

release_CHECKOPTS := --exclude=2013,2024,2155
release_SRC := $(wildcard $(CURDIR)/release/*.sh)

watcher_SRC := $(wildcard $(CURDIR)/watcher/*.sh)
Expand Down Expand Up @@ -45,6 +47,6 @@ serve: ${STAGING_DIR}
check_PIPELINES = $(addprefix check-,$(pipelines))
$(check_PIPELINES): pipe = $(patsubst check-%,%,$@)
$(check_PIPELINES):
shellcheck $($(pipe)_CHECKOPTS) $($(pipe)_SRC)
shellcheck -x $($(pipe)_CHECKOPTS) $($(pipe)_SRC)

check: $(check_PIPELINES)
51 changes: 26 additions & 25 deletions common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ log_line() {
# ${2} - extra indentation

local indent=${1:+$(( (${LOG_INDENT:-0} + ${2:-0}) * 4 ))}
printf "${LOG_DOMAIN:+"[${LOG_DOMAIN}] "}%${indent}s%s\n" "" "${1}"
printf "${LOG_DOMAIN:+"[${LOG_DOMAIN}] "}%${indent}s%s\\n" "" "${1}"
}

log() {
Expand All @@ -31,7 +31,7 @@ log() {
if (( $# < 2 )); then
log_line "${1}"
else
log_line "${1}:" ${3}
log_line "${1}:" "${3}"
log_line "${2}" $((${3:-0} + 1))
fi
}
Expand All @@ -46,15 +46,15 @@ section() {
}

error() {
log "[ERROR] ${1}" ${2:+"${2}"} ${3}
log "[ERROR] ${1}" ${2:+"${2}"} "${3}"
}

info() {
log "[INFO] ${1}" ${2:+"${2}"} ${3}
log "[INFO] ${1}" ${2:+"${2}"} "${3}"
}

warn() {
log "[WARN] ${1}" ${2:+"${2}"} ${3}
log "[WARN] ${1}" ${2:+"${2}"} "${3}"
}

run_and_log() {
Expand All @@ -68,7 +68,7 @@ run_and_log() {
local log_err="${2}_err.log"
local log_dir=${LOG_DIR:-"./logs"}

mkdir -p ${log_dir}
mkdir -p "${log_dir}"

# Log to file
if [[ "${LOG_METHOD}" -eq 0 ]]; then
Expand All @@ -88,15 +88,15 @@ run_and_log() {
# =================

curl() {
command curl --silent --fail $@
command curl --silent --fail "$@"
}

koji_cmd() {
# Downloads fail sometime, try harder!
local result=""
local ret=1
for (( i=0; $i < 10; i++ )); do
result=$(koji ${@} 2> /dev/null) \
for (( i=0; i < 10; i++ )); do
result=$(koji "${@}" 2> /dev/null) \
|| continue

ret=0
Expand All @@ -108,7 +108,7 @@ koji_cmd() {
}

assert_dep () {
command -v $1 > /dev/null 2>&1 || { error "command '$1' not found"; exit 1; }
command -v "$1" > /dev/null 2>&1 || { error "command '$1' not found"; exit 1; }
}

assert_dir () {
Expand All @@ -120,10 +120,10 @@ assert_file() {
}

silentkill () {
if [ ! -z $2 ]; then
kill $2 $1 > /dev/null 2>&1 || true
if [ ! -z "$2" ]; then
kill "$2" "$1" > /dev/null 2>&1 || true
else
kill -KILL $1 > /dev/null 2>&1 || true
kill -KILL "$1" > /dev/null 2>&1 || true
fi
}

Expand All @@ -134,7 +134,7 @@ fetch_config_repo() {
local REPO_HOST=${CONFIG_REPO_HOST:?"CONFIG_REPO_HOST cannot be Null/Unset"}
local REPO_NAME=${NAMESPACE:?"NAMESPACE cannot be Null/Unset"}
log_line "Cloning..." 1
git clone --quiet ${REPO_HOST}${REPO_NAME} config
git clone --quiet "${REPO_HOST}${REPO_NAME}" config
log_line "OK!" 2
else
log_line "Updating..." 1
Expand Down Expand Up @@ -165,13 +165,13 @@ var_save() {

# "unsave"
if [[ ! -v ${1} ]]; then
rm -f ${VARS_DIR}/${1}
rm -f "${VARS_DIR}/${1}"
return 0
fi

[[ -d ${VARS_DIR} ]] || mkdir -p ${VARS_DIR}
[[ -d ${VARS_DIR} ]] || mkdir -p "${VARS_DIR}"

echo "${!1}" > ${VARS_DIR}/${1}
echo "${!1}" > "${VARS_DIR}/${1}"
}

var_load() {
Expand All @@ -180,33 +180,34 @@ var_load() {
return 1
fi

[[ -f ${VARS_DIR}/${1} ]] && declare -g ${1}="$(cat ${VARS_DIR}/${1})" || true
# shellcheck disable=2015
[[ -f ${VARS_DIR}/${1} ]] && declare -g "${1}"="$(cat "${VARS_DIR}/${1}")" || true
}

get_upstream_version() {
CLR_LATEST=${CLR_LATEST:-$(curl ${CLR_PUBLIC_DL_URL}/latest)} || true
CLR_LATEST=${CLR_LATEST:-$(curl "${CLR_PUBLIC_DL_URL}/latest")} || true
if [[ -z $CLR_LATEST ]]; then
error "Failed to fetch Clear Linux latest version."
exit 2
fi

CLR_FORMAT=$(curl ${CLR_PUBLIC_DL_URL}/update/${CLR_LATEST}/format) || true
CLR_FORMAT=$(curl "${CLR_PUBLIC_DL_URL}/update/${CLR_LATEST}/format") || true
if [[ -z $CLR_FORMAT ]]; then
error "Failed to fetch Clear Linux latest format."
exit 2
fi
}

get_downstream_version() {
DS_LATEST=$(cat ${STAGING_DIR}/latest 2>/dev/null) || true
DS_LATEST=$(cat "${STAGING_DIR}/latest" 2>/dev/null) || true
if [[ -z $DS_LATEST ]]; then
info "Failed to fetch Downstream latest version. First Mix?"
DS_FORMAT=${CLR_FORMAT:-1}
elif ((${#DS_LATEST} < 4)); then
error "Downstream Clear Linux version number seems corrupted."
exit 2
else
DS_FORMAT=$(cat ${STAGING_DIR}/update/${DS_LATEST}/format 2>/dev/null) || true
DS_FORMAT=$(cat "${STAGING_DIR}/update/${DS_LATEST}/format" 2>/dev/null) || true
if [[ -z $DS_FORMAT ]]; then
error "Failed to fetch Downstream latest format."
exit 2
Expand All @@ -215,7 +216,7 @@ get_downstream_version() {
DS_UP_VERSION=${DS_LATEST: : -3}
DS_DOWN_VERSION=${DS_LATEST: -3}

DS_UP_FORMAT=$(curl ${CLR_PUBLIC_DL_URL}/update/${DS_UP_VERSION}/format) || true
DS_UP_FORMAT=$(curl "${CLR_PUBLIC_DL_URL}/update/${DS_UP_VERSION}/format") || true
if [[ -z $DS_UP_FORMAT ]]; then
error "Failed to fetch Downstream latest base format."
exit 2
Expand All @@ -231,9 +232,9 @@ get_latest_versions() {
calc_mix_version() {
# Compute initial next version (ignoring the need for format bumps)
if [[ -z ${DS_LATEST} || ${CLR_LATEST} -gt ${DS_UP_VERSION} ]]; then
MIX_VERSION=$((${CLR_LATEST} * 1000 + ${MIX_INCREMENT}))
MIX_VERSION=$((CLR_LATEST * 1000 + MIX_INCREMENT))
elif [[ ${CLR_LATEST} -eq ${DS_UP_VERSION} ]]; then
MIX_VERSION=$((${DS_LATEST} + ${MIX_INCREMENT}))
MIX_VERSION=$((DS_LATEST + MIX_INCREMENT))
if [[ ${MIX_VERSION: -3} -eq 000 ]]; then
error "Invalid Mix Version" \
"No more Downstream versions available for this Upstream version!"
Expand Down
13 changes: 8 additions & 5 deletions koji/update_external_repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# Copyright (C) 2018 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

# shellcheck source=globals.sh
# shellcheck source=common.sh

set -e

SCRIPT_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")

. ${SCRIPT_DIR}/../globals.sh
. ${SCRIPT_DIR}/../common.sh
. "${SCRIPT_DIR}/../globals.sh"
. "${SCRIPT_DIR}/../common.sh"

KOJI_TAG=${KOJI_TAG:-"dist-clear"}
repo_name="dist-clear-external-repo"
Expand All @@ -20,7 +23,7 @@ current_version=${current_version%%${repo_suffix}}

get_upstream_version

(( ${CLR_LATEST} <= ${current_version} )) && exit 0
(( CLR_LATEST <= current_version )) && exit 0

koji edit-external-repo --url="${repo_prefix}${CLR_LATEST}${repo_suffix}" ${repo_name}
koji regen-repo ${KOJI_TAG}-build
koji regen-repo "${KOJI_TAG}-build"
16 changes: 10 additions & 6 deletions release/content.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# Copyright (C) 2018 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

# shellcheck source=globals.sh
# shellcheck source=common.sh

set -e

SCRIPT_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")

. ${SCRIPT_DIR}/../globals.sh
. ${SCRIPT_DIR}/../common.sh
. "${SCRIPT_DIR}/../globals.sh"
. "${SCRIPT_DIR}/../common.sh"

. ./config/config.sh

Expand All @@ -18,15 +21,16 @@ stage "Finalizing Content"

log_line "Building package list:"
# If no content providers fetched packages, this will act as 'touch'
cat ${WORK_DIR}/${PKG_LIST_TMP}* > ${WORK_DIR}/${PKG_LIST_FILE} 2>/dev/null || true
# shellcheck disable=2086
cat ${WORK_DIR}/${PKG_LIST_TMP}* > "${WORK_DIR}/${PKG_LIST_FILE}" 2>/dev/null || true
log_line "OK!" 1

section "Creating Content Repository"
if [[ -z "$(ls -A ${PKGS_DIR})" ]]; then
if [[ -z "$(ls -A "${PKGS_DIR}")" ]]; then
info "Custom Content Not Found" " '${PKGS_DIR}' is empty."
exit 0
fi

log_line # Output too verbose
createrepo_c ${REPO_DIR}/x86_64/os # TODO: log create_repo output and only print its result
createrepo_c "${REPO_DIR}/x86_64/os" # TODO: log create_repo output and only print its result
log_line
50 changes: 27 additions & 23 deletions release/images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
# Copyright (C) 2018 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

# shellcheck source=globals.sh
# shellcheck source=common.sh

set -e

SCRIPT_DIR=$(dirname $(realpath ${BASH_SOURCE[0]}))
SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")

. ${SCRIPT_DIR}/../globals.sh
. ${SCRIPT_DIR}/../common.sh
. ${SCRIPT_DIR}/../config/config.sh
. "${SCRIPT_DIR}/../globals.sh"
. "${SCRIPT_DIR}/../common.sh"
. "${SCRIPT_DIR}/../config/config.sh"

var_load MIX_VERSION

Expand All @@ -21,7 +24,7 @@ create_image() {
# ${1} - File path to image template file
local image=${1}
local tempdir=$(mktemp -d)
local name=$(basename ${image%.json})
local name=$(basename "${image%.json}")
local ister_log="${LOG_DIR}/ister-${name}.log"
local final_file="${IMGS_DIR}/${DSTREAM_NAME}-${MIX_VERSION}-${name}.img.xz"

Expand All @@ -30,64 +33,65 @@ create_image() {
return 1
fi

pushd ${WORK_DIR} > /dev/null
sudo -E ister.py -s ${BUILD_DIR}/Swupd_Root.pem -L debug -S ${tempdir} \
-C file://${BUILD_DIR}/update/www -V file://${BUILD_DIR}/update/www \
-f ${format} -t ${image} > ${ister_log} 2>&1
pushd "${WORK_DIR}" > /dev/null
sudo -E ister.py -s "${BUILD_DIR}/Swupd_Root.pem" -L debug -S "${tempdir}" \
-C "file://${BUILD_DIR}/update/www" -V "file://${BUILD_DIR}/update/www" \
-f "${format}" -t "${image}" > "${ister_log}" 2>&1
local ister_ret=$?
sudo rm -rf ${tempdir}
sudo rm -rf "${tempdir}"

if (( ${ister_ret} )) || [[ ! -s "${name}.img" ]]; then
if (( ister_ret )) || [[ ! -s "${name}.img" ]]; then
log "Image '${name}'" "Failed. See log below:"
echo
cat ${ister_log}
cat "${ister_log}"
echo
return 1
fi

xz -3 --stdout ${name}.img > ${final_file}
sudo rm ${name}.img
xz -3 --stdout "${name}.img" > "${final_file}"
sudo rm "${name}.img"

if [[ ! -s "${final_file}" ]]; then
log "Image '${name}'" "Failed to create compressed file."
return 1
fi

# Only publish template files that successfully built an image
cp -a ${image} ${WORK_DIR}/release/config/
cp -a "${image}" "${WORK_DIR}/release/config/"

popd > /dev/null
log "Image '${name}'" "OK!"
}

parallel_fn() {
# Intended to be used only by GNU Parallel
. ${SCRIPT_DIR}/../globals.sh
. ${SCRIPT_DIR}/../common.sh
. ${SCRIPT_DIR}/../config/config.sh
. "${SCRIPT_DIR}/../globals.sh"
. "${SCRIPT_DIR}/../common.sh"
. "${SCRIPT_DIR}/../config/config.sh"

create_image $@
create_image "$@"
}

# ==============================================================================
# MAIN
# ==============================================================================
stage "Image Generation"

# shellcheck disable=2086
image_list=$(ls ${TEMPLATES_PATH}/*.json 2>/dev/null || true)
if [[ -z "${image_list}" ]]; then
warn "Skipping stage."
warn "No image definition files found in" "${TEMPLATES_PATH}"
exit 0
fi

format=$(< ${BUILD_DIR}/update/www/${MIX_VERSION}/format)
format=$(< "${BUILD_DIR}/update/www/${MIX_VERSION}/format")
if [[ -z "${format}" ]]; then
error "Failed to fetch Downstream current format."
exit 1
fi

mkdir -p ${LOG_DIR}
mkdir -p "${LOG_DIR}"

export IMGS_DIR
export LOG_DIR
Expand All @@ -97,5 +101,5 @@ export -f create_image
export -f parallel_fn
export format
procs=$(nproc --all)
max_jobs=$(( ${procs:=0} > ${PROCS_PER_IMG} ? ${procs} / ${PROCS_PER_IMG} : 1 ))
parallel -j ${max_jobs} parallel_fn <<< ${image_list}
max_jobs=$(( ${procs:=0} > PROCS_PER_IMG ? procs / PROCS_PER_IMG : 1 ))
parallel -j ${max_jobs} parallel_fn <<< "${image_list}"
Loading

0 comments on commit 51e6933

Please sign in to comment.