Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Terraform setup #341

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions infrastructure/terraform/network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
resource "hcloud_network" "hc_private" {
name = "hc_private"
ip_range = var.ip_range
}

resource "hcloud_server_network" "web_network" {
count = var.instances
server_id = hcloud_server.web[count.index].id
subnet_id = hcloud_network_subnet.hc_private_subnet.id
}

resource "hcloud_network_subnet" "hc_private_subnet" {
network_id = hcloud_network.hc_private.id
type = "cloud"
network_zone = "eu-central"
ip_range = var.ip_range
}
13 changes: 13 additions & 0 deletions infrastructure/terraform/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
output "web_servers_status" {
value = {
for server in hcloud_server.web :
server.name => server.status
}
}

output "web_servers_ips" {
value = {
for server in hcloud_server.web :
server.name => server.ipv4_address
}
}
13 changes: 13 additions & 0 deletions infrastructure/terraform/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# hetzner cloud provider
provider "hcloud" {
token = "${var.hcloud_token}"
}

terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
}
}
required_version = ">= 0.13"
}
4 changes: 4 additions & 0 deletions infrastructure/terraform/ssh.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "hcloud_ssh_key" "default" {
name = "hetzner_key"
public_key = file("~/.ssh/tf_hetzner.pub")
}
44 changes: 44 additions & 0 deletions infrastructure/terraform/user_data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#cloud-config
users:
- name: root
groups: users, admin
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa
package_update: true
package_upgrade: true
packages:
- ca-certificates
- curl
- gnupg
- vim
- git
- zip
- unzip
- openssl
- libssl-dev
- build-essential
- libclang-dev
runcmd:
- apt-get install pkg-config cmake clang lldb lld -y
- wget -P /root/ https://nodejs.org/dist/v18.18.0/node-v18.18.0-linux-x64.tar.xz
- tar -xf /root/node-v18.18.0-linux-x64.tar.xz --directory=/usr/local/ --strip-components=1
- rm /root/node-v18.18.0-linux-x64.tar.xz
- npm install --global yarn
- curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
- echo ". /root/.cargo/env" >> /root/.bashrc
- install -m 0755 -d /etc/apt/keyrings
- curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- chmod a+r /etc/apt/keyrings/docker.gpg
- echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
- apt-get update -y
- apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
- apt-get install -y docker-compose
- apt-get install axel
- /root/.cargo/bin/cargo install cargo-nextest
- apt-get install postgresql -y
- /root/.cargo/bin/cargo install sqlx-cli --version 0.5.13
- service postgresql stop
- git clone https://github.com/matter-labs/zksync-era.git /root/zksync-era
- mv /root/.cargo/bin/cargo /usr/bin && export ZKSYNC_HOME=/root/zksync-era && export PATH=$ZKSYNC_HOME/bin:$PATH && cd /root/zksync-era && zk && zk init && zk server
35 changes: 35 additions & 0 deletions infrastructure/terraform/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
variable "hcloud_token" {

}

variable "location" {
default = "nbg1"
}

variable "http_protocol" {
default = "http"
}

variable "http_port" {
default = "80"
}

variable "instances" {
default = "1"
}

variable "server_type" {
default = "cpx31"
}

variable "os_type" {
default = "debian-12"
}

variable "disk_size" {
default = "60"
}

variable "ip_range" {
default = "10.0.1.0/24"
}
14 changes: 14 additions & 0 deletions infrastructure/terraform/volumes.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resource "hcloud_volume" "web_server_volume" {
count = var.instances
name = "zk-server-volume-${count.index}"
size = var.disk_size
location = var.location
format = "xfs"
}

resource "hcloud_volume_attachment" "web_vol_attachment" {
count = var.instances
volume_id = hcloud_volume.web_server_volume[count.index].id
server_id = hcloud_server.web[count.index].id
automount = true
}
12 changes: 12 additions & 0 deletions infrastructure/terraform/web_servers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "hcloud_server" "web" {
count = var.instances
name = "zksync-${count.index}"
image = var.os_type
server_type = var.server_type
location = var.location
ssh_keys = [hcloud_ssh_key.default.id]
labels = {
type = "web"
}
user_data = file("user_data.yml")
}
Loading
Loading