dev.sh 3.19 KB
Newer Older
1 2 3 4
#!/bin/bash -e

DIR="$( cd "$(dirname "$0")" ; pwd -P )"
NAME=$(basename $DIR)
5
export VIRTUAL_ENV_DIR="${DIR}/env"
6
unset PYTHON_HOME
7
export PYTHONPATH=${DIR}:${DIR}/plugins/beiran_package_docker:${DIR}/plugins/beiran_interface_k8s
8 9
export PYTHON_BINARY=python3.6
export PKG_DIR=${VIRTUAL_ENV_DIR}/lib/${PYTHON_BINARY}/site-packages
10 11
export VIRTUALENV=$(which virtualenv3 || which virtualenv || echo 'virtualenv')

12
PIP=$(which pip3 || which pip || echo 'pip')
13
REQS="$PYTHON_BINARY $VIRTUALENV $PIP"
14 15 16 17 18 19 20 21
for req in $REQS; do
	if ! which ${req} >/dev/null; then
		1>&2 echo "${req} is not found on your system, please install ${req}"
		1>&2 echo "or you can use docker instead"
		exit 1
	fi
done

22
if [ ! -d ${VIRTUAL_ENV_DIR} ]; then
23
	$VIRTUALENV env --python=$(which $PYTHON_BINARY)
24
	source ${VIRTUAL_ENV_DIR}/bin/activate
25
	export PIP=$(which pip3 || which pip || echo 'pip')
26
	$PIP install ipython
27 28
else
	source ${VIRTUAL_ENV_DIR}/bin/activate
29
	export PIP=$(which pip3 || which pip || echo 'pip')
30 31
fi

32

33 34 35
STAMP=$(date +%s)
INSTALLED=0
LAST_INSTALL=$(date -r ${VIRTUAL_ENV_DIR}/.last_install +%s 2>/dev/null || echo "0")
36

37 38
DEV_SH_MODIFIED=$(date -r ${DIR}/dev.sh +%s)
if [ $DEV_SH_MODIFIED -gt $LAST_INSTALL ]; then
39 40 41
	#install prerequisite package `yajl`
	apt-get install libyajl2
	apt-get install libyajl-dev
42

43 44 45 46 47 48 49
	# make binary of 'tar-split' with Docker
	tarsplit_dir=${DIR}/plugins/beiran_package_docker/

	if [ ! -e ${tarsplit_dir}/tar-split ]; then
		docker build -t tarsplit -f $tarsplit_dir/DockerfileTarsplit $tarsplit_dir
		docker create --name tarsplit tarsplit && docker cp tarsplit:/tar-split ${tarsplit_dir}/tar-split && docker rm tarsplit && docker rmi tarsplit
	fi
50
fi
51

52 53 54 55 56 57
packages="beiran plugins/*"
for package in $packages; do
	package_name=$(basename $package)
	if [ ! -h ${PKG_DIR}/$package_name ]; then
		ln -s ${DIR}/$package ${PKG_DIR}/
	fi
58

59 60 61 62 63 64 65 66
	if [ -f $package/requirements.txt ]; then
		REQ_MODIFIED=$(date -r $package/requirements.txt +%s)
		if [ $REQ_MODIFIED -gt $LAST_INSTALL ]; then
			$PIP install -r $package/requirements.txt
			INSTALLED=1
		fi
	fi
done
67 68 69 70

if [ $INSTALLED -eq 1 ]; then
	echo $STAMP > ${VIRTUAL_ENV_DIR}/.last_install
fi
71

72
cat > ${VIRTUAL_ENV_DIR}/bin/beiran <<EOF
73
#!/bin/sh -e
74
exec ${PYTHON_BINARY} -m beiran "\$@"
75 76
EOF

77
cat > ${VIRTUAL_ENV_DIR}/bin/beirand <<EOF
78
#!/bin/sh -e
79
exec ${PYTHON_BINARY} -m beiran.daemon "\$@"
80 81
EOF

82
chmod +x ${VIRTUAL_ENV_DIR}/bin/beiran*
83

84
export PATH="${VIRTUAL_ENV_DIR}/bin:$PATH"
85

86 87
export BEIRAN_LOG_LEVEL=DEBUG
export BEIRAN_LOG_FILE=${DIR}/beirand.log
88
export BEIRAN_RUN_DIR=${DIR}
89
export BEIRAN_SOCKET_FILE=${BEIRAN_RUN_DIR}/beirand.sock
90 91
export BEIRAN_PORT=${BEIRAN_PORT:-8888}
export BEIRAN_URL=http://localhost:${BEIRAN_PORT}
92
export BEIRAN_DB_FILE=${DIR}/beiran.db
93
export BEIRAN_LISTEN_ADDRESS=0.0.0.0
94
export BEIRAN_CONFIG_DIR=${DIR}
95
export BEIRAN_DISCOVERY_METHOD=zeroconf
96
export BEIRAN_PLUGINS=beiran.plugins.discovery.zeroconf,package.docker,interface.k8s
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

function ps1_context {
	# For any of these bits of context that exist, display them and append
	# a space.
	virtualenv=${NAME}
	for v in "$debian_chroot" "$virtualenv" "$PS1_CONTEXT"; do
		echo -n "${v:+($v) }"
	done
}

export PS1="$(ps1_context)"'\u@\h:\w\$ '

if [ $(basename $SHELL) == "bash" ]; then
	exec $SHELL --norc -i
fi

exec "${@:-$SHELL}"