-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathload.go
More file actions
156 lines (130 loc) · 4.24 KB
/
load.go
File metadata and controls
156 lines (130 loc) · 4.24 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright © 2022 Alibaba Group Holding Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package buildah
import (
"context"
"fmt"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/containers/common/libimage"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/utils/archive"
)
var LoadError = errors.Errorf("failed to load new image")
func (engine *Engine) Load(opts *options.LoadOptions) error {
imageSrc := opts.Input
if _, err := os.Stat(imageSrc); err != nil {
return err
}
loadOpts := &libimage.LoadOptions{}
if !opts.Quiet {
loadOpts.Writer = os.Stderr
}
srcFile, err := os.Open(filepath.Clean(imageSrc))
if err != nil {
return fmt.Errorf("failed to open %s, err : %v", imageSrc, err)
}
defer func() {
if err := srcFile.Close(); err != nil {
logrus.Errorf("failed to close file: %v", err)
}
}()
tempDir, err := os.MkdirTemp(opts.TmpDir, "sealer-load-tmp")
if err != nil {
return fmt.Errorf("failed to create %s, err: %v", tempDir, err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
logrus.Errorf("failed to delete %s: %v", tempDir, err)
}
}()
// decompress tar file
if _, err = archive.Decompress(srcFile, tempDir, archive.Options{Compress: false}); err != nil {
return err
}
metaFile := filepath.Join(tempDir, common.DefaultMetadataName)
if _, err := os.Stat(metaFile); err != nil {
//assume it is single image to load
return engine.loadOneImage(imageSrc, loadOpts)
}
// get manifestName
metaBytes, err := ioutil.ReadFile(filepath.Clean(metaFile))
if err != nil {
return err
}
manifestName := string(metaBytes)
// delete it if manifestName is already used
if _, err := engine.ImageRuntime().LookupManifestList(manifestName); err == nil {
logrus.Warnf("%s is already in use, will delete it", manifestName)
if delErr := engine.DeleteManifests([]string{manifestName}, &options.ManifestDeleteOpts{}); delErr != nil {
return fmt.Errorf("%s is already in use: %v", manifestName, delErr)
}
}
// walk through temp dir to load each instance
var instancesIDs []string
err = filepath.Walk(tempDir, func(path string, f fs.FileInfo, err error) error {
if err != nil {
return err
}
if !strings.HasSuffix(f.Name(), ".tar") {
return nil
}
instanceSrc := filepath.Join(tempDir, f.Name())
err = engine.loadOneImage(instanceSrc, loadOpts)
if err != nil {
return fmt.Errorf("failed to load %s from %s: %v", f.Name(), imageSrc, err)
}
instancesIDs = append(instancesIDs, strings.TrimSuffix(f.Name(), ".tar"))
return nil
})
if err != nil {
return fmt.Errorf("failed to load image instance %v", err)
}
// create a new manifest and add instance to it.
_, err = engine.CreateManifest(manifestName, &options.ManifestCreateOpts{})
if err != nil {
return fmt.Errorf("failed to create new manifest %s :%v ", manifestName, err)
}
defer func() {
if errors.Is(err, LoadError) {
err = engine.DeleteManifests([]string{manifestName}, &options.ManifestDeleteOpts{})
if err != nil {
logrus.Errorf("failed to delete manifest %s :%v ", manifestName, err)
}
}
}()
for _, imageID := range instancesIDs {
err = engine.AddToManifest(manifestName, imageID, &options.ManifestAddOpts{})
if err != nil {
logrus.Errorf("failed to add new image %s to %s :%v ", imageID, manifestName, err)
return LoadError
}
}
return nil
}
func (engine *Engine) loadOneImage(imageSrc string, loadOpts *libimage.LoadOptions) error {
loadedImages, err := engine.ImageRuntime().Load(context.Background(), imageSrc, loadOpts)
if err != nil {
return err
}
logrus.Infof("Loaded image: " + strings.Join(loadedImages, "\nLoaded image: "))
return nil
}