Skip to content

Commit

Permalink
refactor: simplify variables
Browse files Browse the repository at this point in the history
- do not repeat yourself terraform practices
  • Loading branch information
frrist committed Feb 9, 2024
1 parent 3688ea2 commit b28204e
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 208 deletions.
79 changes: 41 additions & 38 deletions ops/tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,70 @@ module "gcp_network" {

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

aws_credentials = local.aws_credentials
build_config = local.build_config
token_config = local.token_config
gcp_config = local.gcp_config

disk_config = {
boot_size = var.bacalhau_boot_disk_size
repo_size = var.bacalhau_repo_disk_size
}

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
boot_image = var.gcp_boot_image
cloud_init_content = ""
requester_instance_type = var.requester_machine_type

aws_access_key_id = var.aws_access_key_id
aws_secret_access_key = var.aws_secret_access_key
bacalhau_accept_networked_jobs = var.bacalhau_accept_networked_jobs
bacalhau_repo_disk_size = var.bacalhau_repo_disk_size
bacalhau_otel_collector_endpoint = var.bacalhau_otel_collector_endpoint

bacalhau_install_version = var.bacalhau_install_version
bacalhau_install_branch = var.bacalhau_install_branch
bacalhau_install_commit = var.bacalhau_install_commit

bacalhau_requester_api_token = local.bacalhau_requester_api_auth_token
bacalhau_compute_api_token = local.bacalhau_requester_api_auth_token
}

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

network = module.gcp_network.vpc_network_name
subnetwork = module.gcp_network.subnetwork_name
zone = var.gcp_zone
aws_credentials = local.aws_credentials
build_config = local.build_config
token_config = local.token_config
gcp_config = local.gcp_config

disk_config = {
boot_size = var.bacalhau_boot_disk_size
repo_size = var.bacalhau_repo_disk_size
local_size = var.bacalhau_local_disk_size
}

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:
// 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]
boot_image = var.gcp_boot_image
compute_instance_count = var.compute_count
compute_instance_type = var.compute_machine_type

aws_access_key_id = var.aws_access_key_id
aws_secret_access_key = var.aws_secret_access_key
bacalhau_accept_networked_jobs = var.bacalhau_accept_networked_jobs
bacalhau_repo_disk_size = var.bacalhau_repo_disk_size
bacalhau_local_disk_size = var.bacalhau_local_disk_size
bacalhau_otel_collector_endpoint = var.bacalhau_otel_collector_endpoint

bacalhau_install_version = var.bacalhau_install_version
bacalhau_install_branch = var.bacalhau_install_branch
bacalhau_install_commit = var.bacalhau_install_commit

bacalhau_requester_api_token = local.bacalhau_requester_api_auth_token
bacalhau_compute_api_token = local.bacalhau_requester_api_auth_token

}

locals {
bacalhau_requester_api_auth_token = var.bacalhau_requester_api_token != "" ? var.bacalhau_requester_api_token : random_string.bacalhau_requester_api_token.result
bacalhau_compute_api_auth_token = var.bacalhau_compute_api_token != "" ? var.bacalhau_compute_api_token : random_string.bacalhau_compute_api_token.result
token_config = {
requester_api_token = var.bacalhau_requester_api_token != "" ? var.bacalhau_requester_api_token : random_string.bacalhau_requester_api_token.result
compute_api_token = var.bacalhau_compute_api_token != "" ? var.bacalhau_compute_api_token : random_string.bacalhau_compute_api_token.result
}
build_config = {
install_version = var.bacalhau_install_version
install_branch = var.bacalhau_install_branch
install_commit = var.bacalhau_install_commit
}
aws_credentials = {
access_key_id = var.aws_access_key_id
secret_access_key = var.aws_secret_access_key
}
gcp_config = {
network = module.gcp_network.vpc_network_name
subnetwork = module.gcp_network.subnetwork_name
zone = var.gcp_zone
boot_image = var.gcp_boot_image
}
}

