[lxc-devel] [distrobuilder/master] sources: Get latest Arch Linux release by default

monstermunchkin on Github lxc-bot at linuxcontainers.org
Mon Feb 11 13:28:51 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 461 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190211/383a98b8/attachment.bin>
-------------- next part --------------
From c5c899607dbaf9445413e28c9f0741f8b7514ec9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 11 Feb 2019 14:26:13 +0100
Subject: [PATCH] sources: Get latest Arch Linux release by default

If image.release is empty or not set, the latest release will be used.

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 sources/archlinux-http.go      | 36 ++++++++++++++++++++++++++++++----
 sources/archlinux-http_test.go | 16 +++++++++++++++
 2 files changed, 48 insertions(+), 4 deletions(-)
 create mode 100644 sources/archlinux-http_test.go

diff --git a/sources/archlinux-http.go b/sources/archlinux-http.go
index 909f48e..f12ece2 100644
--- a/sources/archlinux-http.go
+++ b/sources/archlinux-http.go
@@ -7,10 +7,12 @@ import (
 	"os"
 	"path"
 	"path/filepath"
-
-	lxd "github.com/lxc/lxd/shared"
+	"strings"
 
 	"github.com/lxc/distrobuilder/shared"
+
+	lxd "github.com/lxc/lxd/shared"
+	"gopkg.in/antchfx/htmlquery.v1"
 )
 
 // ArchLinuxHTTP represents the Arch Linux downloader.
@@ -23,10 +25,22 @@ func NewArchLinuxHTTP() *ArchLinuxHTTP {
 
 // Run downloads an Arch Linux tarball.
 func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) error {
+	release := definition.Image.Release
+
+	if release == "" {
+		var err error
+
+		// Get latest release
+		release, err = s.getLatestRelease()
+		if err != nil {
+			return err
+		}
+	}
+
 	fname := fmt.Sprintf("archlinux-bootstrap-%s-%s.tar.gz",
-		definition.Image.Release, definition.Image.ArchitectureMapped)
+		release, definition.Image.ArchitectureMapped)
 	tarball := fmt.Sprintf("%s/%s/%s", definition.Source.URL,
-		definition.Image.Release, fname)
+		release, fname)
 
 	url, err := url.Parse(tarball)
 	if err != nil {
@@ -84,3 +98,17 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) erro
 	return os.RemoveAll(filepath.Join(rootfsDir, "root."+
 		definition.Image.ArchitectureMapped))
 }
+
+func (s *ArchLinuxHTTP) getLatestRelease() (string, error) {
+	doc, err := htmlquery.LoadURL("https://www.archlinux.org/download/")
+	if err != nil {
+		return "", err
+	}
+
+	node := htmlquery.FindOne(doc, `//*[@id="arch-downloads"]/ul[1]/li[1]/text()`)
+	if node == nil {
+		return "", fmt.Errorf("Failed to determine latest release")
+	}
+
+	return strings.TrimSpace(node.Data), nil
+}
diff --git a/sources/archlinux-http_test.go b/sources/archlinux-http_test.go
new file mode 100644
index 0000000..6f9c3cb
--- /dev/null
+++ b/sources/archlinux-http_test.go
@@ -0,0 +1,16 @@
+package sources
+
+import (
+	"regexp"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestArchLinuxGetLatestRelease(t *testing.T) {
+	var src ArchLinuxHTTP
+
+	release, err := src.getLatestRelease()
+	require.NoError(t, err)
+	require.Regexp(t, regexp.MustCompile(`^\d{4}\.\d{2}\.\d{2}$`), release)
+}


More information about the lxc-devel mailing list