Skip to content

Commit

Permalink
Merge remote-tracking branch 'etienne/hieradata_node'
Browse files Browse the repository at this point in the history
  • Loading branch information
mboisson committed Feb 27, 2024
2 parents 3c88855 + 2746abf commit d63072b
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 47 deletions.
19 changes: 10 additions & 9 deletions aws/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ module "configuration" {
}

module "provision" {
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
sudoer_username = var.sudoer_username
depends_on = [aws_instance.instances, aws_eip.public_ip]
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
hieradata_folder = var.hieradata_folder_path
sudoer_username = var.sudoer_username
depends_on = [aws_instance.instances, aws_eip.public_ip]
}

data "aws_availability_zones" "available" {
Expand Down
19 changes: 10 additions & 9 deletions azure/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ module "configuration" {
}

module "provision" {
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
sudoer_username = var.sudoer_username
depends_on = [ azurerm_linux_virtual_machine.instances ]
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
hieradata_folder = var.hieradata_folder_path
sudoer_username = var.sudoer_username
depends_on = [ azurerm_linux_virtual_machine.instances ]
}


Expand Down
5 changes: 3 additions & 2 deletions common/configuration/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ locals {
})

terraform_facts = yamlencode({
software_stack = var.software_stack
software_stack = var.software_stack,
cloud = {
provider = var.cloud_provider
region = var.cloud_region
Expand All @@ -98,6 +98,7 @@ locals {
cloud_provider = var.cloud_provider
tags = values.tags
node_name = key,
node_prefix = values.prefix,
domain_name = var.domain_name
puppetenv_git = var.config_git_url,
puppetenv_rev = var.config_version,
Expand Down Expand Up @@ -165,4 +166,4 @@ output "bastions" {
for host, values in var.inventory: host => values
if contains(values.tags, var.bastion_tag) && contains(values.tags, "public") && (!contains(values.tags, "pool"))
}
}
}
10 changes: 10 additions & 0 deletions common/configuration/puppet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ runcmd:
- chgrp puppet /etc/puppetlabs/data /etc/puppetlabs/facts
- ln -sf /etc/puppetlabs/data/terraform_data.yaml /etc/puppetlabs/code/environments/production/data/
- ln -sf /etc/puppetlabs/data/user_data.yaml /etc/puppetlabs/code/environments/production/data/
- ln -sf /etc/puppetlabs/data/hieradata /etc/puppetlabs/code/environments/production/data/
- ln -sf /etc/puppetlabs/facts/terraform_facts.yaml /etc/puppetlabs/code/environments/production/site/profile/facts.d
# We use r10k solely to install the modules of the main branch environment.
- "(cd /etc/puppetlabs/code/environments/production; /opt/puppetlabs/puppet/bin/r10k puppetfile install)"
Expand Down Expand Up @@ -139,8 +140,17 @@ write_files:
%{ if cloud_provider != "gcp" } "GCE", %{ endif }
],
}
global : {
external-dir : ["/etc/puppetlabs/facts/external"],
}
path: /etc/puppetlabs/facter/facter.conf
permissions: "0644"
- path: /etc/puppetlabs/facts/external/prefix.yaml
content: |
---
prefix : "${node_prefix}"
permissions: "0640"
%{ if contains(tags, "puppet") ~}
- content: |
---
Expand Down
2 changes: 1 addition & 1 deletion common/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ output "accounts" {
output "ssh_private_key" {
value = module.configuration.ssh_key.private
sensitive = true
}
}
108 changes: 101 additions & 7 deletions common/provision/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,93 @@ variable "bastions" { }
variable "puppetservers" { }
variable "terraform_data" { }
variable "terraform_facts" { }
variable "hieradata_folder" { }
variable "hieradata" { }
variable "sudoer_username" { }
variable "tf_ssh_key" { }

