Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions lepton/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lepton
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -13,7 +14,6 @@ import (
"strings"
"time"

"github.com/go-errors/errors"
"github.com/nanovms/ops/fs"
"github.com/nanovms/ops/log"
"github.com/nanovms/ops/types"
Expand Down Expand Up @@ -42,11 +42,11 @@ func BuildImage(c types.Config) error {
func BuildImageFromPackage(packagepath string, c types.Config) error {
m, err := BuildPackageManifest(packagepath, &c)
if err != nil {
return errors.Wrap(err, 1)
return err
}

if err := createImageFile(&c, m); err != nil {
return errors.Wrap(err, 1)
return err
}

return nil
Expand All @@ -60,7 +60,7 @@ func createFile(filepath string) (*os.File, error) {
}
fd, err := os.Create(filepath)
if err != nil {
return nil, errors.Wrap(err, 1)
return nil, err
}
return fd, nil
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func BuildPackageManifest(packagepath string, c *types.Config) (*fs.Manifest, er

err = setManifestFromConfig(m, c, ppath)
if err != nil {
return nil, errors.Wrap(err, 1)
return nil, err
}

if !c.DisableArgsCopy && len(c.Args) > 1 {
Expand Down Expand Up @@ -273,7 +273,7 @@ func setManifestFromConfig(m *fs.Manifest, c *types.Config, ppath string) error
m.AddKlibs(c.Klibs)

for _, f := range c.Files {
hostPath := f
var hostPath string

if filepath.IsAbs(f) {
hostPath = filepath.Join(c.TargetRoot, f)
Expand Down Expand Up @@ -406,12 +406,12 @@ func BuildManifest(c *types.Config) (*fs.Manifest, error) {

err = setManifestFromConfig(m, c, ppath)
if err != nil {
return nil, errors.Wrap(err, 1)
return nil, err
}

deps, err := getSharedLibs(c.TargetRoot, c.Program, c)
if err != nil {
return nil, errors.Wrap(err, 1)
return nil, err
}
for libpath, hostpath := range deps {
m.AddFile(libpath, hostpath)
Expand Down Expand Up @@ -496,7 +496,7 @@ func createImageFile(c *types.Config, m *fs.Manifest) error {
fd.Close()
}()
if err != nil {
return errors.Wrap(err, 1)
return err
}

defer cleanup(c)
Expand Down Expand Up @@ -569,7 +569,7 @@ func DownloadNightlyImages(c *types.Config) error {

if remote != local || c.Force {
if err = DownloadFileWithProgress(localtar, NightlyReleaseURLm, 600); err != nil {
return errors.Wrap(err, 1)
return err
}
// update local timestamp
updateLocalTimestamp(remote)
Expand Down Expand Up @@ -640,7 +640,7 @@ func DownloadReleaseImages(version string, arch string) error {
return fmt.Errorf("release '%s' is not found", version)
}

return errors.Wrap(err, 1)
return err
}

if _, err := os.Stat(localFolder); os.IsNotExist(err) {
Expand Down Expand Up @@ -729,29 +729,42 @@ func DownloadFile(fpath string, url string, timeout int, showProgress bool) erro
}

// CreateArchive compress files into an archive
func CreateArchive(archive string, files []string) error {
func CreateArchive(archive string, files map[string]string) (err error) {
fd, err := os.Create(archive)
if err != nil {
return err
}
defer func() {
err = errors.Join(err, fd.Close())
}()

gzw := gzip.NewWriter(fd)
defer func() {
err = errors.Join(err, gzw.Close())
}()

tw := tar.NewWriter(gzw)
defer func() {
err = errors.Join(err, tw.Close())
}()

for _, file := range files {
for file, rename := range files {
fstat, err := os.Stat(file)
if err != nil {
return err
}

if rename == "" {
rename = filepath.Base(file)
}
// write the header
if err := tw.WriteHeader(&tar.Header{
Name: filepath.Base(file),
Mode: int64(fstat.Mode()),
Size: fstat.Size(),
Format: tar.FormatGNU,
}); err != nil {
if err := tw.WriteHeader(
&tar.Header{
Name: rename,
Mode: int64(fstat.Mode()),
Size: fstat.Size(),
Format: tar.FormatGNU,
},
); err != nil {
return err
}

Expand All @@ -762,22 +775,12 @@ func CreateArchive(archive string, files []string) error {

// copy file data to tar
if _, err := io.CopyN(tw, fi, fstat.Size()); err != nil {
return err
return errors.Join(err, fi.Close())
}
if err = fi.Close(); err != nil {
return err
}
}

// Explicitly close all writers in correct order without any error
if err := tw.Close(); err != nil {
return err
}
if err := gzw.Close(); err != nil {
return err
}
if err := fd.Close(); err != nil {
return err
}
return nil
}
22 changes: 0 additions & 22 deletions provider/azure/azure_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -65,27 +64,6 @@ func (a *Azure) getArchiveName(ctx *lepton.Context) string {
// CustomizeImage returns image path with adaptations needed by cloud provider
func (a *Azure) CustomizeImage(ctx *lepton.Context) (string, error) {
imagePath := ctx.Config().RunConfig.ImageName
symlink := filepath.Join(filepath.Dir(imagePath), "disk.raw")

if _, err := os.Lstat(symlink); err == nil {
if err := os.Remove(symlink); err != nil {
return "", fmt.Errorf("failed to unlink: %+v", err)
}
}

err := os.Link(imagePath, symlink)
if err != nil {
return "", err
}

archPath := filepath.Join(filepath.Dir(imagePath), a.getArchiveName(ctx))
files := []string{symlink}

err = lepton.CreateArchive(archPath, files)
if err != nil {
return "", err
}

return imagePath, nil
}

Expand Down
22 changes: 5 additions & 17 deletions provider/gcp/gcp_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,13 @@ func (p *GCloud) getArchiveName(ctx *lepton.Context) string {
// CustomizeImage returns image path with adaptations needed by cloud provider
func (p *GCloud) CustomizeImage(ctx *lepton.Context) (string, error) {
imagePath := ctx.Config().RunConfig.ImageName
symlink := filepath.Join(filepath.Dir(imagePath), "disk.raw")

if _, err := os.Lstat(symlink); err == nil {
if err := os.Remove(symlink); err != nil {
return "", fmt.Errorf("failed to unlink: %+v", err)
}
}

err := os.Link(imagePath, symlink)
if err != nil {
return "", err
}

archPath := filepath.Join(filepath.Dir(imagePath), p.getArchiveName(ctx))
files := []string{symlink}

err = lepton.CreateArchive(archPath, files)
files := map[string]string{
imagePath: "disk.raw",
}
err := lepton.CreateArchive(archPath, files)
if err != nil {
return "", fmt.Errorf("failed creating archive: %v", err)
return "", fmt.Errorf("failed creating archive: %w", err)
}
return archPath, nil
}
Expand Down
20 changes: 5 additions & 15 deletions provider/gcp/gcp_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
func (g *GCloud) CreateVolume(ctx *lepton.Context, cv types.CloudVolume, data string, provider string) (lepton.NanosVolume, error) {
config := ctx.Config()

arch := cv.Name + ".tar.gz"

var sizeInGb int64
var vol lepton.NanosVolume
if config.BaseVolumeSz != "" {
Expand All @@ -39,21 +37,13 @@ func (g *GCloud) CreateVolume(ctx *lepton.Context, cv types.CloudVolume, data st
}
defer os.Remove(lv.Path)

link := filepath.Join(filepath.Dir(lv.Path), "disk.raw")
if _, err := os.Lstat(link); err == nil {
if err := os.Remove(link); err != nil {
return lv, fmt.Errorf("failed to unlink: %+v", err)
}
}
defer os.Remove(link)

err = os.Link(lv.Path, link)
if err != nil {
return lv, err
}
arch := cv.Name + ".tar.gz"
archPath := filepath.Join(filepath.Dir(lv.Path), arch)

err = lepton.CreateArchive(archPath, []string{link})
files := map[string]string{
lv.Path: "disk.raw",
}
err = lepton.CreateArchive(archPath, files)
if err != nil {
return lv, err
}
Expand Down