Skip to content

Commit

Permalink
add param to allow custom tmp dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus-Osuna-M committed Nov 15, 2024
1 parent 56431d2 commit 26ee1c3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dtolabs.rundeck.core.common.INodeEntry;
import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
import com.rundeck.plugins.ansible.util.AnsibleUtil;

import java.io.File;
import java.io.PrintWriter;
Expand All @@ -18,7 +19,7 @@ public AnsibleInlineInventoryBuilder(String inline_inventory) {

public File buildInventory() throws ConfigurationException {
try {
File file = File.createTempFile("ansible-inventory", ".inventory");
File file = AnsibleUtil.createTemporaryFile("ansible-inventory", ".inventory","");
file.deleteOnExit();
PrintWriter writer = new PrintWriter(file);
writer.write(inline_inventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.HashMap;

import com.google.gson.Gson;
import com.rundeck.plugins.ansible.util.AnsibleUtil;

public class AnsibleInventoryBuilder {

Expand All @@ -21,7 +22,7 @@ public AnsibleInventoryBuilder(Collection<INodeEntry> nodes) {

public File buildInventory() throws ConfigurationException {
try {
File file = File.createTempFile("ansible-inventory", ".json");
File file = AnsibleUtil.createTemporaryFile("ansible-inventory", ".json","");
file.deleteOnExit();
PrintWriter writer = new PrintWriter(file);
AnsibleInventory ai = new AnsibleInventory();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rundeck.plugins.ansible.ansible;

import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException;
import com.dtolabs.rundeck.core.utils.Utility;
import com.fasterxml.jackson.core.type.TypeReference;
import com.rundeck.plugins.ansible.util.*;
import com.dtolabs.rundeck.core.utils.SSHAgentProcess;
Expand All @@ -14,6 +15,7 @@
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.*;

@Builder
Expand Down Expand Up @@ -531,7 +533,7 @@ public int run() throws Exception {
List<VaultPrompt> stdinVariables = new ArrayList<>();

if(useAnsibleVault || vaultPass != null ){
vaultPromptFile = File.createTempFile("vault-prompt", ".log");
vaultPromptFile = AnsibleUtil.createTemporaryFile("vault-prompt",".log", "");
}

if (useAnsibleVault) {
Expand Down Expand Up @@ -677,7 +679,7 @@ public boolean registerKeySshAgent(String keyPath) throws Exception {

File tempPassVarsFile = null;
if (sshPassphrase != null && sshPassphrase.length() > 0) {
tempPassVarsFile = File.createTempFile("ansible-runner", "ssh-add-check");
tempPassVarsFile = AnsibleUtil.createTemporaryFile("ssh-add-check", "");
tempPassVarsFile.setExecutable(true);

List<String> passScript = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.*;
import com.rundeck.plugins.ansible.ansible.AnsibleException;

import static java.io.File.createTempFile;

@Data
@Builder
public class AnsibleVault {
Expand Down Expand Up @@ -129,7 +131,7 @@ public String encryptVariable(String key,


public static File createVaultScriptAuth(String suffix) throws IOException {
File tempInternalVaultFile = File.createTempFile("ansible-runner", suffix + "-client.py");
File tempInternalVaultFile = AnsibleUtil.createTemporaryFile(suffix + "-client.py", "");

try {
Files.copy(AnsibleUtil.class.getClassLoader().getResourceAsStream("vault-client.py"),
Expand All @@ -138,7 +140,6 @@ public static File createVaultScriptAuth(String suffix) throws IOException {
} catch (IOException e) {
throw new IOException("Failed to copy vault-client.py", e);
}

Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-xr-x");
Files.setPosixFilePermissions(tempInternalVaultFile.toPath(), perms);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.rundeck.plugins.ansible.ansible.AnsibleInventoryList;
import com.rundeck.plugins.ansible.ansible.AnsibleRunner;
import com.rundeck.plugins.ansible.ansible.InventoryList;
import com.rundeck.plugins.ansible.util.AnsibleUtil;
import com.rundeck.plugins.ansible.util.VaultPrompt;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -41,13 +42,11 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -420,7 +419,7 @@ public void processWithGatherFacts(NodeSetImpl nodes, AnsibleRunner.AnsibleRunne
final Gson gson = new Gson();
Path tempDirectory;
try {
tempDirectory = Files.createTempDirectory("ansible-hosts");
tempDirectory = Files.createTempDirectory(Paths.get(AnsibleUtil.getAnsibleTmpPath()), "ansible-hosts");
} catch (IOException e) {
throw new ResourceModelSourceException("Error creating temporary directory: " + e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,23 @@ public static Map<String, String> getRuntimeProperties(ExecutionContext context,


public static File createTemporaryFile(String suffix, String data) throws IOException {
File tempVarsFile = File.createTempFile("ansible-runner", suffix);
return createTemporaryFile("ansible-runner", suffix, data);
}

public static File createTemporaryFile(String prefix, String suffix, String data) throws IOException {
return createTemporaryFile(new File(getAnsibleTmpPath()), suffix, data, prefix);
}

public static File createTemporaryFile(File path, String suffix, String data, String prefix) throws IOException {
File tempVarsFile = File.createTempFile(prefix, suffix, path);
Files.write(tempVarsFile.toPath(), data.getBytes());
return tempVarsFile;
}

public static String getAnsibleTmpPath() {
return System.getProperty("ansible.tmp.path", System.getProperty("java.io.tmpdir"));
}


public static String randomString(){
byte[] bytes = new byte[32];
Expand Down

0 comments on commit 26ee1c3

Please sign in to comment.