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
bb352e50
Commit
bb352e50
authored
Aug 15, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: Add new StepDownload to DRY up downloads
parent
245b569c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
147 additions
and
1 deletion
+147
-1
builder/virtualbox/builder.go
builder/virtualbox/builder.go
+7
-1
common/step_download.go
common/step_download.go
+126
-0
common/step_download_test.go
common/step_download_test.go
+14
-0
No files found.
builder/virtualbox/builder.go
View file @
bb352e50
...
...
@@ -295,7 +295,13 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps
:=
[]
multistep
.
Step
{
new
(
stepDownloadGuestAdditions
),
new
(
stepDownloadISO
),
&
common
.
StepDownload
{
Checksum
:
b
.
config
.
ISOChecksum
,
ChecksumType
:
b
.
config
.
ISOChecksumType
,
Description
:
"ISO"
,
ResultKey
:
"iso_path"
,
Url
:
[]
string
{
b
.
config
.
ISOUrl
},
},
new
(
stepPrepareOutputDir
),
&
common
.
StepCreateFloppy
{
Files
:
b
.
config
.
FloppyFiles
,
...
...
builder/virtualbox/step_download_iso
.go
→
common/step_download
.go
View file @
bb352e50
package
virtualbox
package
common
import
(
"encoding/hex"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
"log"
"time"
)
// This step downloads the ISO specified.
// StepDownload downloads a remote file using the download client within
// this package. This step handles setting up the download configuration,
// progress reporting, interrupt handling, etc.
//
// Uses:
// cache packer.Cache
// config *config
// ui packer.Ui
//
// Produces:
// iso_path string
type
stepDownloadISO
struct
{}
type
StepDownload
struct
{
// The checksum and the type of the checksum for the download
Checksum
string
ChecksumType
string
// A short description of the type of download being done. Example:
// "ISO" or "Guest Additions"
Description
string
// The name of the key where the final path of the ISO will be put
// into the state.
ResultKey
string
// A list of URLs to attempt to download this thing.
Url
[]
string
}
func
(
s
stepDownloadISO
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
func
(
s
*
StepDownload
)
Run
(
state
map
[
string
]
interface
{})
multistep
.
StepAction
{
cache
:=
state
[
"cache"
]
.
(
packer
.
Cache
)
config
:=
state
[
"config"
]
.
(
*
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
checksum
,
err
:=
hex
.
DecodeString
(
config
.
ISO
Checksum
)
checksum
,
err
:=
hex
.
DecodeString
(
s
.
Checksum
)
if
err
!=
nil
{
state
[
"error"
]
=
fmt
.
Errorf
(
"Error parsing checksum: %s"
,
err
)
return
multistep
.
ActionHalt
}
log
.
Printf
(
"Acquiring lock to download the ISO."
)
cachePath
:=
cache
.
Lock
(
config
.
ISOUrl
)
defer
cache
.
Unlock
(
config
.
ISOUrl
)
ui
.
Say
(
fmt
.
Sprintf
(
"Downloading or copying %s"
,
s
.
Description
))
downloadConfig
:=
&
common
.
DownloadConfig
{
Url
:
config
.
ISOUrl
,
var
finalPath
string
for
_
,
url
:=
range
s
.
Url
{
ui
.
Message
(
fmt
.
Sprintf
(
"Downloading or copying: %s"
,
url
))
log
.
Printf
(
"Acquiring lock to download: %s"
,
url
)
cachePath
:=
cache
.
Lock
(
url
)
defer
cache
.
Unlock
(
url
)
config
:=
&
DownloadConfig
{
Url
:
url
,
TargetPath
:
cachePath
,
CopyFile
:
false
,
Hash
:
common
.
HashForType
(
config
.
ISO
ChecksumType
),
Hash
:
HashForType
(
s
.
ChecksumType
),
Checksum
:
checksum
,
}
download
:=
common
.
NewDownloadClient
(
downloadConfig
)
path
,
err
,
retry
:=
s
.
download
(
config
,
state
)
if
err
!=
nil
{
ui
.
Message
(
fmt
.
Sprintf
(
"Error downloading: %s"
,
err
))
}
if
!
retry
{
return
multistep
.
ActionHalt
}
if
err
==
nil
{
finalPath
=
path
break
}
}
if
finalPath
==
""
{
err
:=
fmt
.
Errorf
(
"%s download failed."
,
s
.
Description
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
}
state
[
s
.
ResultKey
]
=
finalPath
return
multistep
.
ActionContinue
}
func
(
s
*
StepDownload
)
Cleanup
(
map
[
string
]
interface
{})
{}
func
(
s
*
StepDownload
)
download
(
config
*
DownloadConfig
,
state
map
[
string
]
interface
{})
(
string
,
error
,
bool
)
{
var
path
string
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
download
:=
NewDownloadClient
(
config
)
downloadCompleteCh
:=
make
(
chan
error
,
1
)
go
func
()
{
ui
.
Say
(
"Copying or downloading ISO. Progress will be reported periodically."
)
cacheP
ath
,
err
=
download
.
Get
()
var
err
error
p
ath
,
err
=
download
.
Get
()
downloadCompleteCh
<-
err
}()
progressTicker
:=
time
.
NewTicker
(
5
*
time
.
Second
)
defer
progressTicker
.
Stop
()
DownloadWaitLoop
:
for
{
select
{
case
err
:=
<-
downloadCompleteCh
:
if
err
!=
nil
{
err
:=
fmt
.
Errorf
(
"Error downloading ISO: %s"
,
err
)
state
[
"error"
]
=
err
ui
.
Error
(
err
.
Error
())
return
multistep
.
ActionHalt
return
""
,
err
,
true
}
break
DownloadWaitLoop
return
path
,
nil
,
true
case
<-
progressTicker
.
C
:
progress
:=
download
.
PercentProgress
()
if
progress
>=
0
{
...
...
@@ -76,15 +119,8 @@ DownloadWaitLoop:
case
<-
time
.
After
(
1
*
time
.
Second
)
:
if
_
,
ok
:=
state
[
multistep
.
StateCancelled
];
ok
{
ui
.
Say
(
"Interrupt received. Cancelling download..."
)
return
multistep
.
ActionHalt
return
""
,
nil
,
false
}
}
}
log
.
Printf
(
"Path to ISO on disk: %s"
,
cachePath
)
state
[
"iso_path"
]
=
cachePath
return
multistep
.
ActionContinue
}
func
(
stepDownloadISO
)
Cleanup
(
map
[
string
]
interface
{})
{}
common/step_download_test.go
0 → 100644
View file @
bb352e50
package
common
import
(
"github.com/mitchellh/multistep"
"testing"
)
func
TestStepDownload_Impl
(
t
*
testing
.
T
)
{
var
raw
interface
{}
raw
=
new
(
StepDownload
)
if
_
,
ok
:=
raw
.
(
multistep
.
Step
);
!
ok
{
t
.
Fatalf
(
"download should be a step"
)
}
}
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