diff --git a/scripts/fox-api/bootstrap-node.sh b/scripts/fox-api/bootstrap-node.sh index cda2887..aef5c79 100644 --- a/scripts/fox-api/bootstrap-node.sh +++ b/scripts/fox-api/bootstrap-node.sh @@ -14,20 +14,43 @@ mysql -u root -p$password -e "CREATE USER 'fox_api'@'%' IDENTIFIED BY '$password mysql -u root -p$password -e "GRANT ALL PRIVILEGES ON fox_api.* TO 'fox_api'@'%';" 2>&1 | grep -v "Warning" mysql -u root -p$password -e "FLUSH PRIVILEGES;" 2>&1 | grep -v "Warning" +echo "[BOOTSTRAP] Downloading database dump..." + +mkdir -p /home/coder/temp +wget -q -O /home/coder/temp/schema.sql https://git.kakio.us/Furality/coder-templates/raw/branch/main/configs/fox-api/mysql/schema.sql +wget -q -O /home/coder/temp/migrations.sql https://git.kakio.us/Furality/coder-templates/raw/branch/main/configs/fox-api/mysql/migration_table.sql + +echo "[BOOTSTRAP] Database dump downloaded." +echo "[BOOTSTRAP] Importing database dump..." + +mysql -u root -p$password fox_api < /home/coder/temp/schema.sql +mysql -u root -p$password fox_api < /home/coder/temp/migrations.sql +rm -rf /home/coder/temp + +echo "[BOOTSTRAP] Database dump imported." + chown coder:coder /home/coder/.dbpass echo "[BOOTSTRAP] DB Configured, setting up .env file..." -# read /home/coder/.coder-env as they contain the workspace name and username +echo "[BOOTSTRAP] Downloading .env file..." + +wget -q -O /home/coder/fox-api/.env https://git.kakio.us/Furality/coder-templates/raw/branch/main/configs/fox-api/example.env + +echo "[BOOTSTRAP] .env file downloaded." WORKSPACE_ENV=$(cat /home/coder/.coder-env | grep WORKSPACE_ENV | cut -d '=' -f2) USERNAME_ENV=$(cat /home/coder/.coder-env | grep USERNAME_ENV | cut -d '=' -f2) -sed -i "s/DATABASE_HOST=10.10.30.11/DATABASE_HOST=localhost/g" /home/coder/fox-api/.env -sed -i "s/DATABASE_PORT=3306/DATABASE_PORT=3306/g" /home/coder/fox-api/.env +sed -i "s/DATABASE_HOST=/DATABASE_HOST=localhost/g" /home/coder/fox-api/.env +sed -i "s/DATABASE_PORT=/DATABASE_PORT=3306/g" /home/coder/fox-api/.env sed -i "s/DATABASE_USER=/DATABASE_USER=fox_api/g" /home/coder/fox-api/.env sed -i "s/DATABASE_PASSWORD=/DATABASE_PASSWORD=$password/g" /home/coder/fox-api/.env sed -i "s/DATABASE_NAME=/DATABASE_NAME=fox_api/g" /home/coder/fox-api/.env -sed -i "s/REDIS_HOST=127.0.0.1/REDIS_HOST=localhost/g" /home/coder/fox-api/.env -sed -i \ No newline at end of file +sed -i "s/REDIS_HOST=/REDIS_HOST=localhost/g" /home/coder/fox-api/.env +sed -i "s/REDIS_PORT=/REDIS_PORT=6379/g" /home/coder/fox-api/.env +sed -i "s/REDIS_PASSWORD=/#REDIS_PASSWORD=/g" /home/coder/fox-api/.env + +sed -i "s/BASE_URL=/BASE_URL=http:\/\/$WORKSPACE_ENV-$USERNAME_ENV.coder.kakio.us/g" /home/coder/fox-api/.env + diff --git a/terraform/fox-api/node/main.tf b/terraform/fox-api/node/main.tf index e69de29..57f7c1c 100644 --- a/terraform/fox-api/node/main.tf +++ b/terraform/fox-api/node/main.tf @@ -0,0 +1,265 @@ +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 = <