Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jerowe committed Oct 25, 2023
1 parent 8e91f80 commit 7172cee
Showing 1 changed file with 92 additions and 77 deletions.
169 changes: 92 additions & 77 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ resource "aws_imagebuilder_component" "custom_stacks" {
depends_on = [
data.local_file.custom_components
]
count = length(data.local_file.custom_components)
name = "${module.this.id}-${local.custom_aws_imagebuilder_components[count.index].name}-${local.dt}"
platform = "Linux"
count = length(data.local_file.custom_components)
name = "${module.this.id}-${local.custom_aws_imagebuilder_components[count.index].name}-${local.dt}"
platform = "Linux"
// Version must be in format: major.minor.patch
# version = "1.0.0"
# version = formatdate("YYYY.MM.DD", timestamp())
version = local.dt_version
data = data.local_file.custom_components[count.index].content
tags = module.this.tags
version = local.dt_version
data = data.local_file.custom_components[count.index].content
tags = module.this.tags
}

output "scientific_stack" {
Expand Down Expand Up @@ -159,36 +159,43 @@ resource "random_string" "amis" {
}

locals {
ami_ids = flatten(data.aws_ami.deeplearning[*].image_id)
ami_names = flatten(data.aws_ami.deeplearning[*].name)
pcluster_ami_long_ids = flatten([
# the ami id already gets the date time attached
# The value supplied for parameter 'name' is not valid. name must match pattern ^[-_A-Za-z-0-9][-_A-Za-z0-9 ]{1,126}[-_A-Za-z-0-9]$
for i in range(length(local.ami_ids)) : trimspace("${module.this.id}-${local.dt_day}-pcluster-${replace(var.pcluster_version, ".", "-")}--${lower(replace(replace(replace(local.ami_names[i], local.replace_orig, local.replace_with), " ", "-") ,".", "-") )}")
ami_ids = flatten(data.aws_ami.deeplearning[*].image_id)
ami_names = flatten(data.aws_ami.deeplearning[*].name)
pcluster_ami_long_ids = flatten([
# the ami id already gets the date time attached
# The value supplied for parameter 'name' is not valid. name must match pattern ^[-_A-Za-z-0-9][-_A-Za-z0-9 ]{1,126}[-_A-Za-z-0-9]$
for i in range(length(local.ami_ids)) :
trimspace("${module.this.id}-${local.dt_day}-pcluster-${replace(var.pcluster_version, ".", "-")}--${lower(replace(replace(replace(local.ami_names[i], local.replace_orig, local.replace_with), " ", "-") ,".", "-") )}")
])
# keep running into issues where this is too long
pcluster_ami_ids = flatten([
# the ami id already gets the date time attached
# The value supplied for parameter 'name' is not valid. name must match pattern ^[-_A-Za-z-0-9][-_A-Za-z0-9 ]{1,126}[-_A-Za-z-0-9]$
# make sure we are using a name pattern that is allowed
# The 'Name' tag has the full name
for i in range(length(local.ami_ids)) : trimspace("pcluster-${replace(var.pcluster_version, ".", "-")}-${random_string.amis[i].id}-${local.dt_day}")
pcluster_ami_ids = flatten([
# the ami id already gets the date time attached
# The value supplied for parameter 'name' is not valid. name must match pattern ^[-_A-Za-z-0-9][-_A-Za-z0-9 ]{1,126}[-_A-Za-z-0-9]$
# make sure we are using a name pattern that is allowed
# The 'Name' tag has the full name
for i in range(length(local.ami_ids)) :
trimspace("pcluster-${replace(var.pcluster_version, ".", "-")}-${random_string.amis[i].id}-${local.dt_day}")
])
pcluster_ami_names = flatten([
# for i in range(length(local.ami_ids)) : replace(trimspace("${local.ami_name} PCluster ${var.pcluster_version} ${local.ami_names[i]}"), "(Amazon Linux 2)", "Amazon Linux 2")
for i in range(length(local.ami_ids)) : replace(trimspace("${local.ami_name} PCluster ${var.pcluster_version} ${local.ami_names[i]}"), local.replace_orig, local.replace_with)
pcluster_ami_names = flatten([
# for i in range(length(local.ami_ids)) : replace(trimspace("${local.ami_name} PCluster ${var.pcluster_version} ${local.ami_names[i]}"), "(Amazon Linux 2)", "Amazon Linux 2")
for i in range(length(local.ami_ids)) :
replace(trimspace("${local.ami_name} PCluster ${var.pcluster_version} ${local.ami_names[i]}"), local.replace_orig, local.replace_with)
])
pcluster_ami_build_config_files = flatten([
for i in range(length(local.ami_ids)) : "files/pcluster-v${var.pcluster_version}/pcluster_build-${local.pcluster_ami_ids[i]}.yaml"
pcluster_ami_build_config_files = flatten([
for i in range(length(local.ami_ids)) :
"files/pcluster-v${var.pcluster_version}/pcluster_build-${local.pcluster_ami_ids[i]}.yaml"
])
pcluster_ami_build_cloudformation_template_files = flatten([
for i in range(length(local.ami_ids)) : "files/pcluster-v${var.pcluster_version}/cloudformation-${local.pcluster_ami_ids[i]}.json"
for i in range(length(local.ami_ids)) :
"files/pcluster-v${var.pcluster_version}/cloudformation-${local.pcluster_ami_ids[i]}.json"
])
pcluster_ami_build_cloudformation_status_files = flatten([
for i in range(length(local.ami_ids)) : "files/pcluster-v${var.pcluster_version}/cloudformation-${local.pcluster_ami_ids[i]}.json"
pcluster_ami_build_cloudformation_status_files = flatten([
for i in range(length(local.ami_ids)) :
"files/pcluster-v${var.pcluster_version}/cloudformation-${local.pcluster_ami_ids[i]}.json"
])
pcluster_ami_build_pcluster_describe_files = flatten([
for i in range(length(local.ami_ids)) : "files/pcluster-v${var.pcluster_version}/pcluster-ami-${local.pcluster_ami_ids[i]}.json"
pcluster_ami_build_pcluster_describe_files = flatten([
for i in range(length(local.ami_ids)) :
"files/pcluster-v${var.pcluster_version}/pcluster-ami-${local.pcluster_ami_ids[i]}.json"
])

}
Expand All @@ -198,64 +205,72 @@ locals {
# TODO Sanity check
# Tags must not have '(' or ')' in their values
pcluster_image_build_template = [
for i in range(length(local.ami_ids)) :
{
Region : var.region,
Image : {
Name : local.pcluster_ami_ids[i]
Tags : [
{
Key : "Name",
Value : replace(local.pcluster_ami_names[i], local.replace_orig, local.replace_with )
},
{
Key : "Version",
Value : var.image_recipe_version,
},
{
Key : "Date",
Value : local.dt
},
{
Key : "ParentAmiID",
Value : local.ami_ids[i]
},
{
Key : "ParentAmiName",
Value : replace(local.ami_names[i], local.replace_orig, local.replace_with)
},
]
},
Build : {
ParentImage : local.ami_ids[i],
InstanceType : var.instance_type,
SubnetId : var.subnet_id,
SecurityGroupIds : var.security_group_ids,
Components : [for component in local.components : { Type : "arn", Value : component }],
Tags : flatten([
[for key in keys(module.this.tags) : { Key : key, Value : module.this.tags[key] }], [
{ Key : "ParentAmiId", Value : local.ami_ids[i] },
{ Key : "ParentAmiName", Value : replace(local.ami_names[i], local.replace_orig, local.replace_with) }
for i in range(length(local.ami_ids)) :
{
Region : var.region,
Image : {
Name : local.pcluster_ami_ids[i]
Tags : [
{
Key : "Name",
Value : replace(local.pcluster_ami_names[i], local.replace_orig, local.replace_with )
},
{
Key : "Version",
Value : var.image_recipe_version,
},
{
Key : "Date",
Value : local.dt
},
{
Key : "ParentAmiID",
Value : local.ami_ids[i]
},
{
Key : "ParentAmiName",
Value : replace(local.ami_names[i], local.replace_orig, local.replace_with)
},
]
])
},
}
},
Build : {
ParentImage : local.ami_ids[i],
InstanceType : var.instance_type,
SubnetId : var.subnet_id,
SecurityGroupIds : var.security_group_ids,
Components : [for component in local.components : { Type : "arn", Value : component }],
Tags : flatten([
[
{
Key : "Version",
Value : var.image_recipe_version,
},
{
Key : "Date",
Value : local.dt
},
{ Key : "ParentAmiId", Value : local.ami_ids[i] },
{ Key : "ParentAmiName", Value : replace(local.ami_names[i], local.replace_orig, local.replace_with) }
]
])
},
}
]
}

locals {
make_dirs_command = flatten([
for i in range(length(local.pcluster_image_build_template)) :
<<EOF
make_dirs_command = flatten([
for i in range(length(local.pcluster_image_build_template)) :
<<EOF
mkdir -p files/pcluster-v${var.pcluster_version}
touch ${local.pcluster_ami_build_config_files[i]}
touch ${local.pcluster_ami_build_cloudformation_template_files[i]}
touch ${local.pcluster_ami_build_pcluster_describe_files[i]}
EOF
])
pcluster_build_command = flatten([
for i in range(length(local.pcluster_image_build_template)) :
<<EOF
for i in range(length(local.pcluster_image_build_template)) :
<<EOF
echo "${local.pcluster_ami_names[i]}"
pcluster delete-image \
Expand All @@ -279,7 +294,7 @@ resource "null_resource" "make_dirs" {
count = length(local.pcluster_image_build_template)
depends_on = [
]
triggers = local.triggers
triggers = local.triggers
provisioner "local-exec" {
command = local.make_dirs_command[count.index]
}
Expand Down Expand Up @@ -338,8 +353,8 @@ data "local_file" "pcluster_amis" {
local_file.pcluster_build_configurations,
null_resource.pcluster_wait,
]
count = length(local.pcluster_image_build_template)
filename = local.pcluster_ami_build_pcluster_describe_files[count.index]
count = length(local.pcluster_image_build_template)
filename = local.pcluster_ami_build_pcluster_describe_files[count.index]
}

locals {
Expand Down

0 comments on commit 7172cee

Please sign in to comment.