-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdo_owncast.tf
134 lines (119 loc) · 3.24 KB
/
do_owncast.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Set provider requirements
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~>2.6.0"
}
}
}
# Set do_token
variable "do_token" {
type = string
}
# Set owncast server URL
variable "owncast_server_url" {
type = string
}
# Configure the DigitalOcean Provider
provider "digitalocean" {
token = var.do_token
}
# Create stream key
resource "random_string" "stream_key" {
length = 64
special = false
number = true
lower = true
upper = true
}
# Build config file from variables/inputs/config files
# I wanted to try and make it simpler to follow/read not sure if I did it.
locals {
ssh_key_local = file(pathexpand("~/.ssh/id_rsa.pub"))
caddyfile_local = templatefile(
"${path.module}/Caddyfile",
{
server_url = var.owncast_server_url
}
)
owncast_config_local = templatefile(
"${path.module}/owncast-config.yaml",
{
stream_key = random_string.stream_key.result
}
)
docker_compose_local = file("${path.module}/docker-compose.yaml")
user_data_local = templatefile(
"${path.module}/cloud-config.yaml",
{
ssh_key = local.ssh_key_local,
caddyfile = base64encode(local.caddyfile_local),
owncast_config = base64encode(local.owncast_config_local)
docker_compose = base64encode(local.docker_compose_local),
server_url = var.owncast_server_url,
stream_key = random_string.stream_key.result,
}
)
}
# Tag for Droplet so the firewall rules are applied
resource "digitalocean_tag" "owncast_tag" {
name = "Owncast"
}
# Image to use
data "digitalocean_images" "docker" {
filter {
key = "distribution"
values = ["Ubuntu"]
}
filter {
key = "name"
values = ["Docker"]
match_by = "substring"
}
filter {
key = "regions"
values = ["sfo1"]
}
sort {
key = "created"
direction = "desc"
}
}
# Create droplet with userdata stored in cloud-config.yaml file
resource "digitalocean_droplet" "owncast" {
name = "owncast-droplet"
size = "c-4"
image = element(tolist(data.digitalocean_images.docker.images), 0).id
region = "sfo3"
ipv6 = false
private_networking = true
user_data = local.user_data_local
tags = [digitalocean_tag.owncast_tag.id]
}
# Assign floating IP to new droplet
resource "digitalocean_floating_ip_assignment" "pub_ip" {
ip_address = "164.90.247.66"
droplet_id = digitalocean_droplet.owncast.id
}
# # output locals for testing
# output "ssh_key_local_test" {
# value = local.ssh_key_local
# }
# output "caddyfile_local_test" {
# value = local.caddyfile_local
# }
# output "owncast_config_local_test" {
# value = local.owncast_config_local
# }
# output "docker_compose_local_test" {
# value = local.docker_compose_local
# }
# # output user_data template
# output "user_data_template" {
# value = local.user_data_local
# }
# output stream_key to user to send on to whoever asked for the server to be set up
output "stream_key" {
value = random_string.stream_key.result
}