majuna/app/terraform/bridge/ovh.py

107 lines
3.6 KiB
Python
Raw Normal View History

2023-02-26 12:52:08 +00:00
from app.models.cloud import CloudProvider
2022-03-10 14:26:22 +00:00
from app.terraform.bridge import BridgeAutomation
class BridgeOvhAutomation(BridgeAutomation):
short_name = "bridge_ovh"
description = "Deploy Tor bridges on OVH Public Cloud"
2023-02-26 12:52:08 +00:00
provider = CloudProvider.OVH
2022-03-10 14:26:22 +00:00
2024-12-06 18:15:47 +00:00
template_parameters = ["ssh_public_key_path", "ssh_private_key_path"]
2022-03-10 14:26:22 +00:00
template = """
terraform {
{{ backend_config }}
2022-03-10 14:26:22 +00:00
required_providers {
random = {
source = "hashicorp/random"
version = "3.1.0"
}
openstack = {
source = "terraform-provider-openstack/openstack"
version = "~> 1.42.0"
}
ovh = {
source = "ovh/ovh"
version = ">= 0.13.0"
}
}
}
2022-05-16 13:29:48 +01:00
2023-02-26 12:52:08 +00:00
locals {
public_ssh_key = "{{ ssh_public_key_path }}"
private_ssh_key = "{{ ssh_private_key_path }}"
}
{% for resource in destroyed_resources %}
{% set bridge, bridgeconf, account = resource %}
2022-03-10 14:26:22 +00:00
provider "openstack" {
auth_url = "https://auth.cloud.ovh.net/v3/"
domain_name = "Default" # Domain name - Always at 'default' for OVHcloud
2023-02-26 12:52:08 +00:00
user_name = "{{ account.credentials["ovh_openstack_user"] }}"
password = "{{ account.credentials["ovh_openstack_password"] }}"
tenant_id = "{{ account.credentials["ovh_openstack_tenant_id"] }}"
alias = "account_{{ bridge.id }}"
2022-03-10 14:26:22 +00:00
}
2023-02-26 12:52:08 +00:00
{% endfor %}
2022-05-16 13:29:48 +01:00
2023-02-26 12:52:08 +00:00
{% for resource in active_resources %}
{% set bridge, bridgeconf, account = resource %}
provider "openstack" {
auth_url = "https://auth.cloud.ovh.net/v3/"
domain_name = "Default" # Domain name - Always at 'default' for OVHcloud
user_name = "{{ account.credentials["ovh_openstack_user"] }}"
password = "{{ account.credentials["ovh_openstack_password"] }}"
tenant_id = "{{ account.credentials["ovh_openstack_tenant_id"] }}"
alias = "account_{{ bridge.id }}"
2022-03-10 14:26:22 +00:00
}
2022-05-16 13:29:48 +01:00
2023-02-26 12:52:08 +00:00
provider "ovh" {
endpoint = "ovh-eu"
application_key = "{{ account.credentials["ovh_cloud_application_key"] }}"
application_secret = "{{ account.credentials["ovh_cloud_application_secret"] }}"
consumer_key = "{{ account.credentials["ovh_cloud_consumer_key"] }}"
alias = "account_{{ bridge.id }}"
2022-03-10 14:26:22 +00:00
}
2022-05-16 13:29:48 +01:00
2023-02-26 12:52:08 +00:00
data "ovh_cloud_project_regions" "regions_{{ bridge.id }}" {
provider = ovh.account_{{ bridge.id }}
service_name = "{{ account.credentials["ovh_openstack_tenant_id"] }}"
2022-03-10 14:26:22 +00:00
has_services_up = ["instance"]
}
2022-05-16 13:29:48 +01:00
2022-03-10 14:26:22 +00:00
resource "random_shuffle" "region_{{ bridge.id }}" {
2023-02-26 12:52:08 +00:00
input = data.ovh_cloud_project_regions.regions_{{ bridge.id }}.names
2022-03-10 14:26:22 +00:00
result_count = 1
2022-05-16 13:29:48 +01:00
2022-03-10 14:26:22 +00:00
lifecycle {
ignore_changes = [input] # don't replace all the bridges if a new region appears
}
}
2022-05-16 13:29:48 +01:00
2022-03-10 14:26:22 +00:00
module "bridge_{{ bridge.id }}" {
2023-02-26 12:52:08 +00:00
providers = {
openstack = openstack.account_{{ bridge.id }}
}
2022-08-30 10:24:58 +01:00
source = "{{ terraform_modules_path }}/terraform-openstack-tor-bridge"
2022-03-10 14:26:22 +00:00
region = one(random_shuffle.region_{{ bridge.id }}.result)
2023-02-26 12:52:08 +00:00
namespace = "{{ global_namespace }}"
name = "bridge"
2022-03-10 14:26:22 +00:00
attributes = ["{{ bridge.id }}"]
2023-02-26 14:07:23 +00:00
ssh_public_key = local.public_ssh_key
ssh_private_key = local.private_ssh_key
2023-02-26 12:52:08 +00:00
contact_info = "hello from onionland"
distribution_method = "{{ bridgeconf.method }}"
2022-03-10 14:26:22 +00:00
}
2022-05-16 13:29:48 +01:00
2022-03-10 14:26:22 +00:00
output "bridge_hashed_fingerprint_{{ bridge.id }}" {
value = module.bridge_{{ bridge.id }}.hashed_fingerprint
}
2022-05-16 13:29:48 +01:00
2022-03-10 14:26:22 +00:00
output "bridge_bridgeline_{{ bridge.id }}" {
value = module.bridge_{{ bridge.id }}.bridgeline
sensitive = true
}
{% endfor %}
"""