Skip to content

Commit 8d73582

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

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-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

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

0 commit comments

Comments
 (0)