Skip to content

Commit

Permalink
issue #13 - Execution of TF Maven Wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jlanka11566 committed Jun 24, 2020
1 parent 8d3c42b commit d45c2fd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 13 deletions.
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

Expand All @@ -25,6 +26,7 @@ public class TerraformCommandLineDecorator implements Executable {
public TerraformCommandLineDecorator(TerraformCommand cmd, Executable commandLine, Logger logger) {
this.commandLine = commandLine;
this.cmd = cmd;

this.logger = Optional.ofNullable(logger);
this.logger.ifPresent(log -> this.commandLine.setLogger(log));
}
Expand Down Expand Up @@ -91,6 +93,12 @@ public Executable getCommandLine() {
}

private String getTerraformCommand(String command) {
return String.format("terraform %1$s %2$s", cmd.toString(), StringUtils.isEmpty(command) ? "" : command);

return String.format("%s %s %s", getTFExecutable(), cmd.toString(), StringUtils.isEmpty(command) ? "" : command);
}

private String getTFExecutable() {
File tfwrapper = new File(".tf/tfw");
return tfwrapper.exists() ? tfwrapper.getAbsolutePath().replaceAll("[\\\\]","/") : "terraform";
}
}
Expand Up @@ -17,8 +17,10 @@

public class TerraformCommandLineDecoratorTest {


@Test
public void executeDelegatesToExecutable() throws IOException, InterruptedException {

String response = "Success!";
String terraformCommand = "terraform init ";
Executable executable = Mockito.mock(Executable.class);
Expand Down Expand Up @@ -88,7 +90,36 @@ public void loggerPassedToTerraformCommandLineDecoratorIsUsedInCommandLineObject

@Test
public void terraformCommandLineDecoratorDoesntBlowUpWithoutLogging() throws IOException, InterruptedException {

Path tfPath = Paths.get(".tf");
File fetchFile = new File(String.format("..%stf-maven-plugin%ssrc%smain%sresources%stf",File.separator,File.separator,File.separator,File.separator,File.separator));
File tfDir = tfPath.toFile();
String[] tfwFileNames = {"tfw", "tfw.cmd", "tfw.ps1", "terraform-maven.properties"};
if (!tfDir.exists()) {
try {
tfDir.mkdir();
} catch (Exception e) {
System.out.printf("Unable to create .tf directory\n");
}
}
for (String tfwFileName : tfwFileNames) {
String tfwFile = (fetchFile.toPath().toFile() + File.separator + tfwFileName);
String tfwFileDestName = (".tf" + File.separator + tfwFileName);
File tfwFileSource = new File(tfwFile);
File tfwFileDest = new File(tfwFileDestName);
if (!tfwFileDest.exists()) {
Files.copy(tfwFileSource.toPath(), tfwFileDest.toPath());
tfwFileDest.setExecutable(true,false);
}
}

TerraformCommandLineDecorator terraformCommandLineDecorator = new TerraformCommandLineDecorator(TerraformCommand.VERSION);
terraformCommandLineDecorator.execute("");
terraformCommandLineDecorator.execute("init");
String[]entries = tfDir.list();
for (String s: entries) {
File currentFile = new File(tfDir.getPath(), s);
currentFile.delete();
}
tfDir.delete();
}
}
@@ -1,6 +1,6 @@
distributionSite=https://releases.hashicorp.com
releaseDir=terraform
releaseName=terraform
releaseVer=0.12.24
releaseVer=0.12.25
releaseOS=windows
releaseSuffix=amd64.zip
17 changes: 10 additions & 7 deletions tf-build-tools/tf-maven-plugin/src/main/resources/tf/tfw
Expand Up @@ -3,7 +3,7 @@ args="$@"
##############################################################
# try to figure out where the root directory of the project is
##############################################################
cur_loc='pwd'
cur_loc=`pwd`
target_cd="$cur_loc/.tf"
projectBaseDir=""

Expand All @@ -21,6 +21,7 @@ done
targetDir="$projectBaseDir/.tf/"
propFile="${targetDir}terraform-maven.properties"
search="="
cd $targetDir

################################################################
# read all lines from the properties file and store in variables
Expand Down Expand Up @@ -57,11 +58,12 @@ releaseWindows="windows"
terraformZipLinux="${targetDir}${releaseName}_${releaseVer}_${releaseLinux}_${releaseSuffix}"
terraformZipWindows="${targetDir}${releaseName}_${releaseVer}_${releaseWindows}_${releaseSuffix}"
terraformBinary="${targetDir}terraform.exe"
terraformBinaryLinux="${targetDir}terraform"
terraformBinaryLinux="terraform"
terraformBinaryWindows="${targetDir}terraform.exe"
releaseSourceLinux="${distributionSite}/${releaseDir}/${releaseVer}/${releaseName}_${releaseVer}_${releaseLinux}_${releaseSuffix}"
releaseSourceWindows="${distributionSite}/${releaseDir}/${releaseVer}/${releaseName}_${releaseVer}_${releaseWindows}_${releaseSuffix}"

releaseOS=Linux
if [[ $releaseOS == "windows" ]]
then
terraformBinary=$terraformBinaryWindows
Expand Down Expand Up @@ -89,8 +91,8 @@ then
echo ""
else
echo "Different version, downloading"
curl -o $terraformZipLinux $releaseSourceLinux
curl -o $terraformZipWindows $releaseSourceWindows
curl --max-time 300 -o $terraformZipLinux $releaseSourceLinux
curl --max-time 300 -o $terraformZipWindows $releaseSourceWindows
rm $terraformBinaryLinux
rm $terraformBinaryWindows
unzip -d $targetDir $terraformZipLinux
Expand All @@ -104,8 +106,8 @@ else
# download and install the desired version
############################################
echo "Terraform not found, installing"
curl -o $terraformZipLinux $releaseSourceLinux
curl -o $terraformZipWindows $releaseSourceWindows
curl --max-time 300 -o $terraformZipLinux $releaseSourceLinux
curl --max-time 300 -o $terraformZipWindows $releaseSourceWindows
if [ -f $terraformBinaryLinux ]
then
rm $terraformBinaryLinux
Expand All @@ -123,5 +125,6 @@ fi
###################################################
# Run terraform with the supplied arguments if any
###################################################
$terraformBinary "$args"
cd $targetDir
$terraformBinary $args
exit
6 changes: 5 additions & 1 deletion tf-build-tools/tf-maven-plugin/src/main/resources/tf/tfw.cmd
@@ -1,4 +1,8 @@
echo off

set ORIG=%cd%
set INPUT=%0%
set TARGET=%INPUT:tfw.cmd=%
cd %TARGET%
powershell -ep bypass -file tfw.ps1 %*
cd %ORIG%
exit /b
Expand Up @@ -67,7 +67,7 @@ public void theFilesAreCreatedAndPutInsideTheTfDirectoryWithDefaults() throws Mo
Assert.assertEquals(props.getProperty("distributionSite"), "https://releases.hashicorp.com");
Assert.assertEquals(props.getProperty("releaseDir"), "terraform");
Assert.assertEquals(props.getProperty("releaseName"), "terraform");
Assert.assertEquals(props.getProperty("releaseVer"), "0.12.24");
Assert.assertEquals(props.getProperty("releaseVer"), "0.12.25");
Assert.assertEquals(props.getProperty("releaseOS"), System.getProperty("os.name").contains("indow") ? "windows" : "linux");
Assert.assertEquals(props.getProperty("releaseSuffix"), "amd64.zip");
}
Expand All @@ -91,10 +91,28 @@ public void theFilesAreCreatedAndPutInsideTheTfDirectoryWithDefaults() throws Mo
Assert.assertEquals(props.getProperty("distributionSite"), "https://releases.hashicorp.com");
Assert.assertEquals(props.getProperty("releaseDir"), "terraform");
Assert.assertEquals(props.getProperty("releaseName"), "terraform");
Assert.assertEquals(props.getProperty("releaseVer"), "0.12.24");
Assert.assertEquals(props.getProperty("releaseVer"), "0.12.25");
Assert.assertEquals(props.getProperty("releaseOS"), System.getProperty("os.name").contains("indow") ? "windows" : "linux");
Assert.assertEquals(props.getProperty("releaseSuffix"), "amd64.zip");
}
if (tfDir.exists()) {
try {
String[]entries = tfDir.list();
if (entries != null) {
for (String s: entries) {
File currentFile = new File(tfDir.getPath(), s);
try {
currentFile.delete();
} catch (Exception e) {
System.out.printf("Unable to delete %s\n", currentFile);
}
}
}
tfDir.delete();
} catch (Exception e) {
System.out.printf("Unable to delete .tf directory\n");
}
}
}

@Test
Expand All @@ -119,5 +137,23 @@ public void setInputParamsAndCheckTheyGetSetCorrectly() throws MojoFailureExcept
Assert.assertEquals(props.getProperty("releaseOS"), "windowsTest");
Assert.assertEquals(props.getProperty("releaseSuffix"), "amd64.zipTest");
}
if (tfDir.exists()) {
try {
String[]entries = tfDir.list();
if (entries != null) {
for (String s: entries) {
File currentFile = new File(tfDir.getPath(), s);
try {
currentFile.delete();
} catch (Exception e) {
System.out.printf("Unable to delete %s\n", currentFile);
}
}
}
tfDir.delete();
} catch (Exception e) {
System.out.printf("Unable to delete .tf directory\n");
}
}
}
}

0 comments on commit d45c2fd

Please sign in to comment.