Skip to content

Commit

Permalink
func: add script to install binary locally
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanadelacuesta committed Dec 3, 2024
1 parent 07d8e1a commit 56d04b2
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 52 deletions.
2 changes: 2 additions & 0 deletions enos/enos-vars.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1
20 changes: 6 additions & 14 deletions enos/modules/fetch_artifactory/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

locals {

// file name extensions for the install packages of Nomad for the various
// architectures, distributions and editions
path = var.edition == "ce" ? "nomad/*" : "nomad-enterprise/*"

artifact_version = var.edition == "ce" ? "${var.product_version}" : "${var.product_version}+ent"

package_extensions = {
amd64 = {
linux = "_linux_amd64.zip"
Expand All @@ -14,17 +16,7 @@ locals {
arm64 = {
linux = "_linux_arm64.zip"
}
}

// product_version --> artifact_version
artifact_version = replace(var.product_version, var.edition, "ent")
}

# Prefix for the artifact name. Ex: nomad_ and nomad-enterprise_
artifact_name_prefix = var.artifact_type == "package" ? local.artifact_package_release_names[var.distro][var.edition] : "nomad_"

# Suffix and extension for the artifact name. Ex: _linux_<arch>.zip,
artifact_name_extension = var.artifact_type == "package" ? local.package_extensions[var.arch][var.distro] : "_linux_${var.arch}.zip"

# Combine prefix/suffix/extension together to form the artifact name
artifact_name = var.artifact_type == "package" ? "${local.artifact_name_prefix}${replace(local.artifact_version, "-", "~")}${local.artifact_name_extension}" : "${local.artifact_name_prefix}${var.product_version}${local.artifact_name_extension}"
artifact_name = "nomad_${local.artifact_version}${local.package_extensions[var.arch][var.os]}"
}
10 changes: 5 additions & 5 deletions enos/modules/fetch_artifactory/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ terraform {
required_providers {
enos = {
source = "registry.terraform.io/hashicorp-forge/enos"
version = ">= 0.2.3"
}
}
}
Expand All @@ -15,19 +14,20 @@ data "enos_artifactory_item" "nomad" {
token = var.artifactory_token
host = var.artifactory_host
repo = var.artifactory_repo
path = var.edition == "ce" ? "nomad/*" : "nomad-enterprise/*"
path = local.path
name = local.artifact_name
properties = tomap({
"commit" = var.revision
"product-name" = var.edition == "ce" ? "nomad" : "nomad-enterprise"
// "product-version" = local.artifact_version
})
}

resource "enos_local_exec" "install_binary" {
environment = {
URL = each.value.stdout
URL = data.enos_artifactory_item.nomad.results[0].url
BINARY_PATH = var.binary_path
TOKEN = var.artifactory_token
}

scripts = [abspath("${path.module}/scripts/install.sh")]
}
}
24 changes: 4 additions & 20 deletions enos/modules/fetch_artifactory/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

output "url" {
value = data.enos_artifactory_item.nomad.results[0].url
description = "The artifactory download url for the artifact"
}

output "sha256" {
value = data.enos_artifactory_item.nomad.results[0].sha256
description = "The sha256 checksum for the artifact"
}

output "size" {
value = data.enos_artifactory_item.nomad.results[0].size
description = "The size in bytes of the artifact"
}

output "name" {
value = data.enos_artifactory_item.nomad.results[0].name
description = "The name of the artifact"
output "local_binary" {
value = var.binary_path
description = "Path where the binary will be placed"
}

output "vault_artifactory_release" {
description = "Binary information returned from the artifactory"
value = {
url = data.enos_artifactory_item.nomad.results[0].url
sha256 = data.enos_artifactory_item.nomad.results[0].sha256
username = var.artifactory_username
token = var.artifactory_token
}
}
29 changes: 29 additions & 0 deletions enos/modules/fetch_artifactory/scripts/install.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
#!/usr/bin/env bash
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

set -xeuo pipefail

# Variables
OUTPUT="nomad.zip" # Name for the downloaded file

# Download the file
wget --header="X-JFrog-Art-Api:$TOKEN" -O- "$OUTPUT" "$URL"

# Check if the file was downloaded
if [ $? -nq 0 ]; then
echo "Error downloading file." >&2
exit 1
fi

# Create the BINARY_PATH directory
mkdir -p "$BINARY_PATH"

# Unzip the file
unzip -o "$OUTPUT" -d "$BINARY_PATH"

# Check if the file was unzipped
if [ $? -nq 0 ]; then
echo "Error unzipping file." >&2
exit 1
fi

# Remove the zip file
rm $OUTPUT
10 changes: 0 additions & 10 deletions enos/modules/fetch_artifactory/terraform.tfvars

This file was deleted.

7 changes: 4 additions & 3 deletions enos/modules/fetch_artifactory/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ variable "product_version" {
default = null
}

variable "artifact_path" {
variable "arch" {
description = "The artifactory path to search for Nomad artifacts"
type = string
}

variable "arch" {
description = "The artifactory path to search for Nomad artifacts"
variable "binary_path" {
description = "The path to donwload and unzip the binary"
type = string
default = "/home/ubuntu/nomad"
}

0 comments on commit 56d04b2

Please sign in to comment.