Guest User

Untitled

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