2018-08-11 00:29:44 +03:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# stop on error
|
|
|
|
set -e
|
|
|
|
|
|
|
|
function show_help
|
|
|
|
{
|
|
|
|
echo "Usage:"
|
2018-08-24 11:03:26 +03:00
|
|
|
echo "build.sh local|test|prod none|minor|major imagename [windows|linux]"
|
2018-08-24 03:26:05 +03:00
|
|
|
echo "local: don't upload to pypi, test: uses pypitest, prod: uses pypi"
|
2018-08-24 11:03:26 +03:00
|
|
|
echo "none: don't bumpversion, minor: bumpversion minor --no-commit --no-tag, major: bumpversion major"
|
2018-09-17 10:08:27 +03:00
|
|
|
echo "imagename: localhost:5000/iotedgedev, jongacr.azurecr.io/iotedgedev, microsoft/iotedgedev"
|
2018-08-24 03:26:05 +03:00
|
|
|
echo "windows: builds only windows container, linux: builds only linux container. omit to build both."
|
|
|
|
echo "NOTES: 1. You must have .pypirc in repo root with pypi and pypitest sections. 2. You must have .env file in root with connection strings set."
|
|
|
|
|
2018-08-11 00:29:44 +03:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
MODE="$1"
|
2018-08-24 03:26:05 +03:00
|
|
|
VERSION_BUMP="$2"
|
|
|
|
IMAGE_NAME="$3"
|
|
|
|
PLATFORM="$4"
|
2018-08-11 00:29:44 +03:00
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
if [ -z "$MODE" ] || [ -z "$VERSION_BUMP" ] || [ -z "$IMAGE_NAME" ]; then
|
2018-08-11 00:29:44 +03:00
|
|
|
show_help
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo -e "\n===== Setting up build environment"
|
2018-08-24 03:26:05 +03:00
|
|
|
if [ "$MODE" = "local" ]; then
|
|
|
|
echo "Environment: $MODE"
|
2018-08-24 11:03:26 +03:00
|
|
|
elif [ "$MODE" = "test" ]; then
|
2018-08-11 00:29:44 +03:00
|
|
|
echo "Environment: $MODE"
|
|
|
|
elif [ "$MODE" = "prod" ]; then
|
|
|
|
echo "Environment: $MODE"
|
|
|
|
else
|
2018-08-24 03:26:05 +03:00
|
|
|
echo "ERROR> Build mode parameter not known. must be 'local', 'prod' or 'test'"
|
2018-08-11 00:29:44 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -z $PLATFORM ]; then
|
|
|
|
echo "Platform: $PLATFORM"
|
|
|
|
fi
|
|
|
|
|
2018-10-08 18:09:25 +03:00
|
|
|
if [ "$OSTYPE" = "msys" ]; then
|
|
|
|
echo -e "\n===== Checking pre-requisistes"
|
|
|
|
IS_ADMIN=$(powershell '([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")')
|
|
|
|
if [ "$IS_ADMIN" = "False" ]; then
|
|
|
|
echo "ERROR> Build script must be run as administrator"
|
|
|
|
exit 1
|
|
|
|
fi
|
2018-08-11 00:29:44 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
#TODO
|
|
|
|
# check if running in administrator mode
|
|
|
|
# make sure docker is in linux mode
|
|
|
|
# make sure docker supports manifest option
|
|
|
|
# stop and restart docker to make sure to avoid networking problem?
|
|
|
|
# check that dockerhub exists and is accessible
|
|
|
|
# check that pipy repo exists and is accessible
|
|
|
|
# make sure there are no pending changes in GIT otherwise bumpversion will complain
|
|
|
|
|
|
|
|
function run_tox {
|
|
|
|
echo -e "\n===== Preventive cleanup"
|
|
|
|
rm __pycache__ -rf
|
|
|
|
rm .pytest_cache -rf
|
|
|
|
rm .tox -rf
|
|
|
|
rm .pytest_cache -rf
|
|
|
|
rm tests/__pycache__ -rf
|
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
echo -e "\n===== Running Tox"
|
2018-08-11 00:29:44 +03:00
|
|
|
tox
|
|
|
|
}
|
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
function get_version
|
|
|
|
{
|
|
|
|
VERSION=$(cat ./iotedgedev/__init__.py | grep '__version__' | grep -oP "'\K[^']+")
|
|
|
|
echo ${VERSION}
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:29:44 +03:00
|
|
|
function run_bumpversion {
|
2018-08-24 03:26:05 +03:00
|
|
|
|
|
|
|
if [ "$VERSION_BUMP" = "none" ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2018-08-11 00:29:44 +03:00
|
|
|
echo -e "\n===== Bumping version"
|
2018-08-24 03:26:05 +03:00
|
|
|
|
|
|
|
if [ "$MODE" = "prod" ]; then
|
|
|
|
bumpversion $VERSION_BUMP
|
|
|
|
else
|
|
|
|
bumpversion $VERSION_BUMP --no-commit --no-tag --allow-dirty
|
|
|
|
fi
|
2018-08-11 00:29:44 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
function run_build_wheel
|
2018-08-11 00:29:44 +03:00
|
|
|
{
|
|
|
|
echo -e "\n===== Building Python Wheel"
|
|
|
|
python setup.py bdist_wheel
|
|
|
|
}
|
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
function run_upload_pypi
|
2018-08-11 00:29:44 +03:00
|
|
|
{
|
2018-08-24 03:26:05 +03:00
|
|
|
if [ "$MODE" != "local" ]; then
|
|
|
|
echo -e "\n===== Uploading to PyPi"
|
|
|
|
PYPI=$([ "$MODE" = "prod" ] && echo "pypi" || echo "pypitest")
|
|
|
|
twine upload -r ${PYPI} --config-file .pypirc dist/iotedgedev-$(get_version)-py2.py3-none-any.whl
|
|
|
|
fi
|
2018-08-11 00:29:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_build_docker
|
|
|
|
{
|
2018-08-24 03:26:05 +03:00
|
|
|
echo -e "\n===== Building Docker Containers"
|
|
|
|
./docker/tool/build-docker.sh $IMAGE_NAME $PLATFORM
|
2018-08-11 00:29:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_push_docker
|
|
|
|
{
|
2018-08-24 03:26:05 +03:00
|
|
|
echo -e "\n===== Pushing Docker Containers"
|
|
|
|
./docker/tool/push-docker.sh $IMAGE_NAME $PLATFORM
|
2018-08-11 00:29:44 +03:00
|
|
|
}
|
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
function run_push_git
|
|
|
|
{
|
|
|
|
if [ "$MODE" = "prod" ]; then
|
2018-09-17 10:08:27 +03:00
|
|
|
echo 'Resetting __init__ file'
|
|
|
|
git checkout ./iotedgedev/__init__.py
|
|
|
|
|
2018-08-24 03:26:05 +03:00
|
|
|
echo -e "\n===== Pushing Tags to Git"
|
|
|
|
git push --tags && git push
|
|
|
|
fi
|
|
|
|
}
|
2018-08-11 00:29:44 +03:00
|
|
|
|
2018-09-17 10:08:27 +03:00
|
|
|
function set_analytics_key
|
|
|
|
{
|
|
|
|
if [[ ${AIKEY//-/} =~ ^[[:xdigit:]]{32}$ ]]; then
|
|
|
|
echo 'Found AIKEY environment variable. Replacing __AIkey__'
|
|
|
|
sed -i "/__AIkey__/c __AIkey__ = '${AIKEY}'" ./iotedgedev/__init__.py
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
set_analytics_key
|
2018-08-24 03:26:05 +03:00
|
|
|
run_bumpversion
|
|
|
|
run_tox
|
|
|
|
run_build_wheel
|
2018-08-11 00:29:44 +03:00
|
|
|
run_build_docker
|
2018-08-24 03:26:05 +03:00
|
|
|
run_upload_pypi
|
|
|
|
run_push_docker
|
|
|
|
run_push_git
|
2018-08-11 00:29:44 +03:00
|
|
|
|
|
|
|
echo -e "\n===== All done"
|