Skip to content

Commit 184ce42

Browse files
committed
download nerdctl-full package with HTTP cache
Signed-off-by: Akihiro Suda <[email protected]>
1 parent 0305550 commit 184ce42

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

pkg/cidata/cidata.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@ package cidata
22

33
import (
44
"bytes"
5+
"fmt"
56
"io"
67
"io/fs"
8+
"io/ioutil"
79
"os"
810
"os/user"
911
"path/filepath"
1012
"strconv"
1113

14+
"github.com/AkihiroSuda/lima/pkg/downloader"
1215
"github.com/AkihiroSuda/lima/pkg/iso9660util"
1316
"github.com/AkihiroSuda/lima/pkg/limayaml"
1417
"github.com/AkihiroSuda/lima/pkg/localpathutil"
1518
"github.com/AkihiroSuda/lima/pkg/sshutil"
1619
"github.com/pkg/errors"
20+
"github.com/sirupsen/logrus"
1721
)
1822

23+
const NerdctlVersion = "0.8.3"
24+
1925
func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
2026
if err := limayaml.ValidateRaw(*y); err != nil {
2127
return err
@@ -81,6 +87,50 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
8187
})
8288
}
8389

90+
if args.Containerd.System || args.Containerd.User {
91+
var nftgzBase string
92+
switch y.Arch {
93+
case limayaml.X8664:
94+
nftgzBase = fmt.Sprintf("nerdctl-full-%s-linux-amd64.tar.gz", NerdctlVersion)
95+
case limayaml.AARCH64:
96+
nftgzBase = fmt.Sprintf("nerdctl-full-%s-linux-arm64.tar.gz", NerdctlVersion)
97+
default:
98+
return errors.Errorf("unexpected arch %q", y.Arch)
99+
}
100+
td, err := ioutil.TempDir("", "lima-download-nerdctl")
101+
if err != nil {
102+
return err
103+
}
104+
defer os.RemoveAll(td)
105+
nftgzLocal := filepath.Join(td, nftgzBase)
106+
nftgzURL := fmt.Sprintf("https://github.com/containerd/nerdctl/releases/download/v%s/%s",
107+
NerdctlVersion, nftgzBase)
108+
logrus.Infof("Downloading %q", nftgzURL)
109+
res, err := downloader.Download(nftgzLocal, nftgzURL, downloader.WithCache())
110+
if err != nil {
111+
return errors.Wrapf(err, "failed to download %q", nftgzURL)
112+
}
113+
switch res.Status {
114+
case downloader.StatusDownloaded:
115+
logrus.Infof("Downloaded %q", nftgzBase)
116+
case downloader.StatusUsedCache:
117+
logrus.Infof("Using cache %q", res.CachePath)
118+
default:
119+
logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
120+
}
121+
// TODO: verify sha256
122+
nftgzR, err := os.Open(nftgzLocal)
123+
if err != nil {
124+
return err
125+
}
126+
defer nftgzR.Close()
127+
layout = append(layout, iso9660util.Entry{
128+
// ISO9660 requires len(Path) <= 30
129+
Path: "nerdctl-full.tgz",
130+
Reader: nftgzR,
131+
})
132+
}
133+
84134
return iso9660util.Write(isoPath, "cidata", layout)
85135
}
86136

pkg/cidata/user-data.TEMPLATE

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,10 @@ write_files:
101101
#!/bin/bash
102102
set -eux -o pipefail
103103
if [ ! -x /usr/local/bin/nerdctl ]; then
104-
version="0.8.3"
105-
goarch="amd64"
106-
if [ "$(uname -m )" = "aarch64" ]; then
107-
goarch="arm64"
108-
fi
109-
curl -fsSL https://github.com/containerd/nerdctl/releases/download/v${version}/nerdctl-full-${version}-linux-${goarch}.tar.gz | tar Cxz /usr/local
104+
mkdir -p -m 600 /mnt/lima-cidata
105+
mount -t iso9660 -o ro /dev/disk/by-label/cidata /mnt/lima-cidata
106+
tar Cxzf /usr/local /mnt/lima-cidata/nerdctl-full.tgz
107+
umount /mnt/lima-cidata
110108
fi
111109
{{- if .Containerd.System}}
112110
cat >"/etc/containerd/config.toml" <<EOF

0 commit comments

Comments
 (0)