Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 5dea748

Browse files
committed
add demo
Signed-off-by: Johan Siebens <[email protected]>
1 parent 3e2a213 commit 5dea748

File tree

20 files changed

+742
-0
lines changed

20 files changed

+742
-0
lines changed

demo/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.terraform
2+
terraform.tfvars
3+
terraform.tfstate
4+
terraform.tfstate.backup

demo/.terraform.lock.hcl

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/image/main.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
terraform {
2+
required_providers {
3+
digitalocean = {
4+
source = "digitalocean/digitalocean"
5+
version = "2.8.0"
6+
}
7+
}
8+
}
9+
10+
locals {
11+
name = random_pet.name.id
12+
}
13+
14+
resource "random_pet" "name" {}
15+
16+
resource "null_resource" "packer_build" {
17+
provisioner "local-exec" {
18+
command = <<EOF
19+
cd ${path.module}/packer && \
20+
packer build -force \
21+
-var 'name=hashi-${local.name}' \
22+
-var 'region=${var.region}' \
23+
-var 'token=${var.do_token}' \
24+
do-packer.pkr.hcl
25+
EOF
26+
}
27+
}
28+
29+
data "digitalocean_image" "hashi" {
30+
depends_on = [null_resource.packer_build]
31+
name = "hashi-${local.name}"
32+
}
33+
34+
output "snapshot_id" {
35+
value = data.digitalocean_image.hashi.id
36+
}

demo/image/packer/do-packer.pkr.hcl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
variable "name" {
2+
}
3+
4+
variable "source_image" {
5+
default = "ubuntu-20-04-x64"
6+
}
7+
8+
variable "ssh_username" {
9+
default = "root"
10+
}
11+
12+
variable "region" {
13+
}
14+
15+
variable "token" {
16+
}
17+
18+
source "digitalocean" "hashistack" {
19+
snapshot_name = "${var.name}"
20+
image = "${var.source_image}"
21+
ssh_username = "${var.ssh_username}"
22+
region = "${var.region}"
23+
size = "512mb"
24+
api_token = "${var.token}"
25+
}
26+
27+
build {
28+
sources = [
29+
"source.digitalocean.hashistack"
30+
]
31+
32+
provisioner "shell" {
33+
inline = [
34+
"sudo mkdir -p /ops",
35+
"sudo chmod 777 /ops"
36+
]
37+
}
38+
39+
provisioner "file" {
40+
source = "./scripts"
41+
destination = "/ops"
42+
}
43+
44+
provisioner "shell" {
45+
script = "./scripts/setup.sh"
46+
}
47+
48+
}

