2022-01-18 13:13:19 -08:00
#!/usr/bin/env bash
2026-03-11 20:11:03 +01:00
# Usage: ./develop.sh [--agpl] [--port <port>]
2022-08-17 09:02:25 -07:00
#
# If the --agpl parameter is specified, builds only the AGPL-licensed code (no
2026-03-11 20:11:03 +01:00
# Coder enterprise features). The --port parameter changes the API port. The
# frontend dev server still listens on port 8080.
2022-08-17 09:02:25 -07:00
2022-09-08 02:40:17 +10:00
SCRIPT_DIR = $( dirname " ${ BASH_SOURCE [0] } " )
2022-11-10 20:47:42 +02:00
# shellcheck source=scripts/lib.sh
2022-09-08 02:40:17 +10:00
source " ${ SCRIPT_DIR } /lib.sh"
2022-06-22 14:02:31 +01:00
# Allow toggling verbose output
2022-09-08 02:40:17 +10:00
[[ -n ${ VERBOSE :- } ]] && set -x
2022-01-18 13:13:19 -08:00
set -euo pipefail
2026-03-11 20:11:03 +01:00
api_port = 3000
web_port = 8080
proxy_port = 3010
CODER_DEV_ACCESS_URL = " ${ CODER_DEV_ACCESS_URL :- } "
access_url_set = 0
if [ -n " ${ CODER_DEV_ACCESS_URL } " ] ; then
access_url_set = 1
fi
2025-07-23 12:48:15 +01:00
DEVELOP_IN_CODER = " ${ DEVELOP_IN_CODER :- 0 } "
2024-03-05 13:29:08 +00:00
debug = 0
2023-03-08 18:50:55 -03:00
DEFAULT_PASSWORD = "SomeSecurePassword!"
password = " ${ CODER_DEV_ADMIN_PASSWORD :- ${ DEFAULT_PASSWORD }} "
2023-04-20 09:48:47 -05:00
use_proxy = 0
2024-07-18 15:43:07 -04:00
multi_org = 0
2022-09-08 02:40:17 +10:00
2025-08-28 17:42:50 +01:00
# Ensure that extant environment variables do not override
# the config dir we use to override auth for dev.coder.com.
unset CODER_SESSION_TOKEN
unset CODER_URL
2026-03-11 20:11:03 +01:00
args = " $( getopt -o "" -l access-url:,use-proxy,agpl,debug,password:,multi-organization,port: -- " $@ " ) "
2022-08-17 09:02:25 -07:00
eval set -- " $args "
while true; do
case " $1 " in
2023-08-21 18:23:07 +01:00
--access-url)
CODER_DEV_ACCESS_URL = " $2 "
2026-03-11 20:11:03 +01:00
access_url_set = 1
shift 2
;;
--port)
api_port = " $2 "
2023-08-21 18:23:07 +01:00
shift 2
;;
2022-08-17 09:02:25 -07:00
--agpl)
2022-09-08 02:40:17 +10:00
export CODER_BUILD_AGPL = 1
2022-08-17 09:02:25 -07:00
shift
;;
2022-09-08 02:40:17 +10:00
--password)
password = " $2 "
shift 2
;;
2023-04-20 09:48:47 -05:00
--use-proxy)
use_proxy = 1
shift
;;
2024-07-18 15:43:07 -04:00
--multi-organization)
multi_org = 1
shift
;;
2024-03-05 13:29:08 +00:00
--debug)
debug = 1
shift
;;
2022-08-17 09:02:25 -07:00
--)
shift
break
;;
*)
error "Unrecognized option: $1 "
;;
esac
done
2023-04-20 09:48:47 -05:00
if [ " ${ CODER_BUILD_AGPL :- 0 } " -gt "0" ] && [ " ${ use_proxy } " -gt "0" ] ; then
echo '== ERROR: cannot use both external proxies and APGL build.' && exit 1
fi
2024-07-18 15:43:07 -04:00
if [ " ${ CODER_BUILD_AGPL :- 0 } " -gt "0" ] && [ " ${ multi_org } " -gt "0" ] ; then
echo '== ERROR: cannot use both multi-organizations and APGL build.' && exit 1
fi
2026-03-11 20:11:03 +01:00
validate_port() {
local port = $1
local flag = $2
if ! [[ " ${ port } " = ~ ^[ 0-9] +$ ]] ; then
error " ${ flag } must be an integer between 1 and 65535"
fi
if [ " ${# port } " -gt 5 ] ; then
error " ${ flag } must be an integer between 1 and 65535"
fi
if (( 10#${ port } < 1 || 10#${ port } > 65535)) ; then
error " ${ flag } must be an integer between 1 and 65535"
fi
}
validate_port " ${ api_port } " "--port"
if [ " ${ api_port } " -eq " ${ web_port } " ] ; then
error "--port cannot use ${ web_port } because the frontend dev server uses that port"
fi
if [ " ${ use_proxy } " -gt "0" ] && [ " ${ api_port } " -eq " ${ proxy_port } " ] ; then
error "--port cannot use ${ proxy_port } when --use-proxy is enabled because the workspace proxy uses that port"
fi
if [ " ${ access_url_set } " -eq 0 ] ; then
CODER_DEV_ACCESS_URL = "http://127.0.0.1: ${ api_port } "
fi
api_url = "http://127.0.0.1: ${ api_port } "
api_local_url = "http://localhost: ${ api_port } "
web_url = "http://127.0.0.1: ${ web_port } "
2025-07-28 09:20:35 +01:00
if [ -n " ${ CODER_AGENT_URL :- } " ] ; then
2025-07-23 12:48:15 +01:00
DEVELOP_IN_CODER = 1
fi
2026-03-11 20:11:03 +01:00
# Preflight checks: ensure we have our required dependencies, and make sure
# nothing is listening on the configured API or frontend ports.
2025-08-13 11:49:00 +01:00
dependencies curl git go jq make pnpm
2026-03-11 20:11:03 +01:00
if curl --silent --fail " ${ api_url } " >/dev/null 2>& 1; then
2025-08-13 11:49:00 +01:00
# Check if this is the Coder development server.
2026-03-11 20:11:03 +01:00
if curl --silent --fail " ${ api_url } /api/v2/buildinfo" 2>& 1 | jq -r '.version' >/dev/null 2>& 1; then
echo "== INFO: Coder development server is already running on port ${ api_port } !" && exit 0
2025-08-13 11:49:00 +01:00
else
2026-03-11 20:11:03 +01:00
echo "== ERROR: something is listening on port ${ api_port } . Kill it and re-run this script." && exit 1
2025-08-13 11:49:00 +01:00
fi
fi
2026-03-11 20:11:03 +01:00
if curl --fail " ${ web_url } " >/dev/null 2>& 1; then
2025-08-13 11:49:00 +01:00
# Check if this is the Coder development frontend.
2026-03-11 20:11:03 +01:00
if curl --silent --fail " ${ web_url } /api/v2/buildinfo" 2>& 1 | jq -r '.version' >/dev/null 2>& 1; then
echo "== ERROR: Coder development frontend is already running on port ${ web_port } , but the requested API on port ${ api_port } is not already running. Stop the frontend and re-run this script." && exit 1
2025-08-13 11:49:00 +01:00
else
2026-03-11 20:11:03 +01:00
echo "== ERROR: something is listening on port ${ web_port } . Kill it and re-run this script." && exit 1
2025-08-13 11:49:00 +01:00
fi
fi
2022-01-18 13:13:19 -08:00
2022-09-08 02:40:17 +10:00
# Compile the CLI binary. This should also compile the frontend and refresh
# node_modules if necessary.
GOOS = " $( go env GOOS) "
GOARCH = " $( go env GOARCH) "
2025-07-23 12:48:15 +01:00
DEVELOP_IN_CODER = " ${ DEVELOP_IN_CODER } " make -j "build/coder_ ${ GOOS } _ ${ GOARCH } "
2022-05-12 12:37:51 -05:00
2022-07-01 11:09:19 +01:00
# Use the coder dev shim so we don't overwrite the user's existing Coder config.
CODER_DEV_SHIM = " ${ PROJECT_ROOT } /scripts/coder-dev.sh"
2022-11-29 15:45:14 +02:00
# Stores the pid of the subshell that runs our main routine.
ppid = 0
# Tracks pids of commands we've started.
2022-11-10 20:47:42 +02:00
pids =()
exit_cleanup() {
set +e
# Set empty interrupt handler so cleanup isn't interrupted.
2026-03-12 16:09:30 +02:00
# HUP is included in case SSH drops while cleanup is already
# in progress from another signal.
trap '' INT TERM HUP
2022-11-10 20:47:42 +02:00
# Remove exit trap to avoid infinite loop.
trap - EXIT
2026-03-12 16:09:30 +02:00
# Send INT for graceful shutdown. On SIGHUP, the Go server
# re-registers via signal.Notify and handles it, but other
# commands may not. INT covers all cases uniformly.
2022-11-10 20:47:42 +02:00
kill -INT " ${ pids [@] } " >/dev/null 2>& 1
2026-03-12 16:09:30 +02:00
# Use the hammer if things take too long. Stdout/stderr are
# closed so the background job can't hold the shell open.
{ sleep 15 && kill -TERM " ${ pids [@] } " ; } >/dev/null 2>& 1 &
2022-11-10 20:47:42 +02:00
# Wait for all children to exit (this can be aborted by hammer).
wait_cmds
2022-11-29 15:45:14 +02:00
2026-03-13 15:40:53 +02:00
# Add a sleep to allow any final output to be flushed to keep the
# terminal clean.
sleep 0.5
2022-11-10 20:47:42 +02:00
exit 1
}
start_cmd() {
2022-11-29 15:45:14 +02:00
name = $1
prefix = $2
shift 2
2022-11-10 20:47:42 +02:00
echo "== CMD: $* " >& 2
2022-11-29 15:45:14 +02:00
2026-03-12 16:09:30 +02:00
# Shield the command from direct SIGHUP via SIG_IGN, which
# is inherited across exec. Go's signal.Notify resets it to
# caught once registered, enabling graceful shutdown.
(
trap '' HUP
FORCE_COLOR = 1 exec " $@ "
) > >(
# Keep draining output until the command's stdout closes.
# Errexit is off so EIO from a dead terminal doesn't kill
# this reader and break the pipe (causing SIGPIPE).
set +e
trap '' INT HUP
2022-11-29 15:45:14 +02:00
while read -r line; do
if [[ $prefix == date ]] ; then
echo "[ $name ] $( date '+%Y-%m-%d %H:%M:%S' ) $line "
else
echo "[ $name ] $line "
fi
done
echo "== CMD EXIT: $* " >& 2
# Let parent know the command exited.
kill -INT $ppid >/dev/null 2>& 1
) 2>& 1 &
2022-11-10 20:47:42 +02:00
pids +=( " $! " )
}
wait_cmds() {
wait " ${ pids [@] } " >/dev/null 2>& 1
}
fatal() {
echo "== FAIL: $* " >& 2
2026-03-13 15:40:53 +02:00
exit_cleanup
2026-03-12 16:09:30 +02:00
exit 1
2022-11-10 20:47:42 +02:00
}
2022-01-18 13:13:19 -08:00
# This is a way to run multiple processes in parallel, and have Ctrl-C work correctly
# to kill both at the same time. For more details, see:
# https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script
2022-02-01 18:02:59 -08:00
(
2022-11-29 15:45:14 +02:00
ppid = $BASHPID
2022-06-27 09:59:08 +01:00
# If something goes wrong, just bail and tear everything down
# rather than leaving things in an inconsistent state.
2026-03-12 16:09:30 +02:00
trap 'exit_cleanup' INT TERM HUP EXIT
2022-11-10 20:47:42 +02:00
trap 'fatal "Script encountered an error"' ERR
2022-06-23 15:16:27 +01:00
cdroot
2026-03-11 20:11:03 +01:00
DEBUG_DELVE = " ${ debug } " DEVELOP_IN_CODER = " ${ DEVELOP_IN_CODER } " start_cmd API "" " ${ CODER_DEV_SHIM } " server --http-address "0.0.0.0: ${ api_port } " --swagger-enable --access-url " ${ CODER_DEV_ACCESS_URL } " --dangerous-allow-cors-requests= true --enable-terraform-debug-mode " $@ "
2022-05-27 20:15:19 +03:00
2022-06-22 14:02:31 +01:00
echo '== Waiting for Coder to become ready'
2022-11-29 15:45:14 +02:00
# Start the timeout in the background so interrupting this script
# doesn't hang for 60s.
2026-03-11 20:11:03 +01:00
timeout 60s bash -c "until curl -s --fail ${ api_local_url } /healthz > /dev/null 2>&1; do sleep 0.5; done" ||
2022-11-29 15:45:14 +02:00
fatal 'Coder did not become ready in time' &
wait $!
2022-11-10 20:47:42 +02:00
# Check if credentials are already set up to avoid setting up again.
" ${ CODER_DEV_SHIM } " list >/dev/null 2>& 1 && touch " ${ PROJECT_ROOT } /.coderv2/developsh-did-first-setup"
2022-06-15 16:02:18 -05:00
2024-11-21 16:04:21 +01:00
if ! " ${ CODER_DEV_SHIM } " whoami >/dev/null 2>& 1; then
2022-09-13 13:31:33 -04:00
# Try to create the initial admin user.
2024-11-21 16:04:21 +01:00
echo "Login required; use admin@coder.com and password ' ${ password } '" >& 2
2026-03-11 20:11:03 +01:00
if " ${ CODER_DEV_SHIM } " login " ${ api_url } " --first-user-username= admin --first-user-email= admin@coder.com --first-user-password= " ${ password } " --first-user-full-name= "Admin User" --first-user-trial= false; then
2022-11-10 20:47:42 +02:00
# Only create this file if an admin user was successfully
# created, otherwise we won't retry on a later attempt.
touch " ${ PROJECT_ROOT } /.coderv2/developsh-did-first-setup"
else
2022-09-13 13:31:33 -04:00
echo 'Failed to create admin user. To troubleshoot, try running this command manually.'
2022-11-10 20:47:42 +02:00
fi
2022-06-15 16:02:18 -05:00
2022-09-13 13:31:33 -04:00
# Try to create a regular user.
2024-06-26 09:00:42 +01:00
" ${ CODER_DEV_SHIM } " users create --email= member@coder.com --username= member --full-name "Regular User" --password= " ${ password } " ||
2022-09-13 13:31:33 -04:00
echo 'Failed to create regular user. To troubleshoot, try running this command manually.'
fi
2022-06-27 09:59:08 +01:00
2024-07-18 15:43:07 -04:00
# Create a new organization and add the member user to it.
if [ " ${ multi_org } " -gt "0" ] ; then
another_org = "second-organization"
if ! " ${ CODER_DEV_SHIM } " organizations show selected --org " ${ another_org } " >/dev/null 2>& 1; then
echo "Creating organization ' ${ another_org } '..."
(
" ${ CODER_DEV_SHIM } " organizations create -y " ${ another_org } "
) || echo "Failed to create organization ' ${ another_org } '"
fi
if ! " ${ CODER_DEV_SHIM } " org members list --org ${ another_org } | grep "^member" >/dev/null 2>& 1; then
echo "Adding member user to organization ' ${ another_org } '..."
(
" ${ CODER_DEV_SHIM } " organizations members add member --org " ${ another_org } "
) || echo "Failed to add member user to organization ' ${ another_org } '"
fi
echo "Starting external provisioner for ' ${ another_org } '..."
(
start_cmd EXT_PROVISIONER "" " ${ CODER_DEV_SHIM } " provisionerd start --tag "scope=organization" --name second-org-daemon --org " ${ another_org } "
) || echo "Failed to start external provisioner. No external provisioner started."
fi
2022-09-08 02:40:17 +10:00
# If we have docker available and the "docker" template doesn't already
# exist, then let's try to create a template!
template_name = "docker"
2024-11-20 14:01:13 +00:00
# Determine the name of the default org with some jq hacks!
first_org_name = $( " ${ CODER_DEV_SHIM } " organizations show me -o json | jq -r '.[] | select(.is_default) | .name' )
2022-09-08 15:22:08 +10:00
if docker info >/dev/null 2>& 1 && ! " ${ CODER_DEV_SHIM } " templates versions list " ${ template_name } " >/dev/null 2>& 1; then
# sometimes terraform isn't installed yet when we go to create the
# template
2024-07-18 15:43:07 -04:00
echo "Waiting for terraform to be installed..."
2022-09-08 15:22:08 +10:00
sleep 5
2024-07-18 15:43:07 -04:00
echo "Initializing docker template..."
2022-09-08 02:40:17 +10:00
temp_template_dir = " $( mktemp -d) "
2023-04-13 15:02:49 +01:00
" ${ CODER_DEV_SHIM } " templates init --id " ${ template_name } " " ${ temp_template_dir } "
2024-11-20 14:01:13 +00:00
# Run terraform init so we get a terraform.lock.hcl
pushd " ${ temp_template_dir } " && terraform init && popd
2022-09-08 02:40:17 +10:00
DOCKER_HOST = " $( docker context inspect --format '{{ .Endpoints.docker.Host }}' ) "
printf 'docker_arch: "%s"\ndocker_host: "%s"\n' " ${ GOARCH } " " ${ DOCKER_HOST } " >" ${ temp_template_dir } /params.yaml"
2022-07-26 14:09:09 +01:00
(
2024-11-20 14:01:13 +00:00
echo "Pushing docker template to ' ${ first_org_name } '..."
" ${ CODER_DEV_SHIM } " templates push " ${ template_name } " --directory " ${ temp_template_dir } " --variables-file " ${ temp_template_dir } /params.yaml" --yes --org " ${ first_org_name } "
2024-07-18 15:43:07 -04:00
if [ " ${ multi_org } " -gt "0" ] ; then
echo "Pushing docker template to ' ${ another_org } '..."
" ${ CODER_DEV_SHIM } " templates push " ${ template_name } " --directory " ${ temp_template_dir } " --variables-file " ${ temp_template_dir } /params.yaml" --yes --org " ${ another_org } "
fi
2022-09-08 02:40:17 +10:00
rm -rfv " ${ temp_template_dir } " # Only delete template dir if template creation succeeds
2022-07-26 14:09:09 +01:00
) || echo "Failed to create a template. The template files are in ${ temp_template_dir } "
2022-06-27 09:59:08 +01:00
fi
2023-04-20 09:48:47 -05:00
if [ " ${ use_proxy } " -gt "0" ] ; then
log "Using external workspace proxy"
(
# Attempt to delete the proxy first, in case it already exists.
2023-08-01 08:50:43 -07:00
" ${ CODER_DEV_SHIM } " wsproxy delete local-proxy --yes || true
2023-04-20 09:48:47 -05:00
# Create the proxy
2023-05-05 13:09:04 -05:00
proxy_session_token = $( " ${ CODER_DEV_SHIM } " wsproxy create --name= local-proxy --display-name= "Local Proxy" --icon= "/emojis/1f4bb.png" --only-token)
2023-04-20 09:48:47 -05:00
# Start the proxy
2026-03-11 20:11:03 +01:00
start_cmd PROXY "" " ${ CODER_DEV_SHIM } " wsproxy server --dangerous-allow-cors-requests= true --http-address= "localhost: ${ proxy_port } " --proxy-session-token= " ${ proxy_session_token } " --primary-access-url= " ${ api_local_url } "
2023-04-20 09:48:47 -05:00
) || echo "Failed to create workspace proxy. No workspace proxy created."
fi
2026-03-11 20:11:03 +01:00
# Start the frontend once we have a template up and running. We pin the
# port because some environments export PORT for unrelated services.
PORT = " ${ web_port } " CODER_HOST = " ${ api_url } " start_cmd SITE date pnpm --dir ./site dev --host
2022-09-08 02:40:17 +10:00
2022-11-10 20:47:42 +02:00
interfaces =( localhost)
2023-01-17 12:45:27 -06:00
if command -v ip >/dev/null; then
2022-11-10 20:47:42 +02:00
# shellcheck disable=SC2207
interfaces +=( $( ip a | awk '/inet / {print $2}' | cut -d/ -f1) )
2023-01-17 12:45:27 -06:00
elif command -v ifconfig >/dev/null; then
2022-11-10 20:47:42 +02:00
# shellcheck disable=SC2207
interfaces +=( $( ifconfig | awk '/inet / {print $2}' ) )
fi
# Space padding used after the URLs to align "==".
space_padding = 26
2022-06-27 09:59:08 +01:00
log
2022-09-08 02:40:17 +10:00
log "===================================================================="
log "== =="
log "== Coder is now running in development mode. =="
2022-11-10 20:47:42 +02:00
for iface in " ${ interfaces [@] } " ; do
2026-03-11 20:11:03 +01:00
log " $( printf "== API: http://%s: ${ api_port } % $(( space_padding - ${# iface } )) s==" " $iface " "" ) "
2022-11-10 20:47:42 +02:00
done
for iface in " ${ interfaces [@] } " ; do
2026-03-11 20:11:03 +01:00
log " $( printf "== Web UI: http://%s: ${ web_port } % $(( space_padding - ${# iface } )) s==" " $iface " "" ) "
2022-11-10 20:47:42 +02:00
done
2023-04-20 09:48:47 -05:00
if [ " ${ use_proxy } " -gt "0" ] ; then
for iface in " ${ interfaces [@] } " ; do
2026-03-11 20:11:03 +01:00
log " $( printf "== Proxy: http://%s: ${ proxy_port } % $(( space_padding - ${# iface } )) s==" " $iface " "" ) "
2023-04-20 09:48:47 -05:00
done
fi
2022-09-08 02:40:17 +10:00
log "== =="
log "== Use ./scripts/coder-dev.sh to talk to this instance! =="
2023-02-05 23:02:34 -06:00
log " $( printf "== alias cdr=%s/scripts/coder-dev.sh% $(( space_padding - ${# PWD } )) s==" " $PWD " "" ) "
2022-09-08 02:40:17 +10:00
log "===================================================================="
2022-06-27 09:59:08 +01:00
log
2022-09-08 02:40:17 +10:00
2022-06-27 09:59:08 +01:00
# Wait for both frontend and backend to exit.
2022-11-10 20:47:42 +02:00
wait_cmds
2022-02-01 18:02:59 -08:00
)