/
package.go
656 lines (551 loc) · 18.2 KB
/
package.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package lepton
import (
"archive/tar"
"crypto/sha256"
"regexp"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/go-errors/errors"
"github.com/nanovms/ops/constants"
"github.com/nanovms/ops/log"
"github.com/nanovms/ops/tools"
"github.com/nanovms/ops/types"
)
// PackageSysRootFolderName is the name of package root folder
const PackageSysRootFolderName = "sysroot"
var packageRegex = regexp.MustCompile(`(?P<packageName>[A-Za-z-]+)[_-](?P<version>\S+)`)
// PackageList contains a list of known packages.
type PackageList struct {
Version int `json:"Version"`
Packages []Package `json:"Packages"`
}
// Package is the definition of an OPS package.
type Package struct {
Runtime string `json:"runtime"`
Version string `json:"version"`
Language string `json:"language"`
Description string `json:"description,omitempty"`
SHA256 string `json:"sha256"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Arch string `json:"arch"`
}
// PackageIdentifier is used to identify a namespaced package
type PackageIdentifier struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Version string `json:"version"`
}
// Match matches a package with all the fields of this identifier
func (pkgidf *PackageIdentifier) Match(pkg Package) bool {
return pkg.Name == pkgidf.Name && pkg.Namespace == pkgidf.Namespace && pkg.Version == pkgidf.Version
}
// List returns the package list
func (pkglist *PackageList) List() []Package {
return pkglist.Packages
}
// ParseIdentifier parses a package identifier which looks like <namespace>/<pkg>:<version>
func ParseIdentifier(identifier string) PackageIdentifier {
tokens := strings.Split(identifier, "/")
var namespace string
if len(tokens) < 2 {
namespace = ""
} else {
namespace = tokens[len(tokens)-2]
}
pkgTokens := strings.Split(tokens[len(tokens)-1], ":")
pkgName := pkgTokens[0]
version := "latest"
if len(pkgTokens) > 1 {
version = pkgTokens[1]
}
return PackageIdentifier{
Name: pkgName,
Namespace: namespace,
Version: version,
}
}
// DownloadPackage downloads package by identifier
func DownloadPackage(identifier string, config *types.Config) (string, error) {
pkgIdf := ParseIdentifier(identifier)
pkg, err := GetPackageMetadata(pkgIdf.Namespace, pkgIdf.Name, pkgIdf.Version)
if err != nil {
return "", err
}
if pkg == nil {
return "", fmt.Errorf("package %q does not exist", identifier)
}
fullpkgq := pkg.Namespace + "/" + pkg.Name + "_" + pkg.Version
archivename := pkg.Namespace + "/" + pkg.Name + "_" + pkg.Version + ".tar.gz"
parchpath := "amd64"
if AltGOARCH != "" {
if AltGOARCH == "arm64" {
parchpath = "arm64"
}
} else {
if RealGOARCH == "arm64" {
parchpath = "arm64"
}
}
archiveFolder := path.Join(PackagesRoot, fullpkgq)
os.MkdirAll(archiveFolder, 0755)
packagepath := path.Join(PackagesRoot, fullpkgq+"/"+parchpath+".tar.gz")
_, err = os.Stat(packagepath)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if err == nil {
return packagepath, nil
}
archivePath := pkg.Namespace + "/" + pkg.Name + "/" + pkg.Version + ".tar.gz"
pkgBaseURL := PackageBaseURL
// Check config override
if config != nil {
cPkgBaseURL := strings.Trim(config.PackageBaseURL, " ")
if len(cPkgBaseURL) > 0 {
pkgBaseURL = cPkgBaseURL
}
}
// Check environment variable override
ePkgBaseURL := os.Getenv("OPS_PACKAGE_BASE_URL")
if ePkgBaseURL != "" {
pkgBaseURL = ePkgBaseURL
}
isNetworkRepo := !strings.HasPrefix(pkgBaseURL, "file://")
if isNetworkRepo {
var fileURL string
if strings.HasSuffix(pkgBaseURL, "/") {
fileURL = pkgBaseURL + archivePath
} else {
fileURL = fmt.Sprintf("%s/%s", pkgBaseURL, archivePath)
}
if AltGOARCH != "" {
if AltGOARCH == "arm64" {
fileURL = strings.ReplaceAll(fileURL, ".tar.gz", "/arm64.tar.gz")
}
} else {
if runtime.GOARCH == "arm64" {
fileURL = strings.ReplaceAll(fileURL, ".tar.gz", "/arm64.tar.gz")
}
}
if err = DownloadFileWithProgress(packagepath, fileURL, 600); err != nil {
return "", err
}
return packagepath, nil
}
pkgBaseURL = strings.TrimPrefix(pkgBaseURL, "file://")
srcPath := filepath.Join(pkgBaseURL, archivename)
srcFile, err := os.Open(srcPath)
if err != nil {
return "", err
}
defer srcFile.Close()
srcStat, err := srcFile.Stat()
if err != nil {
return "", err
}
destFile, err := os.Create(packagepath)
if err != nil {
return "", err
}
defer destFile.Close()
progressCounter := NewWriteCounter(int(srcStat.Size()))
progressCounter.Start()
_, err = io.Copy(destFile, io.TeeReader(srcFile, progressCounter))
if err != nil {
return "", err
}
progressCounter.Finish()
return packagepath, nil
}
// GetPackageList provides list of packages
func GetPackageList(config *types.Config) (*PackageList, error) {
var err error
pkgManifestURL := PackageManifestURL
// Check config override
if config != nil {
cPkgManifestURL := strings.Trim(config.PackageManifestURL, " ")
if len(cPkgManifestURL) > 0 {
pkgManifestURL = cPkgManifestURL
}
}
// Check environment var override
ePkgManifestURL := os.Getenv("OPS_PACKAGE_MANIFEST_URL")
if ePkgManifestURL != "" {
pkgManifestURL = ePkgManifestURL
}
packageManifest := GetPackageManifestFile()
if strings.HasPrefix(pkgManifestURL, "file://") {
destFile, err := os.OpenFile(packageManifest, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return nil, err
}
defer destFile.Close()
pkgManifestURL = strings.TrimPrefix(pkgManifestURL, "file://")
srcFile, err := os.Open(pkgManifestURL)
if err != nil {
return nil, err
}
defer srcFile.Close()
if _, err := io.Copy(destFile, srcFile); err != nil {
return nil, err
}
} else {
stat, err := os.Stat(packageManifest)
if os.IsNotExist(err) || PackageManifestChanged(stat, pkgManifestURL) {
if err = DownloadFile(packageManifest, pkgManifestURL, 10, false); err != nil {
return nil, err
}
}
}
var packages PackageList
data, err := os.ReadFile(packageManifest)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &packages)
if err != nil {
return nil, err
}
return &packages, nil
}
// LocalPackagesRoot is the location of the local_packages root directory.
var LocalPackagesRoot = GetOpsHome() + "/local_packages"
// PackagesRoot is the location of the packages root directory.
var PackagesRoot = GetOpsHome() + "/packages"
// GetLocalPackageList provides list of local packages
func GetLocalPackageList() ([]Package, error) {
packages := []Package{}
arches := []string{"amd64", "arm64"}
for i := 0; i < len(arches); i++ {
localPackagesDir := LocalPackagesRoot + "/" + arches[i]
localPackages, err := os.ReadDir(localPackagesDir)
if err != nil {
return nil, err
}
username := GetLocalUsername()
for _, pkg := range localPackages {
pkgName := pkg.Name()
// ignore packages compressed
if !strings.Contains(pkgName, "tar.gz") {
_, name, _ := GetNSPkgnameAndVersion(pkgName)
manifestLoc := fmt.Sprintf("%s/%s/package.manifest", localPackagesDir, pkgName)
if _, err := os.Stat(manifestLoc); err == nil {
data, err := os.ReadFile(manifestLoc)
if err != nil {
fmt.Printf("having trouble parsing the manifest of package: %s - can you verify the package.manifest is correct via jsonlint.com?\n", pkgName)
os.Exit(1)
return nil, err
}
var pkg Package
err = json.Unmarshal(data, &pkg)
if err != nil {
fmt.Printf("having trouble parsing the manifest of package: %s - can you verify the package.manifest is correct via jsonlint.com?\n", pkgName)
os.Exit(1)
return nil, err
}
pkg.Namespace = username
pkg.Name = name
packages = append(packages, pkg)
}
}
}
}
return packages, nil
}
// GetPackageManifestFile give path for package manifest file
func GetPackageManifestFile() string {
return path.Join(PackagesRoot, PackageManifestFileName)
}
// PackageManifestChanged verifies if package manifest changed
func PackageManifestChanged(fino os.FileInfo, remoteURL string) bool {
res, err := http.Head(remoteURL)
if err != nil {
var netError *net.Error
if errors.Is(err, *netError) {
fmt.Printf(constants.WarningColor, "missing internet?, using local manifest.\n")
} else {
fmt.Printf("probably bad URL: %s, got error %s", remoteURL, err)
os.Exit(1)
}
return false
}
return fino.Size() != res.ContentLength
}
func sha256Of(filename string) string {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}
return fmt.Sprintf("%x", h.Sum(nil))
}
// ExtractPackage extracts package in ops home.
// This function is currently over-loaded.
func ExtractPackage(archive, dest string, config *types.Config) {
sha := sha256Of(archive)
homeDirName := filepath.Base(GetOpsHome())
// hack
// this only verifies for packages - unfortunately this function is
// used for extracting releases (which currently don't have
// checksums)
if strings.Contains(archive, filepath.Join(homeDirName, "packages")) {
d := filepath.Base(archive)
dest = strings.ReplaceAll(d, ".tar.gz", "")
dest = homeDirName + "/packages/" + dest
st := strings.Split(archive, homeDirName+"/packages/")
st = strings.Split(st[1], "/")
namespace := st[0]
fname := st[1]
dest = dest + "/" + namespace
fnameTokens := strings.Split(fname, "_")
pkgName := fnameTokens[0]
version := fnameTokens[len(fnameTokens)-1]
pkg, _ := GetPackageMetadata(namespace, pkgName, version)
if pkg == nil || pkg.SHA256 != sha {
log.Fatalf("This package doesn't match what is in the manifest.")
}
}
in, err := os.Open(archive)
if err != nil {
fmt.Printf("File missing: %s", archive)
os.Exit(1)
}
gzip, err := gzip.NewReader(in)
if err != nil {
fmt.Printf(err.Error())
os.Exit(1)
}
defer gzip.Close()
tr := tar.NewReader(gzip)
for {
header, err := tr.Next()
if err == io.EOF {
return
}
if err != nil {
fmt.Printf(err.Error())
os.Exit(1)
}
if header == nil {
continue
}
if strings.HasPrefix(dest, ".ops") {
dest = strings.ReplaceAll(dest, ".ops", GetOpsHome())
}
target := filepath.Join(dest, header.Name)
switch header.Typeflag {
case tar.TypeDir:
if _, err := os.Stat(target); err != nil {
if err := os.MkdirAll(target, 0755); err != nil {
fmt.Printf("Failed to create directory %s, error is: %s", target, err)
os.Exit(1)
}
}
case tar.TypeReg:
f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
if err != nil {
fmt.Printf("Failed open file %s, error is %s", target, err)
os.Exit(1)
}
if err := f.Truncate(0); err != nil {
fmt.Printf("Failed truncate file %s, error is %s", target, err)
os.Exit(1)
}
if _, err := io.Copy(f, tr); err != nil {
fmt.Printf("Failed tar file %s, error is %s", target, err.Error())
os.Exit(1)
}
f.Close()
case tar.TypeSymlink:
if err := os.Symlink(header.Linkname, target); err != nil {
log.Warn(err.Error())
}
}
}
}
// GetNSPkgnameAndVersion gets the namespace, name and version from the pkg identifier
func GetNSPkgnameAndVersion(pkgIdentifier string) (string, string, string) {
namespace, pkgIdf := ExtractNS(pkgIdentifier)
match := packageRegex.FindStringSubmatch(pkgIdf)
result := make(map[string]string)
// mostly then there is no version in the name and hence we can return all of it
if len(match) == 0 {
return namespace, pkgIdf, "latest"
}
for i, name := range packageRegex.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
return namespace, result["packageName"], result["version"]
}
// ExtractNS extracts namespace from the package identifier of format <namespace>/<packageWithVersion>
// and returns the namespace and package with version
func ExtractNS(identifier string) (string, string) {
namespace := ""
pkgIdfs := strings.Split(identifier, "/")
pkgIdf := pkgIdfs[0]
if len(pkgIdfs) > 1 {
namespace = pkgIdfs[0]
pkgIdf = pkgIdfs[1]
}
return namespace, pkgIdf
}
func localPackageDirectoryPath() string {
return path.Join(GetOpsHome(), "local_packages")
}
// ClonePackage will cloned a package from ~/.ops/packages to
// ~/.ops/local_packages.
func ClonePackage(old string, newPkg string, version string, parch string, oldconfig *types.Config, newconfig *types.Config) {
fmt.Println("cloning old pkg to new")
o := path.Join(GetOpsHome(), "packages", parch, old)
n := path.Join(localPackageDirectoryPath(), parch, newPkg+"_"+version)
tools.ExecCmdList("mkdir -p", n, "&&", "cp -R", o+"/*", n+"/")
ppath := n + "/package.manifest"
c := oldconfig
p := strings.Split(c.Program, "/")
c.Program = newPkg + "_" + version + "/" + p[1]
c.Version = version
addToPackage(newconfig, n)
// nil out Dirs, Files, MapDirs or anything else we already resolved
newconfig.Dirs = []string{}
newconfig.MapDirs = map[string]string{}
newconfig.Files = []string{}
// iterate through fields and copy over anything that is not nil value
// things like env vars need to be appended
for i := 0; i < len(newconfig.Args); i++ {
c.Args = append(c.Args, newconfig.Args[i])
}
json, _ := json.MarshalIndent(c, "", " ")
// would be nice to write only needed config not all config
err := os.WriteFile(ppath, json, 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// CreatePackageFromRun builds a new package as if you were doing an
// 'ops run myprogram'
func CreatePackageFromRun(newPkg string, version string, parch string, mergedCfg *types.Config) {
fmt.Println("creating new pkg")
pkgDir := filepath.Join(localPackageDirectoryPath(), parch, newPkg+"_"+version)
pkgSysrootDir := filepath.Join(pkgDir, PackageSysRootFolderName)
// fixme - convert to stdlib
tools.ExecCmdList("mkdir -p", pkgDir)
tools.ExecCmdList("mkdir -p", pkgSysrootDir)
// package - manifest
pkgCfg := newManifestConfig(mergedCfg)
// customize
pkgCfg.Args = []string{mergedCfg.Program}
pkgCfg.Program = newPkg + "_" + version + "/" + mergedCfg.Program
pkgCfg.Version = version
json, err := json.MarshalIndent(pkgCfg, "", " ")
if err != nil {
fmt.Println(err)
}
err = os.WriteFile(filepath.Join(pkgDir, "package.manifest"), json, 0666)
if err != nil {
fmt.Println(err)
}
// package - program
tools.ExecCmdList("cp", mergedCfg.Program, pkgDir+"/.")
// package - {dirs, mapdirs, files} - config won't be present in manifest
addToPackage(mergedCfg, pkgDir)
// package - shared libs
deps, err := getSharedLibs("", mergedCfg.Program, nil)
if err != nil {
fmt.Println(err)
}
// create dir layout + cp files
for k, v := range deps {
tools.ExecCmdList("mkdir -p", filepath.Join(pkgSysrootDir, filepath.Dir(v)))
tools.ExecCmdList("cp", v, filepath.Join(pkgSysrootDir, k))
}
}
func addToPackage(newconfig *types.Config, newpath string) {
// add any directories/files
// {dirs, mapdirs, files}
// add config that might be present in our directory
// we typically use mkfs to build an image directly from config versus building
// a package with the files
if len(newconfig.Dirs) == 0 && len(newconfig.Files) == 0 && len(newconfig.MapDirs) == 0 {
return
}
pkgSysrootDir := filepath.Join(newpath, PackageSysRootFolderName)
tools.ExecCmdList("mkdir -p", pkgSysrootDir)
for i := 0; i < len(newconfig.Dirs); i++ {
tools.ExecCmdList("cp -R", newconfig.Dirs[i], pkgSysrootDir+"/.")
}
for i := 0; i < len(newconfig.Files); i++ {
tools.ExecCmdList("cp -R", newconfig.Files[i], pkgSysrootDir+"/.")
}
for k, v := range newconfig.MapDirs {
newDir := filepath.Join(pkgSysrootDir, v)
tools.ExecCmdList("mkdir -p", newDir, "&&", "cp -R", k, newDir+"/.")
}
}
// NewManifestConfig - from a merged config, select configs for manifest file,
//
// contains shallow copies
func newManifestConfig(c *types.Config) *types.Config {
return &types.Config{
Program: c.Program, //
Args: c.Args, // shallow copy
BaseVolumeSz: c.BaseVolumeSz, //
Debugflags: c.Debugflags, // shallow copy
Env: c.Env, // shallow copy
ManifestPassthrough: c.ManifestPassthrough, // shallow copy
RebootOnExit: c.RebootOnExit, //
RunConfig: types.RunConfig{
CanIPForward: c.RunConfig.CanIPForward, // gcp
GPUs: c.RunConfig.GPUs, // gcp
GPUType: c.RunConfig.GPUType, // gcp
Ports: c.RunConfig.Ports, // shallow copy
UDPPorts: c.RunConfig.UDPPorts, // shallow copy
},
Klibs: c.Klibs, // shallow copy
Version: c.Version,
Language: c.Language,
Runtime: c.Runtime,
Description: c.Description,
// default zero value
// NoTrace: c.NoTrace, // shallow copy
// Dirs: c.Dirs, // shallow copy
// Files: c.Files, // shallow copy
// DisableArgsCopy: c.DisableArgsCopy, //
// Boot: c.Boot, //
// UefiBoot: c.UefiBoot, //
// Uefi: c.Uefi, //
// BuildDir: c.BuildDir, //
// CloudConfig: c.CloudConfig, // shallow copy
// TargetConfig: c.TargetConfig, // shallow copy
// Force: c.Force, //
// Kernel: c.Kernel, //
// KlibDir: c.KlibDir, //
// MapDirs: c.MapDirs, // shallow copy
// Mounts: c.Mounts, // shallow copy
// NameServers: c.NameServers, // shallow copy
// NanosVersion: c.NanosVersion, //
// NightlyBuild: c.NightlyBuild, //
// ProgramPath: c.ProgramPath, //
// LocalFilesParentDirectory: c.LocalFilesParentDirectory, //
// TargetRoot: c.TargetRoot, //
// VolumesDir: c.VolumesDir, //
// PackageBaseURL: c.PackageBaseURL, //
// PackageManifestURL: c.PackageManifestURL, //
}
}