Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
packer
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kristopher Ruzic
packer
Commits
e5c0bf14
Commit
e5c0bf14
authored
Nov 27, 2014
by
Seth Vargo
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1698 from jmassara/chef_client_prevent_sudo
Use sudo only if prevent_sudo is not set
parents
347f02a7
d5f49777
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
2 deletions
+68
-2
provisioner/chef-client/provisioner.go
provisioner/chef-client/provisioner.go
+14
-2
provisioner/chef-client/provisioner_test.go
provisioner/chef-client/provisioner_test.go
+54
-0
No files found.
provisioner/chef-client/provisioner.go
View file @
e5c0bf14
...
...
@@ -338,8 +338,14 @@ func (p *Provisioner) createJson(ui packer.Ui, comm packer.Communicator) (string
func
(
p
*
Provisioner
)
createDir
(
ui
packer
.
Ui
,
comm
packer
.
Communicator
,
dir
string
)
error
{
ui
.
Message
(
fmt
.
Sprintf
(
"Creating directory: %s"
,
dir
))
mkdirCmd
:=
fmt
.
Sprintf
(
"mkdir -p '%s'"
,
dir
)
if
!
p
.
config
.
PreventSudo
{
mkdirCmd
=
"sudo "
+
mkdirCmd
}
cmd
:=
&
packer
.
RemoteCmd
{
Command
:
fmt
.
Sprintf
(
"sudo mkdir -p '%s'"
,
dir
)
,
Command
:
mkdirCmd
,
}
if
err
:=
cmd
.
StartWithUi
(
comm
,
ui
);
err
!=
nil
{
...
...
@@ -387,8 +393,14 @@ func (p *Provisioner) cleanClient(ui packer.Ui, comm packer.Communicator, node s
func
(
p
*
Provisioner
)
removeDir
(
ui
packer
.
Ui
,
comm
packer
.
Communicator
,
dir
string
)
error
{
ui
.
Message
(
fmt
.
Sprintf
(
"Removing directory: %s"
,
dir
))
rmCmd
:=
fmt
.
Sprintf
(
"rm -rf '%s'"
,
dir
)
if
!
p
.
config
.
PreventSudo
{
rmCmd
=
"sudo "
+
rmCmd
}
cmd
:=
&
packer
.
RemoteCmd
{
Command
:
fmt
.
Sprintf
(
"sudo rm -rf %s"
,
dir
)
,
Command
:
rmCmd
,
}
if
err
:=
cmd
.
StartWithUi
(
comm
,
ui
);
err
!=
nil
{
...
...
provisioner/chef-client/provisioner_test.go
View file @
e5c0bf14
package
chefclient
import
(
"bytes"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/mitchellh/packer/packer"
...
...
@@ -135,3 +137,55 @@ func TestProvisionerPrepare_serverUrl(t *testing.T) {
t
.
Fatalf
(
"err: %s"
,
err
)
}
}
func
TestProvisioner_createDir
(
t
*
testing
.
T
)
{
p1
:=
&
Provisioner
{
config
:
Config
{
PreventSudo
:
true
}}
p2
:=
&
Provisioner
{
config
:
Config
{
PreventSudo
:
false
}}
comm
:=
&
packer
.
MockCommunicator
{}
ui
:=
&
packer
.
BasicUi
{
Reader
:
new
(
bytes
.
Buffer
),
Writer
:
new
(
bytes
.
Buffer
),
}
if
err
:=
p1
.
createDir
(
ui
,
comm
,
"/tmp/foo"
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
strings
.
HasPrefix
(
comm
.
StartCmd
.
Command
,
"sudo"
)
{
t
.
Fatalf
(
"createDir should not use sudo, got:
\"
%s
\"
"
,
comm
.
StartCmd
.
Command
)
}
if
err
:=
p2
.
createDir
(
ui
,
comm
,
"/tmp/foo"
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
!
strings
.
HasPrefix
(
comm
.
StartCmd
.
Command
,
"sudo"
)
{
t
.
Fatalf
(
"createDir should use sudo, got:
\"
%s
\"
"
,
comm
.
StartCmd
.
Command
)
}
}
func
TestProvisioner_removeDir
(
t
*
testing
.
T
)
{
p1
:=
&
Provisioner
{
config
:
Config
{
PreventSudo
:
true
}}
p2
:=
&
Provisioner
{
config
:
Config
{
PreventSudo
:
false
}}
comm
:=
&
packer
.
MockCommunicator
{}
ui
:=
&
packer
.
BasicUi
{
Reader
:
new
(
bytes
.
Buffer
),
Writer
:
new
(
bytes
.
Buffer
),
}
if
err
:=
p1
.
removeDir
(
ui
,
comm
,
"/tmp/foo"
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
strings
.
HasPrefix
(
comm
.
StartCmd
.
Command
,
"sudo"
)
{
t
.
Fatalf
(
"removeDir should not use sudo, got:
\"
%s
\"
"
,
comm
.
StartCmd
.
Command
)
}
if
err
:=
p2
.
removeDir
(
ui
,
comm
,
"/tmp/foo"
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
if
!
strings
.
HasPrefix
(
comm
.
StartCmd
.
Command
,
"sudo"
)
{
t
.
Fatalf
(
"removeDir should use sudo, got:
\"
%s
\"
"
,
comm
.
StartCmd
.
Command
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment