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
30074982
Commit
30074982
authored
Jul 14, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/virtualbox: support generic hash types [GH-175]
parent
c62d7e2a
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
88 additions
and
15 deletions
+88
-15
builder/common/download.go
builder/common/download.go
+12
-0
builder/common/download_test.go
builder/common/download_test.go
+19
-0
builder/virtualbox/builder.go
builder/virtualbox/builder.go
+16
-4
builder/virtualbox/builder_test.go
builder/virtualbox/builder_test.go
+39
-8
builder/virtualbox/step_download_iso.go
builder/virtualbox/step_download_iso.go
+2
-3
No files found.
builder/common/download.go
View file @
30074982
...
...
@@ -2,6 +2,7 @@ package common
import
(
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
...
...
@@ -46,6 +47,17 @@ type DownloadClient struct {
downloader
Downloader
}
// HashForType returns the Hash implementation for the given string
// type, or nil if the type is not supported.
func
HashForType
(
t
string
)
hash
.
Hash
{
switch
t
{
case
"md5"
:
return
md5
.
New
()
default
:
return
nil
}
}
// NewDownloadClient returns a new DownloadClient for the given
// configuration.
func
NewDownloadClient
(
c
*
DownloadConfig
)
*
DownloadClient
{
...
...
builder/common/download_test.go
View file @
30074982
...
...
@@ -40,3 +40,22 @@ func TestDownloadClient_VerifyChecksum(t *testing.T) {
t
.
Fatal
(
"didn't verify"
)
}
}
func
TestHashForType
(
t
*
testing
.
T
)
{
if
h
:=
HashForType
(
"md5"
);
h
==
nil
{
t
.
Fatalf
(
"md5 hash is nil"
)
}
else
{
h
.
Write
([]
byte
(
"foo"
))
result
:=
h
.
Sum
(
nil
)
expected
:=
"acbd18db4cc2f85cedef654fccc4a4d8"
actual
:=
hex
.
EncodeToString
(
result
)
if
actual
!=
expected
{
t
.
Fatalf
(
"bad hash: %s"
,
actual
)
}
}
if
HashForType
(
"fake"
)
!=
nil
{
t
.
Fatalf
(
"fake hash is not nil"
)
}
}
builder/virtualbox/builder.go
View file @
30074982
...
...
@@ -38,7 +38,8 @@ type config struct {
HTTPDir
string
`mapstructure:"http_directory"`
HTTPPortMin
uint
`mapstructure:"http_port_min"`
HTTPPortMax
uint
`mapstructure:"http_port_max"`
ISOMD5
string
`mapstructure:"iso_md5"`
ISOChecksum
string
`mapstructure:"iso_checksum"`
ISOChecksumType
string
`mapstructure:"iso_checksum_type"`
ISOUrl
string
`mapstructure:"iso_url"`
OutputDir
string
`mapstructure:"output_directory"`
ShutdownCommand
string
`mapstructure:"shutdown_command"`
...
...
@@ -155,10 +156,21 @@ func (b *Builder) Prepare(raws ...interface{}) error {
errs
=
append
(
errs
,
errors
.
New
(
"http_port_min must be less than http_port_max"
))
}
if
b
.
config
.
ISO
MD5
==
""
{
errs
=
append
(
errs
,
errors
.
New
(
"Due to large file sizes, an iso_
md5
is required"
))
if
b
.
config
.
ISO
Checksum
==
""
{
errs
=
append
(
errs
,
errors
.
New
(
"Due to large file sizes, an iso_
checksum
is required"
))
}
else
{
b
.
config
.
ISOMD5
=
strings
.
ToLower
(
b
.
config
.
ISOMD5
)
b
.
config
.
ISOChecksum
=
strings
.
ToLower
(
b
.
config
.
ISOChecksum
)
}
if
b
.
config
.
ISOChecksumType
==
""
{
errs
=
append
(
errs
,
errors
.
New
(
"The iso_checksum_type must be specified."
))
}
else
{
b
.
config
.
ISOChecksumType
=
strings
.
ToLower
(
b
.
config
.
ISOChecksumType
)
if
h
:=
common
.
HashForType
(
b
.
config
.
ISOChecksumType
);
h
==
nil
{
errs
=
append
(
errs
,
fmt
.
Errorf
(
"Unsupported checksum type: %s"
,
b
.
config
.
ISOChecksumType
))
}
}
if
b
.
config
.
ISOUrl
==
""
{
...
...
builder/virtualbox/builder_test.go
View file @
30074982
...
...
@@ -10,9 +10,10 @@ import (
func
testConfig
()
map
[
string
]
interface
{}
{
return
map
[
string
]
interface
{}{
"iso_md5"
:
"foo"
,
"iso_url"
:
"http://www.google.com/"
,
"ssh_username"
:
"foo"
,
"iso_checksum"
:
"foo"
,
"iso_checksum_type"
:
"md5"
,
"iso_url"
:
"http://www.google.com/"
,
"ssh_username"
:
"foo"
,
packer
.
BuildNameConfigKey
:
"foo"
,
}
...
...
@@ -283,26 +284,56 @@ func TestBuilderPrepare_InvalidKey(t *testing.T) {
}
}
func
TestBuilderPrepare_ISO
MD5
(
t
*
testing
.
T
)
{
func
TestBuilderPrepare_ISO
Checksum
(
t
*
testing
.
T
)
{
var
b
Builder
config
:=
testConfig
()
// Test bad
config
[
"iso_
md5
"
]
=
""
config
[
"iso_
checksum
"
]
=
""
err
:=
b
.
Prepare
(
config
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
// Test good
config
[
"iso_
md5
"
]
=
"FOo"
config
[
"iso_
checksum
"
]
=
"FOo"
err
=
b
.
Prepare
(
config
)
if
err
!=
nil
{
t
.
Fatalf
(
"should not have error: %s"
,
err
)
}
if
b
.
config
.
ISOMD5
!=
"foo"
{
t
.
Fatalf
(
"should've lowercased: %s"
,
b
.
config
.
ISOMD5
)
if
b
.
config
.
ISOChecksum
!=
"foo"
{
t
.
Fatalf
(
"should've lowercased: %s"
,
b
.
config
.
ISOChecksum
)
}
}
func
TestBuilderPrepare_ISOChecksumType
(
t
*
testing
.
T
)
{
var
b
Builder
config
:=
testConfig
()
// Test bad
config
[
"iso_checksum_type"
]
=
""
err
:=
b
.
Prepare
(
config
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
// Test good
config
[
"iso_checksum_type"
]
=
"mD5"
err
=
b
.
Prepare
(
config
)
if
err
!=
nil
{
t
.
Fatalf
(
"should not have error: %s"
,
err
)
}
if
b
.
config
.
ISOChecksumType
!=
"md5"
{
t
.
Fatalf
(
"should've lowercased: %s"
,
b
.
config
.
ISOChecksumType
)
}
// Test unknown
config
[
"iso_checksum_type"
]
=
"fake"
err
=
b
.
Prepare
(
config
)
if
err
==
nil
{
t
.
Fatal
(
"should have error"
)
}
}
...
...
builder/virtualbox/step_download_iso.go
View file @
30074982
package
virtualbox
import
(
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/mitchellh/multistep"
...
...
@@ -33,7 +32,7 @@ func (s *stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
config
:=
state
[
"config"
]
.
(
*
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
checksum
,
err
:=
hex
.
DecodeString
(
config
.
ISO
MD5
)
checksum
,
err
:=
hex
.
DecodeString
(
config
.
ISO
Checksum
)
if
err
!=
nil
{
state
[
"error"
]
=
fmt
.
Errorf
(
"Error parsing checksum: %s"
,
err
)
return
multistep
.
ActionHalt
...
...
@@ -47,7 +46,7 @@ func (s *stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
Url
:
config
.
ISOUrl
,
TargetPath
:
cachePath
,
CopyFile
:
false
,
Hash
:
md5
.
New
(
),
Hash
:
common
.
HashForType
(
config
.
ISOChecksumType
),
Checksum
:
checksum
,
}
...
...
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