[lxc-devel] [distrobuilder/master] Detect minor release number in Apertis

monstermunchkin on Github lxc-bot at linuxcontainers.org
Tue Sep 3 08:37:46 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 310 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190903/ce5c5aac/attachment.bin>
-------------- next part --------------
From 84719b9ea4c99ab9893f50ca9bf538d1836c7343 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Tue, 3 Sep 2019 10:02:25 +0200
Subject: [PATCH 1/2] sources: Detect minor release number in Apertis

Now it's also possible to specify releases (e.g. 18.12) and specific
releases (e.g. 18.12.0).

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 sources/apertis-http.go      | 59 +++++++++++++++++++++++++++++++-----
 sources/apertis-http_test.go | 38 +++++++++++++++++++++++
 2 files changed, 89 insertions(+), 8 deletions(-)
 create mode 100644 sources/apertis-http_test.go

diff --git a/sources/apertis-http.go b/sources/apertis-http.go
index 0bd8ef1..797eca2 100644
--- a/sources/apertis-http.go
+++ b/sources/apertis-http.go
@@ -3,8 +3,13 @@ package sources
 import (
 	"errors"
 	"fmt"
+	"io/ioutil"
+	"net/http"
 	"net/url"
+	"os"
 	"path/filepath"
+	"regexp"
+	"strings"
 
 	lxd "github.com/lxc/lxd/shared"
 
@@ -21,18 +26,32 @@ func NewApertisHTTP() *ApertisHTTP {
 
 // Run downloads the tarball and unpacks it.
 func (s *ApertisHTTP) Run(definition shared.Definition, rootfsDir string) error {
-	architecture := definition.Image.Architecture
 	release := definition.Image.Release
-	variant := definition.Image.Variant
-	serial := definition.Image.Serial
+	exactRelease := release
 
 	// https://images.apertis.org/daily/v2020dev0/20190830.0/amd64/minimal/ospack_v2020dev0-amd64-minimal_20190830.0.tar.gz
-	baseURL := fmt.Sprintf("%s/%s",
-		definition.Source.URL, definition.Source.Variant)
-	baseURL = fmt.Sprintf("%s/%s/%s/%s/%s/",
-		baseURL, release, serial, architecture, variant)
+	baseURL := fmt.Sprintf("%s/%s/%s",
+		definition.Source.URL, definition.Source.Variant, release)
+
+	resp, err := http.Head(baseURL)
+	if err != nil {
+		return err
+	}
+
+	if resp.StatusCode == http.StatusNotFound {
+		// Possibly, release is a specific release (18.12.0 instead of 18.12). Lets trim the prefix and continue.
+		re := regexp.MustCompile(`\.\d+$`)
+		release = strings.TrimSuffix(release, re.FindString(release))
+		baseURL = fmt.Sprintf("%s/%s/%s",
+			definition.Source.URL, definition.Source.Variant, release)
+	} else {
+		exactRelease = s.getLatestRelease(baseURL, release)
+	}
+
+	baseURL = fmt.Sprintf("%s/%s/%s/%s/",
+		baseURL, exactRelease, definition.Image.ArchitectureMapped, definition.Image.Variant)
 	fname := fmt.Sprintf("ospack_%s-%s-%s_%s.tar.gz",
-		release, architecture, variant, serial)
+		release, definition.Image.ArchitectureMapped, definition.Image.Variant, exactRelease)
 
 	url, err := url.Parse(baseURL)
 	if err != nil {
@@ -57,3 +76,27 @@ func (s *ApertisHTTP) Run(definition shared.Definition, rootfsDir string) error
 
 	return nil
 }
+
+func (s *ApertisHTTP) getLatestRelease(baseURL, release string) string {
+	resp, err := http.Get(baseURL)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err)
+		return ""
+	}
+	defer resp.Body.Close()
+
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		fmt.Fprintln(os.Stderr, err)
+		return ""
+	}
+
+	regex := regexp.MustCompile(fmt.Sprintf(">(%s\\.\\d+)/<", release))
+	releases := regex.FindAllStringSubmatch(string(body), -1)
+
+	if len(releases) > 0 {
+		return releases[len(releases)-1][1]
+	}
+
+	return ""
+}
diff --git a/sources/apertis-http_test.go b/sources/apertis-http_test.go
new file mode 100644
index 0000000..aa899dc
--- /dev/null
+++ b/sources/apertis-http_test.go
@@ -0,0 +1,38 @@
+package sources
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestApertisHTTP_getLatestRelease(t *testing.T) {
+	s := &ApertisHTTP{}
+
+	tests := []struct {
+		release string
+		want    string
+	}{
+		{
+			"17.12",
+			"17.12.1",
+		},
+		{
+			"18.03",
+			"18.03.0",
+		},
+		{
+			"18.12",
+			"18.12.0",
+		},
+		{
+			"v2019pre",
+			"v2019pre.0",
+		},
+	}
+	for _, tt := range tests {
+		baseURL := fmt.Sprintf("https://images.apertis.org/release/%s", tt.release)
+		require.Equal(t, tt.want, s.getLatestRelease(baseURL, tt.release))
+	}
+}

From 5dcbf2e4973bae831d744a7f0c0d19958087fc3d Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Tue, 3 Sep 2019 10:36:46 +0200
Subject: [PATCH 2/2] doc: Update Apertis example

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 doc/examples/apertis | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/doc/examples/apertis b/doc/examples/apertis
index 7648cac..9c171d1 100644
--- a/doc/examples/apertis
+++ b/doc/examples/apertis
@@ -3,9 +3,8 @@ image:
   description: Apertis {{ image.architecture }} {{ image.variant }} {{ image.release }} {{ image.serial }}
   release: v2019pre
   variant: minimal
-  serial: v2019pre.0
   expiry: 30d
-  architecture: amd64
+  architecture: x86_64
 
 source:
   downloader: apertis-http
@@ -61,3 +60,6 @@ environment:
   variables:
     - key: TMPDIR
       set: false
+
+mappings:
+  architecture_map: debian


More information about the lxc-devel mailing list