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
f01b21c6
Commit
f01b21c6
authored
Dec 24, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/vmware: StepCleanVMX
parent
8f8ea60b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
232 additions
and
1 deletion
+232
-1
builder/vmware/common/step_clean_vmx.go
builder/vmware/common/step_clean_vmx.go
+78
-0
builder/vmware/common/step_clean_vmx_test.go
builder/vmware/common/step_clean_vmx_test.go
+142
-0
builder/vmware/common/vmx.go
builder/vmware/common/vmx.go
+11
-0
builder/vmware/iso/builder.go
builder/vmware/iso/builder.go
+1
-1
No files found.
builder/vmware/
iso
/step_clean_vmx.go
→
builder/vmware/
common
/step_clean_vmx.go
View file @
f01b21c6
package
iso
package
common
import
(
"fmt"
"github.com/mitchellh/multistep"
vmwcommon
"github.com/mitchellh/packer/builder/vmware/common"
"github.com/mitchellh/packer/packer"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
...
...
@@ -21,16 +18,15 @@ import (
//
// Produces:
// <nothing>
type
s
tepCleanVMX
struct
{}
type
S
tepCleanVMX
struct
{}
func
(
s
stepCleanVMX
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
isoPath
:=
state
.
Get
(
"iso_path"
)
.
(
string
)
func
(
s
StepCleanVMX
)
Run
(
state
multistep
.
StateBag
)
multistep
.
StepAction
{
ui
:=
state
.
Get
(
"ui"
)
.
(
packer
.
Ui
)
vmxPath
:=
state
.
Get
(
"vmx_path"
)
.
(
string
)
ui
.
Say
(
"Cleaning VMX prior to finishing up..."
)
vmxData
,
err
:=
s
.
r
eadVMX
(
vmxPath
)
vmxData
,
err
:=
R
eadVMX
(
vmxPath
)
if
err
!=
nil
{
state
.
Put
(
"error"
,
fmt
.
Errorf
(
"Error reading VMX: %s"
,
err
))
return
multistep
.
ActionHalt
...
...
@@ -48,26 +44,30 @@ func (s stepCleanVMX) Run(state multistep.StateBag) multistep.StepAction {
vmxData
[
"floppy0.present"
]
=
"FALSE"
}
ui
.
Message
(
"Detaching ISO from CD-ROM device..."
)
devRe
:=
regexp
.
MustCompile
(
`^ide\d:\d\.`
)
for
k
,
_
:=
range
vmxData
{
match
:=
devRe
.
FindString
(
k
)
if
match
==
""
{
continue
}
if
isoPathRaw
,
ok
:=
state
.
GetOk
(
"iso_path"
);
ok
{
isoPath
:=
isoPathRaw
.
(
string
)
ui
.
Message
(
"Detaching ISO from CD-ROM device..."
)
devRe
:=
regexp
.
MustCompile
(
`^ide\d:\d\.`
)
for
k
,
_
:=
range
vmxData
{
match
:=
devRe
.
FindString
(
k
)
if
match
==
""
{
continue
}
filenameKey
:=
match
+
"filename"
if
filename
,
ok
:=
vmxData
[
filenameKey
];
ok
{
if
filename
==
isoPath
{
// Change the CD-ROM device back to auto-detect to eject
vmxData
[
filenameKey
]
=
"auto detect"
vmxData
[
match
+
"devicetype"
]
=
"cdrom-raw"
filenameKey
:=
match
+
"filename"
if
filename
,
ok
:=
vmxData
[
filenameKey
];
ok
{
if
filename
==
isoPath
{
// Change the CD-ROM device back to auto-detect to eject
vmxData
[
filenameKey
]
=
"auto detect"
vmxData
[
match
+
"devicetype"
]
=
"cdrom-raw"
}
}
}
}
// Rewrite the VMX
if
err
:=
vmwcommon
.
WriteVMX
(
vmxPath
,
vmxData
);
err
!=
nil
{
if
err
:=
WriteVMX
(
vmxPath
,
vmxData
);
err
!=
nil
{
state
.
Put
(
"error"
,
fmt
.
Errorf
(
"Error writing VMX: %s"
,
err
))
return
multistep
.
ActionHalt
}
...
...
@@ -75,19 +75,4 @@ func (s stepCleanVMX) Run(state multistep.StateBag) multistep.StepAction {
return
multistep
.
ActionContinue
}
func
(
stepCleanVMX
)
Cleanup
(
multistep
.
StateBag
)
{}
func
(
stepCleanVMX
)
readVMX
(
vmxPath
string
)
(
map
[
string
]
string
,
error
)
{
vmxF
,
err
:=
os
.
Open
(
vmxPath
)
if
err
!=
nil
{
return
nil
,
err
}
defer
vmxF
.
Close
()
vmxBytes
,
err
:=
ioutil
.
ReadAll
(
vmxF
)
if
err
!=
nil
{
return
nil
,
err
}
return
vmwcommon
.
ParseVMX
(
string
(
vmxBytes
)),
nil
}
func
(
StepCleanVMX
)
Cleanup
(
multistep
.
StateBag
)
{}
builder/vmware/common/step_clean_vmx_test.go
0 → 100644
View file @
f01b21c6
package
common
import
(
"io/ioutil"
"os"
"testing"
"github.com/mitchellh/multistep"
)
func
TestStepCleanVMX_impl
(
t
*
testing
.
T
)
{
var
_
multistep
.
Step
=
new
(
StepCleanVMX
)
}
func
TestStepCleanVMX
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCleanVMX
)
vmxPath
:=
testVMXFile
(
t
)
defer
os
.
Remove
(
vmxPath
)
state
.
Put
(
"vmx_path"
,
vmxPath
)
// Test the run
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
ok
{
t
.
Fatal
(
"should NOT have error"
)
}
}
func
TestStepCleanVMX_floppyPath
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCleanVMX
)
vmxPath
:=
testVMXFile
(
t
)
defer
os
.
Remove
(
vmxPath
)
if
err
:=
ioutil
.
WriteFile
(
vmxPath
,
[]
byte
(
testVMXFloppyPath
),
0644
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
state
.
Put
(
"floppy_path"
,
"foo"
)
state
.
Put
(
"vmx_path"
,
vmxPath
)
// Test the run
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
ok
{
t
.
Fatal
(
"should NOT have error"
)
}
// Test the resulting data
vmxContents
,
err
:=
ioutil
.
ReadFile
(
vmxPath
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
vmxData
:=
ParseVMX
(
string
(
vmxContents
))
cases
:=
[]
struct
{
Key
string
Value
string
}{
{
"floppy0.present"
,
"FALSE"
},
{
"floppy0.filetype"
,
""
},
{
"floppy0.filename"
,
""
},
}
for
_
,
tc
:=
range
cases
{
if
tc
.
Value
==
""
{
if
_
,
ok
:=
vmxData
[
tc
.
Key
];
ok
{
t
.
Fatalf
(
"should not have key: %s"
,
tc
.
Key
)
}
}
else
{
if
vmxData
[
tc
.
Key
]
!=
tc
.
Value
{
t
.
Fatalf
(
"bad: %s %#v"
,
tc
.
Key
,
vmxData
[
tc
.
Key
])
}
}
}
}
func
TestStepCleanVMX_isoPath
(
t
*
testing
.
T
)
{
state
:=
testState
(
t
)
step
:=
new
(
StepCleanVMX
)
vmxPath
:=
testVMXFile
(
t
)
defer
os
.
Remove
(
vmxPath
)
if
err
:=
ioutil
.
WriteFile
(
vmxPath
,
[]
byte
(
testVMXISOPath
),
0644
);
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
state
.
Put
(
"iso_path"
,
"foo"
)
state
.
Put
(
"vmx_path"
,
vmxPath
)
// Test the run
if
action
:=
step
.
Run
(
state
);
action
!=
multistep
.
ActionContinue
{
t
.
Fatalf
(
"bad action: %#v"
,
action
)
}
if
_
,
ok
:=
state
.
GetOk
(
"error"
);
ok
{
t
.
Fatal
(
"should NOT have error"
)
}
// Test the resulting data
vmxContents
,
err
:=
ioutil
.
ReadFile
(
vmxPath
)
if
err
!=
nil
{
t
.
Fatalf
(
"err: %s"
,
err
)
}
vmxData
:=
ParseVMX
(
string
(
vmxContents
))
cases
:=
[]
struct
{
Key
string
Value
string
}{
{
"ide0:0.filename"
,
"auto detect"
},
{
"ide0:0.devicetype"
,
"cdrom-raw"
},
{
"ide0:1.filename"
,
"bar"
},
{
"foo"
,
"bar"
},
}
for
_
,
tc
:=
range
cases
{
if
tc
.
Value
==
""
{
if
_
,
ok
:=
vmxData
[
tc
.
Key
];
ok
{
t
.
Fatalf
(
"should not have key: %s"
,
tc
.
Key
)
}
}
else
{
if
vmxData
[
tc
.
Key
]
!=
tc
.
Value
{
t
.
Fatalf
(
"bad: %s %#v"
,
tc
.
Key
,
vmxData
[
tc
.
Key
])
}
}
}
}
const
testVMXFloppyPath
=
`
floppy0.present = "TRUE"
floppy0.filetype = "file"
`
const
testVMXISOPath
=
`
ide0:0.filename = "foo"
ide0:1.filename = "bar"
foo = "bar"
`
builder/vmware/common/vmx.go
View file @
f01b21c6
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
...
...
@@ -68,3 +69,13 @@ func WriteVMX(path string, data map[string]string) (err error) {
return
}
// ReadVMX takes a path to a VMX file and reads it into a k/v mapping.
func
ReadVMX
(
path
string
)
(
map
[
string
]
string
,
error
)
{
data
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
return
nil
,
err
}
return
ParseVMX
(
string
(
data
)),
nil
}
builder/vmware/iso/builder.go
View file @
f01b21c6
...
...
@@ -412,7 +412,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&
common
.
StepProvision
{},
&
stepShutdown
{},
&
vmwcommon
.
StepCleanFiles
{},
&
s
tepCleanVMX
{},
&
vmwcommon
.
S
tepCleanVMX
{},
&
stepCompactDisk
{},
}
...
...
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