Skip to content

Commit

Permalink
Add custom template
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfontaine committed May 14, 2024
1 parent 2026bfc commit 2eea237
Showing 1 changed file with 279 additions and 0 deletions.
279 changes: 279 additions & 0 deletions custom/templates/base.tf.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
{% from "macros.tf.jinja" import argument %}

terraform {
{% block required_providers %}
required_providers {
spacelift = {
source = "spacelift-io/spacelift"
version = "{% block spacelift_provider_version %}~> 1.0{% endblock %}"
}
{% block required_providers_extra %}{% endblock %}
}
{% endblock %}
}

{% block header_extra %}{% endblock %}

{% block spaces %}
{% for space in spaces %}
resource "spacelift_space" "{{ space._migration_id }}" {
{{ argument("description", space.description) }}
{{ argument("inherit_entities", True, default=False) }}
{{ argument("name", space.name, required=True) }}
{{ argument("parent_space_id", "root", required=True) }}
{% block space_arguments_extra scoped %}{% endblock %}
}

{% if space.requires_terraform_workflow_tool %}
resource "spacelift_context" "{{ space._migration_id }}_terraform_workflow_tool" {
{{ argument("description", "Allows to use custom versions of Terraform. Make sure to provide the Terraform binary in the custom runner image for the stack.") }}
{{ argument("name", "Terraform Workflow Tool", required=True) }}
}

resource "spacelift_mounted_file" "workflow" {
{{ argument("context_id", "spacelift_context." ~ space._migration_id ~ "_terraform_workflow_tool.id", required=True, serialize=False) }}
{{ argument("relative_path", ".spacelift/workflow.yml", required=True) }}
{% raw %}
content = base64encode(
<<EOT
# Used to initialize your root module.
init: terraform init -input=false

# Used to select the correct workspace. Only used for Stacks that are using a custom state
# backend, and which have the Workspace setting configured.
workspaceSelect: terraform workspace select "{{ .WorkspaceName }}"

# Used to create a new workspace if none with the required name exists. Only used for Stacks
# that are using a custom state backend, and which have the Workspace setting configured.
workspaceNew: terraform workspace new "{{ .WorkspaceName }}"

# Used to generate a plan of the infrastructure changes that will be applied.
plan: terraform plan -input=false -lock={{ .Lock }} {{ if not .Refresh }}-refresh=false {{ end }}-out={{ .PlanFileName }} {{ range .Targets }}-target='{{ . }}' {{ end }}

# Outputs the current state information as JSON.
showState: terraform show -json

# Used to convert a plan file to its JSON representation.
showPlan: terraform show -json "{{ .PlanFileName }}"

# Used to get the current outputs from the state.
getOutputs: terraform output -json

# Used to apply any changes contained in the specified plan.
apply: terraform apply -auto-approve -input=false "{{ .PlanFileName }}"

# Used to tear down any resources as part of deleting a stack.
destroy: terraform destroy -auto-approve -input=false
EOT
)
{% endraw %}
}
{% endif %}

{% block space_extra scoped %}{% endblock %}
{% endfor %}
{% block spaces_extra %}{% endblock %}
{% endblock %}

{% block stacks %}
{% for stack in stacks %}
{% if stack.vcs.repository %}
resource "spacelift_stack" "{{ stack._relationships.space._migration_id }}_{{ stack._migration_id }}" {
{{ argument("auto_deploy", stack.auto_deploy, default=False) }}
{{ argument("branch", stack.vcs.branch, required=True) }}
{{ argument("description", stack.description) }}
{{ argument("local_preview", stack.local_preview) }}
{{ argument("labels", stack.labels) }}
{{ argument("manage_state", False) }}
{{ argument("name", stack.name, required=True) }}
{{ argument("project_root", stack.vcs.project_root) }}
{{ argument("repository", stack.vcs.repository, required=True) }}
{{ argument("slug", stack.slug, default=stack.name) }}
{{ argument("space_id", "spacelift_space." ~ stack._relationships.space._migration_id ~ ".id", serialize=False) }}
{{ argument("terraform_version", stack.terraform.version, serialize=True) }}
{{ argument("terraform_workflow_tool", stack.terraform.workflow_tool, default="TERRAFORM_FOSS") }}
{% block stack_arguments_extra scoped %}{% endblock %}

{% if stack.terraform.workflow_tool == "CUSTOM" %}
{{ argument("runner_image ", extra_vars.custom_terraform_runner_image ~ ":" ~ stack.terraform.version, required=True) }}
{% endif %}

{% if stack.vcs.provider == "github_custom" %}
github_enterprise {
{{ argument("namespace", stack.vcs.namespace) }}
}
{% elif stack.vcs.provider == "azure_devops" %}
azure_devops {
{{ argument("project", stack.vcs.namespace) }}
}
{% elif stack.vcs.provider %}
{{ stack.vcs.provider }} {
{{ argument("namespace", stack.vcs.namespace) }}
}
{% endif %}
}

