diff --git a/.gitignore b/.gitignore index 2afcc1c..f88cfa4 100644 --- a/.gitignore +++ b/.gitignore @@ -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_* diff --git a/examples/basic/main.tf b/examples/basic/main.tf index d8d5a23..f886f14 100644 --- a/examples/basic/main.tf +++ b/examples/basic/main.tf @@ -1,4 +1,4 @@ -module "TestBasic" { +module "download_latest" { source = "../../" release = "latest" } diff --git a/examples/basic/output.tf b/examples/basic/output.tf index 3fd7048..e5aba67 100644 --- a/examples/basic/output.tf +++ b/examples/basic/output.tf @@ -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 } \ No newline at end of file diff --git a/examples/rpm/main.tf b/examples/rpm/main.tf new file mode 100644 index 0000000..0b5fe22 --- /dev/null +++ b/examples/rpm/main.tf @@ -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" +} diff --git a/examples/rpm/output.tf b/examples/rpm/output.tf new file mode 100644 index 0000000..c6d5d24 --- /dev/null +++ b/examples/rpm/output.tf @@ -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 +} \ No newline at end of file diff --git a/examples/rpm/variables.tf b/examples/rpm/variables.tf new file mode 100644 index 0000000..9838307 --- /dev/null +++ b/examples/rpm/variables.tf @@ -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 +} diff --git a/examples/rpm/versions.tf b/examples/rpm/versions.tf new file mode 100644 index 0000000..3a910d9 --- /dev/null +++ b/examples/rpm/versions.tf @@ -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" + } + } +} diff --git a/examples/selected/main.tf b/examples/selected/main.tf index e86e8d7..0b5fcd8 100644 --- a/examples/selected/main.tf +++ b/examples/selected/main.tf @@ -2,7 +2,7 @@ locals { version = var.release path = var.path } -module "TestSelected" { +module "download_selected" { source = "../../" release = local.version path = local.path diff --git a/examples/selected/output.tf b/examples/selected/output.tf index 9105ce8..4db5165 100644 --- a/examples/selected/output.tf +++ b/examples/selected/output.tf @@ -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 } \ No newline at end of file diff --git a/main.tf b/main.tf index 89386dd..44cfc3f 100644 --- a/main.tf +++ b/main.tf @@ -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.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" { @@ -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 } } diff --git a/tests/rpm_test.go b/tests/rpm_test.go new file mode 100644 index 0000000..7da474f --- /dev/null +++ b/tests/rpm_test.go @@ -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) +} diff --git a/variables.tf b/variables.tf index 6a14da6..ad493c1 100644 --- a/variables.tf +++ b/variables.tf @@ -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" } @@ -17,11 +18,30 @@ 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 @@ -29,4 +49,15 @@ variable "path" { 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 } \ No newline at end of file