resource "random_string" "bacalhau_requester_api_token" {
Expand Down
28 changes: 14 additions & 14 deletions ops/tf/modules/gcp/compute_instances/compute/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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
zone = var.gcp_config.zone

metadata = {
user-data = data.cloudinit_config.compute_cloud_init.rendered
Expand All @@ -13,8 +13,8 @@ resource "google_compute_instance" "compute" {

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

Expand All @@ -24,8 +24,8 @@ resource "google_compute_instance" "compute" {
allow_stopping_for_update = true

network_interface {
network = var.network
subnetwork = var.subnetwork
network = var.gcp_config.network
subnetwork = var.gcp_config.subnetwork
access_config {
// Ephemeral public IP will be assigned
}
Expand All @@ -37,8 +37,8 @@ resource "google_compute_disk" "bacalhau_repo_disks" {
count = var.compute_instance_count
name = "bacalhau-repo-disk-compute-${count.index + 1}"
type = "pd-standard"
zone = var.zone
size = var.bacalhau_repo_disk_size
zone = var.gcp_config.zone
size = var.disk_config.repo_size
}

// attach the disk(s) to instance(s)
Expand All @@ -54,8 +54,8 @@ resource "google_compute_disk" "bacalhau_local_disks" {
count = var.compute_instance_count
name = "bacalhau-local-disk-compute-${count.index + 1}"
type = "pd-standard"
zone = var.zone
size = var.bacalhau_local_disk_size
zone = var.gcp_config.zone
size = var.disk_config.local_size
}

// attach the disk(s) to instance(s)
Expand All @@ -78,8 +78,8 @@ locals {
BACALHAU_ENVIRONMENT = "local"
// TODO make this a variable
OTEL_EXPORTER_OTLP_ENDPOINT = "http://localhost:4318"
AWS_ACCESS_KEY_ID = var.aws_access_key_id
AWS_SECRET_ACCESS_KEY = var.aws_secret_access_key
AWS_ACCESS_KEY_ID = var.aws_credentials.access_key_id
AWS_SECRET_ACCESS_KEY = var.aws_credentials.secret_access_key
# Add more variables here as needed
}
# Convert the map to the required string format for the systemd service file
Expand All @@ -99,7 +99,7 @@ locals {
compute_config_content = templatefile("${path.module}/../../../instance_files/compute_config.yaml", {
requester_ip = var.requester_ip
bacalhau_accept_networked_jobs = var.bacalhau_accept_networked_jobs
compute_api_token = var.bacalhau_compute_api_token
compute_api_token = var.token_config.compute_api_token
})

//
Expand All @@ -108,7 +108,7 @@ locals {

// inject custom bacalhau install based on variables.
// I am sorry reader, terraform requires this be one line
bacalhau_install_cmd_content = var.bacalhau_install_version != "" ? "release ${var.bacalhau_install_version}" : var.bacalhau_install_branch != "" ? "branch ${var.bacalhau_install_branch}" : var.bacalhau_install_commit != "" ? "commit ${var.bacalhau_install_commit}" : ""
bacalhau_install_cmd_content = var.build_config.install_version != "" ? "release ${var.build_config.install_version}" : var.build_config.install_branch != "" ? "branch ${var.build_config.install_branch}" : var.build_config.install_commit != "" ?"commit ${var.build_config.install_commit}" : ""
bacalhau_start_script = templatefile("${path.module}/../../../instance_files/start.sh", {
node_type = "compute"
bacalhau_version_cmd = local.bacalhau_install_cmd_content
Expand Down Expand Up @@ -138,7 +138,7 @@ locals {

// authn
bacalhau_authn_policy_content = templatefile("${path.module}/../../../instance_files/authn_policy.rego", {
bacalhau_secret_user_access_token = var.bacalhau_requester_api_token
bacalhau_secret_user_access_token = var.token_config.requester_api_token
})
// authz
bacalhau_authz_policy_content = templatefile("${path.module}/../../../instance_files/authz_policy.rego", {
Expand Down
119 changes: 45 additions & 74 deletions ops/tf/modules/gcp/compute_instances/compute/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
variable "aws_credentials" {
description = "AWS credentials"
type = object({
access_key_id = string
secret_access_key = string
})
}

variable "build_config" {
description = "Configuration for building specific versions of bacalhau"
type = object({
install_version = string
install_branch = string
install_commit = string
})
}

variable "token_config" {
description = "Configuration for setting up auth tokens"
type = object({
requester_api_token = string
compute_api_token = string
})
}

variable "gcp_config" {
description = "Configuration specific to GCP including networking and boot image"
type = object({
network = string
subnetwork = string
zone = string
boot_image = string
})
}

variable "disk_config" {
description = "Configuration related to local storage disk, repo disk, and boot disk"
type = object({
boot_size = number
repo_size= number
local_size = number
})
}

variable "compute_instance_count" {
description = "Number of compute instances"
type = number
Expand All @@ -13,92 +57,19 @@ variable "requester_ip" {
type = string
}

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

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

variable "boot_image" {
description = "The boot image for the instances"
type = string
}

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 "aws_access_key_id" {
description = "AWS access key id used to authenticate s3 compatible storage"
type = string
}

variable "aws_secret_access_key" {
description = "AWS secret access key used to authenticate s3 compatible storage"
type = string
}

variable "bacalhau_accept_networked_jobs" {
description = "When true bacalhau will accept jobs requiring networking. Otherwise they will be rejected."
type = bool
default = false
}

variable "bacalhau_repo_disk_size" {
description = "The size of the disk in GB bacalhau will to store its repo"
type = number
}

variable "bacalhau_local_disk_size" {
description = "The size of the disk in GB bacalhau will to store local data"
type = number
}

variable "bacalhau_otel_collector_endpoint" {
description = "The opentelemetry collector endpoint to send metrics to"
type = string
}

variable "bacalhau_requester_api_token" {
description = "Auth token for bacalhau requester api"
type = string
}

variable "bacalhau_compute_api_token" {
description = "Auth token for bacalhau compute api"
type = string
}

variable "bacalhau_install_version" {
description = "The version or branch of bacalhau to install. If empty https://get.bacalhau.org/install.sh will be used to install"
type = string
default = ""
}

variable "bacalhau_install_branch" {
description = "The branch of bacalhau to install. If empty default to https://get.bacalhau.org/install.sh"
type = string
default = ""
}

variable "bacalhau_install_commit" {
description = "The commit sha of bacalhau to install. If empty default to https://get.bacalhau.org/install.sh"
type = string
default = ""
}
}
Loading

0 comments on commit b28204e

Please sign in to comment.