The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/distrobuilder/pull/232

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
From cdfb609c4519dcaad961ce2d6630a703c68e76a0 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.h...@canonical.com>
Date: Tue, 3 Sep 2019 17:18:09 +0200
Subject: [PATCH] sources: Detect service releases for OpenWrt

Signed-off-by: Thomas Hipp <thomas.h...@canonical.com>
---
 sources/openwrt-http.go      | 55 +++++++++++++++++++++++++++++++-----
 sources/openwrt-http_test.go | 29 +++++++++++++++++++
 2 files changed, 77 insertions(+), 7 deletions(-)
 create mode 100644 sources/openwrt-http_test.go

diff --git a/sources/openwrt-http.go b/sources/openwrt-http.go
index 9a5c6c5..4c7ee0c 100644
--- a/sources/openwrt-http.go
+++ b/sources/openwrt-http.go
@@ -4,8 +4,12 @@ import (
        "crypto/sha256"
        "errors"
        "fmt"
+       "io/ioutil"
+       "net/http"
        "net/url"
+       "os"
        "path/filepath"
+       "regexp"
        "strings"
 
        lxd "github.com/lxc/lxd/shared"
@@ -23,21 +27,34 @@ func NewOpenWrtHTTP() *OpenWrtHTTP {
 
 // Run downloads the tarball and unpacks it.
 func (s *OpenWrtHTTP) Run(definition shared.Definition, rootfsDir string) 
error {
-       release := definition.Image.Release
+       var baseURL string
 
+       release := definition.Image.Release
+       releaseInFilename := strings.ToLower(release) + "-"
        architecturePath := 
strings.Replace(definition.Image.ArchitectureMapped, "_", "/", 1)
 
-       baseURL := fmt.Sprintf("%s/releases/%s/targets/%s/",
-               definition.Source.URL, release, architecturePath)
+       // Figure out the correct release
        if release == "snapshot" {
+               // Build a daily snapshot.
                baseURL = fmt.Sprintf("%s/snapshots/targets/%s/",
                        definition.Source.URL, architecturePath)
-       }
+               releaseInFilename = ""
+       } else {
+               baseURL = fmt.Sprintf("%s/releases", definition.Source.URL)
 
-       releaseInFilename := strings.ToLower(release) + "-"
+               matched, err := regexp.MatchString(`^\d+\.\d+$`, release)
+               if err != nil {
+                       return err
+               }
 
-       if release == "snapshot" {
-               releaseInFilename = ""
+               if matched {
+                       // A release of the form '18.06' has been provided. We 
need to find
+                       // out the latest service release of the form '18.06.0'.
+                       release = s.getLatestServiceRelease(baseURL, release)
+                       releaseInFilename = strings.ToLower(release) + "-"
+               }
+
+               baseURL = fmt.Sprintf("%s/%s/targets/%s/", baseURL, release, 
architecturePath)
        }
 
        fname := fmt.Sprintf("openwrt-%s%s-generic-rootfs.tar.gz", 
releaseInFilename,
@@ -94,3 +111,27 @@ func (s *OpenWrtHTTP) Run(definition shared.Definition, 
rootfsDir string) error
 
        return nil
 }
+
+func (s *OpenWrtHTTP) getLatestServiceRelease(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/openwrt-http_test.go b/sources/openwrt-http_test.go
new file mode 100644
index 0000000..2f2170d
--- /dev/null
+++ b/sources/openwrt-http_test.go
@@ -0,0 +1,29 @@
+package sources
+
+import (
+       "testing"
+
+       "github.com/stretchr/testify/require"
+)
+
+func TestOpenWrtHTTP_getLatestServiceRelease(t *testing.T) {
+       s := &OpenWrtHTTP{}
+
+       tests := []struct {
+               release string
+               want    string
+       }{
+               {
+                       "17.01",
+                       "17.01.7",
+               },
+               {
+                       "18.06",
+                       "18.06.4",
+               },
+       }
+       for _, tt := range tests {
+               baseURL := "https://downloads.openwrt.org/releases/";
+               require.Equal(t, tt.want, s.getLatestServiceRelease(baseURL, 
tt.release))
+       }
+}
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to