OpenDroneMap-WebODM/webodm.sh

374 wiersze
11 KiB
Bash
Czysty Zwykły widok Historia

2016-12-07 23:57:07 +00:00
#!/bin/bash
2016-12-14 14:06:04 +00:00
set -eo pipefail
2017-11-17 18:42:34 +00:00
__dirname=$(cd $(dirname "$0"); pwd -P)
2018-04-12 20:58:15 +00:00
cd "${__dirname}"
2016-12-07 23:57:07 +00:00
platform="Linux" # Assumed
2016-12-14 14:06:04 +00:00
uname=$(uname)
case $uname in
"Darwin")
platform="MacOS / OSX"
;;
MINGW*)
platform="Windows"
;;
esac
2016-12-07 23:57:07 +00:00
if [[ $platform = "Windows" ]]; then
export COMPOSE_CONVERT_WINDOWS_PATHS=1
fi
2019-08-14 16:24:07 +00:00
default_nodes=1
2019-02-18 17:57:29 +00:00
dev_mode=false
2017-11-17 18:42:34 +00:00
# Load default values
2019-09-09 16:09:13 +00:00
source "${__dirname}/.env"
2017-11-17 19:43:06 +00:00
DEFAULT_PORT="$WO_PORT"
DEFAULT_HOST="$WO_HOST"
DEFAULT_MEDIA_DIR="$WO_MEDIA_DIR"
DEFAULT_SSL="$WO_SSL"
DEFAULT_SSL_INSECURE_PORT_REDIRECT="$WO_SSL_INSECURE_PORT_REDIRECT"
DEFAULT_BROKER="$WO_BROKER"
2017-11-17 18:42:34 +00:00
# Parse args for overrides
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--port)
2017-11-17 19:43:06 +00:00
export WO_PORT="$2"
2017-11-17 18:42:34 +00:00
shift # past argument
shift # past value
;;
--hostname)
2017-11-17 19:43:06 +00:00
export WO_HOST="$2"
2017-11-17 18:42:34 +00:00
shift # past argument
shift # past value
;;
--media-dir)
2017-11-17 19:43:06 +00:00
export WO_MEDIA_DIR=$(realpath "$2")
2017-11-17 18:42:34 +00:00
shift # past argument
shift # past value
;;
--ssl)
export WO_SSL=YES
2017-11-17 18:42:34 +00:00
shift # past argument
;;
--ssl-key)
2017-11-17 19:43:06 +00:00
export WO_SSL_KEY=$(realpath "$2")
2017-11-17 18:42:34 +00:00
shift # past argument
shift # past value
;;
--ssl-cert)
2017-11-17 19:43:06 +00:00
export WO_SSL_CERT=$(realpath "$2")
2017-11-17 18:42:34 +00:00
shift # past argument
shift # past value
;;
--ssl-insecure-port-redirect)
2017-11-17 19:43:06 +00:00
export WO_SSL_INSECURE_PORT_REDIRECT="$2"
2017-11-17 18:42:34 +00:00
shift # past argument
shift # past value
;;
2018-04-22 15:24:17 +00:00
--debug)
export WO_DEBUG=YES
shift # past argument
2019-02-18 17:57:29 +00:00
;;
--dev)
export WO_DEBUG=YES
2019-07-01 13:57:52 +00:00
export WO_DEV=YES
2019-02-18 17:57:29 +00:00
dev_mode=true
shift # past argument
;;
--broker)
export WO_BROKER="$2"
shift # past argument
shift # past value
;;
--no-default-node)
2019-08-14 16:24:07 +00:00
default_nodes=0
2019-08-14 19:12:46 +00:00
echo "ATTENTION: --no-default-node is deprecated. Use --default-nodes instead."
2019-08-14 16:24:07 +00:00
export WO_DEFAULT_NODES=0
shift # past argument
;;
2019-05-24 19:20:20 +00:00
--with-micmac)
2019-05-10 00:24:27 +00:00
load_micmac_node=true
shift # past argument
;;
--detached)
detached=true
shift # past argument
;;
2019-08-14 16:24:07 +00:00
--default-nodes)
default_nodes="$2"
export WO_DEFAULT_NODES="$2"
shift # past argument
shift # past value
;;
2017-11-17 18:42:34 +00:00
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameter
2016-12-07 23:57:07 +00:00
usage(){
2017-11-17 18:42:34 +00:00
echo "Usage: $0 <command>"
2016-12-07 23:57:07 +00:00
echo
echo "This program helps to manage the setup/teardown of the docker containers for running WebODM. We recommend that you read the full documentation of docker at https://docs.docker.com if you want to customize your setup."
echo
echo "Command list:"
2017-11-17 18:42:34 +00:00
echo " start [options] Start WebODM"
echo " stop Stop WebODM"
echo " down Stop and remove WebODM's docker containers"
echo " update Update WebODM to the latest release"
echo " rebuild Rebuild all docker containers and perform cleanups"
echo " checkenv Do an environment check and install missing components"
echo " test Run the unit test suite (developers only)"
echo " resetadminpassword <new password> Reset the administrator's password to a new one. WebODM must be running when executing this command."
2017-11-17 18:42:34 +00:00
echo ""
echo "Options:"
echo " --port <port> Set the port that WebODM should bind to (default: $DEFAULT_PORT)"
echo " --hostname <hostname> Set the hostname that WebODM will be accessible from (default: $DEFAULT_HOST)"
echo " --media-dir <path> Path where processing results will be stored to (default: $DEFAULT_MEDIA_DIR (docker named volume))"
2019-08-14 16:24:07 +00:00
echo " --default-nodes The amount of default NodeODM nodes attached to WebODM on startup (default: 1)"
2019-05-24 19:20:20 +00:00
echo " --with-micmac Create a NodeMICMAC node attached to WebODM on startup. Experimental! (default: disabled)"
2017-11-17 18:42:34 +00:00
echo " --ssl Enable SSL and automatically request and install a certificate from letsencrypt.org. (default: $DEFAULT_SSL)"
echo " --ssl-key <path> Manually specify a path to the private key file (.pem) to use with nginx to enable SSL (default: None)"
echo " --ssl-cert <path> Manually specify a path to the certificate file (.pem) to use with nginx to enable SSL (default: None)"
echo " --ssl-insecure-port-redirect <port> Insecure port number to redirect from when SSL is enabled (default: $DEFAULT_SSL_INSECURE_PORT_REDIRECT)"
2018-04-22 15:24:17 +00:00
echo " --debug Enable debug for development environments (default: disabled)"
2019-02-18 17:57:29 +00:00
echo " --dev Enable development mode. In development mode you can make modifications to WebODM source files and changes will be reflected live. (default: disabled)"
echo " --broker Set the URL used to connect to the celery broker (default: $DEFAULT_BROKER)"
echo " --detached Run WebODM in detached mode. This means WebODM will run in the background, without blocking the terminal (default: disabled)"
2016-12-07 23:57:07 +00:00
exit
}
# $1 = command | $2 = help_text | $3 = install_command (optional)
2016-12-07 23:57:07 +00:00
check_command(){
check_msg_prefix="Checking for $1... "
check_msg_result="\033[92m\033[1m OK\033[0m\033[39m"
2016-12-07 23:57:07 +00:00
hash $1 2>/dev/null || not_found=true
if [[ $not_found ]]; then
# Can we attempt to install it?
if [[ ! -z "$3" ]]; then
echo -e "$check_msg_prefix \033[93mnot found, we'll attempt to install\033[39m"
run "$3 || sudo $3"
# Recurse, but don't pass the install command
check_command "$1" "$2"
else
check_msg_result="\033[91m can't find $1! Check that the program is installed and that you have added the proper path to the program to your PATH environment variable before launching WebODM. If you change your PATH environment variable, remember to close and reopen your terminal. $2\033[39m"
fi
2016-12-07 23:57:07 +00:00
fi
echo -e "$check_msg_prefix $check_msg_result"
2016-12-15 22:13:50 +00:00
if [[ $not_found ]]; then
return 1
fi
2016-12-07 23:57:07 +00:00
}
environment_check(){
check_command "docker" "https://www.docker.com/"
check_command "git" "https://git-scm.com/downloads"
check_command "docker-compose" "Run \033[1mpip install docker-compose\033[0m" "pip install docker-compose"
2016-12-07 23:57:07 +00:00
}
run(){
echo $1
eval $1
2016-12-07 23:57:07 +00:00
}
start(){
2019-02-18 17:57:29 +00:00
if [[ $dev_mode = true ]]; then
echo "Starting WebODM in development mode..."
down
else
echo "Starting WebODM..."
fi
echo ""
echo "Using the following environment:"
echo "================================"
echo "Host: $WO_HOST"
echo "Port: $WO_PORT"
echo "Media directory: $WO_MEDIA_DIR"
echo "SSL: $WO_SSL"
echo "SSL key: $WO_SSL_KEY"
echo "SSL certificate: $WO_SSL_CERT"
echo "SSL insecure port redirect: $WO_SSL_INSECURE_PORT_REDIRECT"
echo "Celery Broker: $WO_BROKER"
2019-08-14 19:12:46 +00:00
echo "Default Nodes: $WO_DEFAULT_NODES"
echo "================================"
echo "Make sure to issue a $0 down if you decide to change the environment."
echo ""
command="docker-compose -f docker-compose.yml"
2019-08-14 16:24:07 +00:00
if [[ $default_nodes > 0 ]]; then
command+=" -f docker-compose.nodeodm.yml"
fi
2019-02-18 17:57:29 +00:00
2019-05-10 00:24:27 +00:00
if [[ $load_micmac_node = true ]]; then
command+=" -f docker-compose.nodemicmac.yml"
fi
2019-02-18 17:57:29 +00:00
if [[ $dev_mode = true ]]; then
command+=" -f docker-compose.dev.yml"
fi
2017-11-17 19:03:24 +00:00
2017-11-17 19:43:06 +00:00
if [ "$WO_SSL" = "YES" ]; then
if [ ! -z "$WO_SSL_KEY" ] && [ ! -e "$WO_SSL_KEY" ]; then
echo -e "\033[91mSSL key file does not exist: $WO_SSL_KEY\033[39m"
2017-11-17 18:42:34 +00:00
exit 1
fi
2017-11-17 19:43:06 +00:00
if [ ! -z "$WO_SSL_CERT" ] && [ ! -e "$WO_SSL_CERT" ]; then
echo -e "\033[91mSSL certificate file does not exist: $WO_SSL_CERT\033[39m"
2017-11-17 18:42:34 +00:00
exit 1
fi
command+=" -f docker-compose.ssl.yml"
method="Lets Encrypt"
2017-11-17 19:43:06 +00:00
if [ ! -z "$WO_SSL_KEY" ] && [ ! -z "$WO_SSL_CERT" ]; then
2017-11-17 18:42:34 +00:00
method="Manual"
command+=" -f docker-compose.ssl-manual.yml"
fi
2017-11-18 16:25:43 +00:00
2017-11-17 19:03:24 +00:00
if [ "$method" = "Lets Encrypt" ]; then
2017-11-17 19:22:48 +00:00
# Check port settings
# as let's encrypt cannot communicate on ports
# different than 80 or 443
2017-11-17 19:43:06 +00:00
if [ "$WO_PORT" != "$DEFAULT_PORT" ]; then
echo -e "\033[93mLets Encrypt cannot run on port: $WO_PORT, switching to 443.\033[39m"
2017-11-17 19:03:24 +00:00
echo "If you need to use a different port, you'll need to generate the SSL certificate files separately and use the --ssl-key and --ssl-certificate options."
fi
2017-11-17 19:43:06 +00:00
export WO_PORT=443
2017-11-18 16:25:43 +00:00
fi
# Make sure we have a hostname
if [ "$WO_HOST" = "localhost" ]; then
echo -e "\033[91mSSL is enabled, but hostname cannot be set to $WO_HOST. Set the --hostname argument to the domain of your WebODM server (for example: www.mywebodm.org).\033[39m"
exit 1
2017-11-17 19:03:24 +00:00
fi
2017-11-17 19:22:48 +00:00
echo "Will enable SSL ($method)"
2017-11-17 18:42:34 +00:00
fi
2017-11-17 19:03:24 +00:00
command="$command start || $command up"
if [[ $detached = true ]]; then
command+=" -d"
fi
2019-08-14 16:24:07 +00:00
if [[ $default_nodes > 0 ]]; then
command+=" --scale node-odm=$default_nodes"
fi
run "$command"
2016-12-07 23:57:07 +00:00
}
down(){
2019-05-10 00:24:27 +00:00
run "docker-compose -f docker-compose.yml -f docker-compose.nodeodm.yml -f docker-compose.nodemicmac.yml down --remove-orphans"
}
2016-12-07 23:57:07 +00:00
rebuild(){
run "docker-compose down --remove-orphans"
run "rm -fr node_modules/ || sudo rm -fr node_modules/"
run "rm -fr nodeodm/external/NodeODM || sudo rm -fr nodeodm/external/NodeODM"
run "docker-compose -f docker-compose.yml -f docker-compose.build.yml build --no-cache"
#run "docker images --no-trunc -aqf \"dangling=true\" | xargs docker rmi"
2017-03-08 15:42:53 +00:00
echo -e "\033[1mDone!\033[0m You can now start WebODM by running $0 start"
2016-12-07 23:57:07 +00:00
}
run_tests(){
2019-02-18 17:57:29 +00:00
# If in a container, we run the actual test commands
# otherwise we launch this command from the container
2019-02-18 18:04:26 +00:00
in_container=$(grep 'docker\|lxc' /proc/1/cgroup || true)
2019-02-18 17:57:29 +00:00
if [[ "$in_container" != "" ]]; then
echo -e "\033[1mRunning frontend tests\033[0m"
run "npm run test"
2019-02-18 17:57:29 +00:00
echo "\033[1mRunning backend tests\033[0m"
run "python manage.py test"
echo ""
echo -e "\033[1mDone!\033[0m Everything looks in order."
else
echo "Running tests in webapp container"
run "docker-compose exec webapp /bin/bash -c \"/webodm/webodm.sh test\""
fi
}
resetpassword(){
newpass=$1
if [[ ! -z "$newpass" ]]; then
container_hash=$(docker ps -q --filter "name=webapp")
if [[ -z "$container_hash" ]]; then
echo -e "\033[91mCannot find webapp docker container. Is WebODM running?\033[39m"
exit 1
fi
docker exec $container_hash bash -c "echo \"from django.contrib.auth.models import User;from django.contrib.auth.hashers import make_password;u=User.objects.filter(is_superuser=True)[0];u.password=make_password('$newpass');u.save();print('The following user was changed: {}'.format(u.username));\" | python manage.py shell"
if [[ "$?" -eq 0 ]]; then
echo -e "\033[1mPassword changed!\033[0m"
else
echo -e "\033[91mCould not change administrator password. If you need help, please visit https://github.com/OpenDroneMap/WebODM/issues/ \033[39m"
fi
else
usage
fi
}
2016-12-07 23:57:07 +00:00
if [[ $1 = "start" ]]; then
environment_check
start
elif [[ $1 = "stop" ]]; then
environment_check
echo "Stopping WebODM..."
2019-05-10 00:24:27 +00:00
run "docker-compose -f docker-compose.yml -f docker-compose.nodeodm.yml -f docker-compose.nodemicmac.yml stop"
elif [[ $1 = "restart" ]]; then
environment_check
echo "Restarting WebODM..."
down
start
elif [[ $1 = "down" ]]; then
environment_check
echo "Tearing down WebODM..."
down
2016-12-07 23:57:07 +00:00
elif [[ $1 = "rebuild" ]]; then
environment_check
echo "Rebuilding WebODM..."
rebuild
elif [[ $1 = "update" ]]; then
2019-01-18 19:30:43 +00:00
down
2016-12-07 23:57:07 +00:00
echo "Updating WebODM..."
run "git pull origin master"
2019-05-24 19:20:20 +00:00
2019-05-24 19:21:59 +00:00
command="docker-compose -f docker-compose.yml"
2019-05-24 19:20:20 +00:00
2019-08-14 16:24:07 +00:00
if [[ $default_nodes > 0 ]]; then
2019-05-24 19:21:59 +00:00
command+=" -f docker-compose.nodeodm.yml"
fi
2019-05-24 19:20:20 +00:00
2019-05-24 19:21:59 +00:00
if [[ $load_micmac_node = true ]]; then
command+=" -f docker-compose.nodemicmac.yml"
fi
2019-05-24 19:20:20 +00:00
2019-05-24 19:21:59 +00:00
command+=" pull"
run "$command"
echo -e "\033[1mDone!\033[0m You can now start WebODM by running $0 start"
elif [[ $1 = "checkenv" ]]; then
environment_check
elif [[ $1 = "test" ]]; then
run_tests
elif [[ $1 = "resetadminpassword" ]]; then
resetpassword $2
2016-12-07 23:57:07 +00:00
else
usage
fi