Skip to content

Commit

Permalink
feat: new terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
frrist committed Jan 15, 2024
1 parent 5b6e7c1 commit eee5820
Show file tree
Hide file tree
Showing 19 changed files with 709 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ops/tf/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
terraform {
backend "gcs" {
bucket = "bacalhau-infra-state"
prefix = "terraform"
}
}
45 changes: 45 additions & 0 deletions ops/tf/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
provider "google" {
project = var.gcp_project_id
region = var.gcp_region
zone = var.gcp_zone
}

module "gcp_network" {
source = "./modules/gcp/network"
region = var.gcp_region
subnet_cidr = "10.0.0.0/16" // Example CIDR, adjust as needed
}

module "requester_instance" {
source = "./modules/gcp/compute_instances/requester"

network = module.gcp_network.vpc_network_name
subnetwork = module.gcp_network.subnetwork_name
requester_static_ip = module.gcp_network.requester_ip
zone = var.gcp_zone

cloud_init_content = ""
requester_instance_type = var.requester_machine_type
install_bacalhau_argument = var.install_bacalhau_argument
}

module "compute_instance" {
source = "./modules/gcp/compute_instances/compute"

network = module.gcp_network.vpc_network_name
subnetwork = module.gcp_network.subnetwork_name
zone = var.gcp_zone

cloud_init_content = ""
// This creates an implicit dependency, meaning Terraform will create the requester_instance before the compute_instance.
// In the event the bacalhau process on the compute instance stars BEFORE the requester instance (which would be
// abnormal but possible) the compute will fail to bootstrap to the requester and fail to start.
// This can happen if setting up the requester VM takes longer than settin up the compute. So there is a TODO here:

Check failure on line 37 in ops/tf/main.tf

View workflow job for this annotation

GitHub Actions / Check for spelling errors

settin ==> setting
// Bacalhau should not stop the node if it fails to connect to a peer, it should instead continue to try until is
// succeeds and complain loudly along the way as it fails.
requester_ip = module.requester_instance.requester_private_ips[0]
compute_instance_count = var.compute_count
compute_instance_type = var.compute_machine_type
install_bacalhau_argument = var.install_bacalhau_argument

}
93 changes: 93 additions & 0 deletions ops/tf/modules/cloud-init/cloud-init.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#cloud-config

# Boot-time commands

# Update package database and upgrade the system
package_update: true
package_upgrade: true

# List of packages to install
packages:
- git
- git-lfs
- make
- npm
- curl
- jq
- wget
- gnupg
- ca-certificates
- lsb-release

write_files:
- path: /etc/config.yaml
encoding: b64
owner: root:root
permissions: "0600"
content: |
${bacalhau_config_file}
- path: /etc/systemd/system/bacalhau.service
encoding: b64
owner: root:root
permissions: "0600"
content: |
${bacalhau_service_file}
- path: /etc/install-bacalhau.sh
encoding: b64
owner: root:root
permissions: "0700"
content: |
${bacalhau_install_file}
runcmd:
#
# Setup Bacalhau Repo
#
- mkdir /data
- chmod 0700 /data
- mv /etc/config.yaml /data/config.yaml
#
# Install go
#
- export HOME=/root
- export GOCACHE="$HOME/.cache/go-build"
- export GOPATH="/root/go"
- export PATH="$PATH:$GOPATH/bin:/usr/local/go/bin"
- mkdir -p "$GOPATH"
# the above steps are from
# build cache is required, but could not be located: GOCACHE is not defined and neither $XDG_CACHE_HOME nor $HOME are defined
# go build -ldflags "-X github.com/bacalhau-project/bacalhau/pkg/version.GITVERSION=v1.1.7-rc1-5-g298e129b" -trimpath -o bin/linux_amd64/bacalhau .
# go: module cache not found: neither GOMODCACHE nor GOPATH is set
# go: module cache not found: neither GOMODCACHE nor GOPATH is set
# make: *** [Makefile:208: bin/linux_amd64/bacalhau] Error 1
# Failed to build bacalhau
- sudo rm -fr /usr/local/go /usr/local/bin/go
- curl --silent --show-error --location --fail 'https://go.dev/dl/go1.20.4.linux-amd64.tar.gz' | sudo tar --extract --gzip --file=- --directory=/usr/local
- sudo ln -s /usr/local/go/bin/go /usr/local/bin/go
- go version
#
# Install docker
#
- sudo apt-get install -y ca-certificates curl gnupg lsb-release
- sudo mkdir -p /etc/apt/keyrings
- |
curl -fsSL "https://download.docker.com/linux/ubuntu/gpg" | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- sudo apt-get update -y
- sudo apt -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
#
# Install Bacalhau
#
- /etc/install-bacalhau.sh ${bacalhau_install_args}
#
# Start services
#
- sudo systemctl daemon-reload
- sudo systemctl enable docker
- sudo systemctl restart docker
- sudo systemctl enable bacalhau.service
- sudo systemctl restart bacalhau.service


55 changes: 55 additions & 0 deletions ops/tf/modules/gcp/compute_instances/compute/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
resource "google_compute_instance" "compute" {
count = var.compute_instance_count
name = "bacalhau-compute-${count.index + 1}"
machine_type = var.compute_instance_type
zone = var.zone

metadata = {
user-data = data.cloudinit_config.compute_cloud_init.rendered
}

boot_disk {
initialize_params {
image = var.boot_image
size = var.boot_size
}
}

network_interface {
network = var.network
subnetwork = var.subnetwork
access_config {
// Ephemeral public IP will be assigned
}
}
}

