[lxc-devel] [distrobuilder/master] sources: Get latest release from mirror

monstermunchkin on Github lxc-bot at linuxcontainers.org
Mon Mar 4 13:05:07 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 379 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190304/d78a80f4/attachment.bin>
-------------- next part --------------
From adb301bf3cb51e258f68ce9a15f4e226d2dbe5a9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Mon, 4 Mar 2019 13:44:43 +0100
Subject: [PATCH] sources: Get latest release from mirror

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 sources/archlinux-http.go | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/sources/archlinux-http.go b/sources/archlinux-http.go
index dd0b51c..df7cd47 100644
--- a/sources/archlinux-http.go
+++ b/sources/archlinux-http.go
@@ -7,7 +7,7 @@ import (
 	"os"
 	"path"
 	"path/filepath"
-	"strings"
+	"regexp"
 
 	"github.com/lxc/distrobuilder/shared"
 
@@ -33,7 +33,7 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) erro
 		var err error
 
 		// Get latest release
-		release, err = s.getLatestRelease()
+		release, err = s.getLatestRelease(definition.Source.URL, definition.Image.ArchitectureMapped)
 		if err != nil {
 			return err
 		}
@@ -110,16 +110,30 @@ func (s *ArchLinuxHTTP) Run(definition shared.Definition, rootfsDir string) erro
 		definition.Image.ArchitectureMapped))
 }
 
-func (s *ArchLinuxHTTP) getLatestRelease() (string, error) {
-	doc, err := htmlquery.LoadURL("https://www.archlinux.org/download/")
+func (s *ArchLinuxHTTP) getLatestRelease(URL string, arch string) (string, error) {
+	u, err := url.Parse(URL)
 	if err != nil {
 		return "", err
 	}
 
-	node := htmlquery.FindOne(doc, `//*[@id="arch-downloads"]/ul[1]/li[1]/text()`)
+	u.Path = path.Join(u.Path, "latest")
+
+	doc, err := htmlquery.LoadURL(u.String())
+	if err != nil {
+		return "", err
+	}
+
+	node := htmlquery.FindOne(doc, `//a[starts-with(text(),'archlinux-bootstrap-')][ends-with(text(),'.tar.gz')][@href]/text()`)
 	if node == nil {
 		return "", fmt.Errorf("Failed to determine latest release")
 	}
 
-	return strings.TrimSpace(node.Data), nil
+	re := regexp.MustCompile(fmt.Sprintf(`^archlinux-bootstrap-(\d{4}\.\d{2}\.\d{2})-%s.tar.gz$`, arch))
+	match := re.FindStringSubmatch(node.Data)
+
+	if len(match) != 2 {
+		return "", fmt.Errorf("Failed to determine latest release")
+	}
+
+	return match[1], nil
 }


More information about the lxc-devel mailing list