moved app-entrypoint.sh functions into /opt/bitnami/express/functions

For reusability in the che platform
This commit is contained in:
Sameer Naik
2017-01-27 12:55:29 +05:30
parent 6c6ca3d93d
commit 01fa8f0111
2 changed files with 142 additions and 137 deletions

View File

@@ -1,146 +1,11 @@
#!/bin/bash
set -e
INIT_SEM=/tmp/initialized.sem
PACKAGE_FILE=/app/package.json
fresh_container() {
[ ! -f $INIT_SEM ]
}
app_present() {
[ -f package.json ]
}
dependencies_up_to_date() {
# It it up to date if the package file is older than
# the last time the container was initialized
[ ! $PACKAGE_FILE -nt $INIT_SEM ]
}
database_tier_exists() {
[ -n "$(getent hosts mongodb mysql mariadb postgresql)" ]
}
__wait_for_db() {
local host=$1
local port=$2
local ip_address=$(getent hosts $1 | awk '{ print $1 }')
log "Connecting to at $host server at $ip_address:$port"
counter=0
until nc -z $ip_address $port; do
counter=$((counter+1))
if [ $counter == 10 ]; then
log "Error: Couldn't connect to $host server."
return 1
fi
log "Trying to connect to $host server at $ip_address:$port. Attempt $counter."
sleep 5
done
log "Connected to $host server"
}
wait_for_db() {
if ! [[ -n $SKIP_DB_WAIT && $SKIP_DB_WAIT -gt 0 ]] && database_tier_exists ; then
if getent hosts mongodb >/dev/null; then
__wait_for_db mongodb 27017
fi
if getent hosts mariadb >/dev/null; then
__wait_for_db mariadb 3306
fi
if getent hosts mysql >/dev/null; then
__wait_for_db mysql 3306
fi
if getent hosts postgresql >/dev/null; then
__wait_for_db postgresql 5432
fi
fi
}
bootstrap_express_app() {
log "Creating express application"
express . -f
log "Adding nodemon npm module (dev)"
npm install nodemon --save-dev
sed -i 's;"start".*;"start": "node ./bin/www", "development": "nodemon ./bin/www";' package.json
}
add_database_support() {
if database_tier_exists; then
if getent hosts mongodb >/dev/null && ! npm ls mongodb >/dev/null; then
log "Adding mongodb npm module"
npm install --save mongodb
fi
if getent hosts mariadb >/dev/null && ! npm ls mysql >/dev/null || getent hosts mysql >/dev/null && ! npm ls mysql >/dev/null; then
log "Adding mysql npm module"
npm install --save mysql
fi
if getent hosts postgresql >/dev/null && ! npm ls pg pg-hstore >/dev/null; then
log "Adding pg pg-hstore npm modules"
npm install --save pg pg-hstore
fi
fi
}
add_sample_code() {
if ! [[ -n $SKIP_SAMPLE_CODE && $SKIP_SAMPLE_CODE -gt 0 ]]; then
log "Adding dist samples"
cp -r /dist/samples .
fi
}
add_dockerfile() {
if [[ ! -f Dockerfile ]]; then
cp -r /dist/Dockerfile.tpl Dockerfile
sed -i 's/{{BITNAMI_IMAGE_VERSION}}/'"$BITNAMI_IMAGE_VERSION"'/g' Dockerfile
[[ ! -f bower.json ]] && sed -i '/^RUN bower install/d' Dockerfile
fi
if [[ ! -f .dockerignore ]]; then
cp -r /dist/.dockerignore .
fi
}
install_packages() {
if ! dependencies_up_to_date; then
if ! [[ -n $SKIP_NPM_INSTALL && $SKIP_NPM_INSTALL -gt 0 ]] && [[ -f package.json ]]; then
log "Installing npm packages"
npm install
fi
if ! [[ -n $SKIP_BOWER_INSTALL && $SKIP_BOWER_INSTALL -gt 0 ]] && [[ -f bower.json ]]; then
log "Installing bower packages"
bower install
fi
fi
}
migrate_db() {
if ! [[ -n $SKIP_DB_MIGRATE && $SKIP_DB_MIGRATE -gt 0 ]] && [[ -f .sequelizerc ]]; then
log "Applying database migrations (sequelize db:migrate)"
sequelize db:migrate
fi
}
log () {
echo -e "\033[0;33m$(date "+%H:%M:%S")\033[0;37m ==> $1."
}
#!/bin/bash -e
. /opt/bitnami/express/functions
if [ "$1" == npm ] && [ "$2" == "start" -o "$2" == "run" ]; then
wait_for_db
if ! app_present; then
bootstrap_express_app
add_database_support
add_sample_code
fi
add_dockerfile

