This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
/
aci-manifest.go
205 lines (180 loc) · 6.02 KB
/
aci-manifest.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package common
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
)
func ExtractManifestContentFromAci(aciPath string) ([]byte, error) {
fields := data.WithField("file", aciPath)
input, err := os.Open(aciPath)
if err != nil {
return nil, errs.WithEF(err, fields, "Cannot open file")
}
defer input.Close()
tr, err := aci.NewCompressedTarReader(input)
if err != nil {
return nil, errs.WithEF(err, fields, "Cannot open file as tar")
}
Tar:
for {
hdr, err := tr.Next()
switch err {
case io.EOF:
break Tar
case nil:
if filepath.Clean(hdr.Name) == aci.ManifestFile {
bytes, err := ioutil.ReadAll(tr)
if err != nil {
return nil, errs.WithEF(err, fields, "Cannot read manifest content in tar")
}
return bytes, nil
}
default:
return nil, errs.WithEF(err, fields, "error reading tarball file")
}
}
return nil, errs.WithEF(err, fields, "Cannot found manifest in file")
}
func ExtractManifestFromAci(aciPath string) (*schema.ImageManifest, error) {
fields := data.WithField("file", aciPath)
content, err := ExtractManifestContentFromAci(aciPath)
if err != nil {
return nil, errs.WithEF(err, fields, "Cannot extract aci manifest content from file")
}
im := &schema.ImageManifest{}
if err = im.UnmarshalJSON(content); err != nil {
return nil, errs.WithEF(err, fields.WithField("content", string(content)), "Cannot unmarshall json content")
}
return im, nil
}
func ExtractNameVersionFromManifest(im *schema.ImageManifest) *ACFullname {
name := string(im.Name)
if val, ok := im.Labels.Get("version"); ok {
name += ":" + val
}
return NewACFullName(name)
}
func WriteAciManifest(m *AciManifest, targetFile string, projectName string, dgrVersion string) error {
fields := data.WithField("name", m.NameAndVersion.String())
name, err := types.NewACIdentifier(projectName)
if err != nil {
return errs.WithEF(err, fields, "aci name is not a valid identifier for rkt")
}
labels := types.Labels{}
if m.NameAndVersion.Version() != "" {
labels = append(labels, types.Label{Name: "version", Value: m.NameAndVersion.Version()})
}
labels = append(labels, types.Label{Name: "os", Value: "linux"})
labels = append(labels, types.Label{Name: "arch", Value: "amd64"})
if m.Aci.App.User == "" {
m.Aci.App.User = "0"
}
if m.Aci.App.Group == "" {
m.Aci.App.Group = "0"
}
im := schema.BlankImageManifest()
im.Annotations = m.Aci.Annotations
//dgrBuilderIdentifier, _ := types.NewACIdentifier(ManifestDrgBuilder)
dgrVersionIdentifier, _ := types.NewACIdentifier(ManifestDrgVersion)
buildDateIdentifier, _ := types.NewACIdentifier("build-date")
im.Annotations.Set(*dgrVersionIdentifier, dgrVersion)
//im.Annotations.Set(*dgrBuilderIdentifier, m.Builder.Image.String())
im.Annotations.Set(*buildDateIdentifier, time.Now().Format(time.RFC3339))
im.Dependencies, err = ToAppcDependencies(m.Aci.Dependencies)
if err != nil {
return errs.WithEF(err, fields, "Failed to prepare dependencies for manifest")
}
im.Name = *name
im.Labels = labels
if len(m.Aci.App.Exec) == 0 {
m.Aci.App.Exec = []string{"/dgr/bin/busybox", "sh"}
}
isolators, err := ToAppcIsolators(m.Aci.App.Isolators)
if err != nil {
return errs.WithEF(err, fields, "Failed to prepare isolators")
}
im.App = &types.App{
Exec: m.Aci.App.Exec,
EventHandlers: []types.EventHandler{{Name: "pre-start", Exec: []string{"/dgr/bin/prestart"}}},
User: m.Aci.App.User,
Group: m.Aci.App.Group,
WorkingDirectory: m.Aci.App.WorkingDirectory,
SupplementaryGIDs: m.Aci.App.SupplementaryGIDs,
Environment: m.Aci.App.Environment,
MountPoints: m.Aci.App.MountPoints,
Ports: m.Aci.App.Ports,
Isolators: isolators,
}
buff, err := json.MarshalIndent(im, "", " ")
if err != nil {
return errs.WithEF(err, fields.WithField("object", im), "Failed to marshal manifest")
}
err = ioutil.WriteFile(targetFile, buff, 0644)
if err != nil {
return errs.WithEF(err, fields.WithField("file", targetFile), "Failed to write manifest file")
}
return nil
}
func ToAppcDependencies(dependencies []ACFullname) (types.Dependencies, error) {
appcDependencies := types.Dependencies{}
for _, dep := range dependencies {
id, err := types.NewACIdentifier(dep.Name())
if err != nil {
return nil, errs.WithEF(err, data.WithField("name", dep.Name()), "invalid identifer name for rkt")
}
t := types.Dependency{ImageName: *id}
if dep.Version() != "" {
t.Labels = types.Labels{}
t.Labels = append(t.Labels, types.Label{Name: "version", Value: dep.Version()})
}
appcDependencies = append(appcDependencies, t)
}
return appcDependencies, nil
}
func FromAppcDependencies(dependencies types.Dependencies) []ACFullname {
deps := []ACFullname{}
for _, dep := range dependencies {
v, _ := dep.Labels.Get("version")
deps = append(deps, *NewACFullnameFromNameAndVersion(string(dep.ImageName), v))
}
return deps
}
func ToAppcIsolators(isos []Isolator) (types.Isolators, error) {
isolators := types.Isolators{}
for _, i := range isos {
content, err := json.Marshal(i)
if err != nil {
return nil, errs.WithEF(err, data.WithField("isolator", i.Name), "Failed to marshall isolator")
}
isolator := types.Isolator{}
if err := isolator.UnmarshalJSON(content); err != nil {
return nil, errs.WithEF(err, data.WithField("isolator", i.Name), "Failed to unmarshall isolator")
}
isolators = append(isolators, isolator)
}
return isolators, nil
}
func FromAppcIsolators(isolators types.Isolators) ([]Isolator, error) {
isos := []Isolator{}
for _, i := range isolators {
var res LinuxCapabilitiesSetValue
if err := json.Unmarshal([]byte(*i.ValueRaw), &res); err != nil {
return isos, errs.WithEF(err, data.WithField("content", string(*i.ValueRaw)), "Failed to prepare isolators")
}
iso := Isolator{
Name: i.Name.String(),
Value: res,
}
i.Value()
isos = append(isos, iso)
}
return isos, nil
}