Skip to content

Commit 1888f81

Browse files
committed
feat: Add examples/do-droplet-linux for Digital Ocean Droplets
1 parent c5f4d80 commit 1888f81

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed

examples/do-droplet-linux/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
name: Develop in Linux on a Digital Ocean Droplet
3+
description: Get started with Linux development on a Digital Ocean Droplet.
4+
tags: [cloud, digitalocean]
5+
---
6+
7+
# do-droplet-linux
8+
9+
This is an example for deploying workspaces on Digital Ocean Droplets.
10+
11+
## Requirements
12+
13+
- Digital Ocean Personal Access Token (PAT)
14+
- Digital Ocean Project ID (e.g. `doctl projects list`)
15+
- Remove `variable "step2_do_project_id"` and `resource "digitalocean_project_resources" "project"` if you don't want project association.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#cloud-config
2+
users:
3+
- name: coder
4+
sudo: ["ALL=(ALL) NOPASSWD:ALL"]
5+
groups: sudo
6+
shell: /bin/bash
7+
packages:
8+
- git
9+
mounts:
10+
- [
11+
"LABEL=${home_volume_label}",
12+
/home/coder,
13+
auto,
14+
"defaults,uid=1000,gid=1000",
15+
]
16+
write_files:
17+
- path: /opt/coder/init
18+
permissions: "0755"
19+
encoding: b64
20+
content: ${init_script}
21+
- path: /etc/systemd/system/coder-agent.service
22+
permissions: "0644"
23+
content: |
24+
[Unit]
25+
Description=Coder Agent
26+
After=network-online.target
27+
Wants=network-online.target
28+
29+
[Service]
30+
User=coder
31+
ExecStart=/opt/coder/init
32+
Environment=CODER_AGENT_TOKEN=${coder_agent_token}
33+
Restart=always
34+
RestartSec=10
35+
TimeoutStopSec=90
36+
KillMode=process
37+
38+
OOMScoreAdjust=-900
39+
SyslogIdentifier=coder-agent
40+
41+
[Install]
42+
WantedBy=multi-user.target
43+
runcmd:
44+
- chown coder:coder /home/coder
45+
- systemctl enable coder-agent
46+
- systemctl start coder-agent

examples/do-droplet-linux/main.tf

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = "0.4.1"
6+
}
7+
digitalocean = {
8+
source = "digitalocean/digitalocean"
9+
version = "~> 2.0"
10+
}
11+
}
12+
}
13+
14+
variable "step1_do_token" {
15+
type = string
16+
description = "Enter token (refer to docs at ...)"
17+
sensitive = true
18+
19+
validation {
20+
condition = length(var.step1_do_token) == 71 && substr(var.step1_do_token, 0, 4) == "dop_"
21+
error_message = "Invalid Digital Ocean Personal Access Token."
22+
}
23+
}
24+
25+
// 8f02033b-cbcd-45f8-9091-11a1ab37b792
26+
variable "step2_do_project_id" {
27+
type = string
28+
description = "Enter project ID (see e.g. doctl projects list)"
29+
sensitive = true
30+
31+
validation {
32+
condition = length(var.step2_do_project_id) == 36
33+
error_message = "Invalid Digital Ocean Project ID."
34+
}
35+
}
36+
37+
variable "droplet_image" {
38+
description = "Which Droplet image would you like to use for your workspace?"
39+
default = "ubuntu-22-04-x64"
40+
validation {
41+
condition = contains(["debian-11-x64", "fedora-36-x64", "ubuntu-22-04-x64"], var.droplet_image)
42+
error_message = "Value must be debian-11-x64, fedora-36-x64 or ubuntu-22-04-x64."
43+
}
44+
}
45+
46+
variable "droplet_size" {
47+
description = "Which Droplet configuration would you like to use?"
48+
validation {
49+
condition = contains(["s-1vcpu-1gb", "s-1vcpu-2gb", "s-2vcpu-2gb"], var.droplet_size)
50+
error_message = "Value must be s-1vcpu-1gb, s-1vcpu-2gb or s-2vcpu-2gb."
51+
}
52+
}
53+
54+
variable "region" {
55+
description = "Which region would you like to use?"
56+
validation {
57+
condition = contains(["nyc1", "nyc3", "ams3"], var.region)
58+
error_message = "Value must be nyc1, nyc3, or ams3."
59+
}
60+
}
61+
62+
# Configure the DigitalOcean Provider
63+
provider "digitalocean" {
64+
token = var.step1_do_token
65+
}
66+
67+
data "coder_workspace" "me" {}
68+
69+
resource "coder_agent" "dev" {
70+
os = "linux"
71+
arch = "amd64"
72+
}
73+
74+
resource "digitalocean_volume" "home_volume" {
75+
region = var.region
76+
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}-home"
77+
size = 20
78+
initial_filesystem_type = "ext4"
79+
initial_filesystem_label = "coder-home"
80+
}
81+
82+
resource "digitalocean_droplet" "workspace" {
83+
region = var.region
84+
count = data.coder_workspace.me.start_count
85+
name = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
86+
image = var.droplet_image
87+
size = var.droplet_size
88+
volume_ids = [digitalocean_volume.home_volume.id]
89+
user_data = templatefile("cloud-config.yaml.tftpl", {
90+
home_volume_label = digitalocean_volume.home_volume.initial_filesystem_label
91+
init_script = base64encode(coder_agent.dev.init_script)
92+
coder_agent_token = coder_agent.dev.token
93+
})
94+
ssh_keys = [34557206]
95+
}
96+
97+
# resource "digitalocean_project_resources" "project" {
98+
# project = var.step2_do_project_id
99+
# # Workaround for terraform plan when using count.
100+
# resources = length(digitalocean_droplet.workspace) > 0 ? [
101+
# digitalocean_volume.home_volume.urn,
102+
# digitalocean_droplet.workspace[0].urn
103+
# ] : [
104+
# digitalocean_volume.home_volume.urn
105+
# ]
106+
# }

0 commit comments

Comments
 (0)