View File

@@ -0,0 +1,140 @@
#!/bin/bash
INIT_SEM=/tmp/initialized.sem
PACKAGE_FILE=/app/package.json
log() {
echo -e "\033[0;33m$(date "+%H:%M:%S")\033[0;37m ==> $1"
}
fresh_container() {
[ ! -f $INIT_SEM ]
}
app_present() {
[ -f package.json ]
}
dependencies_up_to_date() {
# It it up to date if the package file is older than
# the last time the container was initialized
[ ! $PACKAGE_FILE -nt $INIT_SEM ]
}
database_tier_exists() {
[ -n "$(getent hosts mongodb mysql mariadb postgresql)" ]
}
__wait_for_db() {
local host=$1
local port=$2
local ip_address=$(getent hosts $1 | awk '{ print $1 }')
log "Connecting to at $host server at $ip_address:$port."
counter=0
until nc -z $ip_address $port; do
counter=$((counter+1))
if [ $counter == 10 ]; then
log "Error: Couldn't connect to $host server."
return 1
fi
log "Trying to connect to $host server at $ip_address:$port. Attempt $counter."
sleep 5
done
log "Connected to $host server."
}
wait_for_db() {
if ! [[ -n $SKIP_DB_WAIT && $SKIP_DB_WAIT -gt 0 ]] && database_tier_exists ; then
if getent hosts mongodb >/dev/null; then
__wait_for_db mongodb 27017
fi
if getent hosts mariadb >/dev/null; then
__wait_for_db mariadb 3306
fi
if getent hosts mysql >/dev/null; then
__wait_for_db mysql 3306
fi
if getent hosts postgresql >/dev/null; then
__wait_for_db postgresql 5432
fi
fi
}
add_sample_code() {
if ! [[ -n $SKIP_SAMPLE_CODE && $SKIP_SAMPLE_CODE -gt 0 ]]; then
log "Adding dist samples."
cp -r /dist/samples .
fi
}
add_database_support() {
if database_tier_exists; then
if getent hosts mongodb >/dev/null && ! npm ls mongodb >/dev/null; then
log "Adding mongodb npm module."
npm install --save mongodb
fi
if getent hosts mariadb >/dev/null && ! npm ls mysql >/dev/null || getent hosts mysql >/dev/null && ! npm ls mysql >/dev/null; then
log "Adding mysql npm module."
npm install --save mysql
fi
if getent hosts postgresql >/dev/null && ! npm ls pg pg-hstore >/dev/null; then
log "Adding pg pg-hstore npm modules."
npm install --save pg pg-hstore
fi
fi
}
add_nodemon_support() {
log "Adding nodemon npm module (dev)."
npm install nodemon --save-dev
sed -i 's;"start".*;"start": "node ./bin/www", "development": "nodemon ./bin/www";' package.json
}
bootstrap_express_app() {
log "Creating express application"
express . -f
add_database_support
add_nodemon_support
add_sample_code
}
add_dockerfile() {
if [[ ! -f Dockerfile ]]; then
cp -r /dist/Dockerfile.tpl Dockerfile
sed -i 's/{{BITNAMI_IMAGE_VERSION}}/'"$BITNAMI_IMAGE_VERSION"'/g' Dockerfile
[[ ! -f bower.json ]] && sed -i '/^RUN bower install/d' Dockerfile
fi
if [[ ! -f .dockerignore ]]; then
cp -r /dist/.dockerignore .
fi
}
install_packages() {
if ! dependencies_up_to_date; then
if ! [[ -n $SKIP_NPM_INSTALL && $SKIP_NPM_INSTALL -gt 0 ]] && [[ -f package.json ]]; then
log "Installing npm packages."
npm install
fi
if ! [[ -n $SKIP_BOWER_INSTALL && $SKIP_BOWER_INSTALL -gt 0 ]] && [[ -f bower.json ]]; then
log "Installing bower packages."
bower install
fi
fi
}
migrate_db() {
if ! [[ -n $SKIP_DB_MIGRATE && $SKIP_DB_MIGRATE -gt 0 ]] && [[ -f .sequelizerc ]]; then
log "Applying database migrations (sequelize db:migrate)."
sequelize db:migrate
fi
}