Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install terraform with versions #142

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ on:
- Major Version
- Minor Version
- Patch Version

DEFAULT_TERRAFORM_VERSION:
type: string
description: Input the default version of Terraform to install
required: true
default: '1.6.0'
env:
BOT_USER_NAME: eclipse-xpanse-bot
BOT_EMAIL_ID: [email protected]
Expand Down Expand Up @@ -103,7 +107,8 @@ jobs:
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.new_version.outputs.next-version }}
labels: ${{ steps.meta.outputs.labels }}
provenance: false

build-args: |
DEFAULT_TERRAFORM_VERSION=${{ github.event.inputs.DEFAULT_TERRAFORM_VERSION }}
- name: Push POM updates with release version
uses: EndBug/add-and-commit@v9
with:
Expand Down
15 changes: 10 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ FROM eclipse-temurin:21-jre-alpine
RUN addgroup -S terraform-boot && adduser -S -G terraform-boot terraform-boot
RUN apk update && \
apk add --no-cache unzip wget
ENV TERRAFORM_VERSION=1.4.4
RUN wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip
RUN unzip terraform_${TERRAFORM_VERSION}_linux_amd64.zip
RUN mv terraform /usr/bin/terraform

ENV TERRAFORM_INSTALL_PATH=/opt/terraform
ENV DEFAULT_TERRAFORM_VERSION=1.6.0
ENV TERRAFORM_VERSIONS=1.6.0,1.7.0,1.8.0,1.9.0
baixinsui marked this conversation as resolved.
Show resolved Hide resolved
COPY install_terraform.sh /install_terraform.sh
RUN chmod +x /install_terraform.sh
RUN echo "Downloading and installing Terraform with multiple versions $TERRAFORM_VERSIONS into path $TERRAFORM_INSTALL_PATH"; \
/install_terraform.sh "$TERRAFORM_INSTALL_PATH" "$DEFAULT_TERRAFORM_VERSION" "$TERRAFORM_VERSIONS"

