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
52391bb1
Commit
52391bb1
authored
Jun 12, 2013
by
Mitchell Hashimoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
builder/virtualbox: Use the common downloader
parent
a0a78b68
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
108 deletions
+36
-108
builder/virtualbox/step_download_iso.go
builder/virtualbox/step_download_iso.go
+36
-108
No files found.
builder/virtualbox/step_download_iso.go
View file @
52391bb1
...
@@ -5,13 +5,9 @@ import (
...
@@ -5,13 +5,9 @@ import (
"encoding/hex"
"encoding/hex"
"fmt"
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/builder/common"
"github.com/mitchellh/packer/packer"
"github.com/mitchellh/packer/packer"
"io"
"log"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"time"
)
)
...
@@ -31,46 +27,47 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
...
@@ -31,46 +27,47 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
config
:=
state
[
"config"
]
.
(
*
config
)
config
:=
state
[
"config"
]
.
(
*
config
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
ui
:=
state
[
"ui"
]
.
(
packer
.
Ui
)
checksum
,
err
:=
hex
.
DecodeString
(
config
.
ISOMD5
)
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error parsing checksum: %s"
,
err
))
return
multistep
.
ActionHalt
}
log
.
Printf
(
"Acquiring lock to download the ISO."
)
log
.
Printf
(
"Acquiring lock to download the ISO."
)
cachePath
:=
cache
.
Lock
(
config
.
ISOUrl
)
cachePath
:=
cache
.
Lock
(
config
.
ISOUrl
)
defer
cache
.
Unlock
(
config
.
ISOUrl
)
defer
cache
.
Unlock
(
config
.
ISOUrl
)
err
:=
s
.
checkMD5
(
cachePath
,
config
.
ISOMD5
)
downloadConfig
:=
&
common
.
DownloadConfig
{
haveFile
:=
err
==
nil
Url
:
config
.
ISOUrl
,
if
err
!=
nil
{
TargetPath
:
cachePath
,
if
!
os
.
IsNotExist
(
err
)
{
CopyFile
:
false
,
ui
.
Say
(
fmt
.
Sprintf
(
"Error validating MD5 of ISO: %s"
,
err
))
Hash
:
md5
.
New
(),
return
multistep
.
ActionHalt
Checksum
:
checksum
,
}
}
}
if
!
haveFile
{
download
:=
common
.
NewDownloadClient
(
downloadConfig
)
url
,
err
:=
url
.
Parse
(
config
.
ISOUrl
)
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error parsing iso_url: %s"
,
err
))
return
multistep
.
ActionHalt
}
// Start the download in a goroutine so that we cancel it and such.
downloadCompleteCh
:=
make
(
chan
error
,
1
)
var
progress
uint
downloadComplete
:=
make
(
chan
bool
,
1
)
go
func
()
{
go
func
()
{
ui
.
Say
(
"Copying or downloading ISO. Progress will be shown
periodically."
)
ui
.
Say
(
"Copying or downloading ISO. Progress will be reported
periodically."
)
cachePath
,
err
=
s
.
downloadUrl
(
cachePath
,
url
,
&
progress
)
cachePath
,
err
=
download
.
Get
(
)
downloadComplete
<-
true
downloadCompleteCh
<-
err
}()
}()
progressTimer
:=
time
.
NewTicker
(
1
5
*
time
.
Second
)
progressTicker
:=
time
.
NewTicker
(
5
*
time
.
Second
)
defer
progressTim
er
.
Stop
()
defer
progressTick
er
.
Stop
()
DownloadWaitLoop
:
DownloadWaitLoop
:
for
{
for
{
select
{
select
{
case
<-
downloadComplete
:
case
err
:=
<-
downloadCompleteCh
:
log
.
Println
(
"Download of ISO completed."
)
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error downloading ISO: %s"
,
err
))
}
break
DownloadWaitLoop
break
DownloadWaitLoop
case
<-
progressTim
er
.
C
:
case
<-
progressTick
er
.
C
:
ui
.
Say
(
fmt
.
Sprintf
(
"Download progress: %d%%"
,
progress
))
ui
.
Say
(
fmt
.
Sprintf
(
"Download progress: %d%%"
,
download
.
PercentProgress
()
))
case
<-
time
.
After
(
1
*
time
.
Second
)
:
case
<-
time
.
After
(
1
*
time
.
Second
)
:
if
_
,
ok
:=
state
[
multistep
.
StateCancelled
];
ok
{
if
_
,
ok
:=
state
[
multistep
.
StateCancelled
];
ok
{
ui
.
Say
(
"Interrupt received. Cancelling download..."
)
ui
.
Say
(
"Interrupt received. Cancelling download..."
)
...
@@ -79,17 +76,6 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
...
@@ -79,17 +76,6 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
}
}
}
}
if
err
!=
nil
{
ui
.
Error
(
fmt
.
Sprintf
(
"Error downloading ISO: %s"
,
err
))
return
multistep
.
ActionHalt
}
if
err
=
s
.
checkMD5
(
cachePath
,
config
.
ISOMD5
);
err
!=
nil
{
ui
.
Say
(
fmt
.
Sprintf
(
"Error validating MD5 of ISO: %s"
,
err
))
return
multistep
.
ActionHalt
}
}
log
.
Printf
(
"Path to ISO on disk: %s"
,
cachePath
)
log
.
Printf
(
"Path to ISO on disk: %s"
,
cachePath
)
state
[
"iso_path"
]
=
cachePath
state
[
"iso_path"
]
=
cachePath
...
@@ -97,61 +83,3 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
...
@@ -97,61 +83,3 @@ func (s stepDownloadISO) Run(state map[string]interface{}) multistep.StepAction
}
}
func
(
stepDownloadISO
)
Cleanup
(
map
[
string
]
interface
{})
{}
func
(
stepDownloadISO
)
Cleanup
(
map
[
string
]
interface
{})
{}
func
(
stepDownloadISO
)
checkMD5
(
path
string
,
expected
string
)
error
{
f
,
err
:=
os
.
Open
(
path
)
if
err
!=
nil
{
return
err
}
hash
:=
md5
.
New
()
io
.
Copy
(
hash
,
f
)
result
:=
strings
.
ToLower
(
hex
.
EncodeToString
(
hash
.
Sum
(
nil
)))
if
result
!=
expected
{
return
fmt
.
Errorf
(
"result != expected: %s != %s"
,
result
,
expected
)
}
return
nil
}
func
(
stepDownloadISO
)
downloadUrl
(
path
string
,
url
*
url
.
URL
,
progress
*
uint
)
(
string
,
error
)
{
if
url
.
Scheme
==
"file"
{
// If it is just a file URL, then we already have the ISO
return
url
.
Path
,
nil
}
// Otherwise, it is an HTTP URL, and we must download it.
f
,
err
:=
os
.
Create
(
path
)
if
err
!=
nil
{
return
""
,
err
}
defer
f
.
Close
()
log
.
Printf
(
"Beginning download of ISO: %s"
,
url
.
String
())
resp
,
err
:=
http
.
Get
(
url
.
String
())
if
err
!=
nil
{
return
""
,
err
}
var
buffer
[
4096
]
byte
var
totalRead
int64
for
{
n
,
err
:=
resp
.
Body
.
Read
(
buffer
[
:
])
if
err
!=
nil
&&
err
!=
io
.
EOF
{
return
""
,
err
}
totalRead
+=
int64
(
n
)
*
progress
=
uint
((
float64
(
totalRead
)
/
float64
(
resp
.
ContentLength
))
*
100
)
if
_
,
werr
:=
f
.
Write
(
buffer
[
:
n
]);
werr
!=
nil
{
return
""
,
werr
}
if
err
==
io
.
EOF
{
break
}
}
return
path
,
nil
}
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