Skip to content

Commit 6474663

Browse files
authored
Merge pull request #424 from ComputeCanada/clean-cloudinit
Overall cleanup of cloudinit
2 parents 566bef5 + ead0e7e commit 6474663

8 files changed

Lines changed: 70 additions & 83 deletions

File tree

aws/infrastructure.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ module "configuration" {
3333
cloud_provider = local.cloud_provider
3434
cloud_region = local.cloud_region
3535
skip_upgrade = var.skip_upgrade
36-
puppetfile = var.puppetfile
3736
}
3837

3938
module "provision" {

azure/infrastructure.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ module "configuration" {
3333
cloud_provider = local.cloud_provider
3434
cloud_region = local.cloud_region
3535
skip_upgrade = var.skip_upgrade
36-
puppetfile = var.puppetfile
3736
}
3837

3938
module "provision" {

common/configuration/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ variable "guest_passwd" {}
2323
variable "public_keys" {}
2424

2525
variable "skip_upgrade" {}
26-
variable "puppetfile" {}
2726
variable "bastion_tags" {}
2827

2928
resource "tls_private_key" "ssh" {
@@ -84,6 +83,7 @@ locals {
8483
domain_name = var.domain_name
8584
guest_passwd = local.guest_passwd
8685
nb_users = var.nb_users
86+
bastion_tags = var.bastion_tags
8787
}
8888
}
8989
})
@@ -112,7 +112,8 @@ locals {
112112
tf_ssh_public_key = chomp(tls_private_key.ssh.public_key_openssh)
113113
terraform_facts = local.terraform_facts
114114
skip_upgrade = var.skip_upgrade
115-
puppetfile = var.puppetfile
115+
module_path = path.module
116+
user_tf_required = contains(values.tags, "puppet") || length(setintersection(values.tags, var.bastion_tags)) > 0
116117
hostkeys = {
117118
rsa = {
118119
private = chomp(tls_private_key.rsa[values.prefix].private_key_openssh)

common/configuration/puppet.yaml.tftpl

Lines changed: 45 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ mounts:
66
- [ ephemeral0, /mnt/ephemeral0, auto, "defaults,nofail" ]
77

88
users:
9-
%{ if contains(tags, "puppet") || length(setintersection(tags, bastion_tags)) > 0 ~}
9+
%{ if user_tf_required ~}
1010
- name: tf
1111
system: true
1212
passwd: '*'
1313
no_user_group: true
1414
homedir: /tmp
15-
%{ if contains(tags, "puppet") }
15+
%{ if contains(tags, "puppet") ~}
1616
sudo: "ALL = NOPASSWD: /usr/sbin/update_etc_puppetlabs.sh *.zip"
1717
%{ endif ~}
1818
%{ endif ~}
@@ -21,10 +21,10 @@ users:
2121
passwd: '*'
2222
homedir: /${sudoer_username}
2323
sudo: ALL=(ALL) NOPASSWD:ALL
24-
%{ if cloud_provider != "incus" }
24+
%{ if cloud_provider != "incus" ~}
2525
# most container images do not have selinux pre-installed
2626
selinux_user: unconfined_u
27-
%{ endif }
27+
%{ endif ~}
2828
ssh_authorized_keys:
2929
%{ for key in ssh_authorized_keys ~}
3030
- ${key}
@@ -35,18 +35,11 @@ runcmd:
3535
- trap 'flag_cloud_init_failed' ERR
3636
- chmod 755 /etc # avoid issue with Rocky 9.4
3737
- test ! -d /${sudoer_username} && userdel -f -r ${sudoer_username} && cloud-init clean -r
38-
%{ if cloud_provider != "incus" }
39-
# most container images do not have selinux pre-installed
38+
- blkid -L ephemeral0 &>/dev/null || sed -i -E '\;^[[:space:]]*ephemeral0[[:space:]]+/mnt/ephemeral0[[:space:]];s;^;#;' /etc/fstab
39+
%{ if cloud_provider != "incus" ~}
40+
# most container images do not have selinux pre-installed
4041
- restorecon -R /${sudoer_username}
41-
%{ endif }
42-
# Make sure puppet server can be reached by name early in the process if we need to debug.
43-
# For some provider, the ip address of the puppetserver is not known in advance, so we make
44-
# sure it is not empty before adding it to /etc/hosts
45-
%{ for host, ip in puppetservers ~}
46-
%{ if ip != "" ~}
47-
- echo "${ip} ${host}" >> /etc/hosts
4842
%{ endif ~}
49-
%{ endfor ~}
5043
# Install package and configure kernel only if building from a "vanilla" linux image
5144
- |
5245
if ! test -f /etc/magic-castle-release; then
@@ -84,9 +77,16 @@ runcmd:
8477
systemctl disable kdump
8578
grubby --update-kernel=ALL --args="rd.driver.blacklist=nouveau nouveau.modeset=0 crashkernel=0M initcall_blacklist=algif_aead_init"
8679
grub2-mkconfig -o /boot/grub2/grub.cfg
80+
%{ endif ~}
81+
%{ if cloud_provider == "gcp" ~}
82+
# Google Cloud user-data fact generates a warning because its size is greater than what is allowed (<4096 bytes).
83+
# We have no use for it, so we remove startup-script, user-data and user-data-encoding when running in GCE.
84+
sed -i "/gce_data\['instance'\] = instance_data/i \ \ \ \ \ \ \ \ \ \ instance_data['attributes'].delete('startup-script')" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/resolvers/gce.rb
85+
sed -i "/gce_data\['instance'\] = instance_data/i \ \ \ \ \ \ \ \ \ \ instance_data['attributes'].delete('user-data')" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/resolvers/gce.rb
86+
sed -i "/gce_data\['instance'\] = instance_data/i \ \ \ \ \ \ \ \ \ \ instance_data['attributes'].delete('user-data-encoding')" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/resolvers/gce.rb
8787
%{ endif ~}
8888
fi
89-
%{ if contains(tags, "puppet") }
89+
%{ if contains(tags, "puppet") ~}
9090
# Install puppetserver
9191
- dnf -y install openvox-server-8.11.0
9292
# Configure puppet-agent to start after puppetserver when on puppetserver
@@ -127,20 +127,13 @@ runcmd:
127127
- ln -sf /etc/puppetlabs/facts/terraform_facts.yaml /etc/puppetlabs/code/environments/production/site/profile/facts.d
128128
# We use r10k solely to install the modules of the production puppet environment.
129129
- /opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/environments/production/modules --puppetfile=/etc/puppetlabs/code/environments/production/Puppetfile
130-
%{ if puppetfile != "" ~}
131-
- /opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/modules --puppetfile=/etc/puppetlabs/code/Puppetfile
132-
%{ endif ~}
133-
# Backport stdlib patch to support Puppet 8 json parsing until we update to stdlib >= 8.5.0
134-
# Patch origin: https://github.com/puppetlabs/puppetlabs-stdlib/commit/53a8ccf869aa49de7a77c8a35d89dd5aac8fa52d.patch
135-
- sed -i -e 's/PSON/Puppet::Util::Json/' -e "/module Puppet/i require 'puppet/util/json'" /etc/puppetlabs/code/environments/production/modules/stdlib/lib/puppet/parser/functions/parsejson.rb
136-
# Wait for Terraform to scp its YAML data
137-
- while [ ! -e "/etc/puppetlabs/data/terraform_data.yaml" ]; do echo "$(date -I'seconds') Waiting for terraform to scp terraform_data.yaml"; sleep 5; done
138-
%{ if node_name != keys(puppetservers)[0] }
130+
- test -f /etc/puppetlabs/code/Puppetfile && /opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/modules --puppetfile=/etc/puppetlabs/code/Puppetfile
131+
%{ if node_name != keys(puppetservers)[0] ~}
139132
- sed -e '/certificate-authority-service/ s/^/#/' -i /etc/puppetlabs/puppetserver/services.d/ca.cfg
140133
- sed -e '/certificate-authority-disabled-service/ s/^#//' -i /etc/puppetlabs/puppetserver/services.d/ca.cfg
141-
%{ endif }
134+
%{ endif ~}
142135
- chgrp puppet /etc/puppetlabs/puppet/csr_attributes.yaml
143-
%{ endif }
136+
%{ endif ~}
144137
%{ if length(puppetservers) > 0 ~}
145138
- /opt/puppetlabs/bin/puppet config set server ${keys(puppetservers)[0]}
146139
%{ endif ~}
@@ -151,41 +144,44 @@ runcmd:
151144
- systemctl enable puppet
152145
# Remove all ifcfg configuration files that have no corresponding network interface in ip link show.
153146
- for i in /etc/sysconfig/network-scripts/ifcfg-*; do if ! ip link show | grep -q "$${i##*-}:"; then rm -f $i; fi; done
154-
%{ if cloud_provider == "gcp" }
155-
# Google Cloud user-data fact generates a warning because its size is greater than what is allowed (<4096 bytes).
156-
# We have no use for it, so we remove startup-script, user-data and user-data-encoding when running in GCE.
157-
- sed -i "/gce_data\['instance'\] = instance_data/i \ \ \ \ \ \ \ \ \ \ instance_data['attributes'].delete('startup-script')" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/resolvers/gce.rb
158-
- sed -i "/gce_data\['instance'\] = instance_data/i \ \ \ \ \ \ \ \ \ \ instance_data['attributes'].delete('user-data')" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/resolvers/gce.rb
159-
- sed -i "/gce_data\['instance'\] = instance_data/i \ \ \ \ \ \ \ \ \ \ instance_data['attributes'].delete('user-data-encoding')" /opt/puppetlabs/puppet/lib/ruby/vendor_ruby/facter/resolvers/gce.rb
160-
%{ endif }
161-
%{ if contains(tags, "puppet") }
147+
%{ if contains(tags, "puppet") ~}
148+
# Wait for Terraform to scp its YAML data
149+
- while [ ! -e "/etc/puppetlabs/data/terraform_data.yaml" ]; do echo "$(date -I'seconds') Waiting for terraform to scp terraform_data.yaml"; sleep 5; done
162150
# Bootstrap services that are essential to the puppet repo
163151
- (cd /etc/puppetlabs/code/environments/production; test -e bootstrap.sh && ./bootstrap.sh)
164-
%{ endif }
152+
%{ endif ~}
165153
# If the current image has already been configured with Magic Castle Puppet environment,
166154
# we can start puppet and skip reboot, reducing the delay for bringing the node up.
167155
- test -f /etc/magic-castle-release && systemctl start puppet || true
168156
- test -f /run/cloud-init-failed && echo 'WARNING - some steps cloud-init runcmd failed, listed in /run/cloud-init-failed. Manual fixing and rebooting required. ' | tee /etc/motd || true
169157

170158
write_files:
171-
%{ if contains(tags, "puppet") || length(setintersection(tags, bastion_tags)) > 0 ~}
172159
- content: |
160+
%{ for host, ip in puppetservers ~}
161+
%{ if ip != "" ~}
162+
${ip} ${host}
163+
%{ endif ~}
164+
%{ endfor ~}
165+
path: /etc/hosts
166+
append: true
167+
%{ if user_tf_required ~}
168+
- path: /etc/ssh/sshd_config.d/50-authenticationmethods.conf
169+
permissions: "0600"
170+
content: |
173171
Match User tf
174172
AuthorizedKeysFile /etc/ssh/authorized_keys.%u
175173
AuthenticationMethods publickey
176-
path: /etc/ssh/sshd_config.d/50-authenticationmethods.conf
177-
permissions: "0600"
178-
%{ endif ~}
179-
%{ if contains(tags, "puppet") ~}
180-
- content: restrict,pty ${tf_ssh_public_key}
181-
path: /etc/ssh/authorized_keys.tf
174+
- path: /etc/ssh/authorized_keys.tf
182175
permissions: "0644"
176+
content: |
177+
%{ if contains(tags, "puppet") ~}
178+
restrict,pty ${tf_ssh_public_key}
183179
%{ else ~}
184-
%{ if length(setintersection(tags, bastion_tags)) > 0 ~}
185-
# If the ip addresses of the puppet servers are not known in advance, we cannot restrict the ssh connection to them.
186-
- content: restrict,%{if length(compact(values(puppetservers))) == length(keys(puppetservers)) }%{ for host, ip in puppetservers }permitopen="${ip}:22"%{ endfor }%{ else }pty%{ endif },port-forwarding,command="/sbin/nologin" ${tf_ssh_public_key}
187-
path: /etc/ssh/authorized_keys.tf
188-
permissions: "0644"
180+
%{ if length(compact(values(puppetservers))) != length(keys(puppetservers)) ~}
181+
restrict,pty,port-forwarding,command="/sbin/nologin" ${tf_ssh_public_key}
182+
%{ else ~}
183+
restrict,%{ for host, ip in puppetservers }permitopen="${ip}:22",%{ endfor }port-forwarding,command="/sbin/nologin" ${tf_ssh_public_key}
184+
%{ endif ~}
189185
%{ endif ~}
190186
%{ endif ~}
191187
- content: |
@@ -207,29 +203,8 @@ write_files:
207203
path: /etc/puppetlabs/facter/facts.d/prefix.yaml
208204
permissions: "0644"
209205
%{ if contains(tags, "puppet") ~}
210-
- content: |
211-
#!/bin/bash
212-
ZIP_FILE=$${1}
213-
ZIP_DIR=$${ZIP_FILE%.zip}
214-
215-
# unzip is not necessarily installed when connecting, but python is.
216-
/usr/libexec/platform-python -c "import zipfile; zipfile.ZipFile('$${ZIP_FILE}').extractall()"
217-
218-
chmod g-w,o-rwx $(find $${ZIP_DIR}/ -type f ! -path $${ZIP_DIR}/code/*)
219-
chown -R root:52 $${ZIP_DIR}
220-
mkdir -p -m 755 /etc/puppetlabs/
221-
rsync -avh --no-t --exclude 'data' $${ZIP_DIR}/ /etc/puppetlabs/
222-
rsync -avh --no-t --del $${ZIP_DIR}/data/ /etc/puppetlabs/data/
223-
rm -rf $${ZIP_DIR}/
224-
225-
if [ -f /opt/puppetlabs/puppet/bin/r10k ] && [ /etc/puppetlabs/code/Puppetfile -nt /etc/puppetlabs/code/modules ]; then
226-
/opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/modules --puppetfile=/etc/puppetlabs/code/Puppetfile
227-
touch /etc/puppetlabs/code/modules
228-
fi
229-
230-
if [ -f /usr/local/bin/consul ] && [ -f /usr/bin/jq ]; then
231-
/usr/local/bin/consul event -token=$(jq -r .acl.tokens.agent /etc/consul/config.json) -name=puppet $(date +%s)
232-
fi
206+
- content: ${filebase64("${module_path}/update_etc_puppetlabs.sh")}
207+
encoding: b64
233208
permissions: "0755"
234209
path: /usr/sbin/update_etc_puppetlabs.sh
235210
- content: |
@@ -245,12 +220,6 @@ write_files:
245220
permissions: "0640"
246221
- path: /var/log/autosign.log
247222
permissions: "0640"
248-
%{ if puppetfile != "" ~}
249-
- path: /etc/puppetlabs/code/Puppetfile
250-
content: |
251-
${indent(6, puppetfile)}
252-
permissions: "0644"
253-
%{ endif ~}
254223
%{ endif ~}
255224
- content: |
256225
---
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
ZIP_FILE=${1}
3+
ZIP_DIR=${ZIP_FILE%.zip}
4+
5+
# unzip is not necessarily installed when connecting, but python is.
6+
/usr/libexec/platform-python -c "import zipfile; zipfile.ZipFile('${ZIP_FILE}').extractall()"
7+
8+
chmod g-w,o-rwx $(find ${ZIP_DIR}/ -type f ! -path ${ZIP_DIR}/code/*)
9+
chown -R root:52 ${ZIP_DIR}
10+
mkdir -p -m 755 /etc/puppetlabs/
11+
rsync -avh --no-t --exclude 'data' ${ZIP_DIR}/ /etc/puppetlabs/
12+
rsync -avh --no-t --del ${ZIP_DIR}/data/ /etc/puppetlabs/data/
13+
rm -rf ${ZIP_DIR}/
14+
15+
if [ -f /opt/puppetlabs/puppet/bin/r10k ] && [ /etc/puppetlabs/code/Puppetfile -nt /etc/puppetlabs/code/modules ]; then
16+
/opt/puppetlabs/puppet/bin/r10k puppetfile install --moduledir=/etc/puppetlabs/code/modules --puppetfile=/etc/puppetlabs/code/Puppetfile
17+
touch /etc/puppetlabs/code/modules
18+
fi
19+
20+
if [ -f /usr/local/bin/consul ] && [ -f /usr/bin/jq ]; then
21+
/usr/local/bin/consul event -token=$(jq -r .acl.tokens.agent /etc/consul/config.json) -name=puppet $(date +%s)
22+
fi

gcp/infrastructure.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ module "configuration" {
3232
cloud_provider = local.cloud_provider
3333
cloud_region = local.cloud_region
3434
skip_upgrade = var.skip_upgrade
35-
puppetfile = var.puppetfile
3635
}
3736

3837
module "provision" {

incus/infrastructure.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ module "configuration" {
2828
cloud_provider = "incus"
2929
cloud_region = "local"
3030
skip_upgrade = var.skip_upgrade
31-
puppetfile = var.puppetfile
3231
}
3332

3433
module "provision" {

openstack/infrastructure.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ module "configuration" {
2828
cloud_provider = local.cloud_provider
2929
cloud_region = local.cloud_region
3030
skip_upgrade = var.skip_upgrade
31-
puppetfile = var.puppetfile
3231
}
3332

3433
module "provision" {

0 commit comments

Comments
 (0)