Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.5.5
terraform_version: 1.5.6
with_wrapper: false

- name: Init Basic
Expand Down Expand Up @@ -49,6 +49,7 @@ jobs:
terratest:
name: 'Terratest'
runs-on: ubuntu-latest
environment: test
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ override.*
!terraform.md
rke2*
*.gz
examples/*/installer.sh
examples/*/sha256sum.txt
examples/*/install.*
examples/*/sha256sum*
examples/*/tmp
examples/*/rke2
id_ed25519_*
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module "TestBasic" {
module "download_latest" {
source = "../../"
release = "latest"
}
6 changes: 3 additions & 3 deletions examples/basic/output.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
output "tag" {
value = module.TestBasic.tag
value = module.download_latest.tag
}
output "path" {
value = module.TestBasic.path
value = module.download_latest.path
}
output "files" {
value = module.TestBasic.files
value = module.download_latest.files
}
13 changes: 13 additions & 0 deletions examples/rpm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
locals {
version = var.release
path = var.path
}
module "download_rpm" {
source = "../../"
release = local.version
path = local.path
rpm = true
# default: os = "rhel"
# default: os_version = "8"
# default: arch = "amd64"
}
9 changes: 9 additions & 0 deletions examples/rpm/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "tag" {
value = module.download_rpm.tag
}
output "path" {
value = module.download_rpm.path
}
output "files" {
value = module.download_rpm.files
}
12 changes: 12 additions & 0 deletions examples/rpm/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
variable "release" {
type = string
description = <<-EOT
The value of the git tag associated with the release to find.
EOT
}
variable "path" {
type = string
description = <<-EOT
The path to download the files to.
EOT
}
13 changes: 13 additions & 0 deletions examples/rpm/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_version = ">= 1.2.0, < 1.6"
required_providers {
github = {
source = "integrations/github"
version = ">= 5.32"
}
local = {
source = "hashicorp/local"
version = ">= 2.4"
}
}
}
2 changes: 1 addition & 1 deletion examples/selected/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ locals {
version = var.release
path = var.path
}
module "TestSelected" {
module "download_selected" {
source = "../../"
release = local.version
path = local.path
Expand Down
6 changes: 3 additions & 3 deletions examples/selected/output.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
output "tag" {
value = module.TestSelected.tag
value = module.download_selected.tag
}
output "path" {
value = module.TestSelected.path
value = module.download_selected.path
}
output "files" {
value = module.TestSelected.files
value = module.download_selected.files
}
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
bashInteractive
git
terraform
tflint
tfsec
];
shellHook = ''
source .envrc
Expand Down
43 changes: 29 additions & 14 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
locals {
release = var.release
latest = (var.release == "latest" ? true : false)
arch = var.arch
system = var.system
install_url = "https://raw.githubusercontent.com/rancher/rke2/master/install.sh"
files = toset([
"rke2-images.${local.system}-${local.arch}.tar.gz",
"rke2.${local.system}-${local.arch}.tar.gz",
"sha256sum-${local.arch}.txt",
"install.sh",
])
system = var.system
rpm = var.rpm
release = var.release
latest = (var.release == "latest" ? true : false)
sem = (local.latest ? [] : regex("v([0-9]+)\\.([0-9]+)\\.([0-9]+)", var.release)) # extracts the semver from the release string
kube = (local.latest ? "" : "${local.sem[0]}.${local.sem[1]}") # build the major.minor version of kubernetes
arch = var.arch
os = (local.rpm ? var.os : "") # should be rhel, but should only be used when downloading RPMs
os_version = (local.rpm ? var.os_version : "") # should only be used when downloading RPMs
install_url = "https://raw.githubusercontent.com/rancher/rke2/master/install.sh"
rpm_os = (local.os == "rhel" ? "centos" : local.os)
rpm_arch = (local.arch == "amd64" ? "x86_64" : local.arch)
rpm_url = "https://rpm.rancher.io/rke2/stable/${local.kube}/${local.rpm_os}/${local.os_version}"
rpm_release = (local.latest ? "" : "${local.sem[0]}.${local.sem[1]}.${local.sem[2]}~rke2r1-0.el${local.os_version}.${local.arch}")
selected_assets = (can(data.github_release.selected[0].assets) ? { for a in data.github_release.selected[0].assets : a.name => a.browser_download_url } : {})
latest_assets = (can(data.github_release.latest.assets) ? { for a in data.github_release.latest.assets : a.name => a.browser_download_url } : {})
assets = (local.latest ? local.latest_assets : local.selected_assets)
path = abspath(var.path)
base_files = {
"rke2-images.${local.system}-${local.arch}.tar.gz" = local.assets["rke2-images.${local.system}-${local.arch}.tar.gz"],
"rke2.${local.system}-${local.arch}.tar.gz" = local.assets["rke2.${local.system}-${local.arch}.tar.gz"],
"sha256sum-${local.arch}.txt" = local.assets["sha256sum-${local.arch}.txt"],
"install.sh" = local.install_url,
}
files = (local.rpm ? merge(local.base_files, {
"rke2-common.rpm" = "${local.rpm_url}/${local.rpm_arch}/rke2-common-${local.rpm_release}.rpm",
"rke2-server.rpm" = "${local.rpm_url}/${local.rpm_arch}/rke2-server-${local.rpm_release}.rpm",
"rke2-agent.rpm" = "${local.rpm_url}/${local.rpm_arch}/rke2-agent-${local.rpm_release}.rpm",
"rke2-selinux.rpm" = "${local.rpm_url}/noarch/rke2-selinux-0.15-1.el${local.os_version}.noarch.rpm",
}) : local.base_files)
path = abspath(var.path)
}

data "github_release" "selected" {
Expand Down Expand Up @@ -52,10 +67,10 @@ resource "null_resource" "download" {
data.github_release.latest,
local_file.download_dir,
]
for_each = toset(local.files)
for_each = local.files
provisioner "local-exec" {
command = <<-EOT
curl -L -s -o ${abspath("${local.path}/${each.key}")} ${lookup(local.assets, each.key, local.install_url)}
curl -L -s -o ${abspath("${local.path}/${each.key}")} ${each.value}
EOT
}
}
22 changes: 22 additions & 0 deletions tests/rpm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
)

