add rpm option for download

Signed-off-by: matttrach <matttrach@gmail.com>
This commit is contained in:
matttrach 2023-10-12 11:34:48 -05:00
parent 372546ea5e
commit abc2007865
No known key found for this signature in database
GPG Key ID: C00467FDE2D0231F
12 changed files with 140 additions and 25 deletions

4
.gitignore vendored
View File

@ -7,8 +7,8 @@ override.*
!terraform.md !terraform.md
rke2* rke2*
*.gz *.gz
examples/*/installer.sh examples/*/install.*
examples/*/sha256sum.txt examples/*/sha256sum*
examples/*/tmp examples/*/tmp
examples/*/rke2 examples/*/rke2
id_ed25519_* id_ed25519_*

View File

@ -1,4 +1,4 @@
module "TestBasic" { module "download_latest" {
source = "../../" source = "../../"
release = "latest" release = "latest"
} }

View File

@ -1,9 +1,9 @@
output "tag" { output "tag" {
value = module.TestBasic.tag value = module.download_latest.tag
} }
output "path" { output "path" {
value = module.TestBasic.path value = module.download_latest.path
} }
output "files" { output "files" {
value = module.TestBasic.files value = module.download_latest.files
} }

13
examples/rpm/main.tf Normal file
View File

@ -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
examples/rpm/output.tf Normal file
View File

@ -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
examples/rpm/variables.tf Normal file
View File

@ -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
examples/rpm/versions.tf Normal file
View File

@ -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"
}
}
}

View File

@ -2,7 +2,7 @@ locals {
version = var.release version = var.release
path = var.path path = var.path
} }
module "TestSelected" { module "download_selected" {
source = "../../" source = "../../"
release = local.version release = local.version
path = local.path path = local.path

View File

@ -1,9 +1,9 @@
output "tag" { output "tag" {
value = module.TestSelected.tag value = module.download_selected.tag
} }
output "path" { output "path" {
value = module.TestSelected.path value = module.download_selected.path
} }
output "files" { output "files" {
value = module.TestSelected.files value = module.download_selected.files
} }

43
main.tf
View File

@ -1,19 +1,34 @@
locals { locals {
release = var.release system = var.system
latest = (var.release == "latest" ? true : false) rpm = var.rpm
arch = var.arch release = var.release
system = var.system latest = (var.release == "latest" ? true : false)
install_url = "https://raw.githubusercontent.com/rancher/rke2/master/install.sh" sem = (local.latest ? [] : regex("v([0-9]+)\\.([0-9]+)\\.([0-9]+)", var.release)) # extracts the semver from the release string
files = toset([ kube = (local.latest ? "" : "${local.sem[0]}.${local.sem[1]}") # build the major.minor version of kubernetes
"rke2-images.${local.system}-${local.arch}.tar.gz", arch = var.arch
"rke2.${local.system}-${local.arch}.tar.gz", os = (local.rpm ? var.os : "") # should be rhel, but should only be used when downloading RPMs
"sha256sum-${local.arch}.txt", os_version = (local.rpm ? var.os_version : "") # should only be used when downloading RPMs
"install.sh", 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 } : {}) 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 } : {}) 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) 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" { data "github_release" "selected" {
@ -52,10 +67,10 @@ resource "null_resource" "download" {
data.github_release.latest, data.github_release.latest,
local_file.download_dir, local_file.download_dir,
] ]
for_each = toset(local.files) for_each = local.files
provisioner "local-exec" { provisioner "local-exec" {
command = <<-EOT 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 EOT
} }
} }

22
tests/rpm_test.go Normal file
View File

@ -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)
}

View File

@ -3,6 +3,7 @@ variable "release" {
description = <<-EOT description = <<-EOT
The value of the git tag associated with the release to find. The value of the git tag associated with the release to find.
Specify "latest" to find the latest release (default). Specify "latest" to find the latest release (default).
When downloading RPMs, this must be a specific release, not "latest".
EOT EOT
default = "latest" default = "latest"
} }
@ -17,11 +18,30 @@ variable "arch" {
variable "system" { variable "system" {
type = string type = string
description = <<-EOT 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). Valid values are currently just linux (the default).
EOT EOT
default = "linux" 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" { variable "path" {
type = string type = string
description = <<-EOT 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. If not specified, the files will be downloaded to a directory named "rke2" in the root of the module.
EOT EOT
default = "./rke2" 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
} }