Skip to content

Instantly share code, notes, and snippets.

@alexprivalov
Last active March 12, 2018 18:45
Show Gist options
  • Save alexprivalov/3624e6183393dbbb6f7ee251e1e04f8d to your computer and use it in GitHub Desktop.
Save alexprivalov/3624e6183393dbbb6f7ee251e1e04f8d to your computer and use it in GitHub Desktop.
#!/bin/bash
COMPANY_REGISTRY="hub.company_name.com"
set -e
script() {
[ "$#" -lt 3 ] && ( usage && exit 0 ) || main "$@"
}
function main {
work_mode=$2
target_box=$1
source_dir=$3
if [ "$work_mode" != "dev" ] && [ "$work_mode" != "CI" ]; then
echo "work_mode should be \"dev\" or \"CI\""
echo "\"dev\" - developers mode, run interractive bash"
echo "\"CI\" - continuous integration mode"
exit 1
fi
if [ -z "$3" ]; then
echo "You should specify path to directory with sources"
echo "It may be absolute path or relative"
exit 1
fi
resolved_dir=$(readlink -f $source_dir)
if [ ! -d "$resolved_dir" ]; then
echo "\"$resolved_dir\" doesn't exit!"
exit 1
fi
echo "model_name: ${target_box}"
echo "source_dir: ${resolved_dir}"
echo "work_mode: ${work_mode}"
if ([ "$target_box" = "mag256" ] || [ "$target_box" = "mag257" ]); then
docker_image_name="${COMPANY_REGISTRY}:5000/chipmaker_1/box_name_1:latest"
SOME_VARIABLE="some_value"
ADDITIONAL_OPTIONS="SOME_VARIABLE=${SOME_VARIABLE} SOME_VARIABLE2='blabla'"
elif ([ "$target_box" = "mag324" ] || [ "$target_box" = "mag322" ] || [ "$target_box" = "mag325" ]); then
docker_image_name="{COMPANY_REGISTRY}:5000/chipmaker_2/box_name_2:latest"
ADDITIONAL_OPTIONS=""
elif [ "$target_box" = "pc" ]; then
docker_image_name="{COMPANY_REGISTRY}:5000/x86/pc-env:latest"
else
echo "unknown model name: "${target_box}
exit 1
fi
hint_msg=$(echo -e "\
################################################################################\n\
# Will mount host path \"${resolved_dir}\" to container path \"${resolved_dir}\"\n\
# Will run \"make ${make_target}\" inside \"${resolved_dir}\" in the container \n\
# The source is available at \"${resolved_dir}\". \n\
# Type "Ctrl+D" to exit from container. \n\
# root password: \"pas\" \n\
################################################################################\n\
")
if [ "$work_mode" = "CI" ]; then
bash_cmd=$(echo -e "\
echo '${hint_msg}' \
&& cd ${resolved_dir} \
&& make clean \
&& make ${ADDITIONAL_OPTIONS} \
")
else
additional_docker_options="-it"
if [ -z "$5" ]; then
bash_cmd=$(echo -e "\
echo '${hint_msg}' \
&& cd ${resolved_dir} \
&& bash \
")
else
bash_cmd=$(echo -e "\
echo '${hint_msg}' \
&& cd ${resolved_dir} \
&& $5 \
")
fi
fi
containers_to_del=$(docker ps -a -f status=exited | grep ${COMPANY_REGISTRY} | awk '{ print $1 }')
if [ ! -z "${containers_to_del}" ]; then
echo ""
echo "Will remove ${COMPANY_REGISTRY}* stopped containers:"
echo "`docker ps -a -f status=exited | grep ${COMPANY_REGISTRY} | awk '{ print $1, $2 }'`"
echo ""
docker rm $(docker ps -a -f status=exited | grep ${COMPANY_REGISTRY} | awk '{ print $1 }') || true #remove stopped containers
fi
imgs_for_del=$(docker images -f "dangling=true" -q)
if [ ! -z "${imgs_for_del}" ]; then
echo "Will remove dangling layers"
docker rmi $(docker images -f "dangling=true" -q) || true #clean docker dangled images with <none>:<none>
echo ""
fi
docker pull ${docker_image_name} || true
docker run --rm ${additional_docker_options} \
-v ${resolved_dir}:${resolved_dir} \
-v $HOME/.gitconfig:/home/docker-user/.gitconfig:ro \
--net=host -v $HOME/.ssh:/home/docker-user/.ssh:ro \
-e LOCAL_USER_ID=`id -u ${USER}` \
${docker_image_name} \
bash -c "${bash_cmd}"
}
function usage {
self_script_name="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
echo
echo "Usage: "
echo " ${self_script_name} target_box dev|CI path_to_stbapp path_to_platform_lib"
echo "Examples: "
echo " ${self_script_name} mag425 dev dir_with_src"
echo " ${self_script_name} mag256 CI dir_with_src ----> run auto build"
echo " ${self_script_name} mag425 dev dir_with_src make|ls|echo \"ha-ha\""
echo
}
script "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment