summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVivek Singh <vivek.singh@msystechnologies.com>2019-08-06 19:16:55 +0530
committerVivek Singh <vivek.singh@msystechnologies.com>2019-08-06 19:16:55 +0530
commitb595d89891d429606b37e2d6ad9bf99cabd473ec (patch)
treea47d16d3cc8ea1778568f772914ef62e1d043e0c
parentfc07ead366d5ef3acd9f7fe3dd36503e5a3ed1c5 (diff)
downloadchef-b595d89891d429606b37e2d6ad9bf99cabd473ec.tar.gz
Make temp dir using mkdir instead of mktemp
- mktemp does not exist for AIX system. Signed-off-by: Vivek Singh <vivek.singh@msystechnologies.com>
-rw-r--r--lib/chef/knife/bootstrap/train_connector.rb35
-rw-r--r--spec/unit/knife/bootstrap/train_connector_spec.rb21
2 files changed, 32 insertions, 24 deletions
diff --git a/lib/chef/knife/bootstrap/train_connector.rb b/lib/chef/knife/bootstrap/train_connector.rb
index 8487116f08..4290e4593d 100644
--- a/lib/chef/knife/bootstrap/train_connector.rb
+++ b/lib/chef/knife/bootstrap/train_connector.rb
@@ -18,6 +18,7 @@
require "train"
require "tempfile" unless defined?(Tempfile)
require "uri" unless defined?(URI)
+require "securerandom" unless defined?(SecureRandom)
class Chef
class Knife
@@ -32,7 +33,7 @@ class Chef
$tmp.FullName
EOM
- MKTEMP_NIX_COMMAND ||= "bash -c 'd=$(mktemp -d ${TMPDIR:-/tmp}/chef_XXXXXX); echo $d'".freeze
+ DEFAULT_REMOTE_TEMP ||= "/tmp".freeze
def initialize(host_url, default_protocol, opts)
@host_url = host_url
@@ -114,22 +115,24 @@ class Chef
#
# @return [String] the temporary path created on the remote host.
def temp_dir
- cmd = windows? ? MKTEMP_WIN_COMMAND : MKTEMP_NIX_COMMAND
@tmpdir ||= begin
- res = run_command!(cmd)
- # Since pty is enabled in the connection, stderr to be merged into stdout.
- # So, there are cases where unnecessary multi-line output
- # is included before the result of mktemp.
- dir = res.stdout.split.last
- unless windows?
- # Ensure that dir has the correct owner. We are possibly
- # running with sudo right now - so this directory would be owned by root.
- # File upload is performed over SCP as the current logged-in user,
- # so we'll set ownership to ensure that works.
- run_command!("chown #{config[:user]} '#{dir}'")
- end
- dir
- end
+ if windows?
+ run_command!(MKTEMP_WIN_COMMAND).stdout.split.last
+ else
+ # Get a 6 chars string using secure random
+ # eg. /tmp/chef_XXXXXX.
+ # Use mkdir to create TEMP dir to get rid of mktemp
+ dir = "#{DEFAULT_REMOTE_TEMP}/chef_#{SecureRandom.alphanumeric(6)}"
+ cmd = "mkdir -p %s" % dir
+ # Ensure that dir has the correct owner. We are possibly
+ # running with sudo right now - so this directory would be owned by root.
+ # File upload is performed over SCP as the current logged-in user,
+ # so we'll set ownership to ensure that works.
+ cmd += " && chown #{config[:user]} '#{dir}'"
+ run_command!(cmd)
+ dir
+ end
+ end
end
#
diff --git a/spec/unit/knife/bootstrap/train_connector_spec.rb b/spec/unit/knife/bootstrap/train_connector_spec.rb
index 52345f3cde..ff6dfca5b1 100644
--- a/spec/unit/knife/bootstrap/train_connector_spec.rb
+++ b/spec/unit/knife/bootstrap/train_connector_spec.rb
@@ -155,21 +155,26 @@ describe Chef::Knife::Bootstrap::TrainConnector do
context "under linux and unix-like" do
let(:family) { "debian" }
let(:name) { "ubuntu" }
+ let(:random) { "wScHX6" }
+ let(:dir) { "/tmp/chef_#{random}" }
+
+ before do
+ allow(SecureRandom).to receive(:alphanumeric).with(6).and_return(random)
+ end
+
it "uses the *nix command to create the temp dir and sets ownership to logged-in user" do
- expected_command = Chef::Knife::Bootstrap::TrainConnector::MKTEMP_NIX_COMMAND
+ expected_command = "mkdir -p #{dir} && chown user1 '#{dir}'"
expect(subject).to receive(:run_command!).with(expected_command)
- .and_return double("result", stdout: "/a/path")
- expect(subject).to receive(:run_command!).with("chown user1 '/a/path'")
- expect(subject.temp_dir).to eq "/a/path"
+ .and_return double("result", stdout: "\r\n")
+ expect(subject.temp_dir).to eq(dir)
end
context "with noise in stderr" do
it "uses the *nix command to create the temp dir and sets ownership to logged-in user" do
- expected_command = Chef::Knife::Bootstrap::TrainConnector::MKTEMP_NIX_COMMAND
+ expected_command = "mkdir -p #{dir} && chown user1 '#{dir}'"
expect(subject).to receive(:run_command!).with(expected_command)
- .and_return double("result", stdout: "sudo: unable to resolve host hostname.localhost\r\n" + "/a/path\r\n")
- expect(subject).to receive(:run_command!).with("chown user1 '/a/path'")
- expect(subject.temp_dir).to eq "/a/path"
+ .and_return double("result", stdout: "sudo: unable to resolve host hostname.localhost\r\n" + "#{dir}\r\n")
+ expect(subject.temp_dir).to eq(dir)
end
end
end