github.com/web-platform-tests/wpt.fyi@v0.0.0-20240530210107-70cf978996f1/util/docker-dev/run.sh (about) 1 #!/bin/bash 2 3 # Start Docker-based development server as `wptd-dev-instance` in the 4 # foreground. 5 6 DOCKER_DIR=$(dirname $0) 7 source "${DOCKER_DIR}/../commands.sh" 8 source "${DOCKER_DIR}/../logging.sh" 9 10 function usage() { 11 USAGE="USAGE: $(basename ${0}) [-q] [-d] [-s] 12 -d daemon mode: Run in the background rather than blocking then cleaning up 13 -q quiet mode: Assume default for all prompts 14 -s stop daemon: Stop a running daemon" 15 >&2 echo "${USAGE}" 16 } 17 18 function stop() { 19 info "Stopping ${DOCKER_INSTANCE}..." 20 wptd_stop 21 info ""${DOCKER_INSTANCE}" stopped." 22 if [[ "${PR}" == "" ]]; then 23 confirm_preserve_remove "Docker instance ${DOCKER_INSTANCE} still exists" 24 fi 25 if [[ "${PR}" == "r" ]]; then 26 info "Removing ${DOCKER_INSTANCE}..." 27 wptd_rm 28 info "${DOCKER_INSTANCE} removed." 29 fi 30 } 31 32 PR="" 33 function confirm_preserve_remove() { 34 if confirm "${1}. Remove?"; then 35 PR="r" 36 else 37 PR="p" 38 fi 39 } 40 41 DAEMON="false" 42 QUIET="false" 43 while getopts ':dhqs' FLAG; do 44 case "${FLAG}" in 45 d) 46 DAEMON="true" ;; 47 s) 48 stop && exit 0 ;; 49 q) 50 QUIET="true" 51 PR="r" ;; 52 h|*) usage && exit 0 ;; 53 esac 54 done 55 56 info "Creating docker instance for dev server. Instance name: wptd-dev-instance" 57 docker inspect "${DOCKER_INSTANCE}" > /dev/null 2>&1 58 INSPECT_STATUS="${?}" 59 docker inspect "${DOCKER_INSTANCE}" | grep '"Running": true' | read 60 RUNNING_STATUS="${?}" 61 62 function quit() { 63 warn "run.sh: Recieved interrupt. Exiting..." 64 stop 65 exit 0 66 } 67 68 if [ "${INSPECT_STATUS}" == "0" ]; then 69 if [[ "${PR}" == "" ]]; then 70 confirm_preserve_remove "Found existing docker instance ${DOCKER_INSTANCE}" 71 fi 72 if [[ "${PR}" == "r" ]]; then 73 stop 74 fi 75 fi 76 77 set -e 78 79 # Create a docker instance: 80 # 81 # -t Give the container a TTY 82 # -v "${WPTD_PATH}":/wpt.fyi Mount the repository 83 # -u $(id -u $USER) Run as current user 84 # --cap-add=SYS_ADMIN Allow Chrome to use sandbox: 85 # https://github.com/GoogleChrome/puppeteer/blob/main/docs/troubleshooting.md 86 # -p "${WPTD_HOST_WEB_PORT}:8080" Expose web server port 87 # --name "${DOCKER_INSTANCE}" Name the instance 88 # wptd-dev Identify image to use 89 90 VOLUMES="-v ${WPTD_PATH}:/home/user/wpt.fyi" 91 92 if [[ "${INSPECT_STATUS}" != 0 ]] || [[ "${PR}" == "r" ]]; then 93 info "Starting docker instance ${DOCKER_INSTANCE}..." 94 docker run -t -d --entrypoint /bin/bash \ 95 ${VOLUMES} \ 96 -u $(id -u $USER) \ 97 --cap-add=SYS_ADMIN \ 98 -p "${WPTD_HOST_WEB_PORT}:8080" \ 99 -p "${WPTD_HOST_GCD_PORT}:8001" \ 100 --workdir "/home/user/wpt.fyi" \ 101 --name "${DOCKER_INSTANCE}" \ 102 ${DOCKER_IMAGE} 103 info "Setting up local user" 104 wptd_useradd 105 106 info "Ensuring the home directory is owned by the user..." 107 wptd_chown "/home/user" 108 109 info "Instance ${DOCKER_INSTANCE} started." 110 elif [[ "${RUNNING_STATUS}" != "0" ]]; then 111 info "Restarting docker instance ${DOCKER_INSTANCE}..." 112 docker start "${DOCKER_INSTANCE}" 113 info "Instance ${DOCKER_INSTANCE} restarted." 114 else 115 info "Docker instance ${DOCKER_INSTANCE} already running." 116 exit 0 117 fi 118 119 trap quit INT 120 121 info "Updating system/packages..." 122 wptd_exec make sys_deps 123 124 info "Installing dev dependencies..." 125 wptd_exec make dev_appserver_deps 126 127 if [[ "${DAEMON}" == "true" ]]; then 128 exit 0 129 fi 130 131 info "Starting Cloud Datastore emulator. Port forwarded to host: ${WPTD_HOST_GCD_PORT}" 132 info "=== Hit Ctrl+C to end ===" 133 wptd_exec gcloud beta emulators datastore start \ 134 --project=wptdashboard-local \ 135 --consistency=1.0 \ 136 --host-port=localhost:8001 2> /dev/null