#!/bin/bash set -eo pipefail ARCH="$(uname -m)" IMAGE_TYPE='default' RELEASE='9' BASE_DIR='./result' PROGRAM_DIR="$(dirname -- "$0")" VERSION='0.0.1' show_usage() { echo -e 'Generates an MSVSphere container image RootFS and Dockerfile\n' echo -e 'Usage: build-image.sh [OPTION]...\n' echo ' -h, --help show this message and exit' echo " -r, --release RELEASE target MSVSphere release, default is ${RELEASE}" echo ' -t, --type TYPE image type, supported values are: default.' echo " Default value is '${IMAGE_TYPE}'" echo " -o, --output DIR output directory path, default is ${BASE_DIR}" echo ' -v, --version show program version and exit' } log_error() { echo "ERROR : ${1}" >&2 } gen_dockerfile() { local -r image_name="${1}" local -r build_date="$(date --rfc-3339=seconds --utc)" echo 'FROM scratch' echo "ADD ${image_name} /" echo '' # see https://github.com/opencontainers/image-spec/blob/master/annotations.md for details echo "LABEL org.opencontainers.image.title=\"MSVSphere ${RELEASE} ${ARCH} ${IMAGE_TYPE} image\"" echo 'LABEL org.opencontainers.image.vendor="MSVSphere"' echo 'LABEL org.opencontainers.image.licenses="MIT"' echo "LABEL org.opencontainers.image.created=\"${build_date}\"" echo 'LABEL org.opencontainers.image.authors="Eugene Zamriy "' echo 'LABEL org.opencontainers.image.source="https://git.inferitos.ru/msvsphere/docker-images"' echo '' # case "${IMAGE_TYPE}" in default) echo 'CMD ["/bin/bash"]' ;; *) log_error "unsupported image type ${IMAGE_TYPE}" exit 1 ;; esac } collect_logs() { local -r logs_dir="${1}/logs" mkdir "${logs_dir}" if [[ -d anaconda ]]; then mv anaconda "${logs_dir}/" fi mv ./*.log "${logs_dir}/" } main() { local -r image_name="msvsphere-${RELEASE}-${IMAGE_TYPE}.tar.xz" local -r ks_file="${PROGRAM_DIR}/kickstarts/msvsphere-${RELEASE}-${IMAGE_TYPE}.ks" local -r project="MSVSphere ${RELEASE} ${IMAGE_TYPE} Docker image" local -r result_dir="${BASE_DIR%%/}/${RELEASE}-${IMAGE_TYPE}" local -r docker_file="${result_dir}/Dockerfile" if [[ -d "${result_dir}" ]]; then log_error "directory ${result_dir} is already exist, please remove it first" exit 1 elif [[ ! -f "${ks_file}" ]]; then log_error "kickstart file ${ks_file} is not found" exit 1 fi # generate RootFS tarball livemedia-creator --no-virt --make-tar \ --project "${project}" \ --releasever "${RELEASE}" \ --image-name "${image_name}" \ --ks "${ks_file}" \ --resultdir "${result_dir}" \ --anaconda-arg "--nosave all" # generate Dockerfile and save build logs gen_dockerfile "${image_name}" >"${docker_file}" collect_logs "${result_dir}" } if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then ARGS=() while [[ $# -gt 0 ]]; do case ${1} in -h | --help) show_usage exit 0 ;; -t | --type) case "${2}" in default) IMAGE_TYPE="${2}" ;; *) log_error "unsupported image type '${2}'" exit 2 ;; esac shift shift ;; -o | --output) if [[ -z "${2}" ]]; then log_error 'output directory path is required' exit 2 fi BASE_DIR="${2}" shift shift ;; -v | --version) echo "${VERSION}" exit 0 ;; -*) log_error "unknown option ${1}" exit 2 ;; *) ARGS+=("${1}") shift ;; esac done main fi