265 lines
7 KiB
HCL
265 lines
7 KiB
HCL
terraform {
|
|
required_providers {
|
|
coder = {
|
|
source = "coder/coder"
|
|
version = "~> 0.8.3"
|
|
}
|
|
docker = {
|
|
source = "kreuzwerker/docker"
|
|
version = "~> 3.0.1"
|
|
}
|
|
}
|
|
}
|
|
|
|
locals {
|
|
username = data.coder_workspace.me.owner
|
|
}
|
|
|
|
data "coder_provisioner" "me" {
|
|
}
|
|
|
|
provider "docker" {
|
|
}
|
|
|
|
data "coder_workspace" "me" {
|
|
}
|
|
|
|
data "coder_git_auth" "github" {
|
|
# Matches the ID of the git auth provider in Coder.
|
|
id = "primary-github"
|
|
}
|
|
|
|
resource "coder_agent" "main" {
|
|
arch = data.coder_provisioner.me.arch
|
|
os = "linux"
|
|
|
|
startup_script_timeout = 180
|
|
startup_script = <<-EOT
|
|
set -e
|
|
|
|
# start the services
|
|
echo "[SETUP] Starting core services (sql and redis)"
|
|
sudo service mysql start 2>&1 /dev/null
|
|
sudo service redis-server start 2>&1 /dev/null
|
|
echo "[SETUP] Core services started"
|
|
|
|
if [ ! -f ~/.coder-env ]; then
|
|
echo "[SETUP] Creating ~/.coder-env"
|
|
echo "WORKSPACE_ENV=$WORKSPACE_ENV" >> ~/.coder-env
|
|
echo "USERNAME_ENV=$USERNAME_ENV" >> ~/.coder-env
|
|
echo "[SETUP] Created ~/.coder-env"
|
|
fi
|
|
|
|
if [ ! -d ~/fox-api ]; then
|
|
echo "[SETUP] Cloning Github Repo"
|
|
git clone --branch typescript https://github.com/furality/fox-api/
|
|
|
|
#TODO, Call IDP API to get client ID and Secret
|
|
|
|
echo "Downloading setup script from git"
|
|
wget -q -O ~/setup-script.sh https://git.kakio.us/Furality/coder-templates/raw/branch/main/scripts/fox-api/bootstrap-node.sh
|
|
echo "[SETUP] Downloaded setup script from git"
|
|
chmod +x ~/setup-script.sh
|
|
|
|
echo "[SETUP] Running Bootstrap"
|
|
sudo ./setup-script.sh 2>&1 /dev/null
|
|
echo "[SETUP] Bootstrap Done"
|
|
cd ~/fox-api
|
|
|
|
echo "[SETUP] Downloading NPM packages"
|
|
npm i --loglevel=error
|
|
sudo npm -g i node-ts --loglevel=error
|
|
|
|
echo "[SETUP] Running TypeORM Migrations"
|
|
npm run typeorm:run-migrations
|
|
echo "[SETUP] Deleting DB Setup Script"
|
|
rm -rf ~/setup-script.sh
|
|
fi
|
|
|
|
echo "[SETUP] Setup Complete"
|
|
EOT
|
|
|
|
env = {
|
|
GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
|
|
GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
|
|
GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
|
|
GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
|
|
GITHUB_TOKEN = "${data.coder_git_auth.github.access_token}"
|
|
WORKSPACE_ENV = "${data.coder_workspace.me.name}"
|
|
USERNAME_ENV = "${data.coder_workspace.me.owner}"
|
|
}
|
|
|
|
metadata {
|
|
display_name = "CPU Usage"
|
|
key = "0_cpu_usage"
|
|
script = "coder stat cpu"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "RAM Usage"
|
|
key = "1_ram_usage"
|
|
script = "coder stat mem"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Home Disk"
|
|
key = "3_home_disk"
|
|
script = "coder stat disk --path $${HOME}"
|
|
interval = 60
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "CPU Usage (Host)"
|
|
key = "4_cpu_usage_host"
|
|
script = "coder stat cpu --host"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Memory Usage (Host)"
|
|
key = "5_mem_usage_host"
|
|
script = "coder stat mem --host"
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Load Average (Host)"
|
|
key = "6_load_host"
|
|
# get load avg scaled by number of cores
|
|
script = <<EOT
|
|
echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
|
|
EOT
|
|
interval = 60
|
|
timeout = 1
|
|
}
|
|
|
|
metadata {
|
|
display_name = "Swap Usage (Host)"
|
|
key = "7_swap_host"
|
|
script = <<EOT
|
|
free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }'
|
|
EOT
|
|
interval = 10
|
|
timeout = 1
|
|
}
|
|
}
|
|
|
|
resource "docker_volume" "home_volume" {
|
|
name = "coder-${data.coder_workspace.me.id}-home"
|
|
# Protect the volume from being deleted due to changes in attributes.
|
|
lifecycle {
|
|
ignore_changes = all
|
|
}
|
|
# Add labels in Docker to keep track of orphan resources.
|
|
labels {
|
|
label = "coder.owner"
|
|
value = data.coder_workspace.me.owner
|
|
}
|
|
labels {
|
|
label = "coder.owner_id"
|
|
value = data.coder_workspace.me.owner_id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_id"
|
|
value = data.coder_workspace.me.id
|
|
}
|
|
# This field becomes outdated if the workspace is renamed but can
|
|
# be useful for debugging or cleaning out dangling volumes.
|
|
labels {
|
|
label = "coder.workspace_name_at_creation"
|
|
value = data.coder_workspace.me.name
|
|
}
|
|
}
|
|
|
|
resource "docker_volume" "data_volume" {
|
|
name = "coder-${data.coder_workspace.me.id}-data"
|
|
# Protect the volume from being deleted due to changes in attributes.
|
|
lifecycle {
|
|
ignore_changes = all
|
|
}
|
|
# Add labels in Docker to keep track of orphan resources.
|
|
labels {
|
|
label = "coder.owner"
|
|
value = data.coder_workspace.me.owner
|
|
}
|
|
labels {
|
|
label = "coder.owner_id"
|
|
value = data.coder_workspace.me.owner_id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_id"
|
|
value = data.coder_workspace.me.id
|
|
}
|
|
# This field becomes outdated if the workspace is renamed but can
|
|
# be useful for debugging or cleaning out dangling volumes.
|
|
labels {
|
|
label = "coder.workspace_name_at_creation"
|
|
value = data.coder_workspace.me.name
|
|
}
|
|
}
|
|
|
|
data "docker_registry_image" "main" {
|
|
name = "git.kakio.us/kakious/coder-backend:latest"
|
|
}
|
|
|
|
resource "docker_image" "main" {
|
|
name = data.docker_registry_image.main.name
|
|
pull_triggers = [data.docker_registry_image.main.sha256_digest]
|
|
force_remove = false
|
|
keep_locally = true
|
|
}
|
|
|
|
resource "docker_container" "workspace" {
|
|
count = data.coder_workspace.me.start_count
|
|
image = docker_image.main.name
|
|
# Uses lower() to avoid Docker restriction on container names.
|
|
name = "${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
|
|
# Hostname makes the shell more user friendly: coder@my-workspace:~$
|
|
hostname = data.coder_workspace.me.name
|
|
memory = "4098"
|
|
memory_swap = "-1"
|
|
# Use the docker gateway if the access URL is 127.0.0.1
|
|
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
|
|
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
|
|
host {
|
|
host = "host.docker.internal"
|
|
ip = "host-gateway"
|
|
}
|
|
volumes {
|
|
container_path = "/home/coder"
|
|
volume_name = docker_volume.home_volume.name
|
|
read_only = false
|
|
}
|
|
|
|
volumes {
|
|
container_path = "/data"
|
|
volume_name = docker_volume.data_volume.name
|
|
read_only = false
|
|
}
|
|
# Add labels in Docker to keep track of orphan resources.
|
|
labels {
|
|
label = "coder.owner"
|
|
value = data.coder_workspace.me.owner
|
|
}
|
|
labels {
|
|
label = "coder.owner_id"
|
|
value = data.coder_workspace.me.owner_id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_id"
|
|
value = data.coder_workspace.me.id
|
|
}
|
|
labels {
|
|
label = "coder.workspace_name"
|
|
value = data.coder_workspace.me.name
|
|
}
|
|
}
|
|
|
|
|