From 77be74797bceccfa64aabe9c7105d72f065a1914 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 11 May 2023 19:26:54 +0000 Subject: [PATCH] Implement debian package --- distro/debian/cache.go | 8 ++--- distro/debian/cache_test.go | 19 ++++++----- distro/debian/kernel.go | 63 ++++++++++++++++++++++++++++++++++++ distro/debian/kernel_test.go | 16 +++++++++ go.mod | 2 ++ go.sum | 4 +++ 6 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 distro/debian/kernel.go create mode 100644 distro/debian/kernel_test.go diff --git a/distro/debian/cache.go b/distro/debian/cache.go index e059f2b..e5e6b71 100644 --- a/distro/debian/cache.go +++ b/distro/debian/cache.go @@ -2,8 +2,6 @@ package debian import ( "github.com/rapidloop/skv" - - "code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot" ) type Cache struct { @@ -16,11 +14,11 @@ func NewCache(path string) (c *Cache, err error) { return } -func (c Cache) Put(p snapshot.Package) error { - return c.store.Put(p.Version, p) +func (c Cache) Put(p DebianKernel) error { + return c.store.Put(p.Version.Package, p) } -func (c Cache) Get(version string) (p snapshot.Package, err error) { +func (c Cache) Get(version string) (p DebianKernel, err error) { err = c.store.Get(version, &p) return } diff --git a/distro/debian/cache_test.go b/distro/debian/cache_test.go index 36c00ec..88c4d2c 100644 --- a/distro/debian/cache_test.go +++ b/distro/debian/cache_test.go @@ -6,8 +6,6 @@ import ( "testing" "github.com/rapidloop/skv" - - "code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot" ) func TestCache(t *testing.T) { @@ -24,23 +22,24 @@ func TestCache(t *testing.T) { t.Fatal(err) } - packages, err := snapshot.Packages("linux", "4.17.14-1", "amd64", - `^linux-(image|headers)-[0-9\.\-]*-(amd64|amd64-unsigned)$`) + version := "4.17.14-1" + + dk, err := GetDebianKernel(version) if err != nil { t.Fatal(err) } - err = c.Put(packages[0]) + err = c.Put(dk) if err != nil { t.Fatal(err) } - p, err := c.Get(packages[0].Version) + dk2, err := c.Get(version) if err != nil { t.Fatal(err) } - if p.Deb.Hash != packages[0].Deb.Hash { + if dk.Image.Deb.Hash != dk2.Image.Deb.Hash { t.Fatalf("mismatch") } @@ -52,16 +51,16 @@ func TestCache(t *testing.T) { } defer c.Close() - p, err = c.Get(packages[0].Version) + dk3, err := c.Get(version) if err != nil { t.Fatal(err) } - if p.Deb.Hash != packages[0].Deb.Hash { + if dk.Image.Deb.Hash != dk3.Image.Deb.Hash { t.Fatalf("mismatch") } - p, err = c.Get("key not exist") + _, err = c.Get("key not exist") if err == nil || err != skv.ErrNotFound { t.Fatal(err) } diff --git a/distro/debian/kernel.go b/distro/debian/kernel.go new file mode 100644 index 0000000..658eb41 --- /dev/null +++ b/distro/debian/kernel.go @@ -0,0 +1,63 @@ +package debian + +import ( + "errors" + "strings" + + "code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot" +) + +type DebianKernelVersion struct { + // linux-headers-4.17.0-2-amd64_4.17.14-1_amd64.deb + + // Package version, e.g. "4.17.14-1" + // See tags in https://salsa.debian.org/kernel-team/linux + Package string + + // ABI version, e.g. "4.17.0-2" + ABI string +} + +type DebianKernel struct { + Version DebianKernelVersion + Image snapshot.Package + Headers snapshot.Package +} + +func GetDebianKernel(version string) (dk DebianKernel, err error) { + dk.Version.Package = version + + regex := `^linux-(image|headers)-[0-9\.\-]*-(amd64|amd64-unsigned)$` + + packages, err := snapshot.Packages("linux", version, "amd64", regex) + if len(packages) != 2 { + err = errors.New("len(packages) != 2") + return + } + + var imageFound, headersFound bool + for _, p := range packages { + if strings.Contains(p.Name, "image") { + imageFound = true + dk.Image = p + } else if strings.Contains(p.Name, "headers") { + headersFound = true + dk.Headers = p + } + } + + if !imageFound { + err = errors.New("image not found") + return + } + + if !headersFound { + err = errors.New("headers not found") + return + } + + s := strings.Replace(dk.Headers.Name, "linux-headers-", "", -1) + dk.Version.ABI = strings.Replace(s, "-amd64", "", -1) + + return +} diff --git a/distro/debian/kernel_test.go b/distro/debian/kernel_test.go new file mode 100644 index 0000000..6734294 --- /dev/null +++ b/distro/debian/kernel_test.go @@ -0,0 +1,16 @@ +package debian + +import ( + "testing" +) + +func TestGetDebianKernel(t *testing.T) { + dk, err := GetDebianKernel("4.17.14-1") + if err != nil { + t.Fatal(err) + } + + if dk.Version.ABI != "4.17.0-2" { + t.Fatalf("wrong abi") + } +} diff --git a/go.mod b/go.mod index c6bed93..7112601 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/olekukonko/tablewriter v0.0.5 github.com/otiai10/copy v1.11.0 github.com/povsister/scp v0.0.0-20210427074412-33febfd9f13e + github.com/rapidloop/skv v0.0.0-20180909015525-9def2caac4cc github.com/remeh/sizedwaitgroup v1.0.0 github.com/rs/zerolog v1.29.1 github.com/ulikunitz/xz v0.5.11 @@ -34,6 +35,7 @@ require ( github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect github.com/acomagu/bufpipe v1.0.4 // indirect github.com/andybalholm/cascadia v1.3.1 // indirect + github.com/boltdb/bolt v1.3.1 // indirect github.com/cloudflare/circl v1.1.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/go-git/gcfg v1.5.0 // indirect diff --git a/go.sum b/go.sum index 2ad1e10..a1c2826 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFI github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= +github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY= github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= @@ -96,6 +98,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/povsister/scp v0.0.0-20210427074412-33febfd9f13e h1:VtsDti2SgX7M7jy0QAyGgb162PeHLrOaNxmcYOtaGsY= github.com/povsister/scp v0.0.0-20210427074412-33febfd9f13e/go.mod h1:i1Au86ZXK0ZalQNyBp2njCcyhSCR/QP/AMfILip+zNI= +github.com/rapidloop/skv v0.0.0-20180909015525-9def2caac4cc h1:eXQoy66wUI9meNnIdKYJ+EV/Tq3LvXeUe95AB2dPk8g= +github.com/rapidloop/skv v0.0.0-20180909015525-9def2caac4cc/go.mod h1:V5hvlcTzUJ3MOo0fEolWR25CZBBsb7q3wWVAmBYwr54= github.com/remeh/sizedwaitgroup v1.0.0 h1:VNGGFwNo/R5+MJBf6yrsr110p0m4/OX4S3DCy7Kyl5E= github.com/remeh/sizedwaitgroup v1.0.0/go.mod h1:3j2R4OIe/SeS6YDhICBy22RWjJC5eNCJ1V+9+NVNYlo= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=