start.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/bin/bash
  2. ##-- Check if docker is installed --#
  3. if [ -z "$(command -v docker)" ]; then
  4. echo '== Error: Docker binary is not found. Is docker installed?'; exit 56
  5. fi
  6. ##-- Check if pgrok is installed when auto tunneling service is enabled --#
  7. if [ "$DRONE_SERVER_HOST" == "AUTO" ] && [ "$TUNNELING_SERVICE" == "pgrok" ] && [ -z "$(command -v pgrok)" ]; then
  8. echo '== Error: pgrok binary is not found. Is pgrok installed?'; exit 38
  9. fi
  10. ##-- Check if npm is installed when localtunnel is selected --#
  11. if [ "$DRONE_SERVER_HOST" == "AUTO" ] && [ "$TUNNELING_SERVICE" == "localtunnel" ] && [ -z "$(command -v npx)" ]; then
  12. echo '== Error: npx binary is not found. Is npm installed?'; exit 48
  13. fi
  14. ##-- Check if tunnelto is installed and api key when auto tunneling service is enabled --#
  15. if [ "$DRONE_SERVER_HOST" == "AUTO" ] && [ "$TUNNELING_SERVICE" == "tunnelto" ]; then
  16. if [ -z "$(command -v tunnelto)" ]; then
  17. echo '== Error: tunnelto binary is not found. Is tunnelto installed?'; exit 73
  18. fi
  19. if [ -z "$TUNNELTO_API_KEY" ]; then
  20. echo '== Error: tunnelto API key is required. Please visit https://tunnelto.dev/ to get one for free and put it on config.sh in TUNNELTO_API_KEY.'; exit 17
  21. fi
  22. fi
  23. ##-- Deal with configs --##
  24. CURRDIR=$(realpath $(dirname $0))
  25. if [ -f "$CURRDIR/config.sh" ]; then
  26. source "$CURRDIR/config.sh"
  27. else
  28. echo '== config.sh not found, exiting...'; exit 23
  29. fi
  30. #-- Check if repo provider is ok --#
  31. if [ -z "$REPO_PROVIDER" ]; then
  32. echo "== Error: config.sh is probably not updated. Please check the 'Troubleshooting' section on docs/config.md."; exit 63
  33. elif [ "$REPO_PROVIDER" != "gitea" ] && [ "$REPO_PROVIDER" != "gogs" ]; then
  34. echo "== Error: Repo provider ${REPO_PROVIDER} not supported. Please change REPO_PROVIDER on config.sh to a supported one."; exit 39
  35. fi
  36. #-- config derived values --#
  37. export REPO_SERVER_HOST=$(echo "${REPO_SERVER}" | sed -e 's|^[^/]*//||' -e 's|/.*$||')
  38. export DRONE_ALLOWED_USERS="${REPO_USERNAME}@${REPO_SERVER_HOST}"
  39. # To be used for dir name
  40. export REPO_SERVER_HOST_DIR="${REPO_PROVIDER}_"$(echo "${REPO_SERVER_HOST}" | sed 's|[\ ,/,\\,:,*,?,",<,>,\|]|_|g')
  41. ##-- Check if containers are already running from a previous start.sh run --#
  42. if [ -f "${CURRDIR}/run/running_containers" ]; then
  43. read -e -p "Containers are running from previous session. Would you like to stop them first? [y/N] " choice
  44. if [[ "$choice" == [Yy]* ]]; then
  45. ${CURRDIR}/stop.sh
  46. echo '== Stopped. Now continuing to start...'
  47. else
  48. echo "Cannot run what's already running! Exiting..."; exit 190
  49. fi
  50. fi
  51. ##-- Fix for old setup -##
  52. if [ -e "${CURRDIR}/database.sqlite" ] && [ ! -e "${CURRDIR}/data/${REPO_SERVER_HOST_DIR}/database.sqlite" ]; then
  53. read -e -p "== Your Drone CI database.sqlite file is not in place. The directory for it has changed since Droney 0.2.1. Would you like to move it? (Choosing n will cause losing existing build records. Y is recommended.) [Y/n] " choice
  54. if [[ "$choice" == [Nn]* ]]; then
  55. echo '== Chosen no. New database will be created...'
  56. else
  57. mkdir -p "${CURRDIR}/data/${REPO_SERVER_HOST_DIR}/"
  58. mv "${CURRDIR}/database.sqlite" "${CURRDIR}/data/${REPO_SERVER_HOST_DIR}/database.sqlite"
  59. if [ "$?" = 0 ]; then
  60. echo "== Thanks! Database file moved to ${CURRDIR}/data/${REPO_SERVER_HOST_DIR}/database.sqlite..."
  61. else
  62. echo "== Moving database file to ${CURRDIR}/data/${REPO_SERVER_HOST_DIR}/database.sqlite failed... Exiting to save you from data loss..."; exit 29423
  63. fi
  64. fi
  65. fi
  66. ##-- Deal with auto local tunneling, if needed --##
  67. if [ "$DRONE_SERVER_HOST" = "AUTO" ]; then
  68. echo '== Creating localhost tunneling url automatically...'
  69. if [ "$TUNNELING_SERVICE" = "pgrok" ]; then
  70. setsid pgrok -subdomain="${TUNNELING_SERVICE_SUBDOMAIN}" 8090 2>/dev/null &
  71. # Store the pid to kill it later on stop.sh
  72. echo $! > "${CURRDIR}/run/tunneling_pid"
  73. export DRONE_SERVER_HOST="${TUNNELING_SERVICE_SUBDOMAIN}.ejemplo.me"
  74. echo "${DRONE_SERVER_PROTO}://${DRONE_SERVER_HOST}" > "${CURRDIR}/run/tunneling_url"
  75. elif [ "$TUNNELING_SERVICE" = "tunnelto" ]; then
  76. tunnelto set-auth --key "${TUNNELTO_API_KEY}"
  77. setsid tunnelto --subdomain "${TUNNELING_SERVICE_SUBDOMAIN}" --port 8090 2>/dev/null &
  78. # Store the pid to kill it later on stop.sh
  79. echo $! > "${CURRDIR}/run/tunneling_pid"
  80. export DRONE_SERVER_HOST="${TUNNELING_SERVICE_SUBDOMAIN}.tunnelto.dev"
  81. echo "${DRONE_SERVER_PROTO}://${DRONE_SERVER_HOST}" > "${CURRDIR}/run/tunneling_url"
  82. elif [ "$TUNNELING_SERVICE" = "localtunnel" ]; then
  83. mkdir -p /tmp/dronedocker/
  84. setsid npx lt --port 8090 > /tmp/dronedocker/autolt.output &
  85. # Store the pid to kill it later on stop.sh
  86. echo $! > "${CURRDIR}/run/tunneling_pid"
  87. sleep 5
  88. export DRONE_SERVER_HOST=$(grep -Po '(?i)https://\K(.*)' /tmp/dronedocker/autolt.output)
  89. echo "${DRONE_SERVER_PROTO}://${DRONE_SERVER_HOST}" > "${CURRDIR}/run/tunneling_url"
  90. fi
  91. DRONE_RPC_HOST="${DRONE_SERVER_HOST}"
  92. echo "Selecting url automatically as: ${DRONE_SERVER_PROTO}://${DRONE_SERVER_HOST}"
  93. fi
  94. ##-- Create main server --##
  95. CMD="docker run \
  96. --volume=/var/run/docker.sock:/var/run/docker.sock \
  97. --volume='${CURRDIR}/data/${REPO_SERVER_HOST_DIR}':/data"
  98. # Gitea
  99. if [ "$REPO_PROVIDER" = "gitea" ]; then
  100. CMD="${CMD} \
  101. --env=DRONE_GITEA_SERVER='${REPO_SERVER}' \
  102. --env=DRONE_GITEA_CLIENT_ID='${REPO_CLIENT_ID}' \
  103. --env=DRONE_GITEA_CLIENT_SECRET='${REPO_CLIENT_SECRET}'"
  104. # Gogs
  105. elif [ "$REPO_PROVIDER" = "gogs" ]; then
  106. CMD="${CMD} \
  107. --env=DRONE_GOGS_SERVER=${REPO_SERVER}"
  108. fi
  109. CMD="${CMD} \
  110. --env=DRONE_RUNNER_CAPACITY=2 \
  111. --env=DRONE_SERVER_HOST=${DRONE_SERVER_HOST} \
  112. --env=DRONE_RPC_SECRET=${DRONE_RPC_SECRET} \
  113. --env=DRONE_SERVER_PROTO=${DRONE_SERVER_PROTO} \
  114. --env=DRONE_TLS_AUTOCERT=true \
  115. --env=DRONE_USER_CREATE=username:${REPO_USERNAME},admin:true \
  116. --publish=8090:80 \
  117. --publish=443:443 \
  118. --restart=always \
  119. --detach=true \
  120. --name=${DOCKER_MAIN_NAME} \
  121. drone/drone:latest"
  122. eval "${CMD}"
  123. echo "${DOCKER_MAIN_NAME}" >> "${CURRDIR}/run/running_containers"
  124. ##-- Create runner agent --##
  125. sudo docker run -d \
  126. --volume=/var/run/docker.sock:/var/run/docker.sock \
  127. --env=DRONE_RPC_PROTO=${DRONE_RPC_PROTO} \
  128. --env=DRONE_RPC_HOST=${DRONE_RPC_HOST} \
  129. --env=DRONE_RPC_SECRET=${DRONE_RPC_SECRET} \
  130. --env=DRONE_RUNNER_CAPACITY=2 \
  131. --env=DRONE_RUNNER_NAME=${DOCKER_RUNNER_NAME} \
  132. --publish=3000:3000 \
  133. --restart=always \
  134. --name=${DOCKER_RUNNER_NAME} \
  135. drone/drone-runner-docker:latest
  136. echo "${DOCKER_RUNNER_NAME}" >> "${CURRDIR}/run/running_containers"
  137. #-- Setup message --#
  138. echo "== Docker images created"
  139. echo "== Drone URL: ${DRONE_SERVER_PROTO}://${DRONE_SERVER_HOST}/login"
  140. if [ "$REPO_PROVIDER" = 'gogs' ]; then
  141. echo "== Access the above url to continue. You will be asked to enter your credentials because Gogs doesn't have OAuth2 support."
  142. else
  143. echo "== Make sure 'Redirect URL' on your OAuth2 settings is set to: ${DRONE_SERVER_PROTO}://${DRONE_SERVER_HOST}/login"
  144. echo "== Then access the above url to continue"
  145. fi
  146. echo '== Finished.'