Skip to content

Commit

Permalink
Merge pull request #306 from ComputeCanada/refactor_puppetfile
Browse files Browse the repository at this point in the history
Refactor puppetfile
  • Loading branch information
cmd-ntrf authored May 13, 2024
2 parents 4e67e4c + 3a33b01 commit 8e8f9bc
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 30 deletions.
1 change: 1 addition & 0 deletions aws/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module "provision" {
hieradata_dir = var.hieradata_dir
sudoer_username = var.sudoer_username
eyaml_key = var.eyaml_key
puppetfile = var.puppetfile
depends_on = [aws_instance.instances, aws_eip.public_ip]
}

Expand Down
1 change: 1 addition & 0 deletions azure/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module "provision" {
hieradata_dir = var.hieradata_dir
sudoer_username = var.sudoer_username
eyaml_key = var.eyaml_key
puppetfile = var.puppetfile
depends_on = [ azurerm_linux_virtual_machine.instances ]
}

Expand Down
17 changes: 6 additions & 11 deletions common/configuration/puppet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,15 @@ runcmd:
- git clone ${puppetenv_git} /etc/puppetlabs/code/environments/main
- ln -s /etc/puppetlabs/code/environments/main /etc/puppetlabs/code/environments/production
- "(cd /etc/puppetlabs/code/environments/production; git checkout ${puppetenv_rev})"
%{ if puppetfile != "" ~}
%{ if strcontains(puppetfile, "forge") ~}
- cat /etc/puppetlabs/Puppetfile > /etc/puppetlabs/code/environments/production/Puppetfile
%{ else }
- cat /etc/puppetlabs/Puppetfile >> /etc/puppetlabs/code/environments/production/Puppetfile
%{ endif ~}
- rm /etc/puppetlabs/Puppetfile
%{ endif ~}
- mkdir -p /etc/puppetlabs/data /etc/puppetlabs/facts
- chgrp -R puppet /etc/puppetlabs/data /etc/puppetlabs/facts
- ln -sf /etc/puppetlabs/data/{user_data,user_data.yaml,terraform_data.yaml} /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)"
- /opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/environments/production/modules --puppetfile=/etc/puppetlabs/code/environments/production/Puppetfile
%{ if puppetfile != "" ~}
- /opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/modules --puppetfile=/etc/puppetlabs/code/Puppetfile
%{ endif ~}
# Wait for Terraform to scp its YAML data
%{ if ! include_tf_data ~}
- while [ ! -e "/etc/puppetlabs/data/terraform_data.yaml" ]; do echo "$(date -I'seconds') Waiting for terraform to scp terraform_data.yaml"; sleep 5; done
Expand Down Expand Up @@ -172,10 +167,10 @@ write_files:
permissions: "0640"
%{ endif ~}
%{ if puppetfile != "" ~}
- path: /etc/puppetlabs/Puppetfile
- path: /etc/puppetlabs/code/Puppetfile
content: |
${indent(6, puppetfile)}
permissions: "0640"
permissions: "0644"
%{ endif ~}
%{ endif ~}
- content: |
Expand Down
28 changes: 18 additions & 10 deletions common/provision/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ variable "hieradata_dir" { }
variable "sudoer_username" { }
variable "tf_ssh_key" { }
variable "eyaml_key" { }
variable "puppetfile" { }

locals {
provision_folder = "puppetserver_etc"
provision_folder = "etc_puppetlabs"
}

data "archive_file" "puppetserver_files" {
Expand Down Expand Up @@ -47,6 +48,15 @@ data "archive_file" "puppetserver_files" {
filename = "${local.provision_folder}/puppet/eyaml/private_key.pkcs7.pem"
}
}

dynamic "source" {
for_each = var.puppetfile != "" ? [var.puppetfile]: []
iterator = filename
content {
content = var.puppetfile
filename = "${local.provision_folder}/code/Puppetfile"
}
}
}

