Skip to content

Commit

Permalink
Add packer and terraform for running a dev server in Hetzner
Browse files Browse the repository at this point in the history
  • Loading branch information
alexklibisz committed Feb 3, 2024
1 parent 983eb3c commit 47b95c5
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ gradle-app.setting

# Misc log files
*.log

# Terraform
.terraform
.terraform.lock*
terraform.tfstate
terraform.tfstate.backup

File renamed without changes.
62 changes: 62 additions & 0 deletions development/hetzner/hetzner.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
variable "hcloud_token" {
sensitive = true
}

variable "ssh_public_key_path" {
type = string
default = "~/.ssh/id_ed25519.pub"
}

variable "server_image_name_label" {
type = string
default = "elastiknn-development-ubuntu-22.04"
}

variable "server_location" {
type = string
default = "fsn1"
}

variable "server_type" {
type = string
default = "ccx23"
}

terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
}
}
required_version = ">= 0.13"
}

provider "hcloud" {
token = var.hcloud_token
}

resource "hcloud_ssh_key" "elastiknn_development" {
name = "elastiknn-development"
public_key = file(var.ssh_public_key_path)
}

data "hcloud_image" "elastiknn_development" {
with_selector = "name=${var.server_image_name_label}"
}

# Create a new server running debian
resource "hcloud_server" "elastiknn_development" {
name = "elastiknn-development"
image = data.hcloud_image.elastiknn_development.id
server_type = var.server_type
location = var.server_location
ssh_keys = [hcloud_ssh_key.elastiknn_development.id]
public_net {
ipv4_enabled = true
ipv6_enabled = true
}
}

output "ssh_command" {
value = "ssh root@${hcloud_server.elastiknn_development.ipv4_address}"
}
31 changes: 31 additions & 0 deletions development/hetzner/image.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
packer {
required_plugins {
hcloud = {
version = ">= 1.1.1"
source = "github.com/hetznercloud/hcloud"
}
}
}

source "hcloud" "base-amd64" {
image = "ubuntu-22.04"
location = "fsn1"
server_type = "ccx23"
ssh_keys = []
user_data = ""
ssh_username = "root"
snapshot_name = "elastiknn-development-ubuntu-22.04"
snapshot_labels = {
name = "elastiknn-development-ubuntu-22.04"
}
}

build {
sources = ["source.hcloud.base-amd64"]
provisioner "shell" {
scripts = [ "setup-ubuntu-22.04.sh" ]
env = {
BUILDER = "packer"
}
}
}
52 changes: 52 additions & 0 deletions development/hetzner/setup-ubuntu-22.04.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash
set -e

# Install SBT.
echo "deb https://repo.scala-sbt.org/scalasbt/debian all main" | sudo tee /etc/apt/sources.list.d/sbt.list
echo "deb https://repo.scala-sbt.org/scalasbt/debian /" | sudo tee /etc/apt/sources.list.d/sbt_old.list
curl -sL "https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | sudo apt-key add
sudo apt-get update
sudo apt-get install sbt

# Install Docker.
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Install task.
sudo apt-get install -y snapd
sudo snap install --classic task

# Clone and add asdf to the path.
git -c advice.detachedHead=false clone --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf --branch v0.14.0
echo "source $HOME/.asdf/asdf.sh" >> $HOME/.bashrc
source $HOME/.asdf/asdf.sh
asdf --version

# Setup asdf plugins.
asdf plugin add java
asdf plugin add python

# Install a bunch of system-level libraries which are required for asdf to be able to install python.
# It's quite annoying that this is required; ideally asdf install python 3.x.y would just work.
# But I don't know of a way to avoid it, and it seems to come up with all the python version managers.
sudo apt-get update
sudo apt-get install -y gcc make zlib1g-dev libssl-dev lzma liblzma-dev libbz2-dev libsqlite3-dev libreadline-dev libffi-dev libncurses5-dev libncursesw5-dev

# Clone Elastiknn.
git clone https://github.com/alexklibisz/elastiknn.git

# Install the asdf packages specified by elastiknn/.tool-versions
cd elastiknn && asdf install

# Run the benchmark tests to ensure everything installed correctly.
task dockerRunBenchmarkingCluster
task annbTest

0 comments on commit 47b95c5

Please sign in to comment.