COPY target/terraform-boot-*.jar terraform-boot.jar
USER terraform-boot
ENTRYPOINT ["java","-jar","terraform-boot.jar"]
ENTRYPOINT ["java", "-Dterraform.install.dir=${TERRAFORM_INSTALL_PATH}", "-Dterraform.versions=${TERRAFORM_VERSIONS}", "-jar", "terraform-boot.jar"]
42 changes: 42 additions & 0 deletions install_terraform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Error:TERRAFORM_INSTALL_PATH and DEFAULT_OPENTOFU_VERSION could not be empty."
exit 1
fi
TERRAFORM_INSTALL_PATH="$1"
DEFAULT_TERRAFORM_VERSION="$2"
TERRAFORM_VERSIONS="$3"
mkdir -p "${TERRAFORM_INSTALL_PATH}"
# Install default version of Terraform in system path and custom path
echo "Start installing Terraform with default version ${DEFAULT_TERRAFORM_VERSION}"
wget -c "https://releases.hashicorp.com/terraform/${DEFAULT_TERRAFORM_VERSION}/terraform_${DEFAULT_TERRAFORM_VERSION}_linux_amd64.zip"
if [ -f "terraform_${DEFAULT_TERRAFORM_VERSION}_linux_amd64.zip" ]; then
unzip -o "terraform_${DEFAULT_TERRAFORM_VERSION}_linux_amd64.zip"
cp -f terraform /usr/bin/terraform
chmod +x /usr/bin/terraform
mv -f terraform "${TERRAFORM_INSTALL_PATH}/terraform-${DEFAULT_TERRAFORM_VERSION}"
chmod +x "${TERRAFORM_INSTALL_PATH}/terraform-${DEFAULT_TERRAFORM_VERSION}"
rm -f "terraform_${DEFAULT_TERRAFORM_VERSION}_linux_amd64.zip"
echo "Installed Terraform with default version ${DEFAULT_TERRAFORM_VERSION} into path ${TERRAFORM_INSTALL_PATH} successfully."
else
echo "Failed to download zip package of Terraform with default version terraform_${DEFAULT_TERRAFORM_VERSION}_linux_amd64.zip."
fi
if [ -z "$TERRAFORM_VERSIONS" ]; then
echo "No Terraform versions specified, skip installing Terraform versions."
exit 0
fi
# Install versions of Terraform specified in TERRAFORM_VERSIONS into custom path
VERSIONS=$(echo "$TERRAFORM_VERSIONS" | tr ',' '\n' | tr -d ' ')
for version in $VERSIONS; do
echo "Start installing Terraform with version ${version} into path ${TERRAFORM_INSTALL_PATH}"
wget -c "https://releases.hashicorp.com/terraform/${version}/terraform_${version}_linux_amd64.zip"
if [ ! -f "terraform_${version}_linux_amd64.zip" ]; then
echo "Failed to download zip package of Terraform with version terraform_${version}_linux_amd64.zip."
continue
fi
unzip -o "terraform_${version}_linux_amd64.zip"
mv -f terraform "${TERRAFORM_INSTALL_PATH}/terraform-${version}"
chmod +x "${TERRAFORM_INSTALL_PATH}/terraform-${version}"
rm -f "terraform_${version}_linux_amd64.zip"
echo "Installed Terraform with version ${version} into path ${TERRAFORM_INSTALL_PATH} successfully."
done
17 changes: 12 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<eclipse.dash.tool.plugin>1.1.0</eclipse.dash.tool.plugin>
<jgit.version>7.0.0.202409031743-r</jgit.version>
<semver4j.version>5.4.0</semver4j.version>
<jsoup.version>1.18.1</jsoup.version>
<github-api.version>1.326</github-api.version>
<maven.complier.plugin.version>3.13.0</maven.complier.plugin.version>
<maven.surefire.plugin.version>3.5.0</maven.surefire.plugin.version>
<maven.enforcer.plugin.version>3.5.0</maven.enforcer.plugin.version>
Expand All @@ -54,7 +54,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -119,9 +126,9 @@
<version>${semver4j.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>${github-api.version}</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* Main entry class to terraform-boot. This class can be directly executed to start the server.
*/
@EnableRetry
@EnableAsync
@EnableCaching
@EnableScheduling
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class TerraformBootApplication {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.eclipse.xpanse.terraform.boot.models.request.directory.TerraformModifyFromDirectoryRequest;
import org.eclipse.xpanse.terraform.boot.models.response.TerraformResult;
import org.eclipse.xpanse.terraform.boot.models.validation.TerraformValidationResult;
import org.eclipse.xpanse.terraform.boot.terraform.TerraformVersionHelper;
import org.eclipse.xpanse.terraform.boot.terraform.service.TerraformDirectoryService;
import org.eclipse.xpanse.terraform.boot.terraform.tool.TerraformVersionsHelper;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand Down Expand Up @@ -79,7 +79,7 @@ public TerraformValidationResult validateFromDirectory(
@PathVariable("module_directory") String moduleDirectory,
@Parameter(name = "terraform_version",
description = "version of Terraform to execute the module files.")
@NotBlank @Pattern(regexp = TerraformVersionHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@NotBlank @Pattern(regexp = TerraformVersionsHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@PathVariable("terraform_version") String terraformVersion) {
UUID uuid = UUID.randomUUID();
MDC.put(REQUEST_ID, uuid.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*
*/

package org.eclipse.xpanse.terraform.boot.cache;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Caffeine cache configuration class.
*/
@Slf4j
@Configuration
public class CaffeineCacheConfig {

public static final String TERRAFORM_VERSIONS_CACHE_NAME = "TERRAFORM_VERSIONS_CACHE";

/**
* Config cache manager with caffeine.
*
* @return caffeineCacheManager
*/
@Bean
public CacheManager caffeineCacheManager() {
CaffeineCacheManager cacheManager = new CaffeineCacheManager();
cacheManager.registerCustomCache(TERRAFORM_VERSIONS_CACHE_NAME,
getTerraformVersionsCache());
return cacheManager;
}


private Cache<Object, Object> getTerraformVersionsCache() {
return Caffeine.newBuilder().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Map;
import java.util.UUID;
import lombok.Data;
import org.eclipse.xpanse.terraform.boot.terraform.TerraformVersionHelper;
import org.eclipse.xpanse.terraform.boot.terraform.tool.TerraformVersionsHelper;

/**
* Data model for the generating terraform plan.
Expand All @@ -26,7 +26,7 @@ public class TerraformPlanFromDirectoryRequest {

@NotNull
@NotBlank
@Pattern(regexp = TerraformVersionHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Pattern(regexp = TerraformVersionsHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the terraform which will execute the scripts.")
String terraformVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Map;
import java.util.UUID;
import lombok.Data;
import org.eclipse.xpanse.terraform.boot.terraform.TerraformVersionHelper;
import org.eclipse.xpanse.terraform.boot.terraform.tool.TerraformVersionsHelper;

/**
* Data model for the terraform deploy requests.
Expand All @@ -26,7 +26,7 @@ public class TerraformDeployFromDirectoryRequest {

@NotNull
@NotBlank
@Pattern(regexp = TerraformVersionHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Pattern(regexp = TerraformVersionsHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the terraform which will execute the scripts.")
String terraformVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Map;
import java.util.UUID;
import lombok.Data;
import org.eclipse.xpanse.terraform.boot.terraform.TerraformVersionHelper;
import org.eclipse.xpanse.terraform.boot.terraform.tool.TerraformVersionsHelper;

/**
* Data model for the terraform destroy requests.
Expand All @@ -28,7 +28,7 @@ public class TerraformDestroyFromDirectoryRequest {

@NotNull
@NotBlank
@Pattern(regexp = TerraformVersionHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Pattern(regexp = TerraformVersionsHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the terraform which will execute the scripts.")
String terraformVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.Map;
import java.util.UUID;
import lombok.Data;
import org.eclipse.xpanse.terraform.boot.terraform.TerraformVersionHelper;
import org.eclipse.xpanse.terraform.boot.terraform.tool.TerraformVersionsHelper;

/**
* Data model for the terraform modify requests.
Expand All @@ -28,7 +28,7 @@ public class TerraformModifyFromDirectoryRequest {

@NotNull
@NotBlank
@Pattern(regexp = TerraformVersionHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Pattern(regexp = TerraformVersionsHelper.TERRAFORM_REQUIRED_VERSION_REGEX)
@Schema(description = "The required version of the terraform which will execute the scripts.")
String terraformVersion;

Expand Down
Loading