github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/scripts/docker/cozy-app-dev/cozy-app-dev.sh (about) 1 #!/usr/bin/env bash 2 3 set -e 4 set -m 5 6 [ -z "${COZY_STACK_HOST}" ] && COZY_STACK_HOST="cozy.localhost" 7 [ -z "${COZY_STACK_PORT}" ] && COZY_STACK_PORT="8080" 8 [ -z "${COZY_STACK_PASS}" ] && COZY_STACK_PASS="cozy" 9 [ -z "${COZY_STACK_ADMIN_PORT}" ] && COZY_STACK_ADMIN_PORT="6060" 10 [ -z "${COUCHDB_URL}" ] && COUCHDB_URL="http://localhost:5984/" 11 [ -n "${COZY_KONNECTORS_CMD}" ] && COZY_KONNECTORS_CMD_OPTION="--konnectors-cmd $COZY_KONNECTORS_CMD" 12 13 if [ -d "${COZY_STACK_PATH}" ] && [ -f "${COZY_STACK_PATH}/cozy-stack" ]; then 14 COZY_STACK_PATH="${COZY_STACK_PATH}/cozy-stack" 15 fi 16 17 echo_err() { 18 >&2 echo -e "error: ${1}" 19 } 20 21 real_path() { 22 [[ "${1}" = /* ]] && echo "${1}" || echo "${PWD}/${1#./}" 23 } 24 25 usage() { 26 echo -e "Usage: ${0} [-hu] [-d <app path>] [–f <fs directory>]" 27 echo -e "" 28 echo -e " -d <app path> specify the application directory to serve" 29 echo -e " -f <app path> specify the fs directory (optional)" 30 echo -e " -u try to update cozy-stack on start" 31 echo -e " -h show this usage message" 32 echo -e "\nEnvironment variables" 33 echo -e "\n COZY_STACK_PATH" 34 echo -e " specify the path of the cozy-stack binary folder or the binary" 35 echo -e " itself. default: \"\$GOPATH/bin\"." 36 echo -e "\n COZY_STACK_HOST" 37 echo -e " specify the hostname on which the cozy-stack is launched." 38 echo -e " default: cozy.localhost." 39 echo -e "\n COZY_STACK_PORT" 40 echo -e " specify the port on which the cozy-stack is listening." 41 echo -e " default: 8080." 42 echo -e "\n COZY_STACK_ADMIN_PORT" 43 echo -e " specify the admin port on which the cozy-stack is listening." 44 echo -e " default: 6060." 45 echo -e "\n COZY_STACK_PASS" 46 echo -e " specify the password to register the instance with." 47 echo -e " default: cozy." 48 echo -e "\n COUCHDB_URL" 49 echo -e " specify the URL of the CouchDB database. If specified," 50 echo -e " the script won't try to start couchdb." 51 } 52 53 do_start() { 54 if [ -z "${COZY_STACK_PATH}" ]; then 55 COZY_STACK_PATH="${GOPATH}/bin/cozy-stack" 56 if [ ! -f "${COZY_STACK_PATH}" ]; then 57 if [ -z "$(command -v go)" ]; then 58 echo_err "executable \"go\" not found in \$PATH" 59 exit 1 60 fi 61 printf "installing cozy-stack... " 62 go get "github.com/cozy/cozy-stack" 63 echo "ok" 64 fi 65 fi 66 67 if [ -n "${cozy_stack_version}" ]; then 68 echo_err "not implemented... we do not have a release yet" 69 exit 1 70 fi 71 72 if [ "$update" = true ]; then 73 printf "updating cozy-stack... " 74 go get -u "github.com/cozy/cozy-stack" 75 echo "ok" 76 fi 77 78 trap 'kill $(jobs -p)' SIGINT SIGTERM EXIT 79 80 check_not_running "localhost:${COZY_STACK_PORT}" "cozy-stack" 81 do_check_couchdb 82 83 if [ -n "${appdir}" ]; then 84 if [ -f "${appdir}/manifest.webapp" ]; then 85 slug="app" 86 else 87 appsdir="" 88 for i in ${appdir}/*; do 89 if [ -f "${i}/manifest.webapp" ]; then 90 appsdir="${appsdir},$(basename "$i"):${i}" 91 fi 92 if [ -z "$slug" ]; then 93 slug=$(basename "$i") 94 fi 95 done 96 if [ -z "${appsdir}" ]; then 97 echo_err "No manifest found in ${appdir}" 98 exit 1 99 fi 100 appdir=${appsdir:1} 101 fi 102 fi 103 104 echo "starting cozy-stack with ${vfsdir}..." 105 106 ${COZY_STACK_PATH} serve --allow-root \ 107 --appdir "${appdir}" \ 108 --host "::" \ 109 --port "${COZY_STACK_PORT}" \ 110 --admin-port "${COZY_STACK_ADMIN_PORT}" \ 111 --couchdb-url "${COUCHDB_URL}" \ 112 --mailhog \ 113 ${COZY_KONNECTORS_CMD_OPTION} \ 114 --fs-url "file://localhost${vfsdir}" & 115 116 wait_for "localhost:${COZY_STACK_PORT}/version/" "cozy-stack" 117 118 if [ "${COZY_STACK_PORT}" = "80" ]; then 119 cozy_dev_addr="${COZY_STACK_HOST}" 120 else 121 cozy_dev_addr="${COZY_STACK_HOST}:${COZY_STACK_PORT}" 122 fi 123 124 echo "" 125 do_create_instances 126 if [ -n "${slug}" ]; then 127 echo "Everything is setup. Go to http://${slug}.${cozy_dev_addr}/" 128 fi 129 echo "To exit, press ^C" 130 fg 1 > /dev/null 131 } 132 133 do_check_couchdb() { 134 printf "waiting for couchdb..." 135 wait_for "${COUCHDB_URL}" "couchdb" 136 echo "ok" 137 138 printf "checking couchdb on %s... " "${COUCHDB_URL}" 139 couch_test=$(curl -s -XGET "${COUCHDB_URL}" || echo "") 140 141 if [ -z "${couch_test}" ]; then 142 echo "failed" 143 echo_err "could not reach couchdb on ${COUCHDB_URL}" 144 exit 1 145 fi 146 147 echo "ok" 148 149 for dbname in "_users" "_replicator"; do 150 curl -s -XPUT "${COUCHDB_URL}/${dbname}" > /dev/null 151 done 152 } 153 154 do_create_instances() { 155 printf "creating instance %s" "${cozy_dev_addr}" 156 if [ -n "${COZY_STACK_PASS}" ]; then 157 printf " using passphrase \"%s\"" "${COZY_STACK_PASS}" 158 fi 159 printf "... " 160 161 set +e 162 add_instance_val=$( 163 ${COZY_STACK_PATH} instances add \ 164 --context-name dev \ 165 --email dev@cozy.io \ 166 --public-name "Jane Doe" \ 167 --passphrase ${COZY_STACK_PASS} \ 168 "${cozy_dev_addr}" 2>&1 169 ) 170 add_instance_ret="${?}" 171 set -e 172 if [ "${add_instance_ret}" = "0" ]; then 173 echo "ok" 174 else 175 exists_test=$(grep -i "already exists" <<< "${add_instance_val}" || echo "") 176 if [ -z "${exists_test}" ]; then 177 echo_err "\n${add_instance_val} ${add_instance_ret}" 178 exit 1 179 fi 180 echo "ok (already created)" 181 fi 182 } 183 184 wait_for() { 185 i="0" 186 while ! LC_NUMERIC=C curl -s --max-time 0.5 -XGET "${1}" > /dev/null; do 187 sleep 0.5 188 i=$((i+1)) 189 if [ "${i}" -gt "100" ]; then 190 echo_err "could not listen to ${2} on ${1}" 191 exit 1 192 fi 193 done 194 } 195 196 check_not_running() { 197 printf "checking that %s is free... " "${1}" 198 if curl -s --max-time 1 -XGET "${1}" > /dev/null; then 199 printf "\n" 200 echo_err "${2} is already running on ${1}" 201 exit 1 202 fi 203 echo "ok" 204 } 205 206 update=false 207 208 while getopts ":hud:f:v:" optname; do 209 case "${optname}" in 210 "h") 211 usage 212 exit 0 213 ;; 214 "d") 215 appdir="${OPTARG}" 216 ;; 217 "u") 218 update=true 219 ;; 220 "f") 221 vfsdir="${OPTARG}" 222 ;; 223 "v") 224 cozy_stack_version="${OPTARG}" 225 ;; 226 ":") 227 echo_err "Option -${OPTARG} requires an argument" 228 echo_err "Type ${0} -h" 229 exit 1 230 ;; 231 "?") 232 echo_err "Invalid option ${OPTARG}" 233 echo_err "Type ${0} -h" 234 exit 1 235 ;; 236 esac 237 done 238 239 if [ -n "${appdir}" ] && [ ! -d "${appdir}" ]; then 240 echo_err "Application directory ${appdir} does not exit" 241 exit 1 242 fi 243 244 if [ -z "${vfsdir}" ]; then 245 vfsdir="$(pwd)/storage" 246 fi 247 248 [ -n "${appdir}" ] && appdir=$(real_path "${appdir}") 249 [ -n "${vfsdir}" ] && vfsdir=$(real_path "${vfsdir}") 250 251 do_start 252 exit 0