func TestRpm(t *testing.T) {
t.Parallel()
directory := "rpm"
release := getLatestRelease(t, "rancher", "rke2")
terraformVars := map[string]interface{}{
"release": release,
"path": "./rke2",
}
terraformOptions := setup(t, directory, terraformVars)

defer teardown(t, directory)
defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
}
33 changes: 32 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ variable "release" {
description = <<-EOT
The value of the git tag associated with the release to find.
Specify "latest" to find the latest release (default).
When downloading RPMs, this must be a specific release, not "latest".
EOT
default = "latest"
}
Expand All @@ -17,16 +18,46 @@ variable "arch" {
variable "system" {
type = string
description = <<-EOT
The OS of the system to download for.
The kernel of the system to download for.
Valid values are currently just linux (the default).
EOT
default = "linux"
}
variable "os" {
type = string
description = <<-EOT
The OS to download RPMs for.
This is only used for RPM downloads.
This is ignored when rpm is false.
EOT
default = "rhel"
}
variable "os_version" {
type = string
description = <<-EOT
The version of RHEL to download RPMs for.
This is only used for RPM downloads.
This is ignored when rpm is false.
EOT
default = "8"
}

variable "path" {
type = string
description = <<-EOT
The path to download the files to.
If not specified, the files will be downloaded to a directory named "rke2" in the root of the module.
EOT
default = "./rke2"
}
variable "rpm" {
type = bool
description = <<-EOT
Whether or not to download the RPMs.
Defaults to false.
This option requires that the system is linux (specifically RHEL based) and the architecture is amd64(x86_64).
This option requires the computer running terraform to have curl installed.
When using this option, the "release" variable must be set to a specific release, not "latest".
EOT
default = false
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.0.0"
required_version = ">= 1.1.0, < 1.6"
required_providers {
github = {
source = "integrations/github"
Expand Down