diff --git a/builder/image-build.sh b/builder/image-build.sh new file mode 100755 index 0000000..f3c0f72 --- /dev/null +++ b/builder/image-build.sh @@ -0,0 +1,92 @@ +#! /usr/bin/env bash + +set -e # Exit immidiately on non-zero result + +SOURCE_IMAGE="https://github.com/CopterExpress/clever/releases/download/v0.16-alpha.3/clever_v0.16-alpha.3.img.zip" + +export DEBIAN_FRONTEND=${DEBIAN_FRONTEND:='noninteractive'} +export LANG=${LANG:='C.UTF-8'} +export LC_ALL=${LC_ALL:='C.UTF-8'} + +echo_stamp() { + # TEMPLATE: echo_stamp + # TYPE: SUCCESS, ERROR, INFO + + # More info there https://www.shellhacks.com/ru/bash-colors/ + + TEXT="$(date '+[%Y-%m-%d %H:%M:%S]') $1" + TEXT="\e[1m$TEXT\e[0m" # BOLD + + case "$2" in + SUCCESS) + TEXT="\e[32m${TEXT}\e[0m";; # GREEN + ERROR) + TEXT="\e[31m${TEXT}\e[0m";; # RED + *) + TEXT="\e[34m${TEXT}\e[0m";; # BLUE + esac + echo -e ${TEXT} +} + +REPO_DIR="/mnt" +SCRIPTS_DIR="${REPO_DIR}/builder" +IMAGES_DIR="${REPO_DIR}/images" + +[[ ! -d ${SCRIPTS_DIR} ]] && (echo_stamp "Directory ${SCRIPTS_DIR} doesn't exist" "ERROR"; exit 1) +[[ ! -d ${IMAGES_DIR} ]] && mkdir ${IMAGES_DIR} && echo_stamp "Directory ${IMAGES_DIR} was created successful" "SUCCESS" + +if [[ -z ${TRAVIS_TAG} ]]; then IMAGE_VERSION="$(cd ${REPO_DIR}; git log --format=%h -1)"; else IMAGE_VERSION="${TRAVIS_TAG}"; fi +# IMAGE_VERSION="${TRAVIS_TAG:=$(cd ${REPO_DIR}; git log --format=%h -1)}" +REPO_URL="$(cd ${REPO_DIR}; git remote --verbose | grep origin | grep fetch | cut -f2 | cut -d' ' -f1 | sed 's/git@github\.com\:/https\:\/\/github.com\//')" +REPO_NAME="$(basename -s '.git' ${REPO_URL})" +IMAGE_NAME="${REPO_NAME}_${IMAGE_VERSION}.img" +echo_stamp "IMAGE_NAME=${IMAGE_NAME}" "INFO" +IMAGE_PATH="${IMAGES_DIR}/${IMAGE_NAME}" +echo_stamp "IMAGE_PATH=${IMAGE_PATH}" "INFO" + +get_image() { + # TEMPLATE: get_image + local BUILD_DIR=$(dirname $1) + echo_stamp "BUILD_DIR=${BUILD_DIR}" "INFO" + local RPI_ZIP_NAME=$(basename $2) + echo_stamp "RPI_ZIP_NAME=${RPI_ZIP_NAME}" "INFO" + local RPI_IMAGE_NAME=$(echo ${RPI_ZIP_NAME} | sed 's/.zip//') + echo_stamp "RPI_IMAGE_NAME=${RPI_IMAGE_NAME}" "INFO" + + if [ ! -e "${BUILD_DIR}/${RPI_ZIP_NAME}" ]; then + echo_stamp "Downloading original clever distribution" + wget --progress=dot:giga -O ${BUILD_DIR}/${RPI_ZIP_NAME} $2 + echo_stamp "Downloading complete" "SUCCESS" + else echo_stamp "clever distribution already donwloaded" "INFO"; fi + + echo_stamp "Unzipping clever distribution image" \ + && unzip -p ${BUILD_DIR}/${RPI_ZIP_NAME} ${RPI_IMAGE_NAME} > $1 \ + && echo_stamp "Unzipping complete" "SUCCESS" \ + || (echo_stamp "Unzipping was failed!" "ERROR"; exit 1) +} + +get_image ${IMAGE_PATH} ${SOURCE_IMAGE} + +# Make free space +img-resize ${IMAGE_PATH} max '5G' + +# Copy cloned repository to the image +# Include dotfiles in globs (asterisks) +shopt -s dotglob +for dir in ${REPO_DIR}/*; do + # Don't try to copy image into itself + if [[ $dir != *"images" && $dir != *"imgcache" ]]; then + img-chroot ${IMAGE_PATH} copy $dir '/home/pi/CleverSwarm/' + fi; +done + +# Install software +img-chroot ${IMAGE_PATH} exec ${SCRIPTS_DIR}'/image-software.sh' + +# Shrink image +img-resize ${IMAGE_PATH} + + + + + diff --git a/builder/image-software.sh b/builder/image-software.sh new file mode 100755 index 0000000..26b442d --- /dev/null +++ b/builder/image-software.sh @@ -0,0 +1,70 @@ +#! /usr/bin/env bash + +# +# Script for install software to the image. +# +# Copyright (C) 2018 Copter Express Technologies +# +# Author: Artem Smirnov +# + +set -e # Exit immidiately on non-zero result + +echo_stamp() { + # TEMPLATE: echo_stamp + # TYPE: SUCCESS, ERROR, INFO + + # More info there https://www.shellhacks.com/ru/bash-colors/ + + TEXT="$(date '+[%Y-%m-%d %H:%M:%S]') $1" + TEXT="\e[1m${TEXT}\e[0m" # BOLD + + case "$2" in + SUCCESS) + TEXT="\e[32m${TEXT}\e[0m";; # GREEN + ERROR) + TEXT="\e[31m${TEXT}\e[0m";; # RED + *) + TEXT="\e[34m${TEXT}\e[0m";; # BLUE + esac + echo -e ${TEXT} +} + +# https://gist.github.com/letmaik/caa0f6cc4375cbfcc1ff26bd4530c2a3 +# https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh +my_travis_retry() { + local result=0 + local count=1 + while [ $count -le 3 ]; do + [ $result -ne 0 ] && { + echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" >&2 + } + # ! { } ignores set -e, see https://stackoverflow.com/a/4073372 + ! { "$@"; result=$?; } + [ $result -eq 0 ] && break + count=$(($count + 1)) + sleep 1 + done + + [ $count -gt 3 ] && { + echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" >&2 + } + + return $result +} + +echo_stamp "Update apt cache" +apt-get update -qq + +echo_stamp "Software installing" +apt-get install -y \ +samba \ +winbind \ +chrony \ +&& echo_stamp "Everything was installed!" "SUCCESS" \ +|| (echo_stamp "Some packages wasn't installed!" "ERROR"; exit 1) + +echo_stamp "Install python libs" +my_travis_retry pip install pause + +echo_stamp "End of software installation"