-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathurl.go
More file actions
134 lines (115 loc) · 3.08 KB
/
url.go
File metadata and controls
134 lines (115 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package stdpull
import (
"errors"
"fmt"
"io"
"io/fs"
"net/http"
url2 "net/url"
"os"
"path"
"path/filepath"
"github.com/containers/podman/v6/pkg/machine/compression"
"github.com/containers/podman/v6/pkg/machine/define"
"github.com/containers/podman/v6/utils"
"github.com/sirupsen/logrus"
"go.podman.io/storage/pkg/fileutils"
)
type DiskFromURL struct {
u *url2.URL
finalPath *define.VMFile
tempLocation *define.VMFile
cache bool
}
func NewDiskFromURL(inputPath string, finalPath *define.VMFile, tempDir *define.VMFile, optionalTempFileName *string, cache bool) (*DiskFromURL, error) {
var err error
u, err := url2.Parse(inputPath)
if err != nil {
return nil, err
}
// Make sure the temporary location exists before we get too deep
if err := fileutils.Exists(tempDir.GetPath()); err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("temporary download directory %s does not exist", tempDir.GetPath())
}
}
remoteImageName := path.Base(inputPath)
if optionalTempFileName != nil {
remoteImageName = *optionalTempFileName
}
if remoteImageName == "" {
return nil, fmt.Errorf("invalid url: unable to determine image name in %q", inputPath)
}
tempLocation, err := tempDir.AppendToNewVMFile(remoteImageName, nil)
if err != nil {
return nil, err
}
return &DiskFromURL{
u: u,
finalPath: finalPath,
tempLocation: tempLocation,
cache: cache,
}, nil
}
func (d *DiskFromURL) Get() error {
// this fetches the image and writes it to the temporary location
if err := d.pull(); err != nil {
return err
}
if !d.cache {
defer func() {
if err := utils.GuardedRemoveAll(d.tempLocation.GetPath()); err != nil {
if !errors.Is(err, os.ErrNotExist) {
logrus.Warn("failed to clean machine image cache: ", err)
}
}
}()
}
logrus.Debugf("decompressing (if needed) %s to %s", d.tempLocation.GetPath(), d.finalPath.GetPath())
if err := compression.Decompress(d.tempLocation, d.finalPath.GetPath()); err != nil {
return err
}
// Ensure image is writable
if err := os.Chmod(d.finalPath.GetPath(), 0600); err != nil {
return fmt.Errorf("failed to set permissions on machine image: %w", err)
}
return nil
}
func (d *DiskFromURL) pull() error {
out, err := os.Create(d.tempLocation.GetPath())
if err != nil {
return err
}
defer func() {
if err := out.Close(); err != nil {
logrus.Error(err)
}
}()
resp, err := http.Get(d.u.String())
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logrus.Error(err)
}
}()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("downloading VM image %s: %s", d.u.String(), resp.Status)
}
size := resp.ContentLength
prefix := "Downloading VM image: " + filepath.Base(d.tempLocation.GetPath())
onComplete := prefix + ": done"
p, bar := utils.ProgressBar(prefix, size, onComplete)
proxyReader := bar.ProxyReader(resp.Body)
defer func() {
if err := proxyReader.Close(); err != nil {
logrus.Error(err)
}
}()
if _, err := io.Copy(out, proxyReader); err != nil {
return err
}
p.Wait()
return nil
}