demo/image/packer/scripts/client.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
NODE_CLASS=$1
4+
TAG_NAME=$2
5+
API_TOKEN=$3
6+
REGION=$(curl -s http://169.254.169.254/metadata/v1/region)
7+
8+
tee /etc/nomad.d/config/telemetry.hcl >/dev/null <<EOF
9+
telemetry {
10+
collection_interval = "1s"
11+
disable_hostname = true
12+
prometheus_metrics = true
13+
publish_allocation_metrics = true
14+
publish_node_metrics = true
15+
}
16+
EOF
17+
18+
hashi-up consul install \
19+
--version 1.9.5 \
20+
--local \
21+
--client-addr 0.0.0.0 \
22+
--advertise-addr "{{ GetInterfaceIP \"eth1\" }}" \
23+
--connect \
24+
--retry-join "provider=digitalocean region=${REGION} tag_name=${TAG_NAME} api_token=${API_TOKEN}"
25+
26+
hashi-up nomad install \
27+
--version 1.1.0 \
28+
--local \
29+
--client \
30+
--node-class "${NODE_CLASS}" \
31+
--advertise "{{ GetInterfaceIP \"eth1\" }}"
32+
33+
systemctl start consul
34+
systemctl start nomad

demo/image/packer/scripts/server.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
NR_OF_SERVERS=$1
4+
TAG_NAME=$2
5+
API_TOKEN=$3
6+
REGION=$(curl -s http://169.254.169.254/metadata/v1/region)
7+
8+
tee /etc/nomad.d/config/telemetry.hcl >/dev/null <<EOF
9+
telemetry {
10+
collection_interval = "1s"
11+
disable_hostname = true
12+
prometheus_metrics = true
13+
publish_allocation_metrics = true
14+
publish_node_metrics = true
15+
}
16+
EOF
17+
18+
hashi-up consul install \
19+
--version 1.9.5 \
20+
--local \
21+
--server \
22+
--bootstrap-expect ${NR_OF_SERVERS} \
23+
--client-addr 0.0.0.0 \
24+
--advertise-addr "{{ GetInterfaceIP \"eth1\" }}" \
25+
--connect \
26+
--retry-join "provider=digitalocean region=${REGION} tag_name=${TAG_NAME} api_token=${API_TOKEN}"
27+
28+
hashi-up nomad install \
29+
--version 1.1.0 \
30+
--local \
31+
--server \
32+
--bootstrap-expect ${NR_OF_SERVERS} \
33+
--advertise "{{ GetInterfaceIP \"eth1\" }}"
34+
35+
systemctl start consul
36+
systemctl start nomad

demo/image/packer/scripts/setup.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#! /bin/bash
2+
set -euo pipefail
3+
4+
apt-get update
5+
apt-get install -y dnsmasq curl unzip docker.io
6+
7+
mkdir -p /opt/cni/bin
8+
curl -sSL https://github.com/containernetworking/plugins/releases/download/v0.9.1/cni-plugins-linux-amd64-v0.9.1.tgz | tar -xvz -C /opt/cni/bin
9+
10+
curl -sL get.hashi-up.dev | sh
11+
12+
hashi-up consul install \
13+
--version 1.9.5 \
14+
--local \
15+
--skip-enable
16+
17+
hashi-up nomad install \
18+
--version 1.1.0 \
19+
--local \
20+
--skip-enable
21+
22+
echo "server=/consul/127.0.0.1#8600" > /etc/dnsmasq.d/10-consul
23+
echo "server=8.8.8.8" > /etc/dnsmasq.d/99-default
24+
25+
systemctl disable systemd-resolved.service
26+
systemctl stop systemd-resolved
27+
rm /etc/resolv.conf

demo/image/variable.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "do_token" {}
2+
3+
variable "region" {
4+
type = string
5+
}

demo/infrastructure/main.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
terraform {
2+
required_providers {
3+
digitalocean = {
4+
source = "digitalocean/digitalocean"
5+
version = "2.8.0"
6+
}
7+
}
8+
}
9+
10+
locals {
11+
nr_of_servers = 1
12+
server_tag = "hashi-server"
13+
}
14+
15+
resource "digitalocean_vpc" "hashi" {
16+
name = "hashistack"
17+
region = var.region
18+
ip_range = var.ip_range
19+
}
20+
21+
resource "digitalocean_droplet" "hashi-server" {
22+
count = local.nr_of_servers
23+
image = var.snapshot_id
24+
name = "hashi-server-0${count.index + 1}"
25+
region = var.region
26+
size = "s-1vcpu-1gb"
27+
tags = [local.server_tag]
28+
user_data = templatefile("${path.module}/templates/server.sh", { server_tag = local.server_tag, do_token = var.do_token, nr_of_servers = local.nr_of_servers })
29+
vpc_uuid = digitalocean_vpc.hashi.id
30+
ssh_keys = [var.ssh_key]
31+
}
32+
33+
resource "digitalocean_droplet" "platform" {
34+
count = 1
35+
image = var.snapshot_id
36+
name = "hashi-platform-0${count.index + 1}"
37+
region = var.region
38+
size = "s-1vcpu-1gb"
39+
user_data = templatefile("${path.module}/templates/client.sh", { node_class = "platform", server_tag = local.server_tag, do_token = var.do_token })
40+
vpc_uuid = digitalocean_vpc.hashi.id
41+
ssh_keys = [var.ssh_key]
42+
}
43+

demo/infrastructure/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "vpc_uuid" {
2+
value = digitalocean_vpc.hashi.id
3+
}
4+
5+
output "nomad_addr" {
6+
value = "http://${digitalocean_droplet.hashi-server[0].ipv4_address}:4646"
7+
}
8+
9+
output "consul_addr" {
10+
value = "http://${digitalocean_droplet.hashi-server[0].ipv4_address}:8500"
11+
}

0 commit comments

Comments
 (0)