docs: improve structure for example templates (#9842)

Co-authored-by: Kyle Carberry <kyle@carberry.com>
Co-authored-by: Muhammad Atif Ali <atif@coder.com>
Co-authored-by: Muhammad Atif Ali <me@matifali.dev>
This commit is contained in:
Ben Potter
2023-12-17 17:05:13 +03:00
committed by GitHub
co-authored by Kyle Carberry Muhammad Atif Ali Muhammad Atif Ali
parent 3f6096b0d7
commit 2e86b76fb8
38 changed files with 473 additions and 1343 deletions
+26
View File
@@ -0,0 +1,26 @@
---
name: JFrog and Docker
description: Develop inside Docker containers using your local daemon
tags: [local, docker, jfrog]
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`.
# Next steps
Check out our [Docker](../docker/) template for a more fully featured Docker
example.
+28
View File
@@ -0,0 +1,28 @@
FROM ubuntu
RUN apt-get update \
&& apt-get install -y \
curl \
git \
python3-pip \
sudo \
vim \
wget \
npm \
&& rm -rf /var/lib/apt/lists/*
ARG GO_VERSION=1.20.8
RUN mkdir --parents /usr/local/go && curl --silent --show-error --location \
"https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz" -o /usr/local/go.tar.gz && \
tar --extract --gzip --directory=/usr/local/go --file=/usr/local/go.tar.gz --strip-components=1
ENV PATH=$PATH:/usr/local/go/bin
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}
RUN curl -fL https://install-cli.jfrog.io | sh
RUN chmod 755 $(which jf)
USER ${USER}
WORKDIR /home/${USER}
+166
View File
@@ -0,0 +1,166 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
}
docker = {
source = "kreuzwerker/docker"
}
artifactory = {
source = "registry.terraform.io/jfrog/artifactory"
}
}
}
locals {
# take care to use owner_email instead of owner because users can change
# their username.
artifactory_username = data.coder_workspace.me.owner_email
artifactory_repository_keys = {
"npm" = "npm"
"python" = "python"
"go" = "go"
}
workspace_user = data.coder_workspace.me.owner
}
data "coder_provisioner" "me" {
}
provider "docker" {
}
data "coder_workspace" "me" {
}
variable "jfrog_host" {
type = string
description = "JFrog instance hostname. For example, 'YYY.jfrog.io'."
}
variable "artifactory_access_token" {
type = string
description = "The admin-level access token to use for JFrog."
}
# Configure the Artifactory provider
provider "artifactory" {
url = "https://${var.jfrog_host}/artifactory"
access_token = var.artifactory_access_token
}
resource "artifactory_scoped_token" "me" {
# This is hacky, but on terraform plan the data source gives empty strings,
# which fails validation.
username = length(local.artifactory_username) > 0 ? local.artifactory_username : "plan"
}
resource "coder_agent" "main" {
arch = data.coder_provisioner.me.arch
os = "linux"
startup_script_timeout = 180
startup_script = <<-EOT
set -e
# install and start code-server
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
# Install the JFrog VS Code extension.
# Find the latest version number at
# https://open-vsx.org/extension/JFrog/jfrog-vscode-extension.
JFROG_EXT_VERSION=2.4.1
curl -o /tmp/jfrog.vsix -L "https://open-vsx.org/api/JFrog/jfrog-vscode-extension/$JFROG_EXT_VERSION/file/JFrog.jfrog-vscode-extension-$JFROG_EXT_VERSION.vsix"
/tmp/code-server/bin/code-server --install-extension /tmp/jfrog.vsix
# The jf CLI checks $CI when determining whether to use interactive
# flows.
export CI=true
jf c rm 0 || true
echo ${artifactory_scoped_token.me.access_token} | \
jf c add --access-token-stdin --url https://${var.jfrog_host} 0
# Configure the `npm` CLI to use the Artifactory "npm" repository.
cat << EOF > ~/.npmrc
email = ${data.coder_workspace.me.owner_email}
registry = https://${var.jfrog_host}/artifactory/api/npm/${local.artifactory_repository_keys["npm"]}
EOF
jf rt curl /api/npm/auth >> .npmrc
# Configure the `pip` to use the Artifactory "python" repository.
mkdir -p ~/.pip
cat << EOF > ~/.pip/pip.conf
[global]
index-url = https://${local.artifactory_username}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/pypi/${local.artifactory_repository_keys["python"]}/simple
EOF
EOT
# Set GOPROXY to use the Artifactory "go" repository.
env = {
GOPROXY : "https://${local.artifactory_username}:${artifactory_scoped_token.me.access_token}@${var.jfrog_host}/artifactory/api/go/${local.artifactory_repository_keys["go"]}"
# Authenticate with JFrog extension.
JFROG_IDE_URL : "https://${var.jfrog_host}"
JFROG_IDE_USERNAME : "${local.artifactory_username}"
JFROG_IDE_PASSWORD : "${artifactory_scoped_token.me.access_token}"
JFROG_IDE_ACCESS_TOKEN : "${artifactory_scoped_token.me.access_token}"
JFROG_IDE_STORE_CONNECTION : "true"
}
}
resource "coder_app" "code-server" {
agent_id = coder_agent.main.id
slug = "code-server"
display_name = "code-server"
url = "http://localhost:13337/?folder=/home/${local.workspace_user}"
icon = "/icon/code.svg"
subdomain = false
share = "owner"
healthcheck {
url = "http://localhost:13337/healthz"
interval = 5
threshold = 6
}
}
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
}
}
resource "docker_image" "main" {
name = "coder-${data.coder_workspace.me.id}"
build {
context = "${path.module}/build"
build_args = {
USER = local.workspace_user
}
}
triggers = {
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1("${path.module}/${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
entrypoint = ["sh", "-c", coder_agent.main.init_script]
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
host {
host = "host.docker.internal"
ip = "host-gateway"
}
volumes {
container_path = "/home/${local.workspace_user}"
volume_name = docker_volume.home_volume.name
read_only = false
}
}
+16
View File
@@ -0,0 +1,16 @@
module "docker" {
source = "cdr.jfrog.io/tf__main/docker/docker"
jfrog_host = var.jfrog_host
artifactory_access_token = var.artifactory_access_token
}
variable "jfrog_host" {
type = string
description = "JFrog instance hostname. For example, 'YYY.jfrog.io'."
}
variable "artifactory_access_token" {
type = string
description = "The admin-level access token to use for JFrog."
}