10 Commits
24 changed files with 544 additions and 281 deletions
+15 -5
View File
@@ -24,9 +24,19 @@ chpasswd:
expire: false expire: false
hostname: ${hostname} hostname: ${hostname}
fqdn: ${hostname}
# Use this when it's determined that we need a bigger disk image. %{ if install-qemu-agent }
# This must be used in conjuction with 'size' in 'libvirt_volume' packages:
# growpart: # This are only necessary for libvirt.
# mode: auto - qemu-guest-agent
# devices: ['/'] runcmd:
# TODO At some point revisit this, this was added because it seemed like
# apparmor was causing dhclient to not get an IP address for ubuntu. This
# should be double checked.
- echo "/proc/*/task/*/comm wr," | tee -a /etc/apparmor.d/local/sbin.dhclient
# These are only necessary for libvirt.
- systemctl enable qemu-guest-agent
- systemctl start qemu-guest-agent
- systemctl status qemu-guest-agent
%{ endif }
+30 -6
View File
@@ -1,4 +1,4 @@
vm-name-prefix = "docker-ansible-test" vm-name-prefix = "ansible-test"
# A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0' # A CIDR block ending in '/32' equates to a single IP address, '0.0.0.0/0'
# equates to any ip address. # equates to any ip address.
@@ -7,12 +7,22 @@ admin-ips = [ "8.8.8.8/32", "0.0.0.0/0" ]
disk-image-dir = "/path/to/disk/pool/" disk-image-dir = "/path/to/disk/pool/"
libvirt-connection-url = "qemu+ssh://<user>@<host>/system" libvirt-connection-url = "qemu+ssh://<user>@<host>/system"
master-nodes = 1
worker-nodes = 2
node-memory = 2048 node-memory = 2048
node-vcpus = 2 node-vcpus = 2
## libvirt disk size
# 1 GiB = 1073741824
# 4 GiB
# libvirt-node-disk-size = "${4 * 1073741824}"
# 8 GiB
# libvirt-node-disk-size = "${8 * 1073741824}"
# 12 GiB
# libvirt-node-disk-size = "${12 * 1073741824}"
################################################################################
# AWS EC2 instance types
################################################################################
# 1 GiB, 1 vcpu, only one that is free. # 1 GiB, 1 vcpu, only one that is free.
# This one won't work with k8s because it requires at least 2 vcpus. # This one won't work with k8s because it requires at least 2 vcpus.
aws-ec2-instance-type = "t2.micro" aws-ec2-instance-type = "t2.micro"
@@ -50,8 +60,22 @@ base-image = "ami-0dd0ccab7e2801812"
################################################################################ ################################################################################
# base-image = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img" # base-image = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
# From https://cloud.centos.org/centos/7/images/ from 2020-11-12 06:52 # From https://cloud.centos.org/centos/7/images/ from 2020-12-06
# base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2" # base-image = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2111.qcow2"
## Arch
# From https://gitlab.archlinux.org/archlinux/arch-boxes/-/jobs/40102/artifacts/browse/output
# on 2021-11-28
# base-image = "/media/nas/software/isos/Arch-Linux-x86_64-libvirt-20211128.40102.box"
## Arch
# From https://gitlab.archlinux.org/archlinux/arch-boxes/-/jobs/40102/artifacts/browse/output
# on 2021-11-28
# base-image = "/media/nas/software/isos/Arch-Linux-x86_64-libvirt-20211128.40102.box"
################################################################################
# Keys/Passwords
################################################################################
# Password hash created with: # Password hash created with:
# python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))' # python3 -c 'import crypt; print(crypt.crypt("linux", crypt.mksalt(crypt.METHOD_SHA512)))'
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
aws ec2 describe-instance-status | \
jq '.InstanceStatuses[] | {id: .InstanceId, instance_status: .InstanceStatus.Status, system_status: .SystemStatus.Status}'
+81
View File
@@ -0,0 +1,81 @@
#!/bin/bash
# This script will grab the IPs for libvirt VMs. This script is only needed when
# using a bridge as the network for the VMs. This should only be needed while
# https://github.com/dmacvicar/terraform-provider-libvirt/issues/891 is
# unresolved.
# These are the network interfaces that this script will attempt to get the IP
# address for.
# Ubuntu 20.04 ens3
# Centos 7 & 8 eth0
NET_INTERFACES="eth0 ens3"
LIBVIRT_CONNECTION_URL="libvirt-connection-url"
VM_NAME_PREFIX="vm-name-prefix"
INV_GROUPS="$( \
cat terraform.tfstate | \
jq '.resources[] | select(.type=="libvirt_domain") | .module' | \
sed 's/".*\[\\"\(.*\)\\.*$/\1/g' )"
# Grab the connection URL and the vm name prefix. We do this by greping all
# *.tfvars files making sure to cat terraform.tfvars last. Then we just grab the
# last grep result, this way we make sure any value in terraform.tfvars will
# take priority.
CONN_URLS="$( \
find . -name "*.tfvars" -exec grep "$LIBVIRT_CONNECTION_URL" {} \; && \
grep "$LIBVIRT_CONNECTION_URL" terraform.tfvars)"
CONN_URL="$(echo "$CONN_URLS" | tail -n 1 | sed 's/^.*=\s*"\(.*\)"/\1/g')"
NAME_PREFIXES="$( \
find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX" {} \; && \
grep "$VM_NAME_PREFIX" terraform.tfvars)"
NAME_PREFIX="$(echo "$NAME_PREFIXES" | tail -n 1 | sed 's/^.*=\s*"\(.*\)"/\1/g')"
# These can be used for debugging.
# echo "Using connection URL: $CONN_URL"
# echo "Using prefix: $NAME_PREFIX"
# Get the names of our VMs from libvirt.
VMS="$(virsh -c $CONN_URL list --all | grep $NAME_PREFIX | awk '{print $2}')"
# Convert the lines of VM names to an array.
OLD_IFS=$IFS
IFS=$'\n'
VMS=($VMS)
IFS=$OLD_IFS
# Loop over our VM array and grab the ipv4 IP address from libvirt. Then add the
# result to VM_IP_PAIRS as <vm-name>:<ipv4-address>.
VM_IP_PAIRS=""
for VM in "${VMS[@]}"; do
for INTERFACE in $NET_INTERFACES; do
IP="$( \
virsh -c $CONN_URL qemu-agent-command $VM '{"execute": "guest-network-get-interfaces"}' | \
jq '.return[] | select(.name=="'"$INTERFACE"'") | ."ip-addresses"[] | select(."ip-address-type"=="ipv4") | ."ip-address"' | \
sed 's/"//g')"
# Add the VM:IP pair if IP is not empty.
if [ ! -z "$IP" ]; then
VM_IP_PAIRS="$VM_IP_PAIRS"$'\n'"$VM:$IP"
fi
done
done
# Write inventory
cat /dev/null > inventory
for GROUP in $INV_GROUPS; do
echo "[$GROUP]" >> inventory
echo "$VM_IP_PAIRS" | \
grep $GROUP | \
sed 's/^\(.*\):\(.*\)$/\1 ansible_host=\2/g' >> inventory
done
# Print vars
echo "$VM_IP_PAIRS" | \
sed 's/^\(.*\):\(.*\)$/\1=\2/g' | \
sed s/$NAME_PREFIX-//g | \
sed 's/-/_/g' | \
awk '{print toupper($1)}'
+81 -49
View File
@@ -1,60 +1,92 @@
#!/bin/sh #!/bin/bash
# This script will create environment variables for all of the output IPs. It # This script will create environment variables for all of the output IPs. An
# will also create a `ANSIBLE_INV` variable that will be a comma separated # anisble inventory file is created as well.
# string of all the IPs. A anisble inventory file called "inventory is created
# as well.
# #
# Use eval $(./get-vm-ips.sh) to set env vars for ips. # Use eval $(./get-vm-ips.sh) to set env vars for ips.
terraform refresh > /dev/null terraform refresh > /dev/null
# All terraform outputs in json format. # The file to write the inventory to. This file will be completely overridden.
OUTPUTS_JSON="$( INVENTORY_FILE="inventory"
terraform show -json | \
jq '.values.outputs' | \ # Grab the the vm name prefix. We do this by greping all *.tfvars files making
sed 's/-/_/g')" # sure to cat terraform.tfvars last. Then we just grab the last grep result,
# Just the IP address outputs in json format. Also all '-' characters are # this way we make sure any value in terraform.tfvars will take priority.
# replaced by '_' becuase '-' causes jq some problems. VM_NAME_PREFIX_VAR="vm-name-prefix"
IPS_JSON="$( VM_NAME_PREFIXES="$( \
echo $OUTPUTS_JSON | \ find . -name "*.tfvars" -exec grep "$VM_NAME_PREFIX_VAR" {} \; && \
jq 'to_entries | .[] | select(.key | contains("ips"))')" grep "$VM_NAME_PREFIX_VAR" terraform.tfvars)"
# An array of all node "types" VM_NAME_PREFIX="$(
NODE_TYPE_ARRAY="$( echo "$VM_NAME_PREFIXES" | \
echo $IPS_JSON | \ tail -n 1 | \
jq '.key' | \ sed 's/^.*=\s*"\(.*\)"/\1/g')"
# This command stores the output data in the format below.
# [
# {
# "group": "master",
# "vms": [
# {
# "hostname": "ansible-test-master-0",
# "ip": "52.14.114.48"
# }
# ]
# },
# {
# "group": "worker",
# "vms": [
# {
# "hostname": "ansible-test-worker-0",
# "ip": "3.145.121.159"
# },
# {
# "hostname": "ansible-test-worker-1",
# "ip": "18.217.112.176"
# }
# ]
# }
# ]
DATA="$(terraform show -json | \
jq '.values.outputs.groups_hostnames_ips.value | to_entries |
map({group: .key, vms:.value | to_entries |
map({hostname:.key,ip:.value})})')"
# Pull out the groups from $DATA. The format is a single string with the groups
# separated by spaces, ie. "group1 group2 group3".
ANS_GROUPS="$(
echo $DATA | \
jq '.[] | .group' | \
sed 's/"//g' | \ sed 's/"//g' | \
sed -z 's/\n/ /g;s/ $/\n/g')" tr '\n' ' '
)"
# Loop over all the node types and create an export line for each IP. # Clear the inventory file.
VM_IP_EXPORTS="$( cat /dev/null > $INVENTORY_FILE
for TYPE in $NODE_TYPE_ARRAY; do
# Convert type, converts "master-ips" to "MASTER" # For each group, write the VM info to $INVENTORY_FILE and also print a variable
TYPE_UPPER="$(echo ${TYPE^^} | sed s/_.*$//g)" # expression to stdout.
echo "$OUTPUTS_JSON" | \ for GROUP in $ANS_GROUPS; do
jq '.'"$TYPE"'.value[]' | \
# Add line numbers starting with 0.
nl -v 0 | \
# Print an export string with a type placeholder "__TYPE__".
awk '{print "export __TYPE__" $1 "=" $2}' | \
sed s/__TYPE__/$TYPE_UPPER/g
done)"
ANSIBLE_INV="$( # Write the inventory file to $INVENTORY_FILE.
echo "$VM_IP_EXPORTS" | \ echo "[$GROUP]" >> $INVENTORY_FILE
echo $DATA | \
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
"\(.hostname) ansible_host=\(.ip)"' | \
sed 's/"//g' \
>> $INVENTORY_FILE
# For this group, collect expressions into VARS. The format is:
# HOSTNAME1=0.0.0.0
# HOSTNAME2=0.0.0.0
VARS="$(
echo $DATA | \
jq '.[] | select(.group=="'"$GROUP"'") | .vms[] |
"\(.hostname)=\(.ip)"' | \
sed 's/"//g' | \ sed 's/"//g' | \
sed 's/^.*=//g' | \ sed "s/$VM_NAME_PREFIX-//g" | \
sed -z 's/\n/,/g;s/,$/\n/g')" sed 's/-/_/g'
)"
# Create an inventory file for ansible. # Print the contents of $VARS converted to uppercase.
echo "[k8s_nodes]" > inventory echo "${VARS^^}"
echo $VM_IP_EXPORTS | \ done
sed 's/"//g' | \
sed 's/export //g' | \
sed 's/ /\n/g' | \
sed 's/^\(.*\)\(=.*\)$/\1 ansible_host\2/g' \
>> inventory
echo $VM_IP_EXPORTS | sed 's/" /"\n/g'
echo export ANSIBLE_INV=$ANSIBLE_INV
View File
+65 -208
View File
@@ -1,85 +1,31 @@
terraform {
required_version = ">= 0.13" locals {
required_providers { nodes-config = {
libvirt = { "master" = {
source = "dmacvicar/libvirt" base-image = var.centos8-ami
version = "0.6.11" num = 1
},
"worker" = {
base-image = var.centos8-ami
num = 2
} }
} }
install-qemu-agent = false
} }
################################################################################ ################################################################################
# cloud-init # cloud-init
################################################################################ ################################################################################
# data "template_file" "master-node-user-datas" { module "cloud-init-config" {
# template = file("${path.module}/cloud_init.cfg") for_each = local.nodes-config
# vars = { source = "./modules/cloud-init-config"
# admin-passwd = "${var.root-admin-passwd}" cloud-init-template = "${path.module}/cloud_init.cfg"
# admin-pub-key = "${var.root-admin-pub-key}" hostname-prefix = "${var.vm-name-prefix}-${each.key}"
# hostname = "${var.vm-name-prefix}-master-${count.index}" num = each.value.num
# } root-admin-passwd = var.root-admin-passwd
# count = var.master-nodes root-admin-pub-key = var.root-admin-pub-key
# } install-qemu-agent = local.install-qemu-agent
#
# data "template_file" "worker-node-user-datas" {
# template = file("${path.module}/cloud_init.cfg")
# vars = {
# admin-passwd = "${var.root-admin-passwd}"
# admin-pub-key = "${var.root-admin-pub-key}"
# hostname = "${var.vm-name-prefix}-worker-${count.index}"
# }
# count = var.worker-nodes
# }
data "template_file" "amzn2-node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.vm-name-prefix}-amzn2-${count.index}"
}
count = 1
}
data "template_file" "ubuntu-node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.vm-name-prefix}-ubuntu-${count.index}"
}
count = 1
}
data "template_file" "arch-node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.vm-name-prefix}-arch-${count.index}"
}
count = 1
}
data "template_file" "centos7-node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.vm-name-prefix}-centos7-${count.index}"
}
count = 1
}
data "template_file" "centos8-node-user-datas" {
template = file("${path.module}/cloud_init.cfg")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.vm-name-prefix}-centos8-${count.index}"
}
count = 1
} }
################################################################################ ################################################################################
@@ -88,21 +34,35 @@ data "template_file" "centos8-node-user-datas" {
# libvirt modules/resources. # libvirt modules/resources.
################################################################################ ################################################################################
provider "aws" { # This module will grab the latest ami for a variety of distros. Uncomment to
region = "us-east-2" # get a list of the latest AMIs for our supported distros.
# module "aws-amis" {
# source = "./modules/aws-amis"
# }
# output "amis" {
# value = module.aws-amis.amis
# }
################################################################################
# AWS Networking
# Use of the 2 modules below to create resources for the AWS network.
# aws-network-from-scratch will build the AWS network from scratch.
# aws-network-existing will query AWS for an existing VPC.
################################################################################
# module "aws-network-from-scratch" {
# source = "./modules/aws-network-from-scratch"
# name-prefix = var.vm-name-prefix
# vpc-cidr-block = var.aws-vpc-cidr-block
# subnet-cidr-block = var.aws-subnet-cidr-block
# admin-ips = var.admin-ips
# }
module "aws-network-existing" {
source = "./modules/aws-network-existing"
} }
module "aws-amis" { ################################################################################
source = "./modules/aws-amis"
}
module "aws-network" {
source = "./modules/aws-network"
name-prefix = var.vm-name-prefix
vpc-cidr-block = var.aws-vpc-cidr-block
subnet-cidr-block = var.aws-subnet-cidr-block
admin-ips = var.admin-ips
}
# This key pair is not actually used. Keys are added to the nodes via cloud-init # This key pair is not actually used. Keys are added to the nodes via cloud-init
# instead. We just add this here that this key will show up in the AWS console." # instead. We just add this here that this key will show up in the AWS console."
@@ -114,87 +74,18 @@ resource "aws_key_pair" "key" {
} }
} }
module "amzn2-nodes" { module "nodes" {
for_each = local.nodes-config
source = "./modules/aws-nodes" source = "./modules/aws-nodes"
ami = var.base-image ami = each.value.base-image
ec2-instance-type = var.aws-ec2-instance-type ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id] security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.amzn2-node-user-datas user-datas = lookup(module.cloud-init-config, each.key, null).user-datas
num-nodes = 1 num-nodes = each.value.num
name-prefix = "${var.vm-name-prefix}-amzn2" name-prefix = "${var.vm-name-prefix}-${each.key}"
} }
module "ubuntu-nodes" {
source = "./modules/aws-nodes"
ami = "ami-0629230e074c580f2"
ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.ubuntu-node-user-datas
num-nodes = 1
name-prefix = "${var.vm-name-prefix}-ubuntu"
}
module "arch-nodes" {
source = "./modules/aws-nodes"
ami = "ami-02653f06de985e3ba"
ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.ubuntu-node-user-datas
num-nodes = 1
name-prefix = "${var.vm-name-prefix}-arch"
}
module "centos7-nodes" {
source = "./modules/aws-nodes"
ami = "ami-00f8e2c955f7ffa9b"
ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.ubuntu-node-user-datas
num-nodes = 1
name-prefix = "${var.vm-name-prefix}-centos7"
}
module "centos8-nodes" {
source = "./modules/aws-nodes"
ami = "ami-057cacbfbbb471bb3"
ec2-instance-type = var.aws-ec2-instance-type
subnet-id = module.aws-network.subnet.id
security-group-ids = [module.aws-network.default-security-group.id]
user-datas = data.template_file.ubuntu-node-user-datas
num-nodes = 1
name-prefix = "${var.vm-name-prefix}-centos8"
}
output "amis" {
value = module.aws-amis.amis
}
# module "master-nodes" {
# source = "./modules/aws-nodes"
# ami = var.base-image
# ec2-instance-type = var.aws-ec2-instance-type
# subnet-id = module.aws-network.subnet.id
# security-group-ids = [module.aws-network.default-security-group.id]
# user-datas = data.template_file.master-node-user-datas
# num-nodes = var.master-nodes
# name-prefix = "${var.vm-name-prefix}-master"
# }
#
# module "worker-nodes" {
# source = "./modules/aws-nodes"
# ami = var.base-image
# ec2-instance-type = var.aws-ec2-instance-type
# subnet-id = module.aws-network.subnet.id
# security-group-ids = [module.aws-network.default-security-group.id]
# user-datas = data.template_file.worker-node-user-datas
# num-nodes = var.worker-nodes
# name-prefix = "${var.vm-name-prefix}-worker"
# }
################################################################################ ################################################################################
# end aws # end aws
################################################################################ ################################################################################
@@ -209,32 +100,21 @@ output "amis" {
# uri = var.libvirt-connection-url # uri = var.libvirt-connection-url
# } # }
# #
# module "master-nodes" { # module "nodes" {
# for_each = local.nodes-config
# source = "./modules/libvirt-nodes" # source = "./modules/libvirt-nodes"
# pool-name = libvirt_pool.images.name # pool-name = libvirt_pool.images.name
# name-prefix = "${var.vm-name-prefix}-master" # name-prefix = "${var.vm-name-prefix}-${each.key}"
# num-nodes = var.master-nodes # num-nodes = each.value.num
# node-memory = var.node-memory # node-memory = var.node-memory
# node-vcpus = var.node-vcpus # node-vcpus = var.node-vcpus
# base-image = var.base-image # node-disk-size = var.libvirt-node-disk-size
# base-image = each.value.base-image
# network-name = var.libvirt-network-name
# root-admin-passwd = var.root-admin-passwd # root-admin-passwd = var.root-admin-passwd
# root-admin-pub-key = var.root-admin-pub-key # root-admin-pub-key = var.root-admin-pub-key
# libvirt-connection-url = var.libvirt-connection-url # libvirt-connection-url = var.libvirt-connection-url
# user-datas = data.template_file.master-node-user-datas # user-datas = lookup(module.cloud-init-config, each.key, null).user-datas
# }
#
# module "worker-nodes" {
# source = "./modules/libvirt-nodes"
# pool-name = libvirt_pool.images.name
# name-prefix = "${var.vm-name-prefix}-worker"
# num-nodes = var.worker-nodes
# node-memory = var.node-memory
# node-vcpus = var.node-vcpus
# base-image = var.base-image
# root-admin-passwd = var.root-admin-passwd
# root-admin-pub-key = var.root-admin-pub-key
# libvirt-connection-url = var.libvirt-connection-url
# user-datas = data.template_file.worker-node-user-datas
# } # }
# #
# resource "libvirt_pool" "images" { # resource "libvirt_pool" "images" {
@@ -247,31 +127,8 @@ output "amis" {
# end libvirt # end libvirt
################################################################################ ################################################################################
output "amzn2-ips" { # This will output a map of group => [{hostname, ip}].
value = module.amzn2-nodes.ips # TODO A 'names' output needs to be added to libvirt.
output "groups_hostnames_ips" {
value = { for type, node in module.nodes : type => zipmap(node.names, node.ips) }
} }
output "ubuntu-ips" {
value = module.ubuntu-nodes.ips
}
output "arch-ips" {
value = module.arch-nodes.ips
}
output "centos7-ips" {
value = module.centos7-nodes.ips
}
output "centos8-ips" {
value = module.centos8-nodes.ips
}
# TODO REM move to other file?
# output "master-ips" {
# value = module.master-nodes.ips
# }
#
# output "worker-ips" {
# value = module.worker-nodes.ips
# }
+23
View File
@@ -0,0 +1,23 @@
locals {
az-to-subnets = {
for s in data.aws_subnet.subnets : s.availability_zone => s.id...
}
}
data "aws_vpc" "default" {
tags = {
Name = var.default-vpc-name
}
}
data "aws_subnets" "subnet-ids" {
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}
data "aws_subnet" "subnets" {
for_each = toset(data.aws_subnets.subnet-ids.ids)
id = each.key
}
+18
View File
@@ -0,0 +1,18 @@
output "default-vpc" {
value = data.aws_vpc.default
}
output "subnets" {
description = "An array of all subnets in default-vpc."
value = data.aws_subnet.subnets
}
output "k8s-subnets" {
description = "An array of subnets to be used for k8s VMs. These subnets were chosen by selecting a single subnet from each availability_zone."
value = [for k,v in local.az-to-subnets : v[0]]
}
output "az-to-subnets" {
description = "A map of availability zone to array of subnets that are in thet availability zone."
value = local.az-to-subnets
}
@@ -0,0 +1,4 @@
variable "default-vpc-name" {
description = "The name of the existing default VPC. This module will query AWS for a VPC with this name,"
default = "Managed VPC"
}
@@ -33,7 +33,7 @@ resource "aws_default_security_group" "sg" {
} }
tags = { tags = {
Name = "${var.name-prefix}-ssh-from-admins--sg" Name = "${var.name-prefix}-ssh-from-admins-sg"
} }
} }
+1
View File
@@ -1,6 +1,7 @@
resource "aws_instance" "nodes" { resource "aws_instance" "nodes" {
ami = var.ami ami = var.ami
instance_type = var.ec2-instance-type instance_type = var.ec2-instance-type
# TODO REM double check this key.
# key_name = aws_key_pair.debug1.key_name # key_name = aws_key_pair.debug1.key_name
associate_public_ip_address = true associate_public_ip_address = true
subnet_id = var.subnet-id subnet_id = var.subnet-id
+4
View File
@@ -1,3 +1,7 @@
output "ips" { output "ips" {
value = aws_instance.nodes.*.public_ip value = aws_instance.nodes.*.public_ip
} }
output "names" {
value = aws_instance.nodes.*.tags.Name
}
+10
View File
@@ -0,0 +1,10 @@
data "template_file" "user-datas" {
template = file("${var.cloud-init-template}")
vars = {
admin-passwd = "${var.root-admin-passwd}"
admin-pub-key = "${var.root-admin-pub-key}"
hostname = "${var.hostname-prefix}-${count.index}"
install-qemu-agent = var.install-qemu-agent
}
count = var.num
}
+3
View File
@@ -0,0 +1,3 @@
output "user-datas" {
value = data.template_file.user-datas
}
+28
View File
@@ -0,0 +1,28 @@
variable "cloud-init-template" {
default = "../../cloud_init.cfg"
description = "The path to the cloud-init config template."
type = string
}
variable "hostname-prefix" {
description = "This prefix wil be applied as a prefix for the hostnames."
}
variable "install-qemu-agent" {
default = false
description = "This flag determines whether or not qemu-agent is installed."
type = bool
}
variable "num" {
description = "The number of user-datas to create with these parameters."
}
variable "root-admin-passwd" {
description = "This value will be substituted for any occurence of 'admin-password' in the cloud-init config template."
}
variable "root-admin-pub-key" {
description = "This value will be substituted for any occurence of 'admin-pub-key' in the cloud-init config template."
}
+12 -9
View File
@@ -8,18 +8,21 @@ terraform {
} }
} }
provider "libvirt" {
uri = var.libvirt-connection-url
}
resource "libvirt_volume" "node-images" { resource "libvirt_volume" "node-images" {
name = "${var.name-prefix}-${count.index}" name = "${var.name-prefix}-base"
pool = var.pool-name pool = var.pool-name
source = var.base-image source = var.base-image
count = var.num-nodes
format = "qcow2" format = "qcow2"
} }
resource "libvirt_volume" "node-images-resized" {
name = "${var.name-prefix}-${count.index}-resized"
pool = var.pool-name
base_volume_id = libvirt_volume.node-images.id
count = var.num-nodes
size = var.node-disk-size
}
data "template_file" "network-config" { data "template_file" "network-config" {
template = file("${path.module}/network_config.cfg") template = file("${path.module}/network_config.cfg")
} }
@@ -41,9 +44,9 @@ resource "libvirt_domain" "nodes" {
cloudinit = element(libvirt_cloudinit_disk.node-inits.*.id, count.index) cloudinit = element(libvirt_cloudinit_disk.node-inits.*.id, count.index)
network_interface { network_interface {
network_name = "default" network_name = var.network-name
hostname = "${var.name-prefix}-${count.index}" hostname = "${var.name-prefix}-${count.index}"
wait_for_lease = true # wait_for_lease = true
} }
# IMPORTANT: this is a known bug on cloud images, since they expect a console # IMPORTANT: this is a known bug on cloud images, since they expect a console
@@ -62,7 +65,7 @@ resource "libvirt_domain" "nodes" {
} }
disk { disk {
volume_id = element(libvirt_volume.node-images.*.id, count.index) volume_id = element(libvirt_volume.node-images-resized.*.id, count.index)
} }
graphics { graphics {
+5 -3
View File
@@ -1,4 +1,6 @@
output "ips" { # This only works on the default network. They will not work using the bridged
value = libvirt_domain.nodes.*.network_interface.0.addresses.0 # network.
} # output "ips" {
# value = libvirt_domain.nodes.*.network_interface.0.addresses.0
# }
+10
View File
@@ -12,6 +12,16 @@ variable "name-prefix" {
description = "This will be a prefix for all resource names, ie. domains will be created suck as \"k8s-node-2\"." description = "This will be a prefix for all resource names, ie. domains will be created suck as \"k8s-node-2\"."
} }
variable "network-name" {
default = "default"
description = "The name of a pre-existing virtual-network."
}
variable "node-disk-size" {
default = 4294967296
description = "The size of the disk to be used for libvirt nodes. (in bytes)"
}
variable "node-memory" { variable "node-memory" {
default = "2048" default = "2048"
description = "The amount of memory to be used for all the nodes." description = "The amount of memory to be used for all the nodes."
+76
View File
@@ -0,0 +1,76 @@
provider "aws" {
region = "us-gov-west-1"
}
terraform {
required_version = ">= 1.0.8"
backend "s3" {
bucket = "mss-terraform-state"
key = "global/s3/terraform.tfstate"
region = "us-gov-west-1"
dynamodb_table = "mss-terraform-state-lock"
encrypt = true
}
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.6.11"
}
}
}
##
#S3 bucket create to hold our TFState file so we can all share env settings
resource "aws_s3_bucket" "terraform_state" {
bucket = "mss-terraform-state"
# enable versioning for the state files
versioning {
enabled = true
}
#enable server-side encryption
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
##
# no sql database used so that we can lock the TFstate file in the S3 bucket to ensure two people
# are not running a terraform command at the same time
resource "aws_dynamodb_table" "terraform_locks" {
name = "mss-terraform-state-lock"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
##
# output variable to give details on the s3 bucket created
#TODO: move to output.tf
output "s3_bucket_arn" {
value = aws_s3_bucket.terraform_state.arn
description = "The ARN of the S3 bucket"
}
+73
View File
@@ -8,6 +8,11 @@ variable "aws-ec2-instance-type" {
description = "The AWS instance type to use for all nodes." description = "The AWS instance type to use for all nodes."
} }
variable "aws-region" {
default = "us-east-1"
description = "The AWS region to use."
}
variable "aws-subnet-cidr-block" { variable "aws-subnet-cidr-block" {
default = "10.0.1.0/24" default = "10.0.1.0/24"
description = "The address space to be used for this subnet." description = "The address space to be used for this subnet."
@@ -31,6 +36,16 @@ variable "libvirt-connection-url" {
description = "The libvirt connection URI, ie. qemu+ssh://<user>@<host>/system" description = "The libvirt connection URI, ie. qemu+ssh://<user>@<host>/system"
} }
variable "libvirt-network-name" {
default = "default"
description = "The name of a pre-existing libvirt virtual-network."
}
variable "libvirt-node-disk-size" {
default = 4294967296
description = "The size of the disk to be used for libvirt nodes. (in bytes)"
}
variable "node-memory" { variable "node-memory" {
default = "2048" default = "2048"
description = "The amount of memory to be used for all the nodes." description = "The amount of memory to be used for all the nodes."
@@ -71,3 +86,61 @@ variable "vm-name-prefix" {
default = "k8s-tf" default = "k8s-tf"
description = "This prefix will appear before all VM names and hostnames, ie. k8s-tf-master-0." description = "This prefix will appear before all VM names and hostnames, ie. k8s-tf-master-0."
} }
################################################################################
# AWS AMI vars
# These variables are really mor like constants. Using variables improves
# readability. The defaults are manually updated. Use the aws-amis module to get
# the latest for each distro.
################################################################################
variable "amzn2-ami" {
default = "ami-0dd0ccab7e2801812"
description = "The AMI to use for Amazon Linux 2."
}
variable "ubuntu-ami" {
default = "ami-06c7d6c0987eaa46c"
description = "The AMI to use for Ubuntu."
}
variable "centos7-ami" {
default = "ami-00f8e2c955f7ffa9b"
description = "The AMI to use for CentOS 7."
}
variable "centos8-ami" {
default = "ami-057cacbfbbb471bb3"
description = "The AMI to use for CentOS 8."
}
variable "arch-ami" {
default = "ami-02653f06de985e3ba"
description = "The AMI to use for Arch Linux."
}
variable "rhel7-ami" {
default = "ami-0a509b3c2a4d05b3f"
description = "The AMI to use for RHEL 7."
}
variable "rhel8-ami" {
default = "ami-0d871ca8a77af2948"
description = "The AMI to use for RHEL 8."
}
################################################################################
# Libvirt Images
# These variables are really mor like constants. Using variables improves
# readability. The defaults are manually updated.
################################################################################
variable "ubuntu-img" {
default = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
description = "The libvirt image tp use for Ubuntu."
}
variable "centos7-img" {
# Latest as of 2021-12-06.
default = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2111.qcow2"
description = "The libvirt image tp use for CentOS 7."
}
variable "centos8-img" {
default = "https://cloud.centos.org/centos/8/x86_64/images/CentOS-8-GenericCloud-8.4.2105-20210603.0.x86_64.qcow2"
description = "The libvirt image tp use for CentOS 8."
}