locals {
compute_config_content = templatefile("${path.module}/../../../instance_files/compute_config.yaml", {
requester_ip = var.requester_ip
})
bacalhau_service_content = templatefile("${path.module}/../../../instance_files/bacalhau.service", {
args = "" # replace with your actual arguments
})
bacalhau_install_content = filebase64("${path.module}/../../../instance_files/install-bacalhau.sh")
}


data "cloudinit_config" "compute_cloud_init" {
gzip = false
base64_encode = false

// provide parameters to cloud-init like files and arguments to scripts in the above part.
part {
filename = "cloud-config.yaml"
content_type = "text/cloud-config"

content = templatefile("${path.module}/../../../cloud-init/cloud-init.yml", {
bacalhau_config_file : base64encode(local.compute_config_content),
bacalhau_service_file : base64encode(local.bacalhau_service_content),
bacalhau_install_file : local.bacalhau_install_content,
bacalhau_install_args : var.install_bacalhau_argument
requester_ip : var.requester_ip,
})
}
}
8 changes: 8 additions & 0 deletions ops/tf/modules/gcp/compute_instances/compute/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "compute_private_ips" {
value = [for instance in google_compute_instance.compute : instance.network_interface[0].network_ip]
}

output "compute_public_ips" {
value = google_compute_instance.compute.*.network_interface.0.access_config.0.nat_ip
}

54 changes: 54 additions & 0 deletions ops/tf/modules/gcp/compute_instances/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
variable "compute_instance_count" {
description = "Number of compute instances"
type = number
}

variable "compute_instance_type" {
description = "The instance type for the compute"
type = string
}

variable "requester_ip" {
description = "Private IP of the requester instance"
type = string
}

variable "zone" {
description = "The zone in which to provision instances"
type = string
}

variable "boot_image" {
description = "The boot image for the instances"
type = string
default = "projects/ubuntu-os-cloud/global/images/family/ubuntu-2304-amd64"
}

variable "boot_size" {
description = "The size of the boot disk"
type = number
default = 50
}

variable "cloud_init_content" {
description = "Content of the cloud-init script"
type = string
}

variable "network" {
description = "The VPC network to attach to the instances"
type = string
}

variable "subnetwork" {
description = "The subnetwork to attach to the instances"
type = string
}

// Usage: install-bacalhau [release <version> | branch <branch-name>]
variable "install_bacalhau_argument" {
description = "Argument to pass to the install bacalhau script"
type = string
default = ""
}

51 changes: 51 additions & 0 deletions ops/tf/modules/gcp/compute_instances/requester/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
resource "google_compute_instance" "requester" {
name = "bacalhau-requester"
machine_type = var.requester_instance_type
zone = var.zone

metadata = {
user-data = data.cloudinit_config.requester_cloud_init.rendered
}
boot_disk {
initialize_params {
image = var.boot_image
size = var.boot_size
}
}

network_interface {
network = var.network
subnetwork = var.subnetwork
access_config {
nat_ip = var.requester_static_ip // Static IP
}
}
}

locals {
requester_config_content = templatefile("${path.module}/../../../instance_files/requester_config.yaml", {
# add variables you'd like to inject into the config
})
bacalhau_service_content = templatefile("${path.module}/../../../instance_files/bacalhau.service", {
args = "" # replace with your actual arguments
})
bacalhau_install_content = filebase64("${path.module}/../../../instance_files/install-bacalhau.sh")
}

data "cloudinit_config" "requester_cloud_init" {
gzip = false
base64_encode = false

// provide parameters to cloud-init like files and arguments to scripts in the above part.
part {
filename = "cloud-config.yaml"
content_type = "text/cloud-config"

content = templatefile("${path.module}/../../../cloud-init/cloud-init.yml", {
bacalhau_config_file : base64encode(local.requester_config_content),
bacalhau_service_file : base64encode(local.bacalhau_service_content),
bacalhau_install_file : local.bacalhau_install_content,
bacalhau_install_args : var.install_bacalhau_argument
})
}
}
7 changes: 7 additions & 0 deletions ops/tf/modules/gcp/compute_instances/requester/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "requester_public_ips" {
value = google_compute_instance.requester.*.network_interface.0.access_config.0.nat_ip
}

output "requester_private_ips" {
value = google_compute_instance.requester.*.network_interface.0.network_ip
}
50 changes: 50 additions & 0 deletions ops/tf/modules/gcp/compute_instances/requester/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
variable "requester_instance_type" {
description = "The instance type for the requester"
type = string
}

variable "requester_static_ip" {
description = "The static IP address for the requester instance"
type = string
}

variable "zone" {
description = "The zone in which to provision instances"
type = string
}

variable "boot_image" {
description = "The boot image for the instances"
type = string
default = "projects/ubuntu-os-cloud/global/images/family/ubuntu-2304-amd64"
}

variable "boot_size" {
description = "The size of the boot disk"
type = number
default = 50
}

variable "cloud_init_content" {
description = "Content of the cloud-init script"
type = string
}

variable "network" {
description = "The VPC network to attach to the instances"
type = string
}

variable "subnetwork" {
description = "The subnetwork to attach to the instances"
type = string
}

variable "install_bacalhau_argument" {
description = "Argument to pass to the install bacalhau script"
type = string
// Usage: install-bacalhau [release <version> | branch <branch-name>]
default = ""
}


Loading

0 comments on commit eee5820

Please sign in to comment.