resource "terraform_data" "deploy_hieradata" {
for_each = length(var.bastions) > 0 ? var.puppetservers : { }
data "local_file" "hieradata_yaml" {
for_each = var.hieradata_folder != "" ? fileset("${var.hieradata_folder}", "*.yaml") : []
filename = "${var.hieradata_folder}/${each.value}"
}

connection {
type = "ssh"
data "local_file" "hieradata_subfolder" {
for_each = var.hieradata_folder != "" ? fileset("${var.hieradata_folder}", "{prefix,hostname}/**/*.yaml") : []
filename = "${var.hieradata_folder}/${each.value}"
}

locals {
connection_parameters = length(var.bastions) > 0 ? {
bastion_host = var.bastions[keys(var.bastions)[0]].public_ip
bastion_user = var.sudoer_username
bastion_private_key = var.tf_ssh_key.private
user = var.sudoer_username
host = each.value
private_key = var.tf_ssh_key.private
} : null

hieradata_md5 = merge(
{for value in data.local_file.hieradata_yaml: replace(value.filename, var.hieradata_folder, "") => value.content_md5},
{for value in data.local_file.hieradata_subfolder: replace(value.filename, var.hieradata_folder, "") => value.content_md5},
)
triggers_hieradata_folder = {
hieradata_yaml = local.hieradata_md5
}

triggers_replace = {
triggers_hieradata = {
user_data = md5(var.hieradata)
terraform_data = md5(var.terraform_data)
facts = md5(var.terraform_facts)
}
}

resource "terraform_data" "deploy_hieradata_folder" {
for_each = local.connection_parameters != null && length(local.hieradata_md5) > 0 ? var.puppetservers : { }

connection {
type = "ssh"
host = each.value
bastion_host = local.connection_parameters["bastion_host"]
bastion_user = local.connection_parameters["bastion_user"]
bastion_private_key = local.connection_parameters["bastion_private_key"]
user = local.connection_parameters["user"]
private_key = local.connection_parameters["private_key"]
}

triggers_replace = local.triggers_hieradata_folder

provisioner "file" {
source = "${path.cwd}/hieradata"
destination = "hieradata"
}

provisioner "remote-exec" {
inline = [
# clean up
"sudo rm -rf /etc/puppetlabs/data/hieradata || true",
"sudo mkdir -p /etc/puppetlabs/data",
# puppet user and group have been assigned the reserved UID/GID 52
"sudo cp -r hieradata /etc/puppetlabs/data/",
"sudo chown -R root:52 /etc/puppetlabs/data/hieradata",
"sudo chmod -R 650 /etc/puppetlabs/data/hieradata",
"rm -rf hieradata",
]
}
}

resource "terraform_data" "deploy_hieradata" {
for_each = local.connection_parameters != null ? var.puppetservers : { }

connection {
type = "ssh"
host = each.value
bastion_host = local.connection_parameters["bastion_host"]
bastion_user = local.connection_parameters["bastion_user"]
bastion_private_key = local.connection_parameters["bastion_private_key"]
user = local.connection_parameters["user"]
private_key = local.connection_parameters["private_key"]
}

triggers_replace = local.triggers_hieradata

provisioner "file" {
content = var.terraform_data
Expand All @@ -47,7 +112,36 @@ resource "terraform_data" "deploy_hieradata" {
"sudo install -o root -g 52 -m 640 terraform_data.yaml user_data.yaml /etc/puppetlabs/data/",
"sudo install -o root -g 52 -m 640 terraform_facts.yaml /etc/puppetlabs/facts/",
"rm -f terraform_data.yaml user_data.yaml terraform_facts.yaml",
"[ -f /usr/local/bin/consul ] && [ -f /usr/bin/jq ] && consul event -token=$(sudo jq -r .acl.tokens.agent /etc/consul/config.json) -name=puppet $(date +%s) || true",
]
}
}

resource "terraform_data" "update_consul" {
for_each = local.connection_parameters != null ? var.puppetservers : { }

connection {
type = "ssh"
host = each.value
bastion_host = local.connection_parameters["bastion_host"]
bastion_user = local.connection_parameters["bastion_user"]
bastion_private_key = local.connection_parameters["bastion_private_key"]
user = local.connection_parameters["user"]
private_key = local.connection_parameters["private_key"]
}

triggers_replace = merge(
local.triggers_hieradata,
local.triggers_hieradata_folder,
)

depends_on = [
terraform_data.deploy_hieradata,
terraform_data.deploy_hieradata_folder,
]

provisioner "remote-exec" {
inline = [
"[ -f /usr/local/bin/consul ] && [ -f /usr/bin/jq ] && consul event -token=$(sudo jq -r .acl.tokens.agent /etc/consul/config.json) -name=puppet $(date +%s) || true"
]
}
}
6 changes: 6 additions & 0 deletions common/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ variable "hieradata" {
}
}

variable "hieradata_folder_path" {
type = string
default = ""
description = "Path to hieradata folder containing YAML files to be included in the puppet environment"
}

variable "sudoer_username" {
type = string
default = "centos"
Expand Down
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ the provided content will replace entirely the Magic Castle environment's
**Post build modification effect**: None. To modify the Puppetfile after the cluster is initialized, log
on the Puppet server and modify `/etc/puppetlabs/code/environments/production/Puppetfile`.
### 4.20 hieradata_folder_path (optional)
**default value**: empty string
## 5. Cloud Specific Configuration
### 5.1 Amazon Web Services
Expand Down
19 changes: 10 additions & 9 deletions gcp/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ module "configuration" {
}

module "provision" {
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
sudoer_username = var.sudoer_username
depends_on = [ google_compute_instance.instances ]
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
hieradata_folder = var.hieradata_folder_path
sudoer_username = var.sudoer_username
depends_on = [ google_compute_instance.instances ]
}


Expand Down
21 changes: 11 additions & 10 deletions openstack/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ module "configuration" {
}

module "provision" {
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
sudoer_username = var.sudoer_username
depends_on = [local.network_provision_dep]
source = "../common/provision"
bastions = module.configuration.bastions
puppetservers = module.configuration.puppetservers
tf_ssh_key = module.configuration.ssh_key
terraform_data = module.configuration.terraform_data
terraform_facts = module.configuration.terraform_facts
hieradata = var.hieradata
hieradata_folder = var.hieradata_folder_path
sudoer_username = var.sudoer_username
depends_on = [local.network_provision_dep]
}

data "openstack_images_image_v2" "image" {
Expand Down Expand Up @@ -137,4 +138,4 @@ locals {
host => merge(module.configuration.inventory[host], {id=openstack_compute_instance_v2.instances[host].id})
if contains(module.configuration.inventory[host].tags, "public")
}
}
}

0 comments on commit d63072b

Please sign in to comment.