Skip to content
Snippets Groups Projects
cozy-app-dev-with-app.sh 8.55 KiB
Newer Older
  • Learn to ignore specific revisions
  • Hugo NOUTS's avatar
    Hugo NOUTS committed
    #!/usr/bin/env bash
    
    set -e
    set -m
    
    
    [ -z "${COZY_STACK_HOST}" ] && COZY_STACK_HOST="cozy.tools"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    [ -z "${COZY_STACK_PORT}" ] && COZY_STACK_PORT="8080"
    [ -z "${COZY_STACK_PASS}" ] && COZY_STACK_PASS="cozy"
    [ -z "${COZY_STACK_ADMIN_PORT}" ] && COZY_STACK_ADMIN_PORT="6060"
    
    [ -z "${COUCHDB_URL}" ] && COUCHDB_URL="http://admin:password@cozy-db:5984/"
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    [ -n "${COZY_KONNECTORS_CMD}" ] && COZY_KONNECTORS_CMD_OPTION="--konnectors-cmd $COZY_KONNECTORS_CMD"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    if [ -d "${COZY_STACK_PATH}" ] && [ -f "${COZY_STACK_PATH}/cozy-stack" ]; then
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	COZY_STACK_PATH="${COZY_STACK_PATH}/cozy-stack"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    fi
    
    echo_err() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	>&2 echo -e "error: ${1}"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    real_path() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	[[ "${1}" = /* ]] && echo "${1}" || echo "${PWD}/${1#./}"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    usage() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	echo -e "Usage: ${0} [-hu] [-d <app path>] [–f <fs directory>]"
    	echo -e ""
    	echo -e "  -d <app path> specify the application directory to serve"
    	echo -e "  -f <app path> specify the fs directory (optional)"
    	echo -e "  -u try to update cozy-stack on start"
    	echo -e "  -h show this usage message"
    	echo -e "\nEnvironment variables"
    	echo -e "\n  COZY_STACK_PATH"
    	echo -e "    specify the path of the cozy-stack binary folder or the binary"
    	echo -e "    itself. default: \"\$GOPATH/bin\"."
    	echo -e "\n  COZY_STACK_HOST"
    	echo -e "    specify the hostname on which the cozy-stack is launched."
    	echo -e "    default: cozy.localhost."
    	echo -e "\n  COZY_STACK_PORT"
    	echo -e "    specify the port on which the cozy-stack is listening."
    	echo -e "    default: 8080."
    	echo -e "\n  COZY_STACK_ADMIN_PORT"
    	echo -e "    specify the admin port on which the cozy-stack is listening."
    	echo -e "    default: 6060."
    	echo -e "\n  COZY_STACK_PASS"
    	echo -e "    specify the password to register the instance with."
    	echo -e "    default: cozy."
    	echo -e "\n  COUCHDB_URL"
    	echo -e "    specify the URL of the CouchDB database. If specified,"
    	echo -e "    the script won't try to start couchdb."
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    do_start() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	if [ -z "${COZY_STACK_PATH}" ]; then
    		COZY_STACK_PATH="${GOPATH}/bin/cozy-stack"
    		if [ ! -f "${COZY_STACK_PATH}" ]; then
    			if [ -z "$(command -v go)" ]; then
    				echo_err "executable \"go\" not found in \$PATH"
    				exit 1
    			fi
    			printf "installing cozy-stack... "
    			go get "github.com/cozy/cozy-stack"
    			echo "ok"
    		fi
    	fi
    
    	if [ -n "${cozy_stack_version}" ]; then
    		echo_err "not implemented... we do not have a release yet"
    		exit 1
    	fi
    
    	if [ "$update" = true ]; then
    		printf "updating cozy-stack... "
    		go get -u "github.com/cozy/cozy-stack"
    		echo "ok"
    	fi
    
    	trap 'kill $(jobs -p)' SIGINT SIGTERM EXIT
    
    	check_not_running "localhost:${COZY_STACK_PORT}" "cozy-stack"
    	do_check_couchdb
    
    	if [ -n "${appdir}" ]; then
    		if [ -f "${appdir}/manifest.webapp" ]; then
    			slug="app"
    		else
    			appsdir=""
    			for i in ${appdir}/*; do
    				if [ -f "${i}/manifest.webapp" ]; then
    					appsdir="${appsdir},$(basename "$i"):${i}"
    				fi
    				if [ -z "$slug" ]; then
    					slug=$(basename "$i")
    				fi
    			done
    			if [ -z "${appsdir}" ]; then
    				echo_err "No manifest found in ${appdir}"
    				exit 1
    			fi
    			appdir=${appsdir:1}
    		fi
    	fi
    
    	echo "starting cozy-stack with ${vfsdir}..."
    
    	${COZY_STACK_PATH} serve --allow-root \
    		--appdir "${appdir}" \
    		--host "::" \
    		--port "${COZY_STACK_PORT}" \
    		--admin-port "${COZY_STACK_ADMIN_PORT}" \
    		--couchdb-url "${COUCHDB_URL}" \
    		--mailhog \
    		${COZY_KONNECTORS_CMD_OPTION} \
    		--fs-url "file://localhost${vfsdir}" &
    
    	wait_for "localhost:${COZY_STACK_PORT}/version/" "cozy-stack"
    
    	if [ "${COZY_STACK_PORT}" = "80" ]; then
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	else
    
    		cozy_dev_addr="cozy.tools:${COZY_STACK_PORT}"
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	fi
    
    	echo ""
    	do_create_instances
    	if [ -n "${slug}" ]; then
    		echo "Everything is setup. Go to http://${slug}.${cozy_dev_addr}/"
    	fi
    	echo "To exit, press ^C"
    	fg 1 > /dev/null
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    do_check_couchdb() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	printf "waiting for couchdb..."
    	wait_for "${COUCHDB_URL}" "couchdb"
    	echo "ok"
    
    	printf "checking couchdb on %s... " "${COUCHDB_URL}"
    	couch_test=$(curl -s -XGET "${COUCHDB_URL}" || echo "")
    
    	printf "checking couchdb on %s... " "${couch_test}"
    	couch_vers=$(grep "\"version\":\s*\"3" <<< "${couch_test}" || echo "")
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    
    	if [ -z "${couch_test}" ]; then
    		echo "failed"
    		echo_err "could not reach couchdb on ${COUCHDB_URL}"
    		exit 1
    	elif [ -z "${couch_vers}" ]; then
    		echo "failed"
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    		echo_err "couchdb v1 is running on ${COUCHDB_URL}"
    
    		echo_err "you need couchdb version >= 3"
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    		exit 1
    	fi
    
    	echo "ok"
    
    	for dbname in "_users" "_replicator"; do
    		curl -s -XPUT "${COUCHDB_URL}/${dbname}" > /dev/null
    	done
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    do_create_instances() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	printf "creating instance %s" "${cozy_dev_addr}"
    	if [ -n "${COZY_STACK_PASS}" ]; then
    		printf " using passphrase \"%s\"" "${COZY_STACK_PASS}"
    	fi
    	printf "... "
    
    	set +e
    	add_instance_val=$(
    		${COZY_STACK_PATH} instances add \
    			--context-name dev \
    			--email dev@cozy.io \
    			--public-name "Jane Doe" \
    			--passphrase ${COZY_STACK_PASS} \
    			"${cozy_dev_addr}" 2>&1
    	)
    	add_instance_ret="${?}"
    	set -e
    	if [ "${add_instance_ret}" = "0" ]; then
    		echo "ok"
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	else
    		exists_test=$(grep -i "already exists" <<< "${add_instance_val}" || echo "")
    		if [ -z "${exists_test}" ]; then
    			echo_err "\n${add_instance_val} ${add_instance_ret}"
    			exit 1
    		fi
    		echo "ok (already created)"
    	fi
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    do_install_app() {
    
            echo "Installing apps home, store, collect"
    
            if cozy-stack apps ls | grep 'home'; then
                    echo "Removing home..."
                    cozy-stack apps uninstall home
            fi
    
            if cozy-stack apps ls | grep 'store'; then
                    echo "Removing store..."
                    cozy-stack apps uninstall store
    
    				if cozy-stack apps ls | grep 'settings'; then
    								echo "Removing settings..."
    								cozy-stack apps uninstall settings
            fi
            cozy-stack apps install --all-domains home
            cozy-stack apps install --all-domains store
            cozy-stack apps install --all-domains settings
    
            echo "App succesfully installed"
    
            echo "Installing konnectors"
    
            # if cozy-stack konnectors ls | grep 'enedisgrandlyon'; then
            #         echo "Removing konnector enedisgrandlyon..."
            #         cozy-stack konnectors uninstall --domain cozy.tools:8080 enedisgrandlyon
            # fi
    
    				if cozy-stack konnectors ls | grep 'enedissgegrandlyon'; then
                    echo "Removing konnector enedissgegrandlyon..."
                    cozy-stack konnectors uninstall --domain cozy.tools:8080 enedissgegrandlyon
    
            if cozy-stack konnectors ls | grep 'grdfgrandlyon'; then
                    echo "Removing konnector grdfgrandlyon..."
    
                    cozy-stack konnectors uninstall --domain cozy.tools:8080 grdfgrandlyon
    
            if cozy-stack konnectors ls | grep 'eglgrandlyon'; then
                    echo "Removing konnector eglgrandlyon..."
    
                    cozy-stack konnectors uninstall --domain cozy.tools:8080 eglgrandlyon
    
            # cozy-stack konnectors install --domain cozy.tools:8080 enedisgrandlyon git://forge.grandlyon.com/web-et-numerique/factory/llle_project/enedis-konnector.git#build
            cozy-stack konnectors install --domain cozy.tools:8080 enedissgegrandlyon git://forge.grandlyon.com/web-et-numerique/factory/llle_project/enedis-sge-konnector.git#build
            cozy-stack konnectors install --domain cozy.tools:8080 grdfgrandlyon git://forge.grandlyon.com/web-et-numerique/factory/llle_project/grdf-konnector.git#build
            cozy-stack konnectors install --domain cozy.tools:8080 eglgrandlyon git://forge.grandlyon.com/web-et-numerique/factory/llle_project/egl-konnector.git#build
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    wait_for() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	i="0"
    	while ! LC_NUMERIC=C curl -s --max-time 0.5 -XGET "${1}" > /dev/null; do
    		sleep 0.5
    		i=$((i+1))
    		if [ "${i}" -gt "100" ]; then
    			echo_err "could not listen to ${2} on ${1}"
    			exit 1
    		fi
    	done
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    check_not_running() {
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	printf "checking that %s is free... " "${1}"
    	if curl -s --max-time 1 -XGET "${1}" > /dev/null; then
    		printf "\n"
    		echo_err "${2} is already running on ${1}"
    		exit 1
    	fi
    	echo "ok"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    }
    
    update=false
    
    while getopts ":hud:f:v:" optname; do
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	case "${optname}" in
    	"h")
    		usage
    		exit 0
    		;;
    	"d")
    		appdir="${OPTARG}"
    		;;
    	"u")
    		update=true
    		;;
    	"f")
    		vfsdir="${OPTARG}"
    		;;
    	"v")
    		cozy_stack_version="${OPTARG}"
    		;;
    	":")
    		echo_err "Option -${OPTARG} requires an argument"
    		echo_err "Type ${0} -h"
    		exit 1
    		;;
    	"?")
    		echo_err "Invalid option ${OPTARG}"
    		echo_err "Type ${0} -h"
    		exit 1
    		;;
    	esac
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    done
    
    if [ -n "${appdir}" ] && [ ! -d "${appdir}" ]; then
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	echo_err "Application directory ${appdir} does not exit"
    	exit 1
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    fi
    
    if [ -z "${vfsdir}" ]; then
    
    Hugo SUBTIL's avatar
    Hugo SUBTIL committed
    	vfsdir="$(pwd)/storage"
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    fi
    
    [ -n "${appdir}" ] && appdir=$(real_path "${appdir}")
    [ -n "${vfsdir}" ] && vfsdir=$(real_path "${vfsdir}")
    
    do_start
    exit 0