From f541c1b9c1728937084d57478220f9b835225b85 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Tue, 26 Jul 2022 11:41:25 +0100
Subject: [PATCH 1/5] fix: update agent ID in example templates

---
 examples/templates/aws-linux/main.tf            | 2 +-
 examples/templates/aws-windows/main.tf          | 2 +-
 examples/templates/bare/main.tf                 | 6 +++---
 examples/templates/do-linux/main.tf             | 4 ++--
 examples/templates/docker-code-server/main.tf   | 6 +++---
 examples/templates/docker-image-builds/main.tf  | 4 ++--
 examples/templates/docker-with-dotfiles/main.tf | 4 ++--
 examples/templates/docker/main.tf               | 6 +++---
 examples/templates/gcp-linux/main.tf            | 2 +-
 examples/templates/gcp-vm-container/main.tf     | 4 ++--
 examples/templates/gcp-windows/main.tf          | 2 +-
 11 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/examples/templates/aws-linux/main.tf b/examples/templates/aws-linux/main.tf
index 3cbe5d5444cd5..2ba3a7d8cb359 100644
--- a/examples/templates/aws-linux/main.tf
+++ b/examples/templates/aws-linux/main.tf
@@ -109,7 +109,7 @@ Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment; filename="userdata.txt"
 
 #!/bin/bash
-sudo -u ${local.linux_user} sh -c '${coder_agent.dev.init_script}'
+sudo -u ${local.linux_user} sh -c '${coder_agent.main.init_script}'
 --//--
 EOT
 
diff --git a/examples/templates/aws-windows/main.tf b/examples/templates/aws-windows/main.tf
index 599d33017dd4c..6ff855ddb100b 100644
--- a/examples/templates/aws-windows/main.tf
+++ b/examples/templates/aws-windows/main.tf
@@ -83,7 +83,7 @@ locals {
   user_data_start = <<EOT
 <powershell>
 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
-${coder_agent.dev.init_script}
+${coder_agent.main.init_script}
 </powershell>
 <persist>true</persist>
 EOT
diff --git a/examples/templates/bare/main.tf b/examples/templates/bare/main.tf
index 98619f38ee293..ce873984e97eb 100644
--- a/examples/templates/bare/main.tf
+++ b/examples/templates/bare/main.tf
@@ -15,7 +15,7 @@ terraform {
 data "coder_workspace" "me" {
 }
 
-resource "coder_agent" "dev1" {
+resource "coder_agent" "main" {
   os   = "linux"
   arch = "amd64"
   auth = "token"
@@ -32,8 +32,8 @@ resource "null_resource" "fake-compute" {
   # Run the Coder agent init script on resources
   # to access web apps and SSH:
   #
-  # export CODER_AGENT_TOKEN=${coder_agent.dev1.token}
-  # ${coder_agent.dev1.init_script}
+  # export CODER_AGENT_TOKEN=${coder_agent.main.token}
+  # ${coder_agent.main.init_script}
 }
 
 resource "null_resource" "fake-disk" {
diff --git a/examples/templates/do-linux/main.tf b/examples/templates/do-linux/main.tf
index 3a40046f4de2b..499b2ed42a80e 100644
--- a/examples/templates/do-linux/main.tf
+++ b/examples/templates/do-linux/main.tf
@@ -116,8 +116,8 @@ resource "digitalocean_droplet" "workspace" {
   user_data = templatefile("cloud-config.yaml.tftpl", {
     username          = data.coder_workspace.me.owner
     home_volume_label = digitalocean_volume.home_volume.initial_filesystem_label
-    init_script       = base64encode(coder_agent.dev.init_script)
-    coder_agent_token = coder_agent.dev.token
+    init_script       = base64encode(coder_agent.main.init_script)
+    coder_agent_token = coder_agent.main.token
   })
   # Required to provision Fedora.
   ssh_keys = var.step2_do_admin_ssh_key > 0 ? [var.step2_do_admin_ssh_key] : []
diff --git a/examples/templates/docker-code-server/main.tf b/examples/templates/docker-code-server/main.tf
index faed48c6a76f5..29be1ff990219 100644
--- a/examples/templates/docker-code-server/main.tf
+++ b/examples/templates/docker-code-server/main.tf
@@ -53,7 +53,7 @@ resource "coder_agent" "main" {
 }
 
 resource "coder_app" "code-server" {
-  agent_id = coder_agent.dev.id
+  agent_id = coder_agent.main.id
   url      = "http://localhost:8080/?folder=/home/coder"
   icon     = "/icon/code.svg"
 }
@@ -70,8 +70,8 @@ resource "docker_container" "workspace" {
   hostname = lower(data.coder_workspace.me.name)
   dns      = ["1.1.1.1"]
   # Use the docker gateway if the access URL is 127.0.0.1
-  entrypoint = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
-  env        = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
+  entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "127.0.0.1", "host.docker.internal")]
+  env        = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
   host {
     host = "host.docker.internal"
     ip   = "host-gateway"
diff --git a/examples/templates/docker-image-builds/main.tf b/examples/templates/docker-image-builds/main.tf
index 243d31404ced1..a3cfbeb01d872 100644
--- a/examples/templates/docker-image-builds/main.tf
+++ b/examples/templates/docker-image-builds/main.tf
@@ -108,8 +108,8 @@ resource "docker_container" "workspace" {
   hostname = lower(data.coder_workspace.me.name)
   dns      = ["1.1.1.1"]
   # Use the docker gateway if the access URL is 127.0.0.1
-  command = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
-  env     = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
+  command = ["sh", "-c", replace(coder_agent.main.init_script, "127.0.0.1", "host.docker.internal")]
+  env     = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
   host {
     host = "host.docker.internal"
     ip   = "host-gateway"
diff --git a/examples/templates/docker-with-dotfiles/main.tf b/examples/templates/docker-with-dotfiles/main.tf
index dcf9fa19b134c..afc91c34e326b 100644
--- a/examples/templates/docker-with-dotfiles/main.tf
+++ b/examples/templates/docker-with-dotfiles/main.tf
@@ -54,8 +54,8 @@ resource "docker_container" "workspace" {
   name = "coder-${data.coder_workspace.me.owner}-${lower(data.coder_workspace.me.name)}"
   dns  = ["1.1.1.1"]
   # Refer to Docker host when Coder is on localhost
-  command = ["sh", "-c", replace(coder_agent.dev.init_script, "127.0.0.1", "host.docker.internal")]
-  env     = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
+  command = ["sh", "-c", replace(coder_agent.main.init_script, "127.0.0.1", "host.docker.internal")]
+  env     = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
   host {
     host = "host.docker.internal"
     ip   = "host-gateway"
diff --git a/examples/templates/docker/main.tf b/examples/templates/docker/main.tf
index 08687bc18cd11..1fbc191fced6d 100644
--- a/examples/templates/docker/main.tf
+++ b/examples/templates/docker/main.tf
@@ -88,7 +88,7 @@ resource "coder_agent" "main" {
 }
 
 resource "coder_app" "code-server" {
-  agent_id = coder_agent.dev.id
+  agent_id = coder_agent.main.id
   name     = "code-server"
   url      = "http://localhost:13337/?folder=/home/coder"
   icon     = "/icon/code.svg"
@@ -124,10 +124,10 @@ resource "docker_container" "workspace" {
     "sh", "-c",
     <<EOT
     trap '[ $? -ne 0 ] && echo === Agent script exited with non-zero code. Sleeping infinitely to preserve logs... && sleep infinity' EXIT
-    ${replace(coder_agent.dev.init_script, "localhost", "host.docker.internal")}
+    ${replace(coder_agent.main.init_script, "localhost", "host.docker.internal")}
     EOT
   ]
-  env = ["CODER_AGENT_TOKEN=${coder_agent.dev.token}"]
+  env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
   host {
     host = "host.docker.internal"
     ip   = "host-gateway"
diff --git a/examples/templates/gcp-linux/main.tf b/examples/templates/gcp-linux/main.tf
index c72f962bed131..da823e0036185 100644
--- a/examples/templates/gcp-linux/main.tf
+++ b/examples/templates/gcp-linux/main.tf
@@ -79,7 +79,7 @@ set -eux pipefail
 
 mkdir /root || true
 cat <<'EOCODER' > /root/coder_agent.sh
-${coder_agent.dev.init_script}
+${coder_agent.main.init_script}
 EOCODER
 chmod +x /root/coder_agent.sh
 
diff --git a/examples/templates/gcp-vm-container/main.tf b/examples/templates/gcp-vm-container/main.tf
index a0cbf860763ec..fc6ab396144df 100644
--- a/examples/templates/gcp-vm-container/main.tf
+++ b/examples/templates/gcp-vm-container/main.tf
@@ -48,7 +48,7 @@ module "gce-container" {
   container = {
     image   = "mcr.microsoft.com/vscode/devcontainers/go:1"
     command = ["sh"]
-    args    = ["-c", coder_agent.dev.init_script]
+    args    = ["-c", coder_agent.main.init_script]
     securityContext = {
       privileged : true
     }
@@ -85,6 +85,6 @@ resource "google_compute_instance" "dev" {
 
 resource "coder_agent_instance" "dev" {
   count       = data.coder_workspace.me.start_count
-  agent_id    = coder_agent.dev.id
+  agent_id    = coder_agent.main.id
   instance_id = google_compute_instance.dev[0].instance_id
 }
diff --git a/examples/templates/gcp-windows/main.tf b/examples/templates/gcp-windows/main.tf
index 4f7bfd1270552..568183ab2834f 100644
--- a/examples/templates/gcp-windows/main.tf
+++ b/examples/templates/gcp-windows/main.tf
@@ -71,7 +71,7 @@ resource "google_compute_instance" "dev" {
     scopes = ["cloud-platform"]
   }
   metadata = {
-    windows-startup-script-ps1 = coder_agent.dev.init_script
+    windows-startup-script-ps1 = coder_agent.main.init_script
     serial-port-enable         = "TRUE"
   }
 }

From 0f89853e95b420a8b80f69d0852258c689d67377 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Tue, 26 Jul 2022 11:41:45 +0100
Subject: [PATCH 2/5] fix: update agent ID in dogfood template

---
 dogfood/main.tf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/dogfood/main.tf b/dogfood/main.tf
index f8ac5a217edf9..b8837ca71e415 100644
--- a/dogfood/main.tf
+++ b/dogfood/main.tf
@@ -38,7 +38,7 @@ resource "coder_agent" "main" {
 }
 
 resource "coder_app" "code-server" {
-  agent_id = coder_agent.dev.id
+  agent_id = coder_agent.main.id
   name     = "code-server"
   url      = "http://localhost:13337/?folder=/home/coder"
   icon     = "/icon/code.svg"

From 613fd3cf4ca07c1c0f513bbf35f66fd7ac99cdf8 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Tue, 26 Jul 2022 11:42:04 +0100
Subject: [PATCH 3/5] chore: update default agent ID in documentation

---
 docs/ides/configuring-web-ides.md | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/docs/ides/configuring-web-ides.md b/docs/ides/configuring-web-ides.md
index 30ce9e6cc7fad..e235a28939e34 100644
--- a/docs/ides/configuring-web-ides.md
+++ b/docs/ides/configuring-web-ides.md
@@ -17,7 +17,7 @@ be used as a Coder application. For example:
 ```hcl
 # Give template users the portainer.io web UI
 resource "coder_app" "portainer" {
-  agent_id      = coder_agent.dev.id
+  agent_id      = coder_agent.main.id
   name          = "portainer"
   icon          = "https://simpleicons.org/icons/portainer.svg"
   url           = "http://localhost:8000"
@@ -69,7 +69,7 @@ You'll also need to specify a `coder_app` resource related to the agent. This is
 
 ```hcl
 resource "coder_app" "code-server" {
-  agent_id = coder_agent.dev.id
+  agent_id = coder_agent.main.id
   name     = "code-server"
   url      = "http://localhost:13337/?folder=/home/coder"
   icon     = "/icon/code.svg"

From 15d3e93236b6fe642528cdcbfd520bf892d78eda Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Tue, 26 Jul 2022 11:42:44 +0100
Subject: [PATCH 4/5] fix: develop.sh: start FE after template is created;
 leave template dir around if template creation fails

---
 scripts/develop.sh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/scripts/develop.sh b/scripts/develop.sh
index 0c91e30cb65d3..8706daacadd3c 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -44,7 +44,6 @@ CODER_DEV_SHIM="${PROJECT_ROOT}/scripts/coder-dev.sh"
 	# rather than leaving things in an inconsistent state.
 	trap 'kill -TERM -$$' ERR
 	cdroot
-	CODER_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -$$ &
 	"${CODER_DEV_SHIM}" server --address 127.0.0.1:3000 --in-memory --tunnel || kill -INT -$$ &
 
 	echo '== Waiting for Coder to become ready'
@@ -68,10 +67,14 @@ CODER_DEV_SHIM="${PROJECT_ROOT}/scripts/coder-dev.sh"
 		DOCKER_HOST=$(docker context inspect --format '{{.Endpoints.docker.Host}}')
 		printf 'docker_arch: "%s"\ndocker_host: "%s"\n' "${GOARCH}" "${DOCKER_HOST}" | tee "${temp_template_dir}/params.yaml"
 		template_name="docker-${GOARCH}"
-		"${CODER_DEV_SHIM}" templates create "${template_name}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes
-		rm -rfv "${temp_template_dir}"
+		(
+			"${CODER_DEV_SHIM}" templates create "${template_name}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes &&
+			rm -rfv "${temp_template_dir}" # Only delete template dir if template creation succeeds
+		) || echo "Failed to create a template. The template files are in ${temp_template_dir}"
 	fi
 
+	# Start the frontend once we have a template up and running
+	CODER_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev || kill -INT -$$ &
 	log
 	log "======================================================================="
 	log "==                                                                   =="

From dcd67dfc6e89704bb99a49e86ea49b48453671b0 Mon Sep 17 00:00:00 2001
From: Cian Johnston <cian@coder.com>
Date: Tue, 26 Jul 2022 11:50:48 +0100
Subject: [PATCH 5/5] make fmt

---
 scripts/develop.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/develop.sh b/scripts/develop.sh
index 8706daacadd3c..365599aabea17 100755
--- a/scripts/develop.sh
+++ b/scripts/develop.sh
@@ -69,7 +69,7 @@ CODER_DEV_SHIM="${PROJECT_ROOT}/scripts/coder-dev.sh"
 		template_name="docker-${GOARCH}"
 		(
 			"${CODER_DEV_SHIM}" templates create "${template_name}" --directory "${temp_template_dir}" --parameter-file "${temp_template_dir}/params.yaml" --yes &&
-			rm -rfv "${temp_template_dir}" # Only delete template dir if template creation succeeds
+				rm -rfv "${temp_template_dir}" # Only delete template dir if template creation succeeds
 		) || echo "Failed to create a template. The template files are in ${temp_template_dir}"
 	fi