Skip to content

Commit

Permalink
fix: fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
YarikRevich committed Dec 23, 2023
1 parent bda38da commit 6010d76
Show file tree
Hide file tree
Showing 24 changed files with 273 additions and 140 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ lint: ## Run Apache Spotless linter
create-local: ## Create ResourceTracker local directory
ifeq (,$(wildcard $(HOME)/.resourcetracker))
@mkdir -p $(HOME)/.resourcetracker/config
@mkdir -p $(HOME)/.resourcetracker/workspace
endif
ifeq (,$(wildcard $(HOME)/.resourcetracker/config))
@mkdir -p $(HOME)/.resourcetracker/config
endif
ifeq (,$(wildcard $(HOME)/.resourcetracker/workspace))
@mkdir -p $(HOME)/.resourcetracker/workspace
endif

.PHONY: clone-config
clone-config: create-local ## Clone configuration files to local directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class PropertiesEntity {
@ConfigProperty(name = "terraform.directory")
String terraformDirectory;

@ConfigProperty(name = "workspace.directory")
String workspaceDirectory;

@ConfigProperty(name = "remote-storage.name")
String remoteStorageName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.resourcetracker.exception;

import java.nio.file.NoSuchFileException;
import java.util.Arrays;
import java.util.Formatter;

public class WorkspaceUnitFileNotFound extends NoSuchFileException {
public WorkspaceUnitFileNotFound() {
this("");
}

public WorkspaceUnitFileNotFound(Object... message) {
super(
new Formatter()
.format("Workspace unit is not found: %s", Arrays.stream(message).toArray())
.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
package com.resourcetracker.resource;

import com.resourcetracker.api.HealthCheckResourceApi;
import com.resourcetracker.entity.PropertiesEntity;
import com.resourcetracker.model.HealthCheckResult;
import com.resourcetracker.model.HealthCheckStatus;
import com.resourcetracker.service.client.SmallRyeHealthCheckClientService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.WebApplicationException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import org.apache.http.HttpHost;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.inject.RestClient;

@ApplicationScoped
public class HealthCheckResource implements HealthCheckResourceApi {
@Inject
@RestClient
SmallRyeHealthCheckClientService smallRyeHealthCheckClientService;
@Inject @RestClient SmallRyeHealthCheckClientService smallRyeHealthCheckClientService;

@Override
public HealthCheckResult v1HealthGet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TerraformDeploymentApplicationResult v1TerraformApplyPost(

vendorFacade.initBackendStorage(terraformDeploymentApplication);

// String terraformOutput = terraformAdapter.apply(terraformDeploymentApplication);
String terraformOutput = terraformAdapter.apply(terraformDeploymentApplication);

// String machineAddress = vendorFacade.startContainerExecution(
// terraformDeploymentApplication.getProvider(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,133 @@
import com.resourcetracker.entity.PropertiesEntity;
import com.resourcetracker.exception.CommandExecutorException;
import com.resourcetracker.exception.TerraformException;
import com.resourcetracker.exception.WorkspaceUnitFileNotFound;
import com.resourcetracker.model.TerraformDeploymentApplication;
import com.resourcetracker.model.TerraformDestructionApplication;
import com.resourcetracker.service.terraform.provider.ITerraformProvider;
import com.resourcetracker.service.terraform.provider.aws.command.ApplyCommandService;
import com.resourcetracker.service.terraform.provider.aws.command.DestroyCommandService;
import com.resourcetracker.service.terraform.provider.aws.command.InitCommandService;
import com.resourcetracker.service.terraform.provider.aws.command.OutputCommandService;
import com.resourcetracker.service.terraform.provider.executor.CommandExecutorService;
import com.resourcetracker.service.terraform.workspace.WorkspaceService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.Objects;

/** Represents Terraform provider implementation for AWS vendor. */
@ApplicationScoped
public class AWSTerraformProviderService implements ITerraformProvider {
@Inject PropertiesEntity properties;

@Inject WorkspaceService workspaceService;

@Inject CommandExecutorService commandExecutorService;

/**
* @see ITerraformProvider
*/
public String apply(TerraformDeploymentApplication terraformDeploymentApplication)
throws TerraformException {
InitCommandService initCommandService =
new InitCommandService(terraformDeploymentApplication.getCredentials(), properties);

CommandExecutorOutputDto initCommandOutput;
String workspaceUnitKey =
workspaceService.createUnitKey(
terraformDeploymentApplication.getCredentials().getSecrets().getAccessKey(),
terraformDeploymentApplication.getCredentials().getSecrets().getSecretKey());

try {
initCommandOutput = commandExecutorService.executeCommand(initCommandService);
} catch (CommandExecutorException e) {
workspaceService.createUnitDirectory(workspaceUnitKey);
} catch (IOException e) {
throw new TerraformException(e.getMessage());
}

String initCommandErrorOutput = initCommandOutput.getErrorOutput();

if (Objects.nonNull(initCommandErrorOutput)) {
throw new TerraformException(initCommandErrorOutput);
}

ApplyCommandService applyCommandService =
new ApplyCommandService(terraformDeploymentApplication.getCredentials(), properties);

CommandExecutorOutputDto applyCommandOutput;
String workspaceUnitDirectory;

try {
applyCommandOutput = commandExecutorService.executeCommand(applyCommandService);
} catch (CommandExecutorException e) {
workspaceUnitDirectory = workspaceService.getUnitDirectory(workspaceUnitKey);
} catch (WorkspaceUnitFileNotFound e) {
throw new TerraformException(e.getMessage());
}

String applyCommandErrorOutput = applyCommandOutput.getErrorOutput();

if (Objects.nonNull(applyCommandErrorOutput)) {
throw new TerraformException(applyCommandErrorOutput);
}

OutputCommandService outputCommandService =
new OutputCommandService(terraformDeploymentApplication.getCredentials(), properties);
InitCommandService initCommandService =
new InitCommandService(
workspaceUnitDirectory,
terraformDeploymentApplication.getCredentials(),
properties.getTerraformDirectory());

CommandExecutorOutputDto outputCommandOutput;
CommandExecutorOutputDto initCommandOutput;

try {
outputCommandOutput = commandExecutorService.executeCommand(outputCommandService);
initCommandOutput = commandExecutorService.executeCommand(initCommandService);
} catch (CommandExecutorException e) {
throw new TerraformException(e.getMessage());
}

String outputCommandErrorOutput = outputCommandOutput.getErrorOutput();
String initCommandErrorOutput = initCommandOutput.getErrorOutput();

if (Objects.nonNull(outputCommandErrorOutput)) {
throw new TerraformException(outputCommandErrorOutput);
if (Objects.nonNull(initCommandErrorOutput) && !initCommandErrorOutput.isEmpty()) {
throw new TerraformException(initCommandErrorOutput);
}

return outputCommandOutput.getNormalOutput();
// ApplyCommandService applyCommandService =
// new ApplyCommandService(terraformDeploymentApplication.getCredentials(), properties);
//
// CommandExecutorOutputDto applyCommandOutput;
//
// try {
// applyCommandOutput = commandExecutorService.executeCommand(applyCommandService);
// } catch (CommandExecutorException e) {
// throw new TerraformException(e.getMessage());
// }
//
// String applyCommandErrorOutput = applyCommandOutput.getErrorOutput();
//
// if (Objects.nonNull(applyCommandErrorOutput)) {
// throw new TerraformException(applyCommandErrorOutput);
// }
//
// OutputCommandService outputCommandService =
// new OutputCommandService(terraformDeploymentApplication.getCredentials(), properties);
//
// CommandExecutorOutputDto outputCommandOutput;
//
// try {
// outputCommandOutput = commandExecutorService.executeCommand(outputCommandService);
// } catch (CommandExecutorException e) {
// throw new TerraformException(e.getMessage());
// }
//
// String outputCommandErrorOutput = outputCommandOutput.getErrorOutput();
//
// if (Objects.nonNull(outputCommandErrorOutput)) {
// throw new TerraformException(outputCommandErrorOutput);
// }

// return outputCommandOutput.getNormalOutput();
return "";
}

/**
* @see ITerraformProvider
*/
public void destroy(TerraformDestructionApplication terraformDestructionApplication)
throws TerraformException {
// terraformDestructionApplication.getCredentials().get
String workspaceUnitKey =
workspaceService.createUnitKey(
terraformDestructionApplication.getCredentials().getSecrets().getAccessKey(),
terraformDestructionApplication.getCredentials().getSecrets().getSecretKey());

String workspaceUnitDirectory;

try {
workspaceUnitDirectory = workspaceService.getUnitDirectory(workspaceUnitKey);
} catch (WorkspaceUnitFileNotFound e) {
throw new TerraformException(e.getMessage());
}

DestroyCommandService destroyCommandService =
new DestroyCommandService(terraformDestructionApplication.getCredentials(), properties);
new DestroyCommandService(
workspaceUnitDirectory,
terraformDestructionApplication.getCredentials(),
properties.getTerraformDirectory());

CommandExecutorOutputDto destroyCommandOutput;

Expand All @@ -99,10 +140,16 @@ public void destroy(TerraformDestructionApplication terraformDestructionApplicat
throw new TerraformException(e.getMessage());
}

String initCommandErrorOutput = destroyCommandOutput.getErrorOutput();
String destroyCommandErrorOutput = destroyCommandOutput.getErrorOutput();

if (Objects.nonNull(initCommandErrorOutput)) {
throw new TerraformException(initCommandErrorOutput);
if (Objects.nonNull(destroyCommandErrorOutput) && !destroyCommandErrorOutput.isEmpty()) {
throw new TerraformException(destroyCommandErrorOutput);
}

try {
workspaceService.removeUnitDirectory(workspaceUnitKey);
} catch (IOException e) {
throw new TerraformException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.resourcetracker.service.terraform.provider.aws.command;

import com.resourcetracker.entity.PropertiesEntity;
// import com.resourcetracker.model.TerraformDeploymentApplicationCredentials;
import com.resourcetracker.model.CredentialsFields;
import com.resourcetracker.service.terraform.provider.aws.common.AWSProviderConfigurationHelper;
import java.nio.file.Paths;
import process.SProcess;
import process.SProcessExecutor;
import process.SProcessExecutor.OS;
Expand All @@ -12,19 +13,20 @@ public class DestroyCommandService extends SProcess {
private final OS osType;

public DestroyCommandService(
com.resourcetracker.model.CredentialsFields credentials, PropertiesEntity properties) {
String workspaceUnitDirectory, CredentialsFields credentials, String terraformDirectory) {
this.osType = SProcessExecutor.getCommandExecutor().getOSType();

this.command = "";
// this.command = switch (osType){
// case WINDOWS -> null;
// case UNIX, MAC, ANY -> String.format(
// "%s terraform destroy %s -chdir=%s -input=false -auto-approve -no-color",
// AWSProviderConfigurationHelper.getEnvironmentVariables(credentials),
// AWSProviderConfigurationHelper.getBackendConfig(credentials),
// Paths.get(properties.getTerraformDirectory(), Provider.AWS.toString())
// );
// };
this.command =
switch (osType) {
case WINDOWS -> null;
case UNIX, MAC, ANY -> String.format(
"cd %s && %s terraform destroy -force -input=false -auto-approve -no-color",
Paths.get(terraformDirectory, com.resourcetracker.model.Provider.AWS.toString()),
AWSProviderConfigurationHelper.getEnvironmentVariables(
workspaceUnitDirectory, credentials));
};

System.out.println(command);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.resourcetracker.service.terraform.provider.aws.command;

import com.resourcetracker.entity.PropertiesEntity;
// import com.resourcetracker.model.TerraformDeploymentApplicationCredentials;
import com.resourcetracker.model.CredentialsFields;
import com.resourcetracker.model.Provider;
import com.resourcetracker.service.terraform.provider.aws.common.AWSProviderConfigurationHelper;
import java.nio.file.Paths;
import process.SProcess;
import process.SProcessExecutor;
import process.SProcessExecutor.OS;
Expand All @@ -12,18 +14,19 @@ public class InitCommandService extends SProcess {
private final OS osType;

public InitCommandService(
com.resourcetracker.model.CredentialsFields credentials, PropertiesEntity properties) {
String workspaceUnitDirectory, CredentialsFields credentials, String terraformDirectory) {
this.osType = SProcessExecutor.getCommandExecutor().getOSType();

this.command = "";
// this.command = switch (osType){
// case WINDOWS -> null;
// case UNIX, MAC, ANY -> String.format(
// "cd %s && terraform init %s -input=false -no-color -upgrade -reconfigure",
// Paths.get(properties.getTerraformDirectory(), Provider.AWS.toString()),
// AWSProviderConfigurationHelper.getBackendConfig(credentials)
// );
// };
this.command =
switch (osType) {
case WINDOWS -> null;
case UNIX, MAC, ANY -> String.format(
"cd %s && %s terraform init %s -input=false -no-color -upgrade -reconfigure",
Paths.get(terraformDirectory, Provider.AWS.toString()),
AWSProviderConfigurationHelper.getEnvironmentVariables(
workspaceUnitDirectory, credentials),
AWSProviderConfigurationHelper.getBackendConfig(credentials));
};
}

@Override
Expand Down
Loading

0 comments on commit 6010d76

Please sign in to comment.