resource "terraform_data" "deploy_puppetserver_files" {
Expand All @@ -71,17 +81,15 @@ resource "terraform_data" "deploy_puppetserver_files" {
destination = "${local.provision_folder}.zip"
}

provisioner "file" {
content = file("${path.module}/update_etc_puppetlabs.sh")
destination = "update_etc_puppetlabs.sh"
}

provisioner "remote-exec" {
inline = [
# unzip is not necessarily installed when connecting, but python is.
"/usr/libexec/platform-python -c 'import zipfile; zipfile.ZipFile(\"${local.provision_folder}.zip\").extractall()'",
"sudo chmod g-w,o-rwx $(find ${local.provision_folder}/ -type f)",
"sudo chown -R root:52 ${local.provision_folder}",
"sudo mkdir -p -m 755 /etc/puppetlabs/",
"sudo rsync -avh --no-t --exclude 'data' ${local.provision_folder}/ /etc/puppetlabs/",
"sudo rsync -avh --no-t --del ${local.provision_folder}/data/ /etc/puppetlabs/data/",
"sudo rm -rf ${local.provision_folder}/ ${local.provision_folder}.zip",
"[ -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",
"sudo bash update_etc_puppetlabs.sh ${local.provision_folder}.zip",
"rm ${local.provision_folder}.zip update_etc_puppetlabs.sh"
]
}
}
23 changes: 23 additions & 0 deletions common/provision/update_etc_puppetlabs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

ZIP_FILE=${1}
ZIP_DIR=${ZIP_FILE%.zip}

# unzip is not necessarily installed when connecting, but python is.
/usr/libexec/platform-python -c "import zipfile; zipfile.ZipFile('${ZIP_FILE}').extractall()"

chmod g-w,o-rwx $(find ${ZIP_DIR}/ -type f ! -path ${ZIP_DIR}/code/*)
chown -R root:52 ${ZIP_DIR}
mkdir -p -m 755 /etc/puppetlabs/
rsync -avh --no-t --exclude 'data' ${ZIP_DIR}/ /etc/puppetlabs/
rsync -avh --no-t --del ${ZIP_DIR}/data/ /etc/puppetlabs/data/
rm -rf ${ZIP_DIR}/

if [ -f /opt/puppetlabs/puppet/bin/r10k ] && [ /etc/puppetlabs/code/Puppetfile -nt /etc/puppetlabs/code/modules ]; then
/opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/modules --puppetfile=/etc/puppetlabs/code/Puppetfile
touch /etc/puppetlabs/code/modules
fi

if [ -f /usr/local/bin/consul ] && [ -f /usr/bin/jq ]; then
consul event -token=$(sudo jq -r .acl.tokens.agent /etc/consul/config.json) -name=puppet $(date +%s)
fi
2 changes: 1 addition & 1 deletion common/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ variable "skip_upgrade" {
variable "puppetfile" {
type = string
default = ""
description = "Additional content for the pupet environment Puppetfile. If the string includes a `forge` setting, the string replaces the original Puppetfile completely."
description = "Additional Puppet modules to install in the Puppetfile format."
}

variable "eyaml_key" {
Expand Down
13 changes: 5 additions & 8 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -966,14 +966,11 @@ whether they should upgrade the base image packages or not.
**default_value** = `""`
Defines a complement of modules to install with librarian puppet when initializing the Puppet environment
on the first boot of the Puppet server. If the provided string include the
[`forge` setting](https://www.puppet.com/docs/pe/2019.8/puppetfile.html#declare_puppet_forge_modules_in_the_puppetfile),
the provided content will replace entirely the Magic Castle environment's
[Puppetfile](https://github.com/ComputeCanada/puppet-magic_castle/blob/main/Puppetfile).
**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`.
Defines a second [Puppetfile](https://www.puppet.com/docs/pe/2023.2/puppetfile.html) used to
install complementary modules with [r10k](https://github.com/puppetlabs/r10k).
**Post build modification effect**: trigger scp of Puppetfile at next `terraform apply`.
Each instance's Puppet agent will be reloaded following the installation of the new modules.
## 5. Cloud Specific Configuration
Expand Down
1 change: 1 addition & 0 deletions gcp/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module "provision" {
hieradata_dir = var.hieradata_dir
sudoer_username = var.sudoer_username
eyaml_key = var.eyaml_key
puppetfile = var.puppetfile
depends_on = [ google_compute_instance.instances ]
}

Expand Down
1 change: 1 addition & 0 deletions openstack/infrastructure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module "provision" {
hieradata_dir = var.hieradata_dir
sudoer_username = var.sudoer_username
eyaml_key = var.eyaml_key
puppetfile = var.puppetfile
depends_on = [
local.network_provision_dep,
openstack_compute_instance_v2.instances,
Expand Down

0 comments on commit 8e8f9bc

Please sign in to comment.