diff --git a/.env.database-example b/.env.database-example new file mode 100644 index 000000000..ce06a8d34 --- /dev/null +++ b/.env.database-example @@ -0,0 +1,3 @@ +POSTGRES_USER=unicorn_user +POSTGRES_PASSWORD=magical_password +POSTGRES_DB=calendso \ No newline at end of file diff --git a/.env.example b/.env.example deleted file mode 100644 index e086427dc..000000000 --- a/.env.example +++ /dev/null @@ -1,58 +0,0 @@ -# Set this value to 'agree' to accept our license: -# LICENSE: https://github.com/calendso/calendso/blob/main/LICENSE -# -# Summary of terms: -# - The codebase has to stay open source, whether it was modified or not -# - You can not repackage or sell the codebase -# - Acquire a commercial license to remove these terms by emailing: license@cal.com -NEXT_PUBLIC_LICENSE_CONSENT= -LICENSE= - -# BASE_URL and NEXT_PUBLIC_APP_URL are both deprecated. Both are replaced with one variable, NEXT_PUBLIC_WEBAPP_URL -# BASE_URL=http://localhost:3000 -# NEXT_PUBLIC_APP_URL=http://localhost:3000 - -NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 - -# Configure NEXTAUTH_URL manually if needed, otherwise it will resolve to {NEXT_PUBLIC_WEBAPP_URL}/api/auth -# NEXTAUTH_URL=http://localhost:3000/api/auth - -# It is highly recommended that the NEXTAUTH_SECRET must be overridden and very unique -# Use `openssl rand -base64 32` to generate a key -NEXTAUTH_SECRET=secret - -# Encryption key that will be used to encrypt CalDAV credentials, choose a random string, for example with `dd if=/dev/urandom bs=1K count=1 | md5sum` -CALENDSO_ENCRYPTION_KEY=secret - -# Deprecation note: JWT_SECRET is no longer used -# JWT_SECRET=secret - -POSTGRES_USER=unicorn_user -POSTGRES_PASSWORD=magical_password -POSTGRES_DB=calendso -DATABASE_HOST=database:5432 -DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}/${POSTGRES_DB} -GOOGLE_API_CREDENTIALS={} - -# Set this to '1' if you don't want Cal to collect anonymous usage -CALCOM_TELEMETRY_DISABLED= - -# Used for the Office 365 / Outlook.com Calendar integration -MS_GRAPH_CLIENT_ID= -MS_GRAPH_CLIENT_SECRET= - -# Used for the Zoom integration -ZOOM_CLIENT_ID= -ZOOM_CLIENT_SECRET= - -# E-mail settings -# Configures the global From: header whilst sending emails. -EMAIL_FROM=notifications@example.com - -# Configure SMTP settings (@see https://nodemailer.com/smtp/). -EMAIL_SERVER_HOST=smtp.example.com -EMAIL_SERVER_PORT=587 -EMAIL_SERVER_USER=email_user -EMAIL_SERVER_PASSWORD=email_password - -NODE_ENV=production diff --git a/.gitignore b/.gitignore index d0c241807..7939fecd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. -# .env file -.env \ No newline at end of file +# .env files +.env +.env.appStore +.env.database diff --git a/Dockerfile b/Dockerfile index d56ebdbe9..28f6c24e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,19 +2,14 @@ FROM node:18 as builder WORKDIR /calcom -ARG NEXT_PUBLIC_LICENSE_CONSENT -ARG CALCOM_TELEMETRY_DISABLED -ARG DATABASE_URL -ARG NEXTAUTH_SECRET=secret -ARG CALENDSO_ENCRYPTION_KEY=secret ARG MAX_OLD_SPACE_SIZE=4096 ENV NEXT_PUBLIC_WEBAPP_URL=http://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER \ - NEXT_PUBLIC_LICENSE_CONSENT=$NEXT_PUBLIC_LICENSE_CONSENT \ - CALCOM_TELEMETRY_DISABLED=$CALCOM_TELEMETRY_DISABLED \ - DATABASE_URL=$DATABASE_URL \ - NEXTAUTH_SECRET=${NEXTAUTH_SECRET} \ - CALENDSO_ENCRYPTION_KEY=${CALENDSO_ENCRYPTION_KEY} \ + NEXTAUTH_SECRET=NEXTAUTH_SECRET_PLACEHOLDER \ + CALENDSO_ENCRYPTION_KEY=CALENDSO_ENCRYPTION_KEY_PLACEHOLDER \ + NODE_ENV=production \ + CI=true \ + DISABLE_ESLINT_PLUGIN=true \ NODE_OPTIONS=--max-old-space-size=${MAX_OLD_SPACE_SIZE} COPY calcom/package.json calcom/yarn.lock calcom/.yarnrc.yml calcom/playwright.config.ts calcom/turbo.json calcom/git-init.sh calcom/git-setup.sh ./ @@ -25,51 +20,17 @@ COPY calcom/tests ./tests RUN yarn config set httpTimeout 1200000 && \ npx turbo prune --scope=@calcom/web --docker && \ - yarn install && \ - yarn db-deploy && \ - yarn --cwd packages/prisma seed-app-store + yarn install -RUN yarn turbo run build --filter=@calcom/web +RUN yarn build -# RUN yarn plugin import workspace-tools && \ -# yarn workspaces focus --all --production RUN rm -rf node_modules/.cache .yarn/cache apps/web/.next/cache -FROM node:18 as builder-two - -WORKDIR /calcom -ARG NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 - -ENV NODE_ENV production - -COPY calcom/package.json calcom/.yarnrc.yml calcom/yarn.lock calcom/turbo.json ./ -COPY calcom/.yarn ./.yarn -COPY --from=builder /calcom/node_modules ./node_modules -COPY --from=builder /calcom/packages ./packages -COPY --from=builder /calcom/apps/web ./apps/web -COPY --from=builder /calcom/packages/prisma/schema.prisma ./prisma/schema.prisma -COPY scripts scripts - -# Save value used during this build stage. If NEXT_PUBLIC_WEBAPP_URL and BUILT_NEXT_PUBLIC_WEBAPP_URL differ at -# run-time, then start.sh will find/replace static values again. -ENV NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL \ - BUILT_NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL - -RUN scripts/replace-placeholder.sh http://NEXT_PUBLIC_WEBAPP_URL_PLACEHOLDER ${NEXT_PUBLIC_WEBAPP_URL} - FROM node:18 as runner - WORKDIR /calcom -COPY --from=builder-two /calcom ./ -ARG NEXT_PUBLIC_WEBAPP_URL=http://localhost:3000 -ENV NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL \ - BUILT_NEXT_PUBLIC_WEBAPP_URL=$NEXT_PUBLIC_WEBAPP_URL - -ENV NODE_ENV production -EXPOSE 3000 -HEALTHCHECK --interval=30s --timeout=30s --retries=5 \ - CMD wget --spider http://localhost:3000 || exit 1 +COPY --from=builder /calcom ./ +COPY scripts scripts -CMD ["/calcom/scripts/start.sh"] \ No newline at end of file +CMD ["/bin/sh", "/calcom/scripts/start.sh"] diff --git a/README.md b/README.md index f68a1be8a..bf8d04d1c 100644 --- a/README.md +++ b/README.md @@ -51,57 +51,65 @@ If you are evaluating Cal.com or running with minimal to no modifications, this 1. Clone calcom/docker - ```bash - git clone https://github.com/calcom/docker.git - ``` + ```bash + git clone https://github.com/calcom/docker.git + ``` + + Alternatively, you can also use the --recursive flag to clone git submodules. + + ```bash + git clone --recursive https://github.com/calcom/docker.git + ``` 2. Change into the directory - ```bash - cd docker - ``` + ```bash + cd docker + ``` + +3. Prepare your configuration: Rename `.env.*` files and then update them + + ```bash + cp calcom/.env.example .env + cp calcom/.env.appStore.example .env.appStore + cp .env.database.example .env.database + ``` -3. Prepare your configuration: Rename `.env.example` to `.env` and then update `.env` + Most configurations can be left as-is, but for configuration options see [Important Run-time variables](#important-run-time-variables) below. - ```bash - cp .env.example .env - ``` + Update the appropriate values in your .env file, then proceed. - Most configurations can be left as-is, but for configuration options see [Important Run-time variables](#important-run-time-variables) below. - - Update the appropriate values in your .env file, then proceed. - 4. (optional) Pre-Pull the images by running the following command: - - ```bash - docker compose pull - ``` - - This will use the default image locations as specified by `image:` in the docker-compose.yaml file. - - Note: To aid with support, by default Scarf.sh is used as registry proxy for download metrics. - + + ```bash + docker compose pull + ``` + + This will use the default image locations as specified by `image:` in the docker-compose.yaml file. + + Note: To aid with support, by default Scarf.sh is used as registry proxy for download metrics. + 5. Start Cal.com via docker compose - (Most basic users, and for First Run) To run the complete stack, which includes a local Postgres database, Cal.com web app, and Prisma Studio: - - ```bash - docker compose up -d - ``` + (Most basic users, and for First Run) To run the complete stack, which includes a local Postgres database, Cal.com web app, and Prisma Studio: + + ```bash + docker compose up -d + ``` - To run Cal.com web app and Prisma Studio against a remote database, ensure that DATABASE_URL is configured for an available database and run: + To run Cal.com web app and Prisma Studio against a remote database, ensure that DATABASE_URL is configured for an available database and run: - ```bash - docker compose up -d calcom studio - ``` + ```bash + docker compose up -d calcom studio + ``` - To run only the Cal.com web app, ensure that DATABASE_URL is configured for an available database and run: + To run only the Cal.com web app, ensure that DATABASE_URL is configured for an available database and run: - ```bash - docker compose up -d calcom - ``` + ```bash + docker compose up -d calcom + ``` - **Note: to run in attached mode for debugging, remove `-d` from your desired run command.** + **Note: to run in attached mode for debugging, remove `-d` from your desired run command.** 6. Open a browser to [http://localhost:3000](http://localhost:3000), or your defined NEXT_PUBLIC_WEBAPP_URL. The first time you run Cal.com, a setup wizard will initialize. Define your first user, and you're ready to go! @@ -109,87 +117,94 @@ If you are evaluating Cal.com or running with minimal to no modifications, this 1. Stop the Cal.com stack - ```bash - docker compose down - ``` + ```bash + docker compose down + ``` 2. Pull the latest changes - ```bash - docker compose pull - ``` + ```bash + docker compose pull + ``` + 3. Update env vars as necessary. 4. Re-start the Cal.com stack - ```bash - docker compose up -d - ``` + ```bash + docker compose up -d + ``` ## (Advanced users) Build and Run Cal.com 1. Clone calcom/docker - ```bash - git clone https://github.com/calcom/docker.git calcom-docker - ``` + ```bash + git clone https://github.com/calcom/docker.git calcom-docker + ``` 2. Change into the directory - ```bash - cd calcom-docker - ``` + ```bash + cd calcom-docker + ``` -3. Update the calcom submodule. +3. Update the calcom submodule. - ```bash - git submodule update --remote --init - ``` + ```bash + git submodule update --remote --init + ``` - Note: DO NOT use recursive submodule update, otherwise you will receive a git authentication error. + Note: DO NOT use recursive submodule update, otherwise you will receive a git authentication error. -4. Rename `.env.example` to `.env` and then update `.env` +4. Rename `.env.*` files and then update them: - For configuration options see [Build-time variables](#build-time-variables) below. Update the appropriate values in your .env file, then proceed. + ```bash + cp calcom/.env.example .env + cp calcom/.env.appStore.example .env.appStore + cp .env.database.example .env.database + ``` -5. Build the Cal.com docker image: + For configuration options see [Build-time variables](#build-time-variables) below. Update the appropriate values in your .env file, then proceed. - Note: Due to application configuration requirements, an available database is currently required during the build process. +5. Build the Cal.com docker image: - a) If hosting elsewhere, configure the `DATABASE_URL` in the .env file, and skip the next step + Note: Due to application configuration requirements, an available database is currently required during the build process. - b) If a local or temporary database is required, start a local database via docker compose. + a) If hosting elsewhere, configure the `DATABASE_URL` in the .env file, and skip the next step - ```bash - docker compose up -d database - ``` + b) If a local or temporary database is required, start a local database via docker compose. + + ```bash + docker compose up -d database + ``` 6. Build Cal.com via docker compose (DOCKER_BUILDKIT=0 must be provided to allow a network bridge to be used at build time. This requirement will be removed in the future) - ```bash - DOCKER_BUILDKIT=0 docker compose build calcom - ``` + ```bash + DOCKER_BUILDKIT=0 docker compose build calcom + ``` 7. Start Cal.com via docker compose - (Most basic users, and for First Run) To run the complete stack, which includes a local Postgres database, Cal.com web app, and Prisma Studio: + (Most basic users, and for First Run) To run the complete stack, which includes a local Postgres database, Cal.com web app, and Prisma Studio: - ```bash - docker compose up -d - ``` + ```bash + docker compose up -d + ``` - To run Cal.com web app and Prisma Studio against a remote database, ensure that DATABASE_URL is configured for an available database and run: + To run Cal.com web app and Prisma Studio against a remote database, ensure that DATABASE_URL is configured for an available database and run: - ```bash - docker compose up -d calcom studio - ``` + ```bash + docker compose up -d calcom studio + ``` - To run only the Cal.com web app, ensure that DATABASE_URL is configured for an available database and run: + To run only the Cal.com web app, ensure that DATABASE_URL is configured for an available database and run: - ```bash - docker compose up -d calcom - ``` + ```bash + docker compose up -d calcom + ``` - **Note: to run in attached mode for debugging, remove `-d` from your desired run command.** + **Note: to run in attached mode for debugging, remove `-d` from your desired run command.** 8. Open a browser to [http://localhost:3000](http://localhost:3000), or your defined NEXT_PUBLIC_WEBAPP_URL. The first time you run Cal.com, a setup wizard will initialize. Define your first user, and you're ready to go! @@ -199,29 +214,29 @@ If you are evaluating Cal.com or running with minimal to no modifications, this These variables must also be provided at runtime -| Variable | Description | Required | Default | -| --- | --- | --- | --- | -| CALCOM_LICENSE_KEY | Enterprise License Key | optional | | -| NEXT_PUBLIC_WEBAPP_URL | Base URL of the site. NOTE: if this value differs from the value used at build-time, there will be a slight delay during container start (to update the statically built files). | optional | `http://localhost:3000` | -| NEXTAUTH_URL | Location of the auth server. By default, this is the Cal.com docker instance itself. | optional | `{NEXT_PUBLIC_WEBAPP_URL}/api/auth` | -| NEXTAUTH_SECRET | must match build variable | required | `secret` | -| CALENDSO_ENCRYPTION_KEY | must match build variable | required | `secret` | -| DATABASE_URL | database url with credentials | required | `postgresql://unicorn_user:magical_password@database:5432/calendso` | +| Variable | Description | Required | Default | +| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------- | +| CALCOM_LICENSE_KEY | Enterprise License Key | optional | | +| NEXT_PUBLIC_WEBAPP_URL | Base URL of the site. NOTE: if this value differs from the value used at build-time, there will be a slight delay during container start (to update the statically built files). | optional | `http://localhost:3000` | +| NEXTAUTH_URL | Location of the auth server. By default, this is the Cal.com docker instance itself. | optional | `{NEXT_PUBLIC_WEBAPP_URL}/api/auth` | +| NEXTAUTH_SECRET | must match build variable | required | `secret` | +| CALENDSO_ENCRYPTION_KEY | must match build variable | required | `secret` | +| DATABASE_URL | database url with credentials | required | `postgresql://unicorn_user:magical_password@database:5432/calendso` | ### Build-time variables -If building the image yourself, these variables must be provided at the time of the docker build, and can be provided by updating the .env file. Currently, if you require changes to these variables, you must follow the instructions to build and publish your own image. +If building the image yourself, these variables must be provided at the time of the docker build, and can be provided by updating the .env file. Currently, if you require changes to these variables, you must follow the instructions to build and publish your own image. -Updating these variables is not required for evaluation, but is required for running in production. Instructions for generating variables can be found in the [cal.com instructions](https://github.com/calcom/cal.com) +Updating these variables is not required for evaluation, but is required for running in production. Instructions for generating variables can be found in the [cal.com instructions](https://github.com/calcom/cal.com) -| Variable | Description | Required | Default | -| --- | --- | --- | --- | -| NEXT_PUBLIC_WEBAPP_URL | Base URL injected into static files | optional | `http://localhost:3000` | -| NEXT_PUBLIC_LICENSE_CONSENT | license consent - true/false | | | -| CALCOM_TELEMETRY_DISABLED | Allow cal.com to collect anonymous usage data (set to `1` to disable) | | | -| DATABASE_URL | database url with credentials | required | `postgresql://unicorn_user:magical_password@database:5432/calendso` | -| NEXTAUTH_SECRET | Cookie encryption key | required | `secret` | -| CALENDSO_ENCRYPTION_KEY | Authentication encryption key | required | `secret` | +| Variable | Description | Required | Default | +| --------------------------- | --------------------------------------------------------------------- | -------- | ------------------------------------------------------------------- | +| NEXT_PUBLIC_WEBAPP_URL | Base URL injected into static files | optional | `http://localhost:3000` | +| NEXT_PUBLIC_LICENSE_CONSENT | license consent - true/false | | | +| CALCOM_TELEMETRY_DISABLED | Allow cal.com to collect anonymous usage data (set to `1` to disable) | | | +| DATABASE_URL | database url with credentials | required | `postgresql://unicorn_user:magical_password@database:5432/calendso` | +| NEXTAUTH_SECRET | Cookie encryption key | required | `secret` | +| CALENDSO_ENCRYPTION_KEY | Authentication encryption key | required | `secret` | ## Git Submodules @@ -248,8 +263,9 @@ Certain versions may have trouble creating a user if the field `metadata` is emp ### CLIENT_FETCH_ERROR If you experience this error, it may be the way the default Auth callback in the server is using the WEBAPP_URL as a base url. The container does not necessarily have access to the same DNS as your local machine, and therefor needs to be configured to resolve to itself. You may be able to correct this by configuring `NEXTAUTH_URL=http://localhost:3000/api/auth`, to help the backend loop back to itself. + ``` -docker-calcom-1 | @calcom/web:start: [next-auth][error][CLIENT_FETCH_ERROR] +docker-calcom-1 | @calcom/web:start: [next-auth][error][CLIENT_FETCH_ERROR] docker-calcom-1 | @calcom/web:start: https://next-auth.js.org/errors#client_fetch_error request to http://testing.localhost:3000/api/auth/session failed, reason: getaddrinfo ENOTFOUND testing.localhost { docker-calcom-1 | @calcom/web:start: error: { docker-calcom-1 | @calcom/web:start: message: 'request to http://testing.localhost:3000/api/auth/session failed, reason: getaddrinfo ENOTFOUND testing.localhost', @@ -270,5 +286,4 @@ docker-calcom-1 | @calcom/web:start: message: 'request to http://testing.loca docker-calcom-1 | @calcom/web:start: } ``` - diff --git a/calcom b/calcom index ca78be011..325f250d3 160000 --- a/calcom +++ b/calcom @@ -1 +1 @@ -Subproject commit ca78be011c14489f2c268c3d3d56c7a6b52041e9 +Subproject commit 325f250d396470676ea755df49b90abfb0bb24de diff --git a/docker-compose.yaml b/docker-compose.yaml index f2b90efbd..3be802492 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,5 @@ # Use postgres/example user/password credentials -version: '3.8' +version: "3.8" volumes: database-data: @@ -16,49 +16,23 @@ services: restart: always volumes: - database-data:/var/lib/postgresql/data/ - env_file: .env + env_file: .env.database + # ports: + # - 5432:5432 networks: - stack calcom: - image: calcom.docker.scarf.sh/calcom/cal.com build: context: . dockerfile: Dockerfile - args: - NEXT_PUBLIC_WEBAPP_URL: ${NEXT_PUBLIC_WEBAPP_URL} - NEXT_PUBLIC_LICENSE_CONSENT: ${NEXT_PUBLIC_LICENSE_CONSENT} - CALCOM_TELEMETRY_DISABLED: ${CALCOM_TELEMETRY_DISABLED} - NEXTAUTH_SECRET: ${NEXTAUTH_SECRET} - CALENDSO_ENCRYPTION_KEY: ${CALENDSO_ENCRYPTION_KEY} - DATABASE_URL: ${DATABASE_URL} - network: stack restart: always - networks: - - stack + volumes: + - .env:/calcom/.env + - .env.appStore:/calcom/.env.appStore ports: - 3000:3000 - env_file: .env - environment: - - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}/${POSTGRES_DB} - depends_on: - - database - -# Optional use of Prisma Studio. In production, comment out or remove the section below to prevent unwanted access to your database. - studio: - image: calcom.docker.scarf.sh/calcom/cal.com - restart: always networks: - stack - ports: - - 5555:5555 - env_file: .env - environment: - - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DATABASE_HOST}/${POSTGRES_DB} depends_on: - database - command: - - npx - - prisma - - studio -# END SECTION: Optional use of Prisma Studio. diff --git a/scripts/replace-placeholder.sh b/scripts/replace-placeholder.sh deleted file mode 100755 index 57dbabbbc..000000000 --- a/scripts/replace-placeholder.sh +++ /dev/null @@ -1,16 +0,0 @@ -FROM=$1 -TO=$2 - -if [ "${FROM}" = "${TO}" ]; then - echo "Nothing to replace, the value is already set to ${TO}." - - exit 0 -fi - -# Only peform action if $FROM and $TO are different. -echo "Replacing all statically built instances of $FROM with $TO." - -find apps/web/.next/ apps/web/public -type f | -while read file; do - sed -i "s|$FROM|$TO|g" "$file" -done diff --git a/scripts/start.sh b/scripts/start.sh old mode 100755 new mode 100644 index d92bf2e09..d76ca7d95 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -1,11 +1,8 @@ #!/bin/sh -set -x -# Replace the statically built BUILT_NEXT_PUBLIC_WEBAPP_URL with run-time NEXT_PUBLIC_WEBAPP_URL -# NOTE: if these values are the same, this will be skipped. -scripts/replace-placeholder.sh "$BUILT_NEXT_PUBLIC_WEBAPP_URL" "$NEXT_PUBLIC_WEBAPP_URL" +# Database deploy +yarn workspace @calcom/prisma db-deploy +yarn workspace @calcom/prisma seed-app-store -scripts/wait-for-it.sh ${DATABASE_HOST} -- echo "database is up" -npx prisma migrate deploy --schema /calcom/packages/prisma/schema.prisma -npx ts-node --transpile-only /calcom/packages/prisma/seed-app-store.ts +# Server start yarn start diff --git a/scripts/wait-for-it.sh b/scripts/wait-for-it.sh deleted file mode 100755 index 08c8d6d95..000000000 --- a/scripts/wait-for-it.sh +++ /dev/null @@ -1,184 +0,0 @@ -#!/bin/sh - -# The MIT License (MIT) -# -# Copyright (c) 2017 Eficode Oy -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -set -- "$@" -- "$TIMEOUT" "$QUIET" "$PROTOCOL" "$HOST" "$PORT" "$result" -TIMEOUT=15 -QUIET=0 -# The protocol to make the request with, either "tcp" or "http" -PROTOCOL="tcp" - -echoerr() { - if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi -} - -usage() { - exitcode="$1" - cat << USAGE >&2 -Usage: - $0 host:port|url [-t timeout] [-- command args] - -q | --quiet Do not output any status messages - -t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout - -- COMMAND ARGS Execute command with args after the test finishes -USAGE - exit "$exitcode" -} - -wait_for() { - case "$PROTOCOL" in - tcp) - if ! command -v nc >/dev/null; then - echoerr 'nc command is missing!' - exit 1 - fi - ;; - wget) - if ! command -v wget >/dev/null; then - echoerr 'wget command is missing!' - exit 1 - fi - ;; - esac - - while :; do - case "$PROTOCOL" in - tcp) - nc -w 1 -z "$HOST" "$PORT" > /dev/null 2>&1 - ;; - http) - wget --timeout=1 -q "$HOST" -O /dev/null > /dev/null 2>&1 - ;; - *) - echoerr "Unknown protocol '$PROTOCOL'" - exit 1 - ;; - esac - - result=$? - - if [ $result -eq 0 ] ; then - if [ $# -gt 7 ] ; then - for result in $(seq $(($# - 7))); do - result=$1 - shift - set -- "$@" "$result" - done - - TIMEOUT=$2 QUIET=$3 PROTOCOL=$4 HOST=$5 PORT=$6 result=$7 - shift 7 - exec "$@" - fi - exit 0 - fi - - if [ "$TIMEOUT" -le 0 ]; then - break - fi - TIMEOUT=$((TIMEOUT - 1)) - - sleep 1 - done - echo "Operation timed out" >&2 - exit 1 -} - -while :; do - case "$1" in - http://*|https://*) - HOST="$1" - PROTOCOL="http" - shift 1 - ;; - *:* ) - HOST=$(printf "%s\n" "$1"| cut -d : -f 1) - PORT=$(printf "%s\n" "$1"| cut -d : -f 2) - shift 1 - ;; - -q | --quiet) - QUIET=1 - shift 1 - ;; - -q-*) - QUIET=0 - echoerr "Unknown option: $1" - usage 1 - ;; - -q*) - QUIET=1 - result=$1 - shift 1 - set -- -"${result#-q}" "$@" - ;; - -t | --timeout) - TIMEOUT="$2" - shift 2 - ;; - -t*) - TIMEOUT="${1#-t}" - shift 1 - ;; - --timeout=*) - TIMEOUT="${1#*=}" - shift 1 - ;; - --) - shift - break - ;; - --help) - usage 0 - ;; - -*) - QUIET=0 - echoerr "Unknown option: $1" - usage 1 - ;; - *) - QUIET=0 - echoerr "Unknown argument: $1" - usage 1 - ;; - esac -done - -if ! [ "$TIMEOUT" -ge 0 ] 2>/dev/null; then - echoerr "Error: invalid timeout '$TIMEOUT'" - usage 3 -fi - -case "$PROTOCOL" in - tcp) - if [ "$HOST" = "" ] || [ "$PORT" = "" ]; then - echoerr "Error: you need to provide a host and port to test." - usage 2 - fi - ;; - http) - if [ "$HOST" = "" ]; then - echoerr "Error: you need to provide a host to test." - usage 2 - fi - ;; -esac - -wait_for "$@"