88 "fmt"
99 "io"
1010 "log"
11- "net/http"
1211 "os"
1312 "os/exec"
1413 "path/filepath"
@@ -19,6 +18,7 @@ import (
1918 "syscall"
2019 "time"
2120
21+ "github.com/hashicorp/go-getter"
2222 "github.com/hashicorp/nomad/client/allocdir"
2323 "github.com/hashicorp/nomad/client/config"
2424 "github.com/hashicorp/nomad/nomad/structs"
@@ -94,45 +94,25 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
9494 return nil , fmt .Errorf ("Missing required Task Resource: Memory" )
9595 }
9696
97- // Attempt to download the thing
98- // Should be extracted to some kind of Http Fetcher
99- // Right now, assume publicly accessible HTTP url
100- resp , err := http .Get (source )
101- if err != nil {
102- return nil , fmt .Errorf ("Error downloading source for Qemu driver: %s" , err )
103- }
104-
10597 // Get the tasks local directory.
10698 taskDir , ok := ctx .AllocDir .TaskDirs [d .DriverContext .taskName ]
10799 if ! ok {
108100 return nil , fmt .Errorf ("Could not find task directory for task: %v" , d .DriverContext .taskName )
109101 }
110- taskLocal := filepath .Join (taskDir , allocdir .TaskLocal )
111102
112- // Create a location in the local directory to download and store the image .
113- // TODO: Caching
103+ // Create a location to download the binary .
104+ destDir := filepath . Join ( taskDir , allocdir . TaskLocal )
114105 vmID := fmt .Sprintf ("qemu-vm-%s-%s" , structs .GenerateUUID (), filepath .Base (source ))
115- fPath := filepath .Join (taskLocal , vmID )
116- vmPath , err := os .OpenFile (fPath , os .O_CREATE | os .O_WRONLY , 0666 )
117- if err != nil {
118- return nil , fmt .Errorf ("Error opening file to download to: %s" , err )
119- }
120-
121- defer vmPath .Close ()
122- defer resp .Body .Close ()
123-
124- // Copy remote file to local AllocDir for execution
125- // TODO: a retry of sort if io.Copy fails, for large binaries
126- _ , ioErr := io .Copy (vmPath , resp .Body )
127- if ioErr != nil {
128- return nil , fmt .Errorf ("Error copying Qemu image from source: %s" , ioErr )
106+ vmPath := filepath .Join (destDir , vmID )
107+ if err := getter .GetFile (vmPath , source ); err != nil {
108+ return nil , fmt .Errorf ("Error downloading artifact for Qemu driver: %s" , err )
129109 }
130110
131111 // compute and check checksum
132112 if check , ok := task .Config ["checksum" ]; ok {
133113 d .logger .Printf ("[DEBUG] Running checksum on (%s)" , vmID )
134114 hasher := sha256 .New ()
135- file , err := os .Open (vmPath . Name () )
115+ file , err := os .Open (vmPath )
136116 if err != nil {
137117 return nil , fmt .Errorf ("Failed to open file for checksum" )
138118 }
@@ -163,7 +143,7 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
163143 "-machine" , "type=pc,accel=" + accelerator ,
164144 "-name" , vmID ,
165145 "-m" , mem ,
166- "-drive" , "file=" + vmPath . Name () ,
146+ "-drive" , "file=" + vmPath ,
167147 "-nodefconfig" ,
168148 "-nodefaults" ,
169149 "-nographic" ,
@@ -240,7 +220,7 @@ func (d *QemuDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle,
240220 // Create and Return Handle
241221 h := & qemuHandle {
242222 proc : cmd .Process ,
243- vmID : vmPath . Name () ,
223+ vmID : vmPath ,
244224 doneCh : make (chan struct {}),
245225 waitCh : make (chan error , 1 ),
246226 }
0 commit comments