From 4a43715712e0bbaaae5d61b786458fed298c5bd1 Mon Sep 17 00:00:00 2001 From: Nicholas Pease Date: Sun, 4 Jun 2023 01:11:24 +0000 Subject: [PATCH] Added Python3 --- ubuntu-python3/README.md | 35 +++++++ ubuntu-python3/build/Dockerfile | 20 ++++ ubuntu-python3/main.tf | 158 ++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 ubuntu-python3/README.md create mode 100644 ubuntu-python3/build/Dockerfile create mode 100644 ubuntu-python3/main.tf diff --git a/ubuntu-python3/README.md b/ubuntu-python3/README.md new file mode 100644 index 0000000..eabdb92 --- /dev/null +++ b/ubuntu-python3/README.md @@ -0,0 +1,35 @@ +--- +name: Develop in Docker +description: Run workspaces on a Docker host using registry images +tags: [local, docker] +icon: /icon/docker.png +--- + +# docker + +To get started, run `coder templates init`. When prompted, select this template. +Follow the on-screen instructions to proceed. + +## Editing the image + +Edit the `Dockerfile` and run `coder templates push` to update workspaces. + +## code-server + +`code-server` is installed via the `startup_script` argument in the `coder_agent` +resource block. The `coder_app` resource is defined to access `code-server` through +the dashboard UI over `localhost:13337`. + +## Extending this template + +See the [kreuzwerker/docker](https://registry.terraform.io/providers/kreuzwerker/docker) Terraform provider documentation to +add the following features to your Coder template: + +- SSH/TCP docker host +- Registry authentication +- Build args +- Volume mounts +- Custom container spec +- More + +We also welcome contributions! diff --git a/ubuntu-python3/build/Dockerfile b/ubuntu-python3/build/Dockerfile new file mode 100644 index 0000000..8843aba --- /dev/null +++ b/ubuntu-python3/build/Dockerfile @@ -0,0 +1,20 @@ +FROM ubuntu + +RUN apt-get update \ + && apt-get install -y \ + curl \ + git \ + sudo \ + vim \ + nano \ + wget \ + python3 \ + python3-pip \ + && rm -rf /var/lib/apt/lists/* + +ARG USER=coder +RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \ + && echo "${USER} ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/${USER} \ + && chmod 0440 /etc/sudoers.d/${USER} +USER ${USER} +WORKDIR /home/${USER} diff --git a/ubuntu-python3/main.tf b/ubuntu-python3/main.tf new file mode 100644 index 0000000..e9237a7 --- /dev/null +++ b/ubuntu-python3/main.tf @@ -0,0 +1,158 @@ +terraform { + required_providers { + coder = { + source = "coder/coder" + version = "~> 0.8.2" + } + docker = { + source = "kreuzwerker/docker" + version = "~> 3.0.1" + } + } +} + +locals { + username = data.coder_workspace.me.owner +} + +provider "coder" { + feature_use_managed_variables = "true" +} + +data "coder_provisioner" "me" { +} + +provider "docker" { +} + +data "coder_workspace" "me" { +} + +variable "waka_api_url" { + description = "URL For WakaAPI" + type = string + sensitive = true +} + +variable "waka_api_key" { + description = "API Key for WakaAPI" + type = string + sensitive = true +} + +data "coder_parameter" "git_repo" { + name = "git_repo" + display_name= "Git Repo" + description = "Git Repo to Clone" + default = " " +} + +resource "coder_agent" "main" { + arch = data.coder_provisioner.me.arch + os = "linux" + startup_script_timeout = 180 + startup_script = <<-EOT + set -e + # Base Script + curl -fsSL https://gitea.nicholaspease.com/npease/CoderTemplatesV2/raw/branch/main/container-scripts/base.sh | sh + # WakaAPI Setup + echo "[settings]" >> ~/.wakatime.cfg && echo "api_url = ${var.waka_api_url}" >> ~/.wakatime.cfg && echo "api_key = ${var.waka_api_key}" >> ~/.wakatime.cfg + # Clone Git Repo + cd ~/ + echo OUTPUT: '${data.coder_parameter.git_repo.value}' + if [ "${data.coder_parameter.git_repo.value}" != " " ] + then + git clone ${data.coder_parameter.git_repo.value} + fi + + EOT + + # These environment variables allow you to make Git commits right away after creating a + # workspace. Note that they take precedence over configuration defined in ~/.gitconfig! + # You can remove this block if you'd prefer to configure Git manually or using + # dotfiles. (see docs/dotfiles.md) + env = { + GIT_AUTHOR_NAME = "Nicholas Pease" + GIT_COMMITTER_NAME = "Nicholas Pease" + GIT_AUTHOR_EMAIL = "me@nicholaspease.com" + GIT_COMMITTER_EMAIL = "me@nicholaspease.com" + } +} + +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_image" "main" { + name = "coder-${data.coder_workspace.me.id}" + build { + context = "./build" + build_args = { + USER = local.username + } + } + triggers = { + dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)])) + } +} + +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 = "coder-${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 + # 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/${local.username}" + volume_name = docker_volume.home_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 + } +}