{% if stack.terraform.workflow_tool == "CUSTOM" %}
resource "spacelift_context_attachment" "{{ stack._relationships.space._migration_id }}_{{ stack._migration_id }}_terraform_workflow_tool" {
{{ argument("context_id", "spacelift_context." ~ stack._relationships.space._migration_id ~ "_terraform_workflow_tool.id", serialize=False) }}
{{ argument("stack_id", "spacelift_stack." ~ stack._relationships.space._migration_id ~ "_" ~ stack._migration_id ~ ".id", serialize=False) }}
}
{% endif %}

{% if stack.has_variables_with_invalid_name %}
resource "spacelift_mounted_file" "{{ stack._relationships.space._migration_id }}_{{ stack._migration_id }}_env_vars_with_invalid_name" {
{# The content will be uploaded at a later time to avoid having it in the Terraform code#}
{{ argument("content", 'base64encode("placeholder = \\"placeholder\\"")', serialize=False) }}
{{ argument("relative_path", ("source/" ~ stack.vcs.project_root ~ "/tf_vars_with_invalid_name.auto.tfvars")|normalizepath) }}
{{ argument("stack_id", "spacelift_stack." ~ stack._relationships.space._migration_id ~ "_" ~ stack._migration_id ~ ".id", serialize=False) }}
{{ argument("write_only", false) }}

lifecycle {
{{ argument("ignore_changes", "[content]", serialize=False) }}
}
}
{% endif %}

{% if stack.has_secret_variables_with_invalid_name %}
resource "spacelift_mounted_file" "{{ stack._relationships.space._migration_id }}_{{ stack._migration_id }}_secret_env_vars_with_invalid_name" {
{# The content will be uploaded at a later time to avoid having it in the Terraform code#}
{{ argument("content", 'base64encode("placeholder = \\"placeholder\\"")', serialize=False) }}
{{ argument("relative_path", ("source/" ~ stack.vcs.project_root ~ "/tf_secret_vars_with_invalid_name.auto.tfvars")|normalizepath) }}
{{ argument("stack_id", "spacelift_stack." ~ stack._relationships.space._migration_id ~ "_" ~ stack._migration_id ~ ".id", serialize=False) }}
{{ argument("write_only", true) }}

lifecycle {
{{ argument("ignore_changes", "[content]", serialize=False) }}
}
}
{% endif %}

{% block stack_extra scoped %}{% endblock %}
{% endif %}

{% endfor %}
{% block stacks_extra %}{% endblock %}
{% endblock %}

{% block stack_variables %}
{% for variable in stack_variables %}
{% if variable._relationships.stack.vcs.repository and variable.valid_name %}
resource "spacelift_environment_variable" "{{ variable._relationships.space._migration_id }}_{{ variable._relationships.stack._migration_id }}_{{ variable._migration_id }}" {
{% if variable.type == "terraform" %}
{{ argument("name", "TF_VAR_" ~ variable.name, required=True) }}
{% else %}
{{ argument("name", variable.name, required=True) }}
{% endif %}

{{ argument("stack_id", "spacelift_stack." ~ variable._relationships.space._migration_id ~ "_" ~ variable._relationships.stack._migration_id ~ ".id", serialize=False) }}
{% if not variable.write_only %}
{% if variable.hcl %}
{{ argument("value", "jsonencode(" ~ variable.value ~ ")", serialize=False) }}
{% elif variable.value and "\n" in variable.value %}
{{ argument("value", "jsonencode(\"" ~ variable.value|replace("\n", "\\n") ~ "\")", serialize=False) }}
{% else %}
{{ argument("value", variable.value) }}
{% endif %}
{% endif %}
{{ argument("write_only", variable.write_only, default=True) }}

{% if variable.write_only %}
lifecycle {
{{ argument("ignore_changes", "[value]", serialize=False) }}
}
{% endif %}

{% block stack_variable_arguments_extra scoped %}{% endblock %}
}
{% block stack_variable_extra scoped %}{% endblock %}
{% endif %}

{% endfor %}
{% block stack_variables_extra %}{% endblock %}
{% endblock %}

{% block contexts %}
{% for context in contexts %}
resource "spacelift_context" "{{ context._relationships.space._migration_id }}_{{ context._migration_id }}" {
{{ argument("description", context.description) }}
{{ argument("labels", context.labels, default=[]) }}
{{ argument("name", context.name, required=True) }}
{% block context_arguments_extra scoped %}{% endblock %}
}

{% for stack in context._relationships.stacks %}
resource "spacelift_context_attachment" "{{ context._relationships.space._migration_id }}_{{ context._migration_id }}_{{ stack._migration_id }}" {
{{ argument("context_id", "spacelift_context." ~ context._relationships.space._migration_id ~ "_" ~ context._migration_id ~ ".id", required=True, serialize=False) }}
{{ argument("stack_id", "spacelift_stack." ~ context._relationships.space._migration_id ~ "_" ~ stack._migration_id ~ ".id", required=True, serialize=False) }}
}
{% endfor %}

{% block context_extra scoped %}{% endblock %}

{% endfor %}
{% block contexts_extra %}{% endblock %}
{% endblock %}

{% block context_variables %}
{% for variable in context_variables %}

{# KLUDGE: write_only variables are not supported in contexts #}
{% if not variable.write_only %}
resource "spacelift_environment_variable" "{{ variable._relationships.space._migration_id }}_{{ variable._relationships.context._migration_id }}_{{ variable._migration_id }}" {
{% if variable.type == "terraform" %}
{{ argument("name", "TF_VAR_" ~ variable.name, required=True) }}
{% else %}
{{ argument("name", variable.name, required=True) }}
{% endif %}

{{ argument("context_id", "spacelift_context." ~ variable._relationships.space._migration_id ~ "_" ~ variable._relationships.context._migration_id ~ ".id", serialize=False) }}
{% if not variable.write_only %}
{% if variable.hcl %}
{{ argument("value", "jsonencode(" ~ variable.value ~ ")", serialize=False) }}
{% elif variable.value and "\n" in variable.value %}
{{ argument("value", "jsonencode(\"" ~ variable.value|replace("\n", "\\n") ~ "\")", serialize=False) }}
{% else %}
{{ argument("value", variable.value) }}
{% endif %}
{% endif %}
{{ argument("write_only", variable.write_only, default=True) }}

{% if variable.write_only %}
lifecycle {
{{ argument("ignore_changes", "[value]", serialize=False) }}
}
{% endif %}

{% block context_variable_arguments_extra scoped %}{% endblock %}
}
{% block context_variable_extra scoped %}{% endblock %}

{% endif %}
{% endfor %}
{% block context_variables_extra %}{% endblock %}
{% endblock %}

{% block modules %}
{% for module in modules %}
{% if module.status == "setup_complete" and module.visibility == "private" %}
resource "spacelift_module" "{{ module._relationships.space._migration_id }}_{{ module._migration_id }}" {
{{ argument("branch", module.vcs.branch, required=True) }}
{{ argument("name", module.name) }}
{{ argument("repository", module.vcs.repository, required=True) }}
{{ argument("space_id", "spacelift_space." ~ module._relationships.space._migration_id ~ ".id", serialize=False) }}
{{ argument("terraform_provider", module.terraform_provider) }}
{% block module_arguments_extra scoped %}{% endblock %}

{% if module.vcs.provider == "github_custom" %}
github_enterprise {
{{ argument("namespace", module.vcs.namespace) }}
}
{% endif %}
}
{% block module_extra scoped %}{% endblock %}
{% endif %}
{% endfor %}
{% block modules_extra %}{% endblock %}
{% endblock %}

{% block footer_extra %}{% endblock %}

0 comments on commit 2eea237

Please sign in to comment.