Skip to content

Commit 10552c9

Browse files
committed
[CfnHup] Define cfn-hup configuration files within the environment config recipe.
These files used to be defined within the nodes user data, but we decided to move them to the cookbook in order to resize the CloudFormation template. Signed-off-by: Giacomo Marciani <[email protected]>
1 parent 2ab66c4 commit 10552c9

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-0
lines changed

cookbooks/aws-parallelcluster-environment/recipes/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
# spack 'Configure Spack Packages' do
3939
# action :configure
4040
# end
41+
include_recipe 'aws-parallelcluster-environment::config_cfn_hup'
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
#
4+
# Copyright:: 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the
7+
# License. A copy of the License is located at
8+
#
9+
# http://aws.amazon.com/apache2.0/
10+
#
11+
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
12+
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cloudformation_url = "https://cloudformation.#{node['cluster']['region']}.#{node['cluster']['aws_domain']}"
16+
instance_role_name = lambda {
17+
# IMDS is not available on Docker
18+
return "FAKE_INSTANCE_ROLE_NAME" if on_docker?
19+
get_metadata_with_token(get_metadata_token, URI("http://169.254.169.254/latest/meta-data/iam/security-credentials"))
20+
}.call
21+
22+
directory '/etc/cfn' do
23+
owner 'root'
24+
group 'root'
25+
mode '0770'
26+
recursive true
27+
end
28+
29+
directory '/etc/cfn/hooks.d' do
30+
owner 'root'
31+
group 'root'
32+
mode '0770'
33+
recursive true
34+
end
35+
36+
template '/etc/cfn/cfn-hup.conf' do
37+
source 'cfn_bootstrap/cfn-hup.conf.erb'
38+
owner 'root'
39+
group 'root'
40+
mode '0400'
41+
variables(
42+
stack_id: node['cluster']['stack_arn'],
43+
region: node['cluster']['region'],
44+
cloudformation_url: cloudformation_url,
45+
cfn_init_role: instance_role_name
46+
)
47+
end
48+
49+
template '/etc/cfn/hooks.d/pcluster-update.conf' do
50+
source 'cfn_bootstrap/cfn-hook-update.conf.erb'
51+
owner 'root'
52+
group 'root'
53+
mode '0400'
54+
variables(
55+
stack_id: node['cluster']['stack_arn'],
56+
region: node['cluster']['region'],
57+
cloudformation_url: cloudformation_url,
58+
cfn_init_role: instance_role_name,
59+
launch_template_resource_id: node['cluster']['launch_template_id']
60+
)
61+
end
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright:: 2024 Amazon.com, Inc. and its affiliates. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the
6+
# License. A copy of the License is located at
7+
#
8+
# http://aws.amazon.com/apache2.0/
9+
#
10+
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
11+
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
require "spec_helper"
15+
16+
describe "aws-parallelcluster-environment::config_cfn_hup" do
17+
AWS_REGION = "AWS_REGION"
18+
AWS_DOMAIN = "AWS_DOMAIN"
19+
STACK_ID = "STACK_ID"
20+
CLOUDFORMATION_URL = "https://cloudformation.#{AWS_REGION}.#{AWS_DOMAIN}"
21+
INSTANCE_ROLE_NAME = "INSTANCE_ROLE_NAME"
22+
LAUNCH_TEMPLATE_ID = "LAUNCH_TEMPLATE_ID"
23+
24+
for_all_oses do |platform, version|
25+
context "on #{platform}#{version}" do
26+
for_all_node_types do |node_type|
27+
context "when #{node_type}" do
28+
cached(:chef_run) do
29+
runner = runner(platform: platform, version: version) do |node|
30+
allow_any_instance_of(Object).to receive(:get_metadata_token).and_return("IMDS_TOKEN")
31+
allow_any_instance_of(Object).to receive(:get_metadata_with_token)
32+
.with("IMDS_TOKEN", URI("http://169.254.169.254/latest/meta-data/iam/security-credentials"))
33+
.and_return(INSTANCE_ROLE_NAME)
34+
35+
node.override["cluster"]["node_type"] = node_type
36+
node.override["cluster"]["region"] = AWS_REGION
37+
node.override["cluster"]["aws_domain"] = AWS_DOMAIN
38+
# TODO: We inject the stack id into the attribute stack_arn when generating the dna.json in the CLI.
39+
# This should be fixed at the CLI level first and adapt the cookbook accordingly.
40+
node.override["cluster"]["stack_arn"] = STACK_ID
41+
node.override["cluster"]["launch_template_id"] = LAUNCH_TEMPLATE_ID
42+
end
43+
runner.converge(described_recipe)
44+
end
45+
cached(:node) { chef_run.node }
46+
47+
%w(/etc/cfn /etc/cfn/hooks.d).each do |dir|
48+
it "creates the directory #{dir}" do
49+
is_expected.to create_directory(dir).with(
50+
owner: "root",
51+
group: "root",
52+
mode: "0770",
53+
recursive: true
54+
)
55+
end
56+
end
57+
58+
it "creates the file /etc/cfn/cfn-hup.conf" do
59+
is_expected.to create_template("/etc/cfn/cfn-hup.conf")
60+
.with(source: 'cfn_bootstrap/cfn-hup.conf.erb')
61+
.with(user: "root")
62+
.with(group: "root")
63+
.with(mode: "0400")
64+
.with(variables: {
65+
stack_id: STACK_ID,
66+
region: AWS_REGION,
67+
cloudformation_url: CLOUDFORMATION_URL,
68+
cfn_init_role: INSTANCE_ROLE_NAME,
69+
})
70+
end
71+
72+
it "creates the file /etc/cfn/hooks.d/pcluster-update.conf" do
73+
is_expected.to create_template("/etc/cfn/hooks.d/pcluster-update.conf")
74+
.with(source: 'cfn_bootstrap/cfn-hook-update.conf.erb')
75+
.with(user: "root")
76+
.with(group: "root")
77+
.with(mode: "0400")
78+
.with(variables: {
79+
stack_id: STACK_ID,
80+
region: AWS_REGION,
81+
cloudformation_url: CLOUDFORMATION_URL,
82+
cfn_init_role: INSTANCE_ROLE_NAME,
83+
launch_template_resource_id: LAUNCH_TEMPLATE_ID,
84+
})
85+
end
86+
end
87+
end
88+
end
89+
end
90+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright:: 2024 Amazon.com, Inc. and its affiliates. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the
6+
# License. A copy of the License is located at
7+
#
8+
# http://aws.amazon.com/apache2.0/
9+
#
10+
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
11+
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
require 'spec_helper'
15+
16+
describe 'aws-parallelcluster-environment::config' do
17+
before do
18+
@included_recipes = []
19+
# We assume this is the order in which the recipes are included
20+
@expected_recipes = %w(
21+
aws-parallelcluster-environment::ephemeral_drives
22+
aws-parallelcluster-environment::update_fs_mapping
23+
aws-parallelcluster-environment::export_home
24+
aws-parallelcluster-environment::export_internal_use_ebs
25+
aws-parallelcluster-environment::mount_intel_dir
26+
aws-parallelcluster-environment::ebs
27+
aws-parallelcluster-environment::raid
28+
aws-parallelcluster-environment::efs
29+
aws-parallelcluster-environment::fsx
30+
aws-parallelcluster-environment::config_cfn_hup
31+
)
32+
@expected_recipes.each do |recipe_name|
33+
allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with(recipe_name) do
34+
@included_recipes << recipe_name
35+
end
36+
end
37+
end
38+
39+
for_all_oses do |platform, version|
40+
context "on #{platform}#{version}" do
41+
for_all_node_types do |node_type|
42+
context "when #{node_type}" do
43+
cached(:chef_run) do
44+
runner = runner(platform: platform, version: version) do |node|
45+
node.override['cluster']['node_type'] = node_type
46+
node.override['cluster']['shared_storage_type'] = 'ebs'
47+
end
48+
runner.converge(described_recipe)
49+
end
50+
cached(:node) { chef_run.node }
51+
52+
it "includes the recipes in the right order" do
53+
chef_run
54+
expect(@included_recipes).to eq(@expected_recipes)
55+
end
56+
end
57+
end
58+
end
59+
end
60+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[parallelcluster-update]
2+
triggers=post.update
3+
path=Resources.<%= @launch_template_resource_id %>.Metadata.AWS::CloudFormation::Init
4+
action=PATH=/usr/local/bin:/bin:/usr/bin:/opt/aws/bin; . /etc/profile.d/pcluster.sh; cfn-init -v --stack <%= @stack_id %> --resource <%= @launch_template_resource_id %> --configsets update --region <%= @region %> --url <%= @cloudformation_url %> --role <%= @cfn_init_role %>
5+
runas=root
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[main]
2+
stack=<%= @stack_id %>
3+
region=<%= @region %>
4+
url=<%= @cloudformation_url %>
5+
role=<%= @cfn_init_role %>

0 commit comments

Comments
 (0)