zsh2517

Untitled

Dec 18th, 2024
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. terraform {
  2. required_providers {
  3. coder = {
  4. source = "coder/coder"
  5. }
  6. docker = {
  7. source = "kreuzwerker/docker"
  8. }
  9. }
  10. }
  11.  
  12. locals {
  13. username = data.coder_workspace_owner.me.name
  14. }
  15.  
  16. data "coder_provisioner" "me" {
  17. }
  18.  
  19. provider "docker" {
  20. }
  21.  
  22. data "coder_workspace" "me" {
  23. }
  24. data "coder_workspace_owner" "me" {}
  25.  
  26. resource "coder_agent" "main" {
  27. arch = data.coder_provisioner.me.arch
  28. os = "linux"
  29. startup_script = <<-EOT
  30. set -e
  31. sudo dockerd &
  32. # Prepare user home with default files on first start.
  33. if [ ! -f ~/.init_done ]; then
  34. cp -rT /etc/skel ~
  35. touch ~/.init_done
  36. fi
  37.  
  38. # install and start code-server
  39. curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.19.1
  40. /tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
  41. EOT
  42.  
  43. # These environment variables allow you to make Git commits right away after creating a
  44. # workspace. Note that they take precedence over configuration defined in ~/.gitconfig!
  45. # You can remove this block if you'd prefer to configure Git manually or using
  46. # dotfiles. (see docs/dotfiles.md)
  47. env = {
  48. GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
  49. GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
  50. GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
  51. GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
  52. }
  53.  
  54. # The following metadata blocks are optional. They are used to display
  55. # information about your workspace in the dashboard. You can remove them
  56. # if you don't want to display any information.
  57. # For basic resources, you can use the `coder stat` command.
  58. # If you need more control, you can write your own script.
  59. metadata {
  60. display_name = "CPU Usage"
  61. key = "0_cpu_usage"
  62. script = "coder stat cpu"
  63. interval = 10
  64. timeout = 1
  65. }
  66.  
  67. metadata {
  68. display_name = "RAM Usage"
  69. key = "1_ram_usage"
  70. script = "coder stat mem"
  71. interval = 10
  72. timeout = 1
  73. }
  74.  
  75. metadata {
  76. display_name = "Home Disk"
  77. key = "3_home_disk"
  78. script = "coder stat disk --path $${HOME}"
  79. interval = 60
  80. timeout = 1
  81. }
  82.  
  83. metadata {
  84. display_name = "CPU Usage (Host)"
  85. key = "4_cpu_usage_host"
  86. script = "coder stat cpu --host"
  87. interval = 10
  88. timeout = 1
  89. }
  90.  
  91. metadata {
  92. display_name = "Memory Usage (Host)"
  93. key = "5_mem_usage_host"
  94. script = "coder stat mem --host"
  95. interval = 10
  96. timeout = 1
  97. }
  98.  
  99. metadata {
  100. display_name = "Load Average (Host)"
  101. key = "6_load_host"
  102. # get load avg scaled by number of cores
  103. script = <<EOT
  104. echo "`cat /proc/loadavg | awk '{ print $1 }'` `nproc`" | awk '{ printf "%0.2f", $1/$2 }'
  105. EOT
  106. interval = 60
  107. timeout = 1
  108. }
  109.  
  110. metadata {
  111. display_name = "Swap Usage (Host)"
  112. key = "7_swap_host"
  113. script = <<EOT
  114. free -b | awk '/^Swap/ { printf("%.1f/%.1f", $3/1024.0/1024.0/1024.0, $2/1024.0/1024.0/1024.0) }'
  115. EOT
  116. interval = 10
  117. timeout = 1
  118. }
  119. }
  120.  
  121. resource "coder_app" "code-server" {
  122. agent_id = coder_agent.main.id
  123. slug = "code-server"
  124. display_name = "code-server"
  125. ## url = "http://localhost:13337/?folder=/home/${local.username}"
  126. url = "http://localhost:13337/?folder=/home/coder"
  127. icon = "/icon/code.svg"
  128. subdomain = true
  129. share = "owner"
  130.  
  131. healthcheck {
  132. url = "http://localhost:13337/healthz"
  133. interval = 5
  134. threshold = 6
  135. }
  136. }
  137.  
  138. resource "coder_app" "app-in-code-new-domain" {
  139. agent_id = coder_agent.main.id
  140. slug = "app-in-code-new-domain"
  141. display_name = "app-in-code-new-domain"
  142. ## url = "http://localhost:13337/?folder=/home/${local.username}"
  143. url = "http://localhost:13337/?folder=/home/coder/app"
  144. icon = "/icon/code.svg"
  145. subdomain = true
  146. share = "owner"
  147.  
  148. healthcheck {
  149. url = "http://localhost:13337/healthz"
  150. interval = 5
  151. threshold = 6
  152. }
  153. }
  154.  
  155. resource "coder_app" "app-in-code-subdir" {
  156. agent_id = coder_agent.main.id
  157. slug = "app-in-code-subdir"
  158. display_name = "app-in-code-subdir"
  159. ## url = "http://localhost:13337/?folder=/home/${local.username}"
  160. url = "http://localhost:13337/?folder=/home/coder/app"
  161. icon = "/icon/code.svg"
  162. subdomain = false
  163. share = "owner"
  164.  
  165. healthcheck {
  166. url = "http://localhost:13337/healthz"
  167. interval = 5
  168. threshold = 6
  169. }
  170. }
  171.  
  172. resource "docker_volume" "home_volume" {
  173. name = "coder-${data.coder_workspace.me.id}-home"
  174. # Protect the volume from being deleted due to changes in attributes.
  175. lifecycle {
  176. ignore_changes = all
  177. }
  178. # Add labels in Docker to keep track of orphan resources.
  179. labels {
  180. label = "coder.owner"
  181. value = data.coder_workspace_owner.me.name
  182. }
  183. labels {
  184. label = "coder.owner_id"
  185. value = data.coder_workspace_owner.me.id
  186. }
  187. labels {
  188. label = "coder.workspace_id"
  189. value = data.coder_workspace.me.id
  190. }
  191. # This field becomes outdated if the workspace is renamed but can
  192. # be useful for debugging or cleaning out dangling volumes.
  193. labels {
  194. label = "coder.workspace_name_at_creation"
  195. value = data.coder_workspace.me.name
  196. }
  197. }
  198.  
  199. # resource "docker_image" "main" {
  200. # name = "coder-${data.coder_workspace.me.id}"
  201. # build {
  202. # context = "./build"
  203. # build_args = {
  204. # USER = local.username
  205. # }
  206. # }
  207. # triggers = {
  208. # dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
  209. # }
  210. # }
  211.  
  212. resource "docker_container" "workspace" {
  213. image = "codercom/enterprise-base:ubuntu"
  214. command = ["sh", "-c", coder_agent.main.init_script]
  215. runtime = "sysbox-runc"
  216. # user = local.username
  217. count = data.coder_workspace.me.start_count
  218. ## image = docker_image.main.name
  219.  
  220. # Uses lower() to avoid Docker restriction on container names.
  221. name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
  222. # Hostname makes the shell more user friendly: coder@my-workspace:~$
  223. hostname = data.coder_workspace.me.name
  224. # Use the docker gateway if the access URL is 127.0.0.1
  225. entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
  226. env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
  227. host {
  228. host = "host.docker.internal"
  229. ip = "host-gateway"
  230. }
  231. volumes {
  232. # container_path = "/home/${local.username}"
  233. container_path = "/home/coder"
  234. volume_name = docker_volume.home_volume.name
  235. read_only = false
  236. }
  237.  
  238. # Add labels in Docker to keep track of orphan resources.
  239. labels {
  240. label = "coder.owner"
  241. value = data.coder_workspace_owner.me.name
  242. }
  243. labels {
  244. label = "coder.owner_id"
  245. value = data.coder_workspace_owner.me.id
  246. }
  247. labels {
  248. label = "coder.workspace_id"
  249. value = data.coder_workspace.me.id
  250. }
  251. labels {
  252. label = "coder.workspace_name"
  253. value = data.coder_workspace.me.name
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment