-
Notifications
You must be signed in to change notification settings - Fork 62
/
pack.go
179 lines (149 loc) · 4.87 KB
/
pack.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package manifest
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/unikraft"
)
type mpack struct {
manifest *Manifest
version string
}
const ManifestFormat pack.PackageFormat = "manifest"
// NewPackageFromManifestWithVersion generates a new package based on an input
// manifest which in itself may contain various versions and channels. With the
// provided version as a positional parameter, the manifest can be reduced to
// represent a specific version.
func NewPackageFromManifestWithVersion(manifest *Manifest, version string, opts ...ManifestOption) (pack.Package, error) {
var channels []ManifestChannel
var versions []ManifestVersion
// Tear down the manifest such that it only represents specific version
for _, channel := range manifest.Channels {
if channel.Name == version {
channels = append(channels, channel)
}
}
for _, ver := range manifest.Versions {
if ver.Version == version || ver.Unikraft == version {
// resource = ver.Resource
versions = append(versions, ver)
}
}
manifest.Channels = channels
manifest.Versions = versions
if len(channels) == 0 && len(versions) == 0 {
return nil, fmt.Errorf("unknown version: %s", version)
}
return &mpack{manifest, version}, nil
}
// NewPackageFromManifest generates a manifest implementation of the
// pack.Package construct based on the input Manifest using its default channel
func NewPackageFromManifest(manifest *Manifest, opts ...ManifestOption) (pack.Package, error) {
channel, err := manifest.DefaultChannel()
if err != nil {
return nil, err
}
return NewPackageFromManifestWithVersion(manifest, channel.Name, opts...)
}
func (mp mpack) Type() unikraft.ComponentType {
return mp.manifest.Type
}
func (mp mpack) Name() string {
return mp.manifest.Name
}
func (mp mpack) Version() string {
return mp.version
}
func (mp mpack) Metadata() any {
return mp.manifest
}
func (mp mpack) Push(ctx context.Context, opts ...pack.PushOption) error {
return fmt.Errorf("not implemented: manifest.ManifestPackage.Push")
}
func (mp mpack) Pull(ctx context.Context, opts ...pack.PullOption) error {
log.G(ctx).
WithField("package", unikraft.TypeNameVersion(mp)).
Debugf("pulling manifest")
if mp.manifest.Provider == nil {
return fmt.Errorf("uninitialized manifest provider")
}
opts = append(opts, pack.WithPullVersion(mp.version))
return mp.manifest.Provider.PullManifest(ctx, mp.manifest, opts...)
}
// Delete deletes the manifest package from host machine.
func (mp mpack) Delete(ctx context.Context, version string) error {
for _, channel := range mp.manifest.Channels {
if channel.Name == version && !strings.HasPrefix(channel.Resource, "http") {
err := os.RemoveAll(channel.Resource)
return err
}
}
for _, verObj := range mp.manifest.Versions {
if verObj.Version == version && !strings.HasPrefix(verObj.Resource, "http") {
err := os.RemoveAll(verObj.Resource)
return err
}
}
cacheDir := mp.manifest.mopts.cacheDir
localAvailablePackages, err := os.ReadDir(cacheDir)
if err != nil {
return err
}
for _, localPackage := range localAvailablePackages {
if strings.Contains(localPackage.Name(), mp.Name()) &&
strings.Contains(localPackage.Name(), mp.Version()) {
if err = os.Remove(path.Join(cacheDir, localPackage.Name())); err != nil {
return err
}
}
}
return nil
}
// resourceCacheChecksum returns the resource path, checksum and the cache
// location for a given Manifestt which only has one channel or one version. If
// the Manifest has more than one, then it is not possible to determine which
// resource should be looked up.
func resourceCacheChecksum(manifest *Manifest) (string, string, string, error) {
var err error
var resource string
var checksum string
var cache string
if manifest.mopts.cacheDir == "" {
err = fmt.Errorf("cannot determine cache dir")
} else if len(manifest.Channels) == 1 {
ext := filepath.Ext(manifest.Channels[0].Resource)
if ext == ".gz" {
ext = ".tar.gz"
}
resource = manifest.Channels[0].Resource
checksum = manifest.Channels[0].Sha256
cache = filepath.Join(
manifest.mopts.cacheDir, manifest.Name+"-"+manifest.Channels[0].Name+ext,
)
} else if len(manifest.Versions) == 1 {
ext := filepath.Ext(manifest.Versions[0].Resource)
if ext == ".gz" {
ext = ".tar.gz"
}
resource = manifest.Versions[0].Resource
checksum = manifest.Versions[0].Sha256
cache = filepath.Join(
manifest.mopts.cacheDir, manifest.Name+"-"+manifest.Versions[0].Version+ext,
)
} else {
err = fmt.Errorf("too many options")
}
return resource, cache, checksum, err
}
func (mp mpack) Format() pack.PackageFormat {
return ManifestFormat
}