mirror of
https://github.com/jpetazzo/container.training.git
synced 2026-07-16 03:19:18 +00:00
🏭️ Support multiple Terraform configurations
Historically, we only support one Terraform configuration, through the "openstack-tf" infraclass. With these changes, we support multiple Terraform configurations, including (at this point) "openstack" and "oci" (Oracle Cloud). Existing infra files that use INFRACLASS=openstack-tf should be changed as follows: INFRACLASS=terraform TERRAFORM=openstack
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
INFRACLASS=openstack-tf
|
||||
INFRACLASS=terraform
|
||||
TERRAFORM=openstack
|
||||
|
||||
# If you are using OpenStack, copy this file (e.g. to "openstack" or "enix")
|
||||
# and customize the variables below.
|
||||
@@ -178,6 +178,13 @@ _cmd_clusterize() {
|
||||
# install --owner=ubuntu --mode=600 /root/.ssh/authorized_keys --target-directory /home/ubuntu/.ssh"
|
||||
#fi
|
||||
|
||||
# Special case for oracle since their iptables blocks everything but SSH
|
||||
pssh "
|
||||
if [ -f /etc/iptables/rules.v4 ]; then
|
||||
sudo sed -i 's/-A INPUT -j REJECT --reject-with icmp-host-prohibited//' /etc/iptables/rules.v4
|
||||
sudo netfilter-persistent start
|
||||
fi"
|
||||
|
||||
# Copy settings and install Python YAML parser
|
||||
pssh -I tee /tmp/settings.yaml <tags/$TAG/settings.yaml
|
||||
pssh "
|
||||
@@ -185,10 +192,10 @@ _cmd_clusterize() {
|
||||
sudo apt-get install -y python-yaml"
|
||||
|
||||
# If there is no "python" binary, symlink to python3
|
||||
#pssh "
|
||||
#if ! which python; then
|
||||
# ln -s $(which python3) /usr/local/bin/python
|
||||
#fi"
|
||||
pssh "
|
||||
if ! which python; then
|
||||
sudo ln -s $(which python3) /usr/local/bin/python
|
||||
fi"
|
||||
|
||||
# Copy postprep.py to the remote machines, and execute it, feeding it the list of IP addresses
|
||||
pssh -I tee /tmp/clusterize.py <lib/clusterize.py
|
||||
@@ -248,7 +255,7 @@ _cmd_docker() {
|
||||
|
||||
##VERSION## https://github.com/docker/compose/releases
|
||||
if [ "$ARCHITECTURE" ]; then
|
||||
COMPOSE_VERSION=v2.0.1
|
||||
COMPOSE_VERSION=v2.2.3
|
||||
COMPOSE_PLATFORM='linux-$(uname -m)'
|
||||
else
|
||||
COMPOSE_VERSION=1.29.2
|
||||
@@ -1051,7 +1058,8 @@ _cmd_webssh() {
|
||||
need_tag
|
||||
pssh "
|
||||
sudo apt-get update &&
|
||||
sudo apt-get install python-tornado python-paramiko -y"
|
||||
sudo apt-get install python-tornado python-paramiko -y ||
|
||||
sudo apt-get install python3-tornado python3-paramiko -y"
|
||||
pssh "
|
||||
cd /opt
|
||||
[ -d webssh ] || sudo git clone https://github.com/jpetazzo/webssh"
|
||||
|
||||
@@ -1,7 +1,26 @@
|
||||
error_terraform_configuration() {
|
||||
error "When using the terraform infraclass, the TERRAFORM"
|
||||
error "environment variable must be set to one of the available"
|
||||
error "terraform configurations. These configurations are in"
|
||||
error "the prepare-vm/terraform subdirectory. You should probably"
|
||||
error "update your infra file and set the variable."
|
||||
error "(e.g. with TERRAFORM=openstack)"
|
||||
}
|
||||
|
||||
if [ "$TERRAFORM" = "" ]; then
|
||||
error_terraform_configuration
|
||||
die "Aborting because TERRAFORM variable is not set."
|
||||
fi
|
||||
|
||||
if [ ! -d terraform/$TERRAFORM ]; then
|
||||
error_terraform_configuration
|
||||
die "Aborting because no terraform configuration was found in 'terraform/$TERRAFORM'."
|
||||
fi
|
||||
|
||||
infra_start() {
|
||||
COUNT=$1
|
||||
|
||||
cp terraform-openstack/*.tf tags/$TAG
|
||||
cp terraform/$TERRAFORM/*.tf tags/$TAG
|
||||
(
|
||||
cd tags/$TAG
|
||||
if ! terraform init; then
|
||||
48
prepare-vms/terraform/oci/main.tf
Normal file
48
prepare-vms/terraform/oci/main.tf
Normal file
@@ -0,0 +1,48 @@
|
||||
resource "oci_identity_compartment" "_" {
|
||||
name = var.prefix
|
||||
description = var.prefix
|
||||
enable_delete = true
|
||||
}
|
||||
|
||||
locals {
|
||||
compartment_id = oci_identity_compartment._.id
|
||||
}
|
||||
|
||||
data "oci_identity_availability_domains" "_" {
|
||||
compartment_id = local.compartment_id
|
||||
}
|
||||
|
||||
data "oci_core_images" "_" {
|
||||
compartment_id = local.compartment_id
|
||||
shape = var.shape
|
||||
operating_system = "Canonical Ubuntu"
|
||||
operating_system_version = "20.04"
|
||||
#operating_system = "Oracle Linux"
|
||||
#operating_system_version = "7.9"
|
||||
}
|
||||
|
||||
resource "oci_core_instance" "_" {
|
||||
count = var.how_many_nodes
|
||||
display_name = format("%s-%04d", var.prefix, count.index + 1)
|
||||
availability_domain = data.oci_identity_availability_domains._.availability_domains[var.availability_domain].name
|
||||
compartment_id = local.compartment_id
|
||||
shape = var.shape
|
||||
shape_config {
|
||||
memory_in_gbs = var.memory_in_gbs_per_node
|
||||
ocpus = var.ocpus_per_node
|
||||
}
|
||||
source_details {
|
||||
source_id = data.oci_core_images._.images[0].id
|
||||
source_type = "image"
|
||||
}
|
||||
create_vnic_details {
|
||||
subnet_id = oci_core_subnet._.id
|
||||
}
|
||||
metadata = {
|
||||
ssh_authorized_keys = local.authorized_keys
|
||||
}
|
||||
}
|
||||
|
||||
output "ip_addresses" {
|
||||
value = join("", formatlist("%s\n", oci_core_instance._.*.public_ip))
|
||||
}
|
||||
63
prepare-vms/terraform/oci/network.tf
Normal file
63
prepare-vms/terraform/oci/network.tf
Normal file
@@ -0,0 +1,63 @@
|
||||
resource "oci_core_vcn" "_" {
|
||||
compartment_id = local.compartment_id
|
||||
cidr_block = "10.0.0.0/16"
|
||||
display_name = "tf-vcn"
|
||||
}
|
||||
|
||||
#
|
||||
# On OCI, you can have either "public" or "private" subnets.
|
||||
# In both cases, instances get addresses in the VCN CIDR block;
|
||||
# but instances in "public" subnets also get a public address.
|
||||
#
|
||||
# Then, to enable communication to the outside world, you need:
|
||||
# - for public subnets, an "internet gateway"
|
||||
# (will allow inbound and outbound traffic)
|
||||
# - for private subnets, a "NAT gateway"
|
||||
# (will only allow outbound traffic)
|
||||
# - optionally, for private subnets, a "service gateway"
|
||||
# (to access other OCI services, e.g. object store)
|
||||
#
|
||||
# In this configuration, we use public subnets, and since we
|
||||
# need outside access, we add an internet gateway.
|
||||
#
|
||||
# Note that the default routing table in a VCN is empty, so we
|
||||
# add the internet gateway to the default routing table.
|
||||
# Similarly, the default security group in a VCN blocks almost
|
||||
# everything, so we add a blanket rule in that security group.
|
||||
#
|
||||
|
||||
resource "oci_core_internet_gateway" "_" {
|
||||
compartment_id = local.compartment_id
|
||||
display_name = "tf-igw"
|
||||
vcn_id = oci_core_vcn._.id
|
||||
}
|
||||
|
||||
resource "oci_core_default_route_table" "_" {
|
||||
manage_default_resource_id = oci_core_vcn._.default_route_table_id
|
||||
route_rules {
|
||||
destination = "0.0.0.0/0"
|
||||
destination_type = "CIDR_BLOCK"
|
||||
network_entity_id = oci_core_internet_gateway._.id
|
||||
}
|
||||
}
|
||||
|
||||
resource "oci_core_default_security_list" "_" {
|
||||
manage_default_resource_id = oci_core_vcn._.default_security_list_id
|
||||
ingress_security_rules {
|
||||
protocol = "all"
|
||||
source = "0.0.0.0/0"
|
||||
}
|
||||
egress_security_rules {
|
||||
protocol = "all"
|
||||
destination = "0.0.0.0/0"
|
||||
}
|
||||
}
|
||||
|
||||
resource "oci_core_subnet" "_" {
|
||||
compartment_id = local.compartment_id
|
||||
cidr_block = "10.0.0.0/20"
|
||||
vcn_id = oci_core_vcn._.id
|
||||
display_name = "tf-subnet"
|
||||
route_table_id = oci_core_default_route_table._.id
|
||||
security_list_ids = [oci_core_default_security_list._.id]
|
||||
}
|
||||
8
prepare-vms/terraform/oci/provider.tf
Normal file
8
prepare-vms/terraform/oci/provider.tf
Normal file
@@ -0,0 +1,8 @@
|
||||
terraform {
|
||||
required_version = ">= 1"
|
||||
required_providers {
|
||||
openstack = {
|
||||
source = "hashicorp/oci"
|
||||
version = "4.48.0" }
|
||||
}
|
||||
}
|
||||
42
prepare-vms/terraform/oci/variables.tf
Normal file
42
prepare-vms/terraform/oci/variables.tf
Normal file
@@ -0,0 +1,42 @@
|
||||
variable "prefix" {
|
||||
type = string
|
||||
default = "provisioned-with-terraform"
|
||||
}
|
||||
|
||||
variable "how_many_nodes" {
|
||||
type = number
|
||||
default = 2
|
||||
}
|
||||
|
||||
locals {
|
||||
authorized_keys = file("~/.ssh/id_rsa.pub")
|
||||
}
|
||||
|
||||
/*
|
||||
Available flex shapes:
|
||||
"VM.Optimized3.Flex" # Intel Ice Lake
|
||||
"VM.Standard3.Flex" # Intel Ice Lake
|
||||
"VM.Standard.A1.Flex" # Ampere Altra
|
||||
"VM.Standard.E3.Flex" # AMD Rome
|
||||
"VM.Standard.E4.Flex" # AMD Milan
|
||||
*/
|
||||
|
||||
variable "shape" {
|
||||
type = string
|
||||
default = "VM.Standard.A1.Flex"
|
||||
}
|
||||
|
||||
variable "availability_domain" {
|
||||
type = number
|
||||
default = 0
|
||||
}
|
||||
|
||||
variable "ocpus_per_node" {
|
||||
type = number
|
||||
default = 1
|
||||
}
|
||||
|
||||
variable "memory_in_gbs_per_node" {
|
||||
type = number
|
||||
default = 4
|
||||
}
|
||||
Reference in